8 use File::Spec::Functions qw(catfile updir tmpdir);
11 use Test::Differences;
12 use Test::SQL::Translator qw(maybe_plan);
13 use SQL::Translator::Schema::Constants;
14 use Storable 'dclone';
18 use_ok('SQL::Translator::Diff') or die "Cannot continue\n";
20 my $tr = SQL::Translator->new;
22 my ( $source_schema, $target_schema, $parsed_sql_schema ) = map {
23 my $t = SQL::Translator->new;
26 my $out = $t->translate( catfile($Bin, qw/data diff/, $_ ) )
29 my $schema = $t->schema;
30 unless ( $schema->name ) {
34 } (qw( create1.yml create2.yml ));
36 # Test for differences
37 my $out = SQL::Translator::Diff::schema_diff( $source_schema, 'MySQL', $target_schema, 'MySQL', { no_batch_alters => 1, producer_options => { quote_table_names => 0 } } );
38 eq_or_diff($out, <<'## END OF DIFF', "Diff as expected");
39 -- Convert schema 'create1.yml' to 'create2.yml':;
43 SET foreign_key_checks=0;
49 SET foreign_key_checks=1;
51 ALTER TABLE old_name RENAME TO new_name;
53 ALTER TABLE employee DROP FOREIGN KEY FK5302D47D93FE702E;
55 ALTER TABLE person DROP INDEX UC_age_name;
57 ALTER TABLE person DROP INDEX u_name;
59 ALTER TABLE employee DROP COLUMN job_title;
61 ALTER TABLE new_name ADD COLUMN new_field integer;
63 ALTER TABLE person ADD COLUMN is_rock_star tinyint(4) DEFAULT '1';
65 ALTER TABLE person CHANGE COLUMN person_id person_id integer(11) NOT NULL auto_increment;
67 ALTER TABLE person CHANGE COLUMN name name varchar(20) NOT NULL;
69 ALTER TABLE person CHANGE COLUMN age age integer(11) DEFAULT '18';
71 ALTER TABLE person CHANGE COLUMN iq iq integer(11) DEFAULT '0';
73 ALTER TABLE person CHANGE COLUMN description physical_description text;
75 ALTER TABLE person ADD UNIQUE INDEX unique_name (name);
77 ALTER TABLE employee ADD CONSTRAINT FK5302D47D93FE702E_diff FOREIGN KEY (employee_id) REFERENCES person (person_id);
79 ALTER TABLE person ADD UNIQUE UC_person_id (person_id);
81 ALTER TABLE person ADD UNIQUE UC_age_name (age, name);
83 ALTER TABLE person ENGINE=InnoDB;
85 ALTER TABLE deleted DROP FOREIGN KEY fk_fake;
94 $out = SQL::Translator::Diff::schema_diff($source_schema, 'MySQL', $target_schema, 'MySQL',
95 { ignore_index_names => 1,
96 ignore_constraint_names => 1,
97 producer_options => { quote_table_names => 0 },
100 eq_or_diff($out, <<'## END OF DIFF', "Diff as expected");
101 -- Convert schema 'create1.yml' to 'create2.yml':;
105 SET foreign_key_checks=0;
111 SET foreign_key_checks=1;
113 ALTER TABLE employee DROP COLUMN job_title;
115 ALTER TABLE old_name RENAME TO new_name,
116 ADD COLUMN new_field integer;
118 ALTER TABLE person DROP INDEX UC_age_name,
119 ADD COLUMN is_rock_star tinyint(4) DEFAULT '1',
120 CHANGE COLUMN person_id person_id integer(11) NOT NULL auto_increment,
121 CHANGE COLUMN name name varchar(20) NOT NULL,
122 CHANGE COLUMN age age integer(11) DEFAULT '18',
123 CHANGE COLUMN iq iq integer(11) DEFAULT '0',
124 CHANGE COLUMN description physical_description text,
125 ADD UNIQUE UC_person_id (person_id),
126 ADD UNIQUE UC_age_name (age, name),
129 ALTER TABLE deleted DROP FOREIGN KEY fk_fake;
140 $out = SQL::Translator::Diff::schema_diff($source_schema, 'MySQL', $source_schema, 'MySQL' );
142 eq_or_diff($out, <<'## END OF DIFF', "No differences found");
143 -- Convert schema 'create1.yml' to 'create1.yml':;
145 -- No differences found;
150 my $t = SQL::Translator->new;
151 $t->parser( 'MySQL' )
153 my $out = $t->translate( catfile($Bin, qw/data mysql create.sql/ ) )
156 # Lets remove the renamed table so we dont have to change the SQL or other tests
157 $target_schema->drop_table('new_name');
159 my $schema = $t->schema;
160 unless ( $schema->name ) {
161 $schema->name( 'create.sql' );
164 # Now lets change the type of one of the 'integer' columns so that it
165 # matches what the mysql parser sees for '<col> interger'.
166 my $field = $target_schema->get_table('employee')->get_field('employee_id');
167 $field->data_type('integer');
169 $out = SQL::Translator::Diff::schema_diff($schema, 'MySQL', $target_schema, 'MySQL', { producer_options => { quote_table_names => 0 } } );
170 eq_or_diff($out, <<'## END OF DIFF', "No differences found");
171 -- Convert schema 'create.sql' to 'create2.yml':;
175 SET foreign_key_checks=0;
181 SET foreign_key_checks=1;
183 ALTER TABLE employee DROP FOREIGN KEY FK5302D47D93FE702E,
184 DROP COLUMN job_title,
185 ADD CONSTRAINT FK5302D47D93FE702E_diff FOREIGN KEY (employee_id) REFERENCES person (person_id);
187 ALTER TABLE person DROP INDEX UC_age_name,
189 ADD COLUMN is_rock_star tinyint(4) DEFAULT '1',
190 CHANGE COLUMN person_id person_id integer(11) NOT NULL auto_increment,
191 CHANGE COLUMN name name varchar(20) NOT NULL,
192 CHANGE COLUMN age age integer(11) DEFAULT '18',
193 CHANGE COLUMN iq iq integer(11) DEFAULT '0',
194 CHANGE COLUMN description physical_description text,
195 ADD UNIQUE INDEX unique_name (name),
196 ADD UNIQUE UC_person_id (person_id),
197 ADD UNIQUE UC_age_name (age, name),
208 # Test InnoDB stupidness. Have to drop constraints before re-adding them if
209 # they are just alters.
213 my $s1 = SQL::Translator::Schema->new;
214 my $s2 = SQL::Translator::Schema->new;
216 $s1->name('Schema 1');
217 $s2->name('Schema 2');
219 my $t1 = $s1->add_table($target_schema->get_table('employee'));
220 my $t2 = $s2->add_table(dclone($target_schema->get_table('employee')));
223 my ($c) = grep { $_->name eq 'FK5302D47D93FE702E_diff' } $t2->get_constraints;
224 $c->on_delete('CASCADE');
227 name => 'new_constraint',
228 type => 'FOREIGN KEY',
229 fields => ['employee_id'],
230 reference_fields => ['fake'],
231 reference_table => 'patty',
239 my $out = SQL::Translator::Diff::schema_diff($s1, 'MySQL', $s2, 'MySQL' );
241 eq_or_diff($out, <<'## END OF DIFF', "Batch alter of constraints work for InnoDB");
242 -- Convert schema 'Schema 1' to 'Schema 2':;
246 ALTER TABLE employee DROP FOREIGN KEY FK5302D47D93FE702E_diff;
248 ALTER TABLE employee ADD COLUMN new integer,
249 ADD CONSTRAINT FK5302D47D93FE702E_diff FOREIGN KEY (employee_id) REFERENCES person (person_id) ON DELETE CASCADE,
250 ADD CONSTRAINT new_constraint FOREIGN KEY (employee_id) REFERENCES patty (fake);
259 # Test other things about renaming tables to - namely that renames
260 # constraints are still formated right.
262 my $s1 = SQL::Translator::Schema->new;
263 my $s2 = SQL::Translator::Schema->new;
265 $s1->name('Schema 3');
266 $s2->name('Schema 4');
268 my $t1 = $s1->add_table(dclone($target_schema->get_table('employee')));
269 my $t2 = dclone($target_schema->get_table('employee'));
271 $t2->extra(renamed_from => 'employee');
277 type => 'FOREIGN KEY',
278 fields => ['employee_id'],
279 reference_fields => ['id'],
280 reference_table => 'bar',
284 type => 'FOREIGN KEY',
285 fields => ['employee_id'],
286 reference_fields => ['id'],
287 reference_table => 'foo',
290 my $out = SQL::Translator::Diff::schema_diff($s1, 'MySQL', $s2, 'MySQL' );
291 eq_or_diff($out, <<'## END OF DIFF', "Alter/drop constraints works with rename table");
292 -- Convert schema 'Schema 3' to 'Schema 4':;
296 ALTER TABLE employee RENAME TO fnord,
297 DROP FOREIGN KEY bar_fk,
298 ADD CONSTRAINT foo_fk FOREIGN KEY (employee_id) REFERENCES foo (id);
305 # Test quoting works too.
306 $out = SQL::Translator::Diff::schema_diff($s1, 'MySQL', $s2, 'MySQL',
307 { producer_options => { quote_table_names => '`' } }
309 eq_or_diff($out, <<'## END OF DIFF', "Quoting can be turned on");
310 -- Convert schema 'Schema 3' to 'Schema 4':;
314 ALTER TABLE `employee` RENAME TO `fnord`,
315 DROP FOREIGN KEY bar_fk,
316 ADD CONSTRAINT foo_fk FOREIGN KEY (employee_id) REFERENCES `foo` (id);