Commit | Line | Data |
4d438549 |
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 | |
07d6e5f7 |
14 | plan tests => 5; |
4d438549 |
15 | |
16 | use_ok('SQL::Translator::Diff') or die "Cannot continue\n"; |
17 | |
18 | my $tr = SQL::Translator->new; |
19 | |
07d6e5f7 |
20 | my ( $source_schema, $target_schema, $parsed_sql_schema ) = map { |
4d438549 |
21 | my $t = SQL::Translator->new; |
22 | $t->parser( 'YAML' ) |
23 | or die $tr->error; |
24 | my $out = $t->translate( catfile($Bin, qw/data diff/, $_ ) ) |
25 | or die $tr->error; |
26 | |
27 | my $schema = $t->schema; |
28 | unless ( $schema->name ) { |
29 | $schema->name( $_ ); |
30 | } |
31 | ($schema); |
07d6e5f7 |
32 | } (qw( create1.yml create2.yml )); |
4d438549 |
33 | |
34 | # Test for differences |
35 | my $out = SQL::Translator::Diff::schema_diff( $source_schema, 'MySQL', $target_schema, 'MySQL', { no_batch_alters => 1} ); |
36 | eq_or_diff($out, <<'## END OF DIFF', "Diff as expected"); |
37 | -- Convert schema 'create1.yml' to 'create2.yml': |
38 | |
39 | BEGIN TRANSACTION; |
40 | |
41 | SET foreign_key_checks=0; |
42 | |
43 | |
44 | CREATE TABLE added ( |
45 | id integer(11) |
46 | ); |
47 | |
48 | |
49 | SET foreign_key_checks=1; |
50 | |
51 | |
52 | ALTER TABLE employee DROP FOREIGN KEY FK5302D47D93FE702E; |
53 | ALTER TABLE person DROP UNIQUE UC_age_name; |
54 | ALTER TABLE person DROP INDEX u_name; |
55 | ALTER TABLE employee DROP COLUMN job_title; |
56 | ALTER TABLE person ADD COLUMN is_rock_star tinyint(4) DEFAULT '1'; |
57 | ALTER TABLE person CHANGE COLUMN person_id person_id integer(11) NOT NULL auto_increment; |
58 | ALTER TABLE person CHANGE COLUMN name name varchar(20) NOT NULL; |
59 | ALTER TABLE person CHANGE COLUMN age age integer(11) DEFAULT '18'; |
60 | ALTER TABLE person CHANGE COLUMN iq iq integer(11) DEFAULT '0'; |
61 | ALTER TABLE person CHANGE COLUMN description physical_description text; |
62 | ALTER TABLE person ADD UNIQUE INDEX unique_name (name); |
63 | ALTER TABLE employee ADD CONSTRAINT FK5302D47D93FE702E_diff FOREIGN KEY (employee_id) REFERENCES person (person_id); |
64 | ALTER TABLE person ADD UNIQUE UC_person_id (person_id); |
65 | ALTER TABLE person ADD UNIQUE UC_age_name (age, name); |
66 | ALTER TABLE person ENGINE=InnoDB; |
67 | ALTER TABLE deleted DROP FOREIGN KEY fk_fake; |
68 | DROP TABLE deleted; |
69 | |
70 | COMMIT; |
71 | ## END OF DIFF |
72 | |
4d438549 |
73 | $out = SQL::Translator::Diff::schema_diff($source_schema, 'MySQL', $target_schema, 'MySQL', |
74 | { ignore_index_names => 1, |
75 | ignore_constraint_names => 1 |
76 | }); |
77 | |
78 | eq_or_diff($out, <<'## END OF DIFF', "Diff as expected"); |
79 | -- Convert schema 'create1.yml' to 'create2.yml': |
80 | |
81 | BEGIN TRANSACTION; |
82 | |
83 | SET foreign_key_checks=0; |
84 | |
85 | |
86 | CREATE TABLE added ( |
87 | id integer(11) |
88 | ); |
89 | |
90 | |
91 | SET foreign_key_checks=1; |
92 | |
93 | |
94 | ALTER TABLE employee DROP COLUMN job_title; |
95 | ALTER TABLE person DROP UNIQUE UC_age_name, |
96 | ADD COLUMN is_rock_star tinyint(4) DEFAULT '1', |
97 | CHANGE COLUMN person_id person_id integer(11) NOT NULL auto_increment, |
98 | CHANGE COLUMN name name varchar(20) NOT NULL, |
99 | CHANGE COLUMN age age integer(11) DEFAULT '18', |
100 | CHANGE COLUMN iq iq integer(11) DEFAULT '0', |
101 | CHANGE COLUMN description physical_description text, |
102 | ADD UNIQUE UC_person_id (person_id), |
103 | ADD UNIQUE UC_age_name (age, name), |
104 | ENGINE=InnoDB; |
105 | ALTER TABLE deleted DROP FOREIGN KEY fk_fake; |
106 | DROP TABLE deleted; |
107 | |
108 | COMMIT; |
109 | ## END OF DIFF |
110 | |
111 | |
112 | # Test for sameness |
113 | $out = SQL::Translator::Diff::schema_diff($source_schema, 'MySQL', $source_schema, 'MySQL' ); |
114 | |
115 | eq_or_diff($out, <<'## END OF DIFF', "No differences found"); |
116 | -- Convert schema 'create1.yml' to 'create1.yml': |
117 | |
118 | -- No differences found |
119 | |
120 | ## END OF DIFF |
121 | |
07d6e5f7 |
122 | { |
123 | my $t = SQL::Translator->new; |
124 | $t->parser( 'MySQL' ) |
125 | or die $tr->error; |
126 | my $out = $t->translate( catfile($Bin, qw/data mysql create.sql/ ) ) |
127 | or die $tr->error; |
128 | |
129 | my $schema = $t->schema; |
130 | unless ( $schema->name ) { |
131 | $schema->name( 'create.sql' ); |
132 | } |
133 | |
134 | # Now lets change the type of one of the 'integer' columns so that it |
135 | # matches what the mysql parser sees for '<col> interger'. |
136 | my $field = $target_schema->get_table('employee')->get_field('employee_id'); |
137 | $field->data_type('integer'); |
138 | $field->size(0); |
139 | $out = SQL::Translator::Diff::schema_diff($schema, 'MySQL', $target_schema, 'MySQL' ); |
140 | eq_or_diff($out, <<'## END OF DIFF', "No differences found"); |
141 | -- Convert schema 'create.sql' to 'create2.yml': |
142 | |
143 | BEGIN TRANSACTION; |
144 | |
145 | SET foreign_key_checks=0; |
146 | |
147 | |
148 | CREATE TABLE added ( |
149 | id integer(11) |
150 | ); |
151 | |
152 | |
153 | SET foreign_key_checks=1; |
154 | |
155 | |
156 | ALTER TABLE employee DROP FOREIGN KEY FK5302D47D93FE702E, |
157 | DROP COLUMN job_title, |
1c680eb9 |
158 | ADD CONSTRAINT FK5302D47D93FE702E_diff FOREIGN KEY (employee_id) REFERENCES person (person_id); |
07d6e5f7 |
159 | ALTER TABLE person DROP UNIQUE UC_age_name, |
160 | DROP INDEX u_name, |
161 | ADD COLUMN is_rock_star tinyint(4) DEFAULT '1', |
162 | CHANGE COLUMN person_id person_id integer(11) NOT NULL auto_increment, |
163 | CHANGE COLUMN name name varchar(20) NOT NULL, |
164 | CHANGE COLUMN age age integer(11) DEFAULT '18', |
165 | CHANGE COLUMN iq iq integer(11) DEFAULT '0', |
166 | CHANGE COLUMN description physical_description text, |
167 | ADD UNIQUE INDEX unique_name (name), |
168 | ADD UNIQUE UC_person_id (person_id), |
169 | ADD UNIQUE UC_age_name (age, name), |
170 | ENGINE=InnoDB; |
171 | DROP TABLE deleted; |
172 | |
173 | COMMIT; |
174 | ## END OF DIFF |
175 | } |