143f6a57e112834cf2883348dacbe1f84e98d69b
[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 );
103
104 INSERT INTO employee_temp_alter SELECT position, employee_id FROM employee;
105
106 DROP TABLE employee;
107
108 CREATE TABLE employee (
109   position varchar(50) NOT NULL,
110   employee_id int(11) NOT NULL,
111   PRIMARY KEY (position, employee_id)
112 );
113
114 INSERT INTO employee SELECT position, employee_id FROM employee_temp_alter;
115
116 DROP TABLE employee_temp_alter;
117
118 ALTER TABLE old_name RENAME TO new_name;
119
120 ALTER TABLE new_name ADD COLUMN new_field int;
121
122 CREATE TEMPORARY TABLE person_temp_alter (
123   person_id INTEGER PRIMARY KEY NOT NULL,
124   name varchar(20) NOT NULL,
125   age int(11) DEFAULT '18',
126   weight double(11,2),
127   iq int(11) DEFAULT '0',
128   is_rock_star tinyint(4) DEFAULT '1',
129   physical_description text
130 );
131
132 INSERT INTO person_temp_alter SELECT person_id, name, age, weight, iq, is_rock_star, physical_description FROM person;
133
134 DROP TABLE person;
135
136 CREATE TABLE person (
137   person_id INTEGER PRIMARY KEY NOT NULL,
138   name varchar(20) NOT NULL,
139   age int(11) DEFAULT '18',
140   weight double(11,2),
141   iq int(11) DEFAULT '0',
142   is_rock_star tinyint(4) DEFAULT '1',
143   physical_description text
144 );
145
146 CREATE UNIQUE INDEX unique_name02 ON person (name);
147
148 CREATE UNIQUE INDEX UC_person_id02 ON person (person_id);
149
150 CREATE UNIQUE INDEX UC_age_name02 ON person (age, name);
151
152 INSERT INTO person SELECT person_id, name, age, weight, iq, is_rock_star, physical_description FROM person_temp_alter;
153
154 DROP TABLE person_temp_alter;
155
156 DROP TABLE deleted;
157
158
159 COMMIT;
160
161 ## END OF DIFF
162
163 # Note the 02 in the 3 names above (end of diff) are an implementation
164 # quirk - there is nothing to reset the global seen-names register
165 # The rewrite should abolish this altogether, and carry the register in
166 # the main schema object
167
168 # Test for sameness
169 $out = SQL::Translator::Diff::schema_diff($source_schema, 'MySQL', $source_schema, 'MySQL' );
170
171 eq_or_diff($out, <<'## END OF DIFF', "No differences found");
172 -- Convert schema 'create1.yml' to 'create1.yml':;
173
174 -- No differences found;
175
176 ## END OF DIFF
177