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),
102 FOREIGN KEY (employee_id) REFERENCES person(person_id)
105 INSERT INTO employee_temp_alter( position, employee_id) SELECT position, employee_id FROM employee;
109 CREATE TABLE employee (
110 position varchar(50) NOT NULL,
111 employee_id int(11) NOT NULL,
112 PRIMARY KEY (position, employee_id),
113 FOREIGN KEY (employee_id) REFERENCES person(person_id)
116 INSERT INTO employee SELECT position, employee_id FROM employee_temp_alter;
118 DROP TABLE employee_temp_alter;
120 ALTER TABLE old_name RENAME TO new_name;
122 ALTER TABLE new_name ADD COLUMN new_field int;
124 CREATE TEMPORARY TABLE person_temp_alter (
125 person_id INTEGER PRIMARY KEY NOT NULL,
126 name varchar(20) NOT NULL,
127 age int(11) DEFAULT 18,
129 iq int(11) DEFAULT 0,
130 is_rock_star tinyint(4) DEFAULT 1,
131 value double(8,2) DEFAULT 0.00,
132 physical_description text
135 INSERT INTO person_temp_alter( person_id, name, age, weight, iq, is_rock_star, value, physical_description) SELECT person_id, name, age, weight, iq, is_rock_star, value, physical_description FROM person;
139 CREATE TABLE person (
140 person_id INTEGER PRIMARY KEY NOT NULL,
141 name varchar(20) NOT NULL,
142 age int(11) DEFAULT 18,
144 iq int(11) DEFAULT 0,
145 is_rock_star tinyint(4) DEFAULT 1,
146 value double(8,2) DEFAULT 0.00,
147 physical_description text
150 CREATE UNIQUE INDEX unique_name02 ON person (name);
152 CREATE UNIQUE INDEX UC_person_id02 ON person (person_id);
154 CREATE UNIQUE INDEX UC_age_name02 ON person (age, name);
156 INSERT INTO person SELECT person_id, name, age, weight, iq, is_rock_star, value, physical_description FROM person_temp_alter;
158 DROP TABLE person_temp_alter;
167 # Note the 02 in the 3 names above (end of diff) are an implementation
168 # quirk - there is nothing to reset the global seen-names register
169 # The rewrite should abolish this altogether, and carry the register in
170 # the main schema object
173 $out = SQL::Translator::Diff::schema_diff($source_schema, 'MySQL', $source_schema, 'MySQL' );
175 eq_or_diff($out, <<'## END OF DIFF', "No differences found");
176 -- Convert schema 'create1.yml' to 'create1.yml':;
178 -- No differences found;