8 use File::Spec::Functions qw(catfile updir tmpdir);
11 use Test::Differences;
12 use Test::SQL::Translator qw(maybe_plan);
16 use_ok('SQL::Translator::Diff') or die "Cannot continue\n";
18 my $tr = SQL::Translator->new;
20 my ( $source_schema, $target_schema ) = map {
21 my $t = SQL::Translator->new;
24 my $out = $t->translate( catfile($Bin, qw/data diff/, $_ ) )
27 my $schema = $t->schema;
28 unless ( $schema->name ) {
32 } (qw/create1.yml create2.yml/);
34 # Test for differences
35 my $out = SQL::Translator::Diff::schema_diff( $source_schema, 'SQLite', $target_schema, 'SQLite',
36 { no_batch_alters => 1,
37 ignore_missing_methods => 1,
38 output_db => 'SQLite',
42 eq_or_diff($out, <<'## END OF DIFF', "Diff as expected");
43 -- Convert schema 'create1.yml' to 'create2.yml':;
51 ALTER TABLE old_name RENAME TO new_name;
53 DROP INDEX FK5302D47D93FE702E;
55 DROP INDEX UC_age_name;
59 -- SQL::Translator::Producer::SQLite cant drop_field;
61 ALTER TABLE new_name ADD COLUMN new_field int;
63 ALTER TABLE person ADD COLUMN is_rock_star tinyint(4) DEFAULT 1;
65 -- SQL::Translator::Producer::SQLite cant alter_field;
67 -- SQL::Translator::Producer::SQLite cant rename_field;
69 CREATE UNIQUE INDEX unique_name ON person (name);
71 CREATE UNIQUE INDEX UC_person_id ON person (person_id);
73 CREATE UNIQUE INDEX UC_age_name ON person (age, name);
83 $out = SQL::Translator::Diff::schema_diff($source_schema, 'SQLite', $target_schema, 'SQLite',
84 { ignore_index_names => 1,
85 ignore_constraint_names => 1,
86 output_db => 'SQLite',
89 eq_or_diff($out, <<'## END OF DIFF', "Diff as expected");
90 -- Convert schema 'create1.yml' to 'create2.yml':;
98 CREATE TEMPORARY TABLE employee_temp_alter (
99 position varchar(50) NOT NULL,
100 employee_id int(11) NOT NULL,
101 PRIMARY KEY (position, employee_id)
104 INSERT INTO employee_temp_alter SELECT position, employee_id FROM employee;
108 CREATE TABLE employee (
109 position varchar(50) NOT NULL,
110 employee_id int(11) NOT NULL,
111 PRIMARY KEY (position, employee_id)
114 INSERT INTO employee SELECT position, employee_id FROM employee_temp_alter;
116 DROP TABLE employee_temp_alter;
118 ALTER TABLE old_name RENAME TO new_name;
120 ALTER TABLE new_name ADD COLUMN new_field int;
122 CREATE TEMPORARY TABLE person_temp_alter (
123 person_id INTEGER PRIMARY KEY NOT NULL,
124 name varchar(20) NOT NULL,
125 age int(11) DEFAULT 18,
127 iq int(11) DEFAULT 0,
128 is_rock_star tinyint(4) DEFAULT 1,
129 physical_description text
132 INSERT INTO person_temp_alter SELECT person_id, name, age, weight, iq, is_rock_star, physical_description FROM person;
136 CREATE TABLE person (
137 person_id INTEGER PRIMARY KEY NOT NULL,
138 name varchar(20) NOT NULL,
139 age int(11) DEFAULT 18,
141 iq int(11) DEFAULT 0,
142 is_rock_star tinyint(4) DEFAULT 1,
143 physical_description text
146 CREATE UNIQUE INDEX unique_name02 ON person (name);
148 CREATE UNIQUE INDEX UC_person_id02 ON person (person_id);
150 CREATE UNIQUE INDEX UC_age_name02 ON person (age, name);
152 INSERT INTO person SELECT person_id, name, age, weight, iq, is_rock_star, physical_description FROM person_temp_alter;
154 DROP TABLE person_temp_alter;
163 # Note the 02 in the 3 names above (end of diff) are an implementation
164 # quirk - there is nothing to reset the global seen-names register
165 # The rewrite should abolish this altogether, and carry the register in
166 # the main schema object
169 $out = SQL::Translator::Diff::schema_diff($source_schema, 'MySQL', $source_schema, 'MySQL' );
171 eq_or_diff($out, <<'## END OF DIFF', "No differences found");
172 -- Convert schema 'create1.yml' to 'create1.yml':;
174 -- No differences found;