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':;
47 CREATE TABLE 'added' (
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':;
94 CREATE TABLE 'added' (
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' SELECT 'position', 'employee_id' FROM 'employee';
107 DROP TABLE '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,
128 'weight' double(11,2),
129 'iq' int(11) DEFAULT 0,
130 'is_rock_star' tinyint(4) DEFAULT 1,
131 'physical_description' text
134 INSERT INTO 'person_temp_alter' SELECT 'person_id', 'name', 'age', 'weight', 'iq', 'is_rock_star', 'physical_description' FROM 'person';
138 CREATE TABLE 'person' (
139 'person_id' INTEGER PRIMARY KEY NOT NULL,
140 'name' varchar(20) NOT NULL,
141 'age' int(11) DEFAULT 18,
142 'weight' double(11,2),
143 'iq' int(11) DEFAULT 0,
144 'is_rock_star' tinyint(4) DEFAULT 1,
145 'physical_description' text
148 CREATE UNIQUE INDEX 'unique_name02' ON 'person' ('name');
150 CREATE UNIQUE INDEX 'UC_person_id02' ON 'person' ('person_id');
152 CREATE UNIQUE INDEX 'UC_age_name02' ON 'person' ('age', 'name');
154 INSERT INTO 'person' SELECT 'person_id', 'name', 'age', 'weight', 'iq', 'is_rock_star', 'physical_description' FROM 'person_temp_alter';
156 DROP TABLE 'person_temp_alter';
158 DROP TABLE 'deleted';
165 # Note the 02 in the 3 names above (end of diff) are an implementation
166 # quirk - there is nothing to reset the global seen-names register
167 # The rewrite should abolish this altogether, and carry the register in
168 # the main schema object
171 $out = SQL::Translator::Diff::schema_diff($source_schema, 'MySQL', $source_schema, 'MySQL' );
173 eq_or_diff($out, <<'## END OF DIFF', "No differences found");
174 -- Convert schema 'create1.yml' to 'create1.yml':;
176 -- No differences found;