Skip to content

Commits to increase test coverage #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Feb 11, 2012
4 changes: 3 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,11 @@ end
begin
require 'rcov/rcovtask'
Rcov::RcovTask.new do |test|
test.libs << 'test'
test.libs << 'lib'
test.libs << 'test/lib'
test.pattern = 'test/**/*test.rb'
test.verbose = true
test.rcov_opts << "--exclude gems/*"
end
rescue LoadError
task :rcov do
Expand Down
2 changes: 1 addition & 1 deletion lib/mysql2psql/converter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def convert

_time2 = Time.now
tables.each do |table|
writer.truncate(table) if force_truncate && supress_ddl
writer.truncate(table) if force_truncate && !supress_ddl
writer.write_contents(table, reader)
end unless @supress_data

Expand Down
15 changes: 1 addition & 14 deletions lib/mysql2psql/postgres_db_writer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,20 +94,7 @@ def write_indexes(table)
next if index[:primary]
unique = index[:unique] ? "UNIQUE " : nil

#MySQL allows an index name which could be equal to a table name, Postgres doesn't
indexname = index[:name]
if indexname.eql?(table.name)
indexnamenew = "#{indexname}_index"
puts "WARNING: index \"#{indexname}\" equals table name. This is not allowed by postgres and will be renamed to \"#{indexnamenew}\""
indexname = indexnamenew
end

if @conn.server_version < 80200
@conn.exec("DROP INDEX #{PGconn.quote_ident(indexname)} CASCADE;") if exists?(indexname)
else
@conn.exec("DROP INDEX IF EXISTS #{PGconn.quote_ident(indexname)} CASCADE;")
end
@conn.exec("CREATE #{unique}INDEX #{PGconn.quote_ident(indexname)} ON #{PGconn.quote_ident(table.name)} (#{index[:columns].map {|col| PGconn.quote_ident(col)}.join(", ")});")
@conn.exec("CREATE #{unique}INDEX ON #{PGconn.quote_ident(table.name)} (#{index[:columns].map {|col| PGconn.quote_ident(col)}.join(", ")});")
end


Expand Down
4 changes: 0 additions & 4 deletions lib/mysql2psql/postgres_writer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ def column_description(column)
"#{PGconn.quote_ident(column[:name])} #{column_type_info(column)}"
end

def column_type(column)
column_type_info(column).split(" ").first
end

def column_type(column)
if column[:auto_increment]
'integer'
Expand Down
32 changes: 32 additions & 0 deletions test/fixtures/seed_integration_tests.sql
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ INSERT INTO numeric_types_basics VALUES
( 5, 127, 255, 32767, 65535, 8388607, 16777215, 2147483647, 4294967295, 2147483647, 4294967295, 9223372036854775807, 18446744073709551615, 1, 1, 1, 1, 1, 1);



DROP TABLE IF EXISTS basic_autoincrement;
CREATE TABLE basic_autoincrement (
auto_id INT(11) NOT NULL AUTO_INCREMENT,
Expand Down Expand Up @@ -85,3 +86,34 @@ INSERT INTO test_boolean_conversion (test_name, tinyint_1) VALUES ('test-true-no
CREATE OR REPLACE VIEW test_view AS
SELECT b.test_name
FROM test_boolean_conversion b;

DROP TABLE IF EXISTS test_null_conversion;
CREATE TABLE test_null_conversion (column_a VARCHAR(10));
INSERT INTO test_null_conversion (column_a) VALUES (NULL);

DROP TABLE IF EXISTS test_datetime_conversion;
CREATE TABLE test_datetime_conversion (
column_a DATETIME,
column_b TIMESTAMP,
column_c DATETIME DEFAULT '0000-00-00',
column_d DATETIME DEFAULT '0000-00-00 00:00',
column_e DATETIME DEFAULT '0000-00-00 00:00:00',
column_f TIME
);
INSERT INTO test_datetime_conversion (column_a, column_f) VALUES ('0000-00-00 00:00', '08:15:30');

DROP TABLE IF EXISTS test_index_conversion;
CREATE TABLE test_index_conversion (column_a VARCHAR(10));
CREATE UNIQUE INDEX test_index_conversion ON test_index_conversion (column_a);

DROP TABLE IF EXISTS test_foreign_keys_child;
DROP TABLE IF EXISTS test_foreign_keys_parent;
CREATE TABLE test_foreign_keys_parent (id INT NOT NULL, PRIMARY KEY (id)) ENGINE=INNODB;
CREATE TABLE test_foreign_keys_child (id INT, test_foreign_keys_parent_id INT,
INDEX par_ind (test_foreign_keys_parent_id),
FOREIGN KEY (test_foreign_keys_parent_id) REFERENCES test_foreign_keys_parent(id) ON DELETE CASCADE
) ENGINE=INNODB;

DROP TABLE IF EXISTS test_enum;
CREATE TABLE test_enum (name ENUM('small', 'medium', 'large'));
INSERT INTO test_enum (name) VALUES ('medium');
80 changes: 79 additions & 1 deletion test/integration/convert_to_db_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,22 @@
class ConvertToDbTest < Test::Unit::TestCase

def setup
$stdout = StringIO.new
$stderr = StringIO.new

seed_test_database
@options=get_test_config_by_label(:localmysql_to_db_convert_all)
@mysql2psql = Mysql2psql.new([@options.filepath])
@mysql2psql.convert
@mysql2psql.writer.open
end

def teardown
@mysql2psql.writer.close
delete_files_for_test_config(@options)

$stdout = STDOUT
$stderr = STDERR
end

def exec_sql_on_psql(sql, parameters=nil)
Expand Down Expand Up @@ -50,5 +57,76 @@ def test_boolean_conversion_of_null
assert_nil null_record['bit_1']
assert_nil null_record['tinyint_1']
end


def test_null_conversion
result = exec_sql_on_psql('SELECT column_a FROM test_null_conversion').first
assert_nil result['column_a']
end

def test_datetime_conversion
result = exec_sql_on_psql('SELECT column_a, column_f FROM test_datetime_conversion').first
assert_equal '1970-01-01 00:00:00', result['column_a']
assert_equal '08:15:30', result['column_f']
end

def test_datetime_defaults
result = exec_sql_on_psql(<<-SQL)
SELECT a.attname,
pg_catalog.format_type(a.atttypid, a.atttypmod),
(SELECT substring(pg_catalog.pg_get_expr(d.adbin, d.adrelid) for 128)
FROM pg_catalog.pg_attrdef d
WHERE d.adrelid = a.attrelid AND d.adnum = a.attnum AND a.atthasdef) AS default
FROM pg_catalog.pg_attribute a
WHERE a.attrelid = 'test_datetime_conversion'::regclass AND a.attnum > 0
SQL

assert_equal 6, result.count

result.each do |row|
if row["attname"] == "column_f"
assert_equal "time without time zone", row["format_type"]
else
assert_equal "timestamp without time zone", row["format_type"]
end

case row["attname"]
when "column_a"
assert_nil row["default"]
when "column_b"
assert_equal "now()", row["default"]
when "column_c", "column_d", "column_e"
assert_equal "'1970-01-01 00:00:00'::timestamp without time zone", row["default"]
end
end
end

def test_index_conversion
result = exec_sql_on_psql('SELECT pg_get_indexdef(indexrelid) FROM pg_index WHERE indrelid = \'test_index_conversion\'::regclass').first
assert_equal "CREATE UNIQUE INDEX test_index_conversion_column_a_idx ON test_index_conversion USING btree (column_a)", result["pg_get_indexdef"]
end

def test_foreign_keys
result = exec_sql_on_psql("SELECT conname, pg_catalog.pg_get_constraintdef(r.oid, true) as condef FROM pg_catalog.pg_constraint r WHERE r.conrelid = 'test_foreign_keys_child'::regclass")
expected = {"condef" => "FOREIGN KEY (test_foreign_keys_parent_id) REFERENCES test_foreign_keys_parent(id)", "conname" => "test_foreign_keys_child_test_foreign_keys_parent_id_fkey"}
assert_equal expected, result.first
end

def test_output
$stdout.rewind
actual = $stdout.read

assert_match /Counting rows of test_foreign_keys_child/, actual
end

def test_enum
result = exec_sql_on_psql(<<-SQL)
SELECT r.conname, pg_catalog.pg_get_constraintdef(r.oid, true)
FROM pg_catalog.pg_constraint r
WHERE r.conrelid = 'test_enum'::regclass AND r.contype = 'c'
ORDER BY 1
SQL

assert_equal 1, result.count
assert_equal "CHECK (name::text = ANY (ARRAY['small'::character varying, 'medium'::character varying, 'large'::character varying]::text[]))", result.first["pg_get_constraintdef"]
end
end
4 changes: 4 additions & 0 deletions test/integration/convert_to_file_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,8 @@ def test_autoincrement
def test_should_not_copy_views_as_tables
assert_no_match /CREATE TABLE "test_view"/, content
end

def test_truncate
assert_match /TRUNCATE/, content
end
end
2 changes: 1 addition & 1 deletion test/lib/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def get_test_config_by_label(name)
when :localmysql_to_file_convert_nothing
get_new_test_config(true, ['unobtainium'], ['kryptonite'], true, true, false)
when :localmysql_to_file_convert_all
get_new_test_config(true, [], [], false, false, false)
get_new_test_config(true, [], [], false, false, true)
when :localmysql_to_db_convert_all
get_new_test_config(false, [], [], false, false, false)
when :localmysql_to_db_convert_nothing
Expand Down