Tab/WS crusade
[dbsrgits/SQL-Translator.git] / t / 30sqlt-new-diff-sqlite.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
14 plan tests => 4;
15
16 use_ok('SQL::Translator::Diff') or die "Cannot continue\n";
17
18 my $tr            = SQL::Translator->new;
19
20 my ( $source_schema, $target_schema ) = map {
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);
32 } (qw/create1.yml create2.yml/);
33
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',
39   }
40 );
41
42 eq_or_diff($out, <<'## END OF DIFF', "Diff as expected");
43 -- Convert schema 'create1.yml' to 'create2.yml':;
44
45 BEGIN;
46
47 CREATE TABLE 'added' (
48   'id' int(11)
49 );
50
51 ALTER TABLE 'old_name' RENAME TO 'new_name';
52
53 DROP INDEX 'FK5302D47D93FE702E';
54
55 DROP INDEX 'UC_age_name';
56
57 DROP INDEX 'u_name';
58
59 -- SQL::Translator::Producer::SQLite cant drop_field;
60
61 ALTER TABLE 'new_name' ADD COLUMN 'new_field' int;
62
63 ALTER TABLE 'person' ADD COLUMN 'is_rock_star' tinyint(4) DEFAULT 1;
64
65 -- SQL::Translator::Producer::SQLite cant alter_field;
66
67 -- SQL::Translator::Producer::SQLite cant rename_field;
68
69 CREATE UNIQUE INDEX 'unique_name' ON 'person' ('name');
70
71 CREATE UNIQUE INDEX 'UC_person_id' ON 'person' ('person_id');
72
73 CREATE UNIQUE INDEX 'UC_age_name' ON 'person' ('age', 'name');
74
75 DROP TABLE 'deleted';
76
77
78 COMMIT;
79
80 ## END OF DIFF
81
82
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',
87     });
88
89 eq_or_diff($out, <<'## END OF DIFF', "Diff as expected");
90 -- Convert schema 'create1.yml' to 'create2.yml':;
91
92 BEGIN;
93
94 CREATE TABLE 'added' (
95   'id' int(11)
96 );
97
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')
103 );
104
105 INSERT INTO 'employee_temp_alter' SELECT 'position', 'employee_id' FROM 'employee';
106
107 DROP TABLE 'employee';
108
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')
114 );
115
116 INSERT INTO 'employee' SELECT 'position', 'employee_id' FROM 'employee_temp_alter';
117
118 DROP TABLE 'employee_temp_alter';
119
120 ALTER TABLE 'old_name' RENAME TO 'new_name';
121
122 ALTER TABLE 'new_name' ADD COLUMN 'new_field' int;
123
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
132 );
133
134 INSERT INTO 'person_temp_alter' SELECT 'person_id', 'name', 'age', 'weight', 'iq', 'is_rock_star', 'physical_description' FROM 'person';
135
136 DROP TABLE 'person';
137
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
146 );
147
148 CREATE UNIQUE INDEX 'unique_name02' ON 'person' ('name');
149
150 CREATE UNIQUE INDEX 'UC_person_id02' ON 'person' ('person_id');
151
152 CREATE UNIQUE INDEX 'UC_age_name02' ON 'person' ('age', 'name');
153
154 INSERT INTO 'person' SELECT 'person_id', 'name', 'age', 'weight', 'iq', 'is_rock_star', 'physical_description' FROM 'person_temp_alter';
155
156 DROP TABLE 'person_temp_alter';
157
158 DROP TABLE 'deleted';
159
160
161 COMMIT;
162
163 ## END OF DIFF
164
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
169
170 # Test for sameness
171 $out = SQL::Translator::Diff::schema_diff($source_schema, 'MySQL', $source_schema, 'MySQL' );
172
173 eq_or_diff($out, <<'## END OF DIFF', "No differences found");
174 -- Convert schema 'create1.yml' to 'create1.yml':;
175
176 -- No differences found;
177
178 ## END OF DIFF
179