3a2c957255241fa67e5a48f4c00a20afb5f811f1
[dbsrgits/SQL-Translator.git] / t / 30sqlt-new-diff-pgsql.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 use SQL::Translator::Schema::Constants;
14 use Storable 'dclone';
15
16 plan tests => 4;
17
18 use_ok('SQL::Translator::Diff') or die "Cannot continue\n";
19
20 my $tr = SQL::Translator->new;
21
22 my ( $source_schema, $target_schema, $parsed_sql_schema ) = map {
23     my $t = SQL::Translator->new;
24     $t->parser( 'YAML' )
25       or die $tr->error;
26     my $out = $t->translate( catfile($Bin, qw/data diff pgsql/, $_ ) )
27       or die $tr->error;
28     
29     my $schema = $t->schema;
30     unless ( $schema->name ) {
31         $schema->name( $_ );
32     }
33     ($schema);
34 } (qw( create1.yml create2.yml ));
35
36 # Test for differences
37 my $out = SQL::Translator::Diff::schema_diff(
38     $source_schema,
39    'PostgreSQL',
40     $target_schema,
41    'PostgreSQL',
42    {
43      producer_args => {
44          quote_table_names => 0,
45      }
46    }
47 );
48 eq_or_diff($out, <<'## END OF DIFF', "Diff as expected");
49 -- Convert schema 'create1.yml' to 'create2.yml':;
50
51 BEGIN;
52
53 CREATE TABLE added (
54   "id" bigint
55 );
56
57 ALTER TABLE old_name RENAME TO new_name;
58
59 ALTER TABLE employee DROP CONSTRAINT FK5302D47D93FE702E;
60
61 ALTER TABLE person DROP CONSTRAINT UC_age_name;
62
63 DROP INDEX u_name;
64
65 ALTER TABLE employee DROP COLUMN job_title;
66
67 ALTER TABLE new_name ADD COLUMN new_field integer;
68
69 ALTER TABLE person ADD COLUMN is_rock_star smallint DEFAULT '1';
70
71 ALTER TABLE person ALTER COLUMN person_id TYPE serial;
72
73 ALTER TABLE person ALTER COLUMN name SET NOT NULL;
74
75 ALTER TABLE person ALTER COLUMN age SET DEFAULT 18;
76
77 ALTER TABLE person ALTER COLUMN iq TYPE bigint;
78
79 ALTER TABLE person RENAME COLUMN description TO physical_description;
80
81 ALTER TABLE person ADD CONSTRAINT "unique_name" UNIQUE (name);
82
83 ALTER TABLE employee ADD FOREIGN KEY (employee_id)
84   REFERENCES person (person_id) DEFERRABLE;
85
86 ALTER TABLE person ADD CONSTRAINT "UC_person_id" UNIQUE (person_id);
87
88 ALTER TABLE person ADD CONSTRAINT "UC_age_name" UNIQUE (age, name);
89
90 DROP TABLE deleted CASCADE;
91
92
93 COMMIT;
94
95 ## END OF DIFF
96
97 $out = SQL::Translator::Diff::schema_diff(
98     $source_schema, 'PostgreSQL', $target_schema, 'PostgreSQL',
99     { ignore_index_names => 1,
100       ignore_constraint_names => 1,
101       producer_args => {
102          quote_table_names => 0,
103          quote_field_names => 0,
104       }
105     });
106
107 eq_or_diff($out, <<'## END OF DIFF', "Diff as expected");
108 -- Convert schema 'create1.yml' to 'create2.yml':;
109
110 BEGIN;
111
112 CREATE TABLE added (
113   id bigint
114 );
115
116 ALTER TABLE old_name RENAME TO new_name;
117
118 ALTER TABLE person DROP CONSTRAINT UC_age_name;
119
120 ALTER TABLE employee DROP COLUMN job_title;
121
122 ALTER TABLE new_name ADD COLUMN new_field integer;
123
124 ALTER TABLE person ADD COLUMN is_rock_star smallint DEFAULT '1';
125
126 ALTER TABLE person ALTER COLUMN person_id TYPE serial;
127
128 ALTER TABLE person ALTER COLUMN name SET NOT NULL;
129
130 ALTER TABLE person ALTER COLUMN age SET DEFAULT 18;
131
132 ALTER TABLE person ALTER COLUMN iq TYPE bigint;
133
134 ALTER TABLE person RENAME COLUMN description TO physical_description;
135
136 ALTER TABLE person ADD CONSTRAINT "UC_person_id" UNIQUE (person_id);
137
138 ALTER TABLE person ADD CONSTRAINT "UC_age_name" UNIQUE (age, name);
139
140 DROP TABLE deleted CASCADE;
141
142
143 COMMIT;
144
145 ## END OF DIFF
146
147
148 # Test for sameness
149 $out = SQL::Translator::Diff::schema_diff(
150     $source_schema, 'PostgreSQL', $source_schema, 'PostgreSQL'
151 );
152
153 eq_or_diff($out, <<'## END OF DIFF', "No differences found");
154 -- Convert schema 'create1.yml' to 'create1.yml':;
155
156 -- No differences found;
157
158 ## END OF DIFF