fixed alter_drop_constraint for foreign keys and applying multiple changes via alter_...
[dbsrgits/SQL-Translator.git] / t / 47postgres-producer.t
CommitLineData
8c4efd11 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6use Test::More;
7use Test::Exception;
8use Test::SQL::Translator qw(maybe_plan);
5f31ed66 9use SQL::Translator::Schema::Constants;
8c4efd11 10use Data::Dumper;
11use FindBin qw/$Bin/;
12
13# Testing 1,2,3,4...
14#=============================================================================
15
16BEGIN {
c50d1a0a 17 maybe_plan(48,
8c4efd11 18 'SQL::Translator::Producer::PostgreSQL',
19 'Test::Differences',
20 )
21}
22use Test::Differences;
23use SQL::Translator;
24
bc8e2aa1 25my $PRODUCER = \&SQL::Translator::Producer::PostgreSQL::create_field;
8c4efd11 26
27my $table = SQL::Translator::Schema::Table->new( name => 'mytable');
28
29my $field1 = SQL::Translator::Schema::Field->new( name => 'myfield',
30 table => $table,
31 data_type => 'VARCHAR',
32 size => 10,
33 default_value => undef,
34 is_auto_increment => 0,
35 is_nullable => 1,
36 is_foreign_key => 0,
37 is_unique => 0 );
38
39my $field1_sql = SQL::Translator::Producer::PostgreSQL::create_field($field1);
40
41is($field1_sql, 'myfield character varying(10)', 'Create field works');
42
aacb3187 43my $field_array = SQL::Translator::Schema::Field->new( name => 'myfield',
44 table => $table,
45 data_type => 'character varying[]',
46 size => 10,
47 default_value => undef,
48 is_auto_increment => 0,
49 is_nullable => 1,
50 is_foreign_key => 0,
51 is_unique => 0 );
52
53my $field_array_sql = SQL::Translator::Producer::PostgreSQL::create_field($field_array);
54
55is($field_array_sql, 'myfield character varying(10)[]', 'Create field works');
56
8c4efd11 57my $field2 = SQL::Translator::Schema::Field->new( name => 'myfield',
58 table => $table,
59 data_type => 'VARCHAR',
60 size => 25,
61 default_value => undef,
62 is_auto_increment => 0,
63 is_nullable => 0,
64 is_foreign_key => 0,
65 is_unique => 0 );
66
c50d1a0a 67my $pk_constraint = SQL::Translator::Schema::Constraint->new(
68 table => $table,
69 name => 'foo',
70 fields => [qw(myfield)],
71 type => 'PRIMARY_KEY',
72);
73
74my ($pk_constraint_def_ref, $pk_constraint_fk_ref ) = SQL::Translator::Producer::PostgreSQL::create_constraint($pk_constraint);
75ok(@{$pk_constraint_def_ref} == 1 && @{$pk_constraint_fk_ref} == 0, 'precheck of create_Primary Key constraint');
76is($pk_constraint_def_ref->[0], 'CONSTRAINT foo PRIMARY KEY (myfield)', 'Create Primary Key Constraint works');
77
78my $alter_pk_constraint = SQL::Translator::Producer::PostgreSQL::alter_drop_constraint($pk_constraint);
79is($alter_pk_constraint, 'ALTER TABLE mytable DROP CONSTRAINT foo', 'Alter drop Primary Key constraint works');
80
81my $table2 = SQL::Translator::Schema::Table->new( name => 'mytable2');
82
83my $field1_2 = SQL::Translator::Schema::Field->new( name => 'myfield_2',
84 table => $table,
85 data_type => 'VARCHAR',
86 size => 10,
87 default_value => undef,
88 is_auto_increment => 0,
89 is_nullable => 1,
90 is_foreign_key => 0,
91 is_unique => 0 );
92
93my $fk_constraint = SQL::Translator::Schema::Constraint->new(
94 table => $table,
95 name => 'foo',
96 fields => [qw(myfield)],
97 type => 'FOREIGN_KEY',
98 reference_table => $table2,
99 reference_fields => [qw(myfield_2)],
100);
101
102my ($fk_constraint_def_ref, $fk_constraint_fk_ref ) = SQL::Translator::Producer::PostgreSQL::create_constraint($fk_constraint);
103
104ok(@{$fk_constraint_def_ref} == 0 && @{$fk_constraint_fk_ref} == 1, 'precheck of create_Foreign Key constraint');
105is($fk_constraint_fk_ref->[0], 'ALTER TABLE mytable ADD FOREIGN KEY (myfield)
106 REFERENCES mytable2 (myfield_2) DEFERRABLE', 'Create Foreign Key Constraint works');
107
108my $alter_fk_constraint = SQL::Translator::Producer::PostgreSQL::alter_drop_constraint($fk_constraint);
109is($alter_fk_constraint, 'ALTER TABLE mytable DROP CONSTRAINT mytable_myfield_fkey', 'Alter drop Foreign Key constraint works');
110
8c4efd11 111my $alter_field = SQL::Translator::Producer::PostgreSQL::alter_field($field1,
112 $field2);
c50d1a0a 113is($alter_field, qq[ALTER TABLE mytable ALTER COLUMN myfield SET NOT NULL;
3406fd5b 114ALTER TABLE mytable ALTER COLUMN myfield TYPE character varying(25)],
8c4efd11 115 'Alter field works');
116
c50d1a0a 117my $field1_complex = SQL::Translator::Schema::Field->new(
118 name => 'my_complex_field',
119 table => $table,
120 data_type => 'VARCHAR',
121 size => 10,
122 default_value => undef,
123 is_auto_increment => 0,
124 is_nullable => 1,
125 is_foreign_key => 0,
126 is_unique => 0
127);
128
129my $field2_complex = SQL::Translator::Schema::Field->new(
130 name => 'my_altered_field',
131 table => $table,
132 data_type => 'VARCHAR',
133 size => 60,
134 default_value => 'whatever',
135 is_auto_increment => 0,
136 is_nullable => 1,
137 is_foreign_key => 0,
138 is_unique => 0
139);
140
141my $alter_field_complex = SQL::Translator::Producer::PostgreSQL::alter_field($field1_complex, $field2_complex);
142is(
143 $alter_field_complex,
144 q{ALTER TABLE mytable RENAME COLUMN my_complex_field TO my_altered_field;
145ALTER TABLE mytable ALTER COLUMN my_altered_field TYPE character varying(60);
146ALTER TABLE mytable ALTER COLUMN my_altered_field SET DEFAULT 'whatever'},
147 'Complex Alter field works'
148);
149
8c4efd11 150$field1->name('field3');
151my $add_field = SQL::Translator::Producer::PostgreSQL::add_field($field1);
152
3406fd5b 153is($add_field, 'ALTER TABLE mytable ADD COLUMN field3 character varying(10)', 'Add field works');
8c4efd11 154
155my $drop_field = SQL::Translator::Producer::PostgreSQL::drop_field($field2);
3406fd5b 156is($drop_field, 'ALTER TABLE mytable DROP COLUMN myfield', 'Drop field works');
8c4efd11 157
e56dabb7 158my $field3 = SQL::Translator::Schema::Field->new( name => 'time_field',
159 table => $table,
160 data_type => 'TIME',
161 default_value => undef,
162 is_auto_increment => 0,
163 is_nullable => 0,
164 is_foreign_key => 0,
165 is_unique => 0 );
166
167my $field3_sql = SQL::Translator::Producer::PostgreSQL::create_field($field3);
168
169is($field3_sql, 'time_field time NOT NULL', 'Create time field works');
170
621cb859 171my $field3_datetime_with_TZ = SQL::Translator::Schema::Field->new(
172 name => 'datetime_with_TZ',
173 table => $table,
174 data_type => 'timestamp with time zone',
175 size => 7,
176);
177
178my $field3_datetime_with_TZ_sql =
179 SQL::Translator::Producer::PostgreSQL::create_field(
180 $field3_datetime_with_TZ
181 );
182
183is(
184 $field3_datetime_with_TZ_sql,
185 'datetime_with_TZ timestamp(6) with time zone',
186 'Create time field with time zone and size, works'
187);
188
189my $field3_time_without_TZ = SQL::Translator::Schema::Field->new(
190 name => 'time_without_TZ',
191 table => $table,
192 data_type => 'time without time zone',
193 size => 2,
194);
195
196my $field3_time_without_TZ_sql
197 = SQL::Translator::Producer::PostgreSQL::create_field(
198 $field3_time_without_TZ
199 );
200
201is(
202 $field3_time_without_TZ_sql,
203 'time_without_TZ time(2) without time zone',
204 'Create time field without time zone but with size, works'
205);
206
c3bddac9 207my $field_num = SQL::Translator::Schema::Field->new( name => 'num',
208 table => $table,
209 data_type => 'numeric',
210 size => [10,2],
211 );
212my $fieldnum_sql = SQL::Translator::Producer::PostgreSQL::create_field($field_num);
213
214is($fieldnum_sql, 'num numeric(10,2)', 'Create numeric field works');
215
216
e56dabb7 217my $field4 = SQL::Translator::Schema::Field->new( name => 'bytea_field',
218 table => $table,
219 data_type => 'bytea',
220 size => '16777215',
221 default_value => undef,
222 is_auto_increment => 0,
223 is_nullable => 0,
224 is_foreign_key => 0,
225 is_unique => 0 );
226
227my $field4_sql = SQL::Translator::Producer::PostgreSQL::create_field($field4);
8c4efd11 228
e56dabb7 229is($field4_sql, 'bytea_field bytea NOT NULL', 'Create bytea field works');
5342f5c1 230
231my $field5 = SQL::Translator::Schema::Field->new( name => 'enum_field',
232 table => $table,
233 data_type => 'enum',
234 extra => { list => [ 'Foo', 'Bar' ] },
235 is_auto_increment => 0,
236 is_nullable => 0,
237 is_foreign_key => 0,
238 is_unique => 0 );
239
240my $field5_sql = SQL::Translator::Producer::PostgreSQL::create_field($field5,{ postgres_version => 8.3 });
241
242is($field5_sql, 'enum_field mytable_enum_field_type NOT NULL', 'Create real enum field works');
243
90726ffd 244
245
246
247my $field6 = SQL::Translator::Schema::Field->new(
248 name => 'character',
249 table => $table,
250 data_type => 'character',
251 size => '123',
252 default_value => 'foobar',
253 is_auto_increment => 0,
254 is_nullable => 0,
255 is_foreign_key => 0,
256 is_unique => 0);
257
258my $field7 = SQL::Translator::Schema::Field->new(
259 name => 'character',
260 table => $table,
261 data_type => 'character',
262 size => '123',
263 default_value => undef,
264 is_auto_increment => 0,
265 is_nullable => 0,
266 is_foreign_key => 0,
267 is_unique => 0);
268
269$alter_field = SQL::Translator::Producer::PostgreSQL::alter_field($field6,
270 $field7);
271
272is($alter_field, q(ALTER TABLE mytable ALTER COLUMN character DROP DEFAULT), 'DROP DEFAULT');
273
274$field7->default_value(q(foo'bar'));
275
276$alter_field = SQL::Translator::Producer::PostgreSQL::alter_field($field6,
277 $field7);
278
279is($alter_field, q(ALTER TABLE mytable ALTER COLUMN character SET DEFAULT 'foo''bar'''), 'DEFAULT with escaping');
280
281$field7->default_value(\q(foobar));
282
283$alter_field = SQL::Translator::Producer::PostgreSQL::alter_field($field6,
284 $field7);
285
286is($alter_field, q(ALTER TABLE mytable ALTER COLUMN character SET DEFAULT foobar), 'DEFAULT unescaped if scalarref');
287
288$field7->is_nullable(1);
289$field7->default_value(q(foobar));
290
291$alter_field = SQL::Translator::Producer::PostgreSQL::alter_field($field6,
292 $field7);
293
294is($alter_field, q(ALTER TABLE mytable ALTER COLUMN character DROP NOT NULL), 'DROP NOT NULL');
295
ad258776 296my $field8 = SQL::Translator::Schema::Field->new( name => 'ts_field',
297 table => $table,
298 data_type => 'timestamp with time zone',
299 size => 6,
300 is_auto_increment => 0,
301 is_nullable => 0,
302 is_foreign_key => 0,
303 is_unique => 0 );
304
305my $field8_sql = SQL::Translator::Producer::PostgreSQL::create_field($field8,{ postgres_version => 8.3 });
306
307is($field8_sql, 'ts_field timestamp(6) with time zone NOT NULL', 'timestamp with precision');
308
309my $field9 = SQL::Translator::Schema::Field->new( name => 'time_field',
310 table => $table,
311 data_type => 'time with time zone',
312 size => 6,
313 is_auto_increment => 0,
314 is_nullable => 0,
315 is_foreign_key => 0,
316 is_unique => 0 );
317
318my $field9_sql = SQL::Translator::Producer::PostgreSQL::create_field($field9,{ postgres_version => 8.3 });
319
320is($field9_sql, 'time_field time(6) with time zone NOT NULL', 'time with precision');
321
322my $field10 = SQL::Translator::Schema::Field->new( name => 'interval_field',
323 table => $table,
324 data_type => 'interval',
325 size => 6,
326 is_auto_increment => 0,
327 is_nullable => 0,
328 is_foreign_key => 0,
329 is_unique => 0 );
330
331my $field10_sql = SQL::Translator::Producer::PostgreSQL::create_field($field10,{ postgres_version => 8.3 });
332
333is($field10_sql, 'interval_field interval(6) NOT NULL', 'time with precision');
334
335
336my $field11 = SQL::Translator::Schema::Field->new( name => 'time_field',
337 table => $table,
338 data_type => 'time without time zone',
339 size => 6,
340 is_auto_increment => 0,
341 is_nullable => 0,
342 is_foreign_key => 0,
343 is_unique => 0 );
344
345my $field11_sql = SQL::Translator::Producer::PostgreSQL::create_field($field11,{ postgres_version => 8.3 });
346
347is($field11_sql, 'time_field time(6) without time zone NOT NULL', 'time with precision');
348
349
350
351my $field12 = SQL::Translator::Schema::Field->new( name => 'time_field',
352 table => $table,
353 data_type => 'timestamp',
354 is_auto_increment => 0,
355 is_nullable => 0,
356 is_foreign_key => 0,
357 is_unique => 0 );
358
359my $field12_sql = SQL::Translator::Producer::PostgreSQL::create_field($field12,{ postgres_version => 8.3 });
360
361is($field12_sql, 'time_field timestamp NOT NULL', 'time with precision');
362
79f55d7e 363my $field13 = SQL::Translator::Schema::Field->new( name => 'enum_field_with_type_name',
364 table => $table,
365 data_type => 'enum',
366 extra => { list => [ 'Foo', 'Bar' ],
367 custom_type_name => 'real_enum_type' },
368 is_auto_increment => 0,
369 is_nullable => 0,
370 is_foreign_key => 0,
371 is_unique => 0 );
372
373my $field13_sql = SQL::Translator::Producer::PostgreSQL::create_field($field13,{ postgres_version => 8.3 });
374
375is($field13_sql, 'enum_field_with_type_name real_enum_type NOT NULL', 'Create real enum field works');
376
90726ffd 377
bc8e2aa1 378{
379 # let's test default values! -- rjbs, 2008-09-30
380 my %field = (
381 table => $table,
382 data_type => 'VARCHAR',
383 size => 10,
384 is_auto_increment => 0,
385 is_nullable => 1,
386 is_foreign_key => 0,
387 is_unique => 0,
388 );
389
390 {
391 my $simple_default = SQL::Translator::Schema::Field->new(
392 %field,
393 name => 'str_default',
394 default_value => 'foo',
395 );
396
397 is(
398 $PRODUCER->($simple_default),
399 q{str_default character varying(10) DEFAULT 'foo'},
400 'default str',
401 );
402 }
403
404 {
405 my $null_default = SQL::Translator::Schema::Field->new(
406 %field,
407 name => 'null_default',
408 default_value => \'NULL',
409 );
410
411 is(
412 $PRODUCER->($null_default),
413 q{null_default character varying(10) DEFAULT NULL},
414 'default null',
415 );
416 }
417
418 {
419 my $null_default = SQL::Translator::Schema::Field->new(
420 %field,
421 name => 'null_default_2',
422 default_value => 'NULL', # XXX: this should go away
423 );
424
425 is(
426 $PRODUCER->($null_default),
427 q{null_default_2 character varying(10) DEFAULT NULL},
428 'default null from special cased string',
429 );
430 }
431
432 {
433 my $func_default = SQL::Translator::Schema::Field->new(
434 %field,
435 name => 'func_default',
436 default_value => \'func(funky)',
437 );
438
439 is(
440 $PRODUCER->($func_default),
441 q{func_default character varying(10) DEFAULT func(funky)},
442 'unquoted default from scalar ref',
443 );
444 }
445}
446
447
296c2701 448my $view1 = SQL::Translator::Schema::View->new(
449 name => 'view_foo',
450 fields => [qw/id name/],
451 sql => 'SELECT id, name FROM thing',
452);
453my $create_opts = { add_replace_view => 1, no_comments => 1 };
454my $view1_sql1 = SQL::Translator::Producer::PostgreSQL::create_view($view1, $create_opts);
455
f59b2c0e 456my $view_sql_replace = "CREATE VIEW view_foo ( id, name ) AS
296c2701 457 SELECT id, name FROM thing
f59b2c0e 458";
296c2701 459is($view1_sql1, $view_sql_replace, 'correct "CREATE OR REPLACE VIEW" SQL');
460
461my $view2 = SQL::Translator::Schema::View->new(
462 name => 'view_foo2',
463 sql => 'SELECT id, name FROM thing',
464 extra => {
465 'temporary' => '1',
466 'check_option' => 'cascaded',
467 },
468);
469my $create2_opts = { add_replace_view => 1, no_comments => 1 };
470my $view2_sql1 = SQL::Translator::Producer::PostgreSQL::create_view($view2, $create2_opts);
471
f59b2c0e 472my $view2_sql_replace = "CREATE TEMPORARY VIEW view_foo2 AS
296c2701 473 SELECT id, name FROM thing
f59b2c0e 474 WITH CASCADED CHECK OPTION";
296c2701 475is($view2_sql1, $view2_sql_replace, 'correct "CREATE OR REPLACE VIEW" SQL 2');
5f31ed66 476
477{
478 my $table = SQL::Translator::Schema::Table->new( name => 'foobar', fields => [qw( foo bar )] );
479 my $quote = { quote_table_names => '"', quote_field_names => '"' };
480
481 {
482 my $index = $table->add_index(name => 'myindex', fields => ['foo']);
483 my ($def) = SQL::Translator::Producer::PostgreSQL::create_index($index);
484 is($def, "CREATE INDEX myindex on foobar (foo)", 'index created');
485 ($def) = SQL::Translator::Producer::PostgreSQL::create_index($index, $quote);
486 is($def, 'CREATE INDEX "myindex" on "foobar" ("foo")', 'index created w/ quotes');
487 }
488
489 {
490 my $index = $table->add_index(name => 'myindex', fields => ['lower(foo)']);
491 my ($def) = SQL::Translator::Producer::PostgreSQL::create_index($index);
492 is($def, "CREATE INDEX myindex on foobar (lower(foo))", 'index created');
493 ($def) = SQL::Translator::Producer::PostgreSQL::create_index($index, $quote);
494 is($def, 'CREATE INDEX "myindex" on "foobar" (lower(foo))', 'index created w/ quotes');
495 }
496
497 {
498 my $index = $table->add_index(name => 'myindex', fields => ['bar', 'lower(foo)']);
499 my ($def) = SQL::Translator::Producer::PostgreSQL::create_index($index);
500 is($def, "CREATE INDEX myindex on foobar (bar, lower(foo))", 'index created');
501 ($def) = SQL::Translator::Producer::PostgreSQL::create_index($index, $quote);
502 is($def, 'CREATE INDEX "myindex" on "foobar" ("bar", lower(foo))', 'index created w/ quotes');
503 }
504
505 {
506 my $constr = $table->add_constraint(name => 'constr', type => UNIQUE, fields => ['foo']);
507 my ($def) = SQL::Translator::Producer::PostgreSQL::create_constraint($constr);
508 is($def->[0], 'CONSTRAINT constr UNIQUE (foo)', 'constraint created');
509 ($def) = SQL::Translator::Producer::PostgreSQL::create_constraint($constr, $quote);
510 is($def->[0], 'CONSTRAINT "constr" UNIQUE ("foo")', 'constraint created w/ quotes');
511 }
512
513 {
514 my $constr = $table->add_constraint(name => 'constr', type => UNIQUE, fields => ['lower(foo)']);
515 my ($def) = SQL::Translator::Producer::PostgreSQL::create_constraint($constr);
516 is($def->[0], 'CONSTRAINT constr UNIQUE (lower(foo))', 'constraint created');
517 ($def) = SQL::Translator::Producer::PostgreSQL::create_constraint($constr, $quote);
518 is($def->[0], 'CONSTRAINT "constr" UNIQUE (lower(foo))', 'constraint created w/ quotes');
519 }
520
521 {
522 my $constr = $table->add_constraint(name => 'constr', type => UNIQUE, fields => ['bar', 'lower(foo)']);
523 my ($def) = SQL::Translator::Producer::PostgreSQL::create_constraint($constr);
524 is($def->[0], 'CONSTRAINT constr UNIQUE (bar, lower(foo))', 'constraint created');
525 ($def) = SQL::Translator::Producer::PostgreSQL::create_constraint($constr, $quote);
526 is($def->[0], 'CONSTRAINT "constr" UNIQUE ("bar", lower(foo))', 'constraint created w/ quotes');
527 }
528}
25c74c43 529
530my $drop_view_opts1 = { add_drop_view => 1, no_comments => 1, postgres_version => 8.001 };
531my $drop_view_8_1_produced = SQL::Translator::Producer::PostgreSQL::create_view($view1, $drop_view_opts1);
532
533my $drop_view_8_1_expected = "DROP VIEW view_foo;
534CREATE VIEW view_foo ( id, name ) AS
535 SELECT id, name FROM thing
536";
537
538is($drop_view_8_1_produced, $drop_view_8_1_expected, "My DROP VIEW statement for 8.1 is correct");
539
540my $drop_view_opts2 = { add_drop_view => 1, no_comments => 1, postgres_version => 9.001 };
541my $drop_view_9_1_produced = SQL::Translator::Producer::PostgreSQL::create_view($view1, $drop_view_opts2);
542
543my $drop_view_9_1_expected = "DROP VIEW IF EXISTS view_foo;
544CREATE VIEW view_foo ( id, name ) AS
545 SELECT id, name FROM thing
546";
547
548is($drop_view_9_1_produced, $drop_view_9_1_expected, "My DROP VIEW statement for 9.1 is correct");