By royal decree, produced statements in list context shall not end in a semi-colon...
[dbsrgits/SQL-Translator.git] / t / 30sqlt-new-diff-mysql.t
1 #!/usr/bin/perl
2 # vim: set ft=perl:
3
4 use strict;
5 use warnings;
6 use SQL::Translator;
7
8 use File::Spec::Functions qw(catfile updir tmpdir);
9 use FindBin qw($Bin);
10 use Test::More;
11 use Test::Differences;
12 use Test::SQL::Translator qw(maybe_plan);
13 use SQL::Translator::Schema::Constants;
14 use Storable 'dclone';
15
16 plan tests => 8;
17
18 use_ok('SQL::Translator::Diff') or die "Cannot continue\n";
19
20 my $tr            = SQL::Translator->new;
21
22 my ( $source_schema, $target_schema, $parsed_sql_schema ) = map {
23     my $t = SQL::Translator->new;
24     $t->parser( 'YAML' )
25       or die $tr->error;
26     my $out = $t->translate( catfile($Bin, qw/data diff/, $_ ) )
27       or die $tr->error;
28     
29     my $schema = $t->schema;
30     unless ( $schema->name ) {
31         $schema->name( $_ );
32     }
33     ($schema);
34 } (qw( create1.yml create2.yml ));
35
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':;
40
41 BEGIN;
42
43 SET foreign_key_checks=0;
44
45 CREATE TABLE added (
46   id integer(11)
47 );
48
49 SET foreign_key_checks=1;
50
51 ALTER TABLE old_name RENAME TO new_name;
52
53 ALTER TABLE employee DROP FOREIGN KEY FK5302D47D93FE702E;
54
55 ALTER TABLE person DROP INDEX UC_age_name;
56
57 ALTER TABLE person DROP INDEX u_name;
58
59 ALTER TABLE employee DROP COLUMN job_title;
60
61 ALTER TABLE new_name ADD COLUMN new_field integer;
62
63 ALTER TABLE person ADD COLUMN is_rock_star tinyint(4) DEFAULT '1';
64
65 ALTER TABLE person CHANGE COLUMN person_id person_id integer(11) NOT NULL auto_increment;
66
67 ALTER TABLE person CHANGE COLUMN name name varchar(20) NOT NULL;
68
69 ALTER TABLE person CHANGE COLUMN age age integer(11) DEFAULT '18';
70
71 ALTER TABLE person CHANGE COLUMN iq iq integer(11) DEFAULT '0';
72
73 ALTER TABLE person CHANGE COLUMN description physical_description text;
74
75 ALTER TABLE person ADD UNIQUE INDEX unique_name (name);
76
77 ALTER TABLE employee ADD CONSTRAINT FK5302D47D93FE702E_diff FOREIGN KEY (employee_id) REFERENCES person (person_id);
78
79 ALTER TABLE person ADD UNIQUE UC_person_id (person_id);
80
81 ALTER TABLE person ADD UNIQUE UC_age_name (age, name);
82
83 ALTER TABLE person ENGINE=InnoDB;
84
85 ALTER TABLE deleted DROP FOREIGN KEY fk_fake;
86
87 DROP TABLE deleted;
88
89
90 COMMIT;
91
92 ## END OF DIFF
93
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 },
98     });
99
100 eq_or_diff($out, <<'## END OF DIFF', "Diff as expected");
101 -- Convert schema 'create1.yml' to 'create2.yml':;
102
103 BEGIN;
104
105 SET foreign_key_checks=0;
106
107 CREATE TABLE added (
108   id integer(11)
109 );
110
111 SET foreign_key_checks=1;
112
113 ALTER TABLE employee DROP COLUMN job_title;
114
115 ALTER TABLE old_name RENAME TO new_name,
116                      ADD COLUMN new_field integer;
117
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),
127                    ENGINE=InnoDB;
128
129 ALTER TABLE deleted DROP FOREIGN KEY fk_fake;
130
131 DROP TABLE deleted;
132
133
134 COMMIT;
135
136 ## END OF DIFF
137
138
139 # Test for sameness
140 $out = SQL::Translator::Diff::schema_diff($source_schema, 'MySQL', $source_schema, 'MySQL' );
141
142 eq_or_diff($out, <<'## END OF DIFF', "No differences found");
143 -- Convert schema 'create1.yml' to 'create1.yml':;
144
145 -- No differences found;
146
147 ## END OF DIFF
148
149 {
150   my $t = SQL::Translator->new;
151   $t->parser( 'MySQL' )
152     or die $tr->error;
153   my $out = $t->translate( catfile($Bin, qw/data mysql create.sql/ ) )
154     or die $tr->error;
155
156   # Lets remove the renamed table so we dont have to change the SQL or other tests
157   $target_schema->drop_table('new_name');
158   
159   my $schema = $t->schema;
160   unless ( $schema->name ) {
161       $schema->name( 'create.sql' );
162   }
163
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');
168   $field->size(0);
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':;
172
173 BEGIN;
174
175 SET foreign_key_checks=0;
176
177 CREATE TABLE added (
178   id integer(11)
179 );
180
181 SET foreign_key_checks=1;
182
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);
186
187 ALTER TABLE person DROP INDEX UC_age_name,
188                    DROP INDEX u_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),
198                    ENGINE=InnoDB;
199
200 DROP TABLE deleted;
201
202
203 COMMIT;
204
205 ## END OF DIFF
206 }
207
208 # Test InnoDB stupidness. Have to drop constraints before re-adding them if
209 # they are just alters.
210
211
212 {
213   my $s1 = SQL::Translator::Schema->new;
214   my $s2 = SQL::Translator::Schema->new;
215
216   $s1->name('Schema 1');
217   $s2->name('Schema 2');
218
219   my $t1 = $s1->add_table($target_schema->get_table('employee'));
220   my $t2 = $s2->add_table(dclone($target_schema->get_table('employee')));
221
222
223   my ($c) = grep { $_->name eq 'FK5302D47D93FE702E_diff' } $t2->get_constraints;
224   $c->on_delete('CASCADE');
225
226   $t2->add_constraint(
227     name => 'new_constraint',
228     type => 'FOREIGN KEY',
229     fields => ['employee_id'],
230     reference_fields => ['fake'],
231     reference_table => 'patty',
232   );
233
234   $t2->add_field(
235     name => 'new',
236     data_type => 'int'
237   );
238
239   my $out = SQL::Translator::Diff::schema_diff($s1, 'MySQL', $s2, 'MySQL' );
240
241   eq_or_diff($out, <<'## END OF DIFF', "Batch alter of constraints work for InnoDB");
242 -- Convert schema 'Schema 1' to 'Schema 2':;
243
244 BEGIN;
245
246 ALTER TABLE employee DROP FOREIGN KEY FK5302D47D93FE702E_diff;
247
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);
251
252
253 COMMIT;
254
255 ## END OF DIFF
256 }
257
258 {
259   # Test other things about renaming tables to - namely that renames 
260   # constraints are still formated right.
261
262   my $s1 = SQL::Translator::Schema->new;
263   my $s2 = SQL::Translator::Schema->new;
264
265   $s1->name('Schema 3');
266   $s2->name('Schema 4');
267
268   my $t1 = $s1->add_table(dclone($target_schema->get_table('employee')));
269   my $t2 = dclone($target_schema->get_table('employee'));
270   $t2->name('fnord');
271   $t2->extra(renamed_from => 'employee');
272   $s2->add_table($t2);
273
274
275   $t1->add_constraint(
276     name => 'bar_fk',
277     type => 'FOREIGN KEY',
278     fields => ['employee_id'],
279     reference_fields => ['id'],
280     reference_table => 'bar',
281   );
282   $t2->add_constraint(
283     name => 'foo_fk',
284     type => 'FOREIGN KEY',
285     fields => ['employee_id'],
286     reference_fields => ['id'],
287     reference_table => 'foo',
288   );
289
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':;
293
294 BEGIN;
295
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);
299
300
301 COMMIT;
302
303 ## END OF DIFF
304
305   # Test quoting works too.
306   $out = SQL::Translator::Diff::schema_diff($s1, 'MySQL', $s2, 'MySQL', 
307     { producer_options => { quote_table_names => '`' } }
308   );
309   eq_or_diff($out, <<'## END OF DIFF', "Quoting can be turned on");
310 -- Convert schema 'Schema 3' to 'Schema 4':;
311
312 BEGIN;
313
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);
317
318
319 COMMIT;
320
321 ## END OF DIFF
322 }