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