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 | |
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 | eq_or_diff($out, <<'## END OF DIFF', "Diff as expected"); |
42 | -- Convert schema 'create1.yml' to 'create2.yml': |
43 | |
44 | BEGIN TRANSACTION; |
45 | |
46 | CREATE TABLE added ( |
47 | id int(11) |
48 | ); |
49 | |
50 | |
46bf5655 |
51 | ALTER TABLE old_name RENAME TO new_name; |
4d438549 |
52 | DROP INDEX FK5302D47D93FE702E ON employee; |
53 | DROP INDEX UC_age_name ON person; |
54 | DROP INDEX u_name ON person; |
55 | -- SQL::Translator::Producer::SQLite cant drop_field |
46bf5655 |
56 | ALTER TABLE new_name ADD COLUMN new_field int; |
4d438549 |
57 | ALTER TABLE person ADD COLUMN is_rock_star tinyint(4) DEFAULT '1'; |
58 | -- SQL::Translator::Producer::SQLite cant alter_field |
59 | -- SQL::Translator::Producer::SQLite cant rename_field |
60 | CREATE UNIQUE INDEX unique_name_person ON person (name); |
61 | CREATE UNIQUE INDEX UC_person_id_person ON person (person_id); |
62 | CREATE UNIQUE INDEX UC_age_name_person ON person (age, name); |
63 | DROP TABLE deleted; |
64 | |
65 | COMMIT; |
66 | ## END OF DIFF |
67 | |
68 | |
69 | $out = SQL::Translator::Diff::schema_diff($source_schema, 'SQLite', $target_schema, 'SQLite', |
70 | { ignore_index_names => 1, |
71 | ignore_constraint_names => 1, |
72 | output_db => 'SQLite', |
73 | }); |
74 | |
75 | eq_or_diff($out, <<'## END OF DIFF', "Diff as expected"); |
76 | -- Convert schema 'create1.yml' to 'create2.yml': |
77 | |
78 | BEGIN TRANSACTION; |
79 | |
80 | CREATE TABLE added ( |
81 | id int(11) |
82 | ); |
83 | |
84 | |
85 | |
86 | CREATE TEMPORARY TABLE employee_temp_alter ( |
87 | position varchar(50) NOT NULL, |
88 | employee_id int(11) NOT NULL, |
89 | PRIMARY KEY (position, employee_id) |
90 | ); |
91 | INSERT INTO employee_temp_alter SELECT position, employee_id FROM employee; |
92 | DROP TABLE employee; |
93 | CREATE TABLE employee ( |
94 | position varchar(50) NOT NULL, |
95 | employee_id int(11) NOT NULL, |
96 | PRIMARY KEY (position, employee_id) |
97 | ); |
98 | INSERT INTO employee SELECT position, employee_id FROM employee_temp_alter; |
99 | DROP TABLE employee_temp_alter; |
100 | |
46bf5655 |
101 | ALTER TABLE old_name RENAME TO new_name; |
102 | ALTER TABLE new_name ADD COLUMN new_field int; |
4d438549 |
103 | CREATE TEMPORARY TABLE person_temp_alter ( |
104 | person_id INTEGER PRIMARY KEY NOT NULL, |
105 | name varchar(20) NOT NULL, |
106 | age int(11) DEFAULT '18', |
107 | weight double(11,2), |
108 | iq int(11) DEFAULT '0', |
109 | is_rock_star tinyint(4) DEFAULT '1', |
110 | physical_description text |
111 | ); |
112 | INSERT INTO person_temp_alter SELECT person_id, name, age, weight, iq, is_rock_star, physical_description FROM person; |
113 | DROP TABLE person; |
114 | CREATE TABLE person ( |
115 | person_id INTEGER PRIMARY KEY NOT NULL, |
116 | name varchar(20) NOT NULL, |
117 | age int(11) DEFAULT '18', |
118 | weight double(11,2), |
119 | iq int(11) DEFAULT '0', |
120 | is_rock_star tinyint(4) DEFAULT '1', |
121 | physical_description text |
122 | ); |
123 | CREATE UNIQUE INDEX unique_name_person02 ON person (name); |
124 | CREATE UNIQUE INDEX UC_person_id_person02 ON person (person_id); |
125 | CREATE UNIQUE INDEX UC_age_name_person02 ON person (age, name); |
126 | INSERT INTO person SELECT person_id, name, age, weight, iq, is_rock_star, physical_description FROM person_temp_alter; |
127 | DROP TABLE person_temp_alter; |
128 | |
129 | DROP TABLE deleted; |
130 | |
131 | COMMIT; |
132 | ## END OF DIFF |
133 | |
134 | |
135 | # Test for sameness |
136 | $out = SQL::Translator::Diff::schema_diff($source_schema, 'MySQL', $source_schema, 'MySQL' ); |
137 | |
138 | eq_or_diff($out, <<'## END OF DIFF', "No differences found"); |
139 | -- Convert schema 'create1.yml' to 'create1.yml': |
140 | |
141 | -- No differences found |
142 | |
143 | ## END OF DIFF |
144 | |