8 use File::Spec::Functions qw(catfile updir tmpdir);
11 use Test::Differences;
12 use Test::SQL::Translator qw(maybe_plan);
13 use SQL::Translator::Schema::Constants;
14 use Storable 'dclone';
18 use_ok('SQL::Translator::Diff') or die "Cannot continue\n";
20 my $tr = SQL::Translator->new;
22 my ( $source_schema, $target_schema, $parsed_sql_schema ) = map {
23 my $t = SQL::Translator->new;
26 my $out = $t->translate( catfile($Bin, qw/data diff pgsql/, $_ ) )
29 my $schema = $t->schema;
30 unless ( $schema->name ) {
34 } (qw( create1.yml create2.yml ));
36 # Test for differences
37 my $out = SQL::Translator::Diff::schema_diff(
44 quote_identifiers => 0,
49 eq_or_diff($out, <<'## END OF DIFF', "Diff as expected");
50 -- Convert schema 'create1.yml' to 'create2.yml':;
58 ALTER TABLE old_name RENAME TO new_name;
60 ALTER TABLE employee DROP CONSTRAINT FK5302D47D93FE702E;
62 ALTER TABLE person DROP CONSTRAINT UC_age_name;
66 ALTER TABLE employee DROP COLUMN job_title;
68 ALTER TABLE new_name ADD COLUMN new_field integer;
70 ALTER TABLE person ADD COLUMN is_rock_star smallint DEFAULT 1;
72 ALTER TABLE person ALTER COLUMN person_id TYPE serial;
74 ALTER TABLE person ALTER COLUMN name SET NOT NULL;
76 ALTER TABLE person ALTER COLUMN age SET DEFAULT 18;
78 ALTER TABLE person ALTER COLUMN iq TYPE bigint;
80 ALTER TABLE person RENAME COLUMN description TO physical_description;
82 ALTER TABLE person ADD CONSTRAINT unique_name UNIQUE (name);
84 ALTER TABLE employee ADD CONSTRAINT FK5302D47D93FE702E_diff FOREIGN KEY (employee_id)
85 REFERENCES person (person_id) DEFERRABLE;
87 ALTER TABLE person ADD CONSTRAINT UC_person_id UNIQUE (person_id);
89 ALTER TABLE person ADD CONSTRAINT UC_age_name UNIQUE (age, name);
91 DROP TABLE deleted CASCADE;
98 $out = SQL::Translator::Diff::schema_diff(
99 $source_schema, 'PostgreSQL', $target_schema, 'PostgreSQL',
100 { ignore_index_names => 1,
101 ignore_constraint_names => 1,
103 quote_table_names => 0,
104 quote_field_names => 0,
108 eq_or_diff($out, <<'## END OF DIFF', "Diff as expected");
109 -- Convert schema 'create1.yml' to 'create2.yml':;
117 ALTER TABLE old_name RENAME TO new_name;
119 ALTER TABLE person DROP CONSTRAINT UC_age_name;
121 ALTER TABLE employee DROP COLUMN job_title;
123 ALTER TABLE new_name ADD COLUMN new_field integer;
125 ALTER TABLE person ADD COLUMN is_rock_star smallint DEFAULT 1;
127 ALTER TABLE person ALTER COLUMN person_id TYPE serial;
129 ALTER TABLE person ALTER COLUMN name SET NOT NULL;
131 ALTER TABLE person ALTER COLUMN age SET DEFAULT 18;
133 ALTER TABLE person ALTER COLUMN iq TYPE bigint;
135 ALTER TABLE person RENAME COLUMN description TO physical_description;
137 ALTER TABLE person ADD CONSTRAINT UC_person_id UNIQUE (person_id);
139 ALTER TABLE person ADD CONSTRAINT UC_age_name UNIQUE (age, name);
141 DROP TABLE deleted CASCADE;
150 $out = SQL::Translator::Diff::schema_diff(
151 $source_schema, 'PostgreSQL', $source_schema, 'PostgreSQL'
154 eq_or_diff($out, <<'## END OF DIFF', "No differences found");
155 -- Convert schema 'create1.yml' to 'create1.yml':;
157 -- No differences found;