1#!/usr/bin/perl
2# vim: set ft=perl:
3
4use strict;
5use warnings;
6use SQL::Translator;
7
8use File::Spec::Functions qw(catfile updir tmpdir);
9use FindBin qw($Bin);
10use Test::More;
11use Test::Differences;
12use Test::SQL::Translator qw(maybe_plan);
13
14plan tests => 4;
15
16use_ok('SQL::Translator::Diff') or die "Cannot continue\n";
17
18my $tr            = SQL::Translator->new;
19
20my ( $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
35my $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
42eq_or_diff($out, <<'## END OF DIFF', "Diff as expected");
43-- Convert schema 'create1.yml' to 'create2.yml':;
44
45BEGIN;
46
47CREATE TABLE added (
48  id int(11)
49);
50
51ALTER TABLE old_name RENAME TO new_name;
52
53DROP INDEX FK5302D47D93FE702E;
54
55DROP INDEX UC_age_name;
56
57DROP INDEX u_name;
58
59-- SQL::Translator::Producer::SQLite cant drop_field;
60
61ALTER TABLE new_name ADD COLUMN new_field int;
62
63ALTER 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
69CREATE UNIQUE INDEX unique_name ON person (name);
70
71CREATE UNIQUE INDEX UC_person_id ON person (person_id);
72
73CREATE UNIQUE INDEX UC_age_name ON person (age, name);
74
75DROP TABLE deleted;
76
77
78COMMIT;
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
89eq_or_diff($out, <<'## END OF DIFF', "Diff as expected");
90-- Convert schema 'create1.yml' to 'create2.yml':;
91
92BEGIN;
93
94CREATE TABLE added (
95  id int(11)
96);
97
98CREATE 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
105INSERT INTO employee_temp_alter( position, employee_id) SELECT position, employee_id FROM employee;
106
107DROP TABLE employee;
108
109CREATE 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
116INSERT INTO employee SELECT position, employee_id FROM employee_temp_alter;
117
118DROP TABLE employee_temp_alter;
119
120ALTER TABLE old_name RENAME TO new_name;
121
122ALTER TABLE new_name ADD COLUMN new_field int;
123
124CREATE 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  value double(8,2) DEFAULT 0.00,
132  physical_description text
133);
134
135INSERT INTO person_temp_alter( person_id, name, age, weight, iq, value, physical_description) SELECT person_id, name, age, weight, iq, value, description FROM person;
136
137DROP TABLE person;
138
139CREATE TABLE person (
140  person_id INTEGER PRIMARY KEY NOT NULL,
141  name varchar(20) NOT NULL,
142  age int(11) DEFAULT 18,
143  weight double(11,2),
144  iq int(11) DEFAULT 0,
145  is_rock_star tinyint(4) DEFAULT 1,
146  value double(8,2) DEFAULT 0.00,
147  physical_description text
148);
149
150CREATE UNIQUE INDEX unique_name02 ON person (name);
151
152CREATE UNIQUE INDEX UC_person_id02 ON person (person_id);
153
154CREATE UNIQUE INDEX UC_age_name02 ON person (age, name);
155
156INSERT INTO person SELECT person_id, name, age, weight, iq, is_rock_star, value, physical_description FROM person_temp_alter;
157
158DROP TABLE person_temp_alter;
159
160DROP TABLE deleted;
161
162
163COMMIT;
164
165## END OF DIFF
166
167# Note the 02 in the 3 names above (end of diff) are an implementation
168# quirk - there is nothing to reset the global seen-names register
169# The rewrite should abolish this altogether, and carry the register in
170# the main schema object
171
172# Test for sameness
173$out = SQL::Translator::Diff::schema_diff($source_schema, 'MySQL', $source_schema, 'MySQL' );
174
175eq_or_diff($out, <<'## END OF DIFF', "No differences found");
176-- Convert schema 'create1.yml' to 'create1.yml':;
177
178-- No differences found;
179
180## END OF DIFF
181
182