8 use Test::SQL::Translator qw(maybe_plan);
9 use SQL::Translator::Schema::Constants;
14 #=============================================================================
18 'SQL::Translator::Producer::PostgreSQL',
22 use Test::Differences;
25 my $PRODUCER = \&SQL::Translator::Producer::PostgreSQL::create_field;
28 my $table = SQL::Translator::Schema::Table->new( name => 'foo.bar',
29 comments => [ "multi\nline",'single line' ] );
30 my $field = SQL::Translator::Schema::Field->new( name => 'baz',
31 comments => [ "multi\nline",'single line' ],
33 data_type => 'VARCHAR',
35 default_value => 'quux',
36 is_auto_increment => 0,
40 $table->add_field($field);
41 my ($create, $fks) = SQL::Translator::Producer::PostgreSQL::create_table($table, { quote_table_names => q{"} });
42 is($table->name, 'foo.bar');
44 my $expected = <<EOESQL;
54 CREATE TABLE "foo"."bar" (
58 "baz" character varying(10) DEFAULT 'quux' NOT NULL
62 $expected =~ s/\n\z//;
63 is($create, $expected);
66 my $table = SQL::Translator::Schema::Table->new( name => 'mytable');
68 my $field1 = SQL::Translator::Schema::Field->new( name => 'myfield',
70 data_type => 'VARCHAR',
72 default_value => undef,
73 is_auto_increment => 0,
78 my $field1_sql = SQL::Translator::Producer::PostgreSQL::create_field($field1);
80 is($field1_sql, 'myfield character varying(10)', 'Create field works');
82 my $field_array = SQL::Translator::Schema::Field->new( name => 'myfield',
84 data_type => 'character varying[]',
86 default_value => undef,
87 is_auto_increment => 0,
92 my $field_array_sql = SQL::Translator::Producer::PostgreSQL::create_field($field_array);
94 is($field_array_sql, 'myfield character varying(10)[]', 'Create field works');
96 my $field2 = SQL::Translator::Schema::Field->new( name => 'myfield',
98 data_type => 'VARCHAR',
100 default_value => undef,
101 is_auto_increment => 0,
106 my $pk_constraint = SQL::Translator::Schema::Constraint->new(
109 fields => [qw(myfield)],
110 type => 'PRIMARY_KEY',
113 my ($pk_constraint_def_ref, $pk_constraint_fk_ref ) = SQL::Translator::Producer::PostgreSQL::create_constraint($pk_constraint);
114 ok(@{$pk_constraint_def_ref} == 1 && @{$pk_constraint_fk_ref} == 0, 'precheck of create_Primary Key constraint');
115 is($pk_constraint_def_ref->[0], 'CONSTRAINT foo PRIMARY KEY (myfield)', 'Create Primary Key Constraint works');
117 my $alter_pk_constraint = SQL::Translator::Producer::PostgreSQL::alter_drop_constraint($pk_constraint);
118 is($alter_pk_constraint, 'ALTER TABLE mytable DROP CONSTRAINT foo', 'Alter drop Primary Key constraint works');
120 my $table2 = SQL::Translator::Schema::Table->new( name => 'mytable2');
122 my $field1_2 = SQL::Translator::Schema::Field->new( name => 'myfield_2',
124 data_type => 'VARCHAR',
126 default_value => undef,
127 is_auto_increment => 0,
132 # check named, and unnamed foreign keys
133 for my $name ( 'foo', undef ) {
134 my $fk_constraint = SQL::Translator::Schema::Constraint->new(
137 fields => [qw(myfield)],
138 type => 'FOREIGN_KEY',
139 reference_table => $table2,
140 reference_fields => [qw(myfield_2)],
142 my $fk_constraint_2 = SQL::Translator::Schema::Constraint->new(
145 fields => [qw(myfield)],
146 type => 'FOREIGN_KEY',
147 reference_table => $table2,
148 reference_fields => [qw(myfield_2)],
151 my ($fk_constraint_def_ref, $fk_constraint_fk_ref ) = SQL::Translator::Producer::PostgreSQL::create_constraint($fk_constraint);
153 ok(@{$fk_constraint_def_ref} == 0 && @{$fk_constraint_fk_ref} == 1, 'precheck of create_Foreign Key constraint');
156 is($fk_constraint_fk_ref->[0], "ALTER TABLE mytable ADD CONSTRAINT $name FOREIGN KEY (myfield)
157 REFERENCES mytable2 (myfield_2) DEFERRABLE", 'Create Foreign Key Constraint works');
159 # ToDo: may we should check if the constraint name was valid, or if next
160 # unused_name created has choosen a different one
161 my $alter_fk_constraint = SQL::Translator::Producer::PostgreSQL::alter_drop_constraint($fk_constraint);
162 is($alter_fk_constraint, "ALTER TABLE mytable DROP CONSTRAINT $name", 'Alter drop Foreign Key constraint works');
165 is($fk_constraint_fk_ref->[0], 'ALTER TABLE mytable ADD FOREIGN KEY (myfield)
166 REFERENCES mytable2 (myfield_2) DEFERRABLE', 'Create un-named Foreign Key Constraint works');
168 my $alter_fk_constraint = SQL::Translator::Producer::PostgreSQL::alter_drop_constraint($fk_constraint);
169 is($alter_fk_constraint, 'ALTER TABLE mytable DROP CONSTRAINT mytable_myfield_fkey', 'Alter drop un-named Foreign Key constraint works');
173 # check named, and unnamed primary keys
174 for my $name ( 'foo', undef ) {
175 my $pk_constraint = SQL::Translator::Schema::Constraint->new(
178 fields => [qw(myfield)],
179 type => 'PRIMARY_KEY',
181 my $pk_constraint_2 = SQL::Translator::Schema::Constraint->new(
184 fields => [qw(myfield)],
185 type => 'PRIMARY_KEY',
188 my ($pk_constraint_def_ref, $pk_constraint_pk_ref ) = SQL::Translator::Producer::PostgreSQL::create_constraint($pk_constraint);
191 is($pk_constraint_def_ref->[0], "CONSTRAINT $name PRIMARY KEY (myfield)", 'Create Primary Key Constraint works');
193 # ToDo: may we should check if the constraint name was valid, or if next
194 # unused_name created has choosen a different one
195 my $alter_pk_constraint = SQL::Translator::Producer::PostgreSQL::alter_drop_constraint($pk_constraint);
196 is($alter_pk_constraint, "ALTER TABLE mytable DROP CONSTRAINT $name", 'Alter drop Primary Key constraint works');
199 is($pk_constraint_def_ref->[0], 'PRIMARY KEY (myfield)', 'Create un-named Primary Key Constraint works');
201 my $alter_pk_constraint = SQL::Translator::Producer::PostgreSQL::alter_drop_constraint($pk_constraint);
202 is($alter_pk_constraint, 'ALTER TABLE mytable DROP CONSTRAINT mytable_pkey', 'Alter drop un-named Foreign Key constraint works');
206 my $alter_field = SQL::Translator::Producer::PostgreSQL::alter_field($field1,
208 is($alter_field, qq[ALTER TABLE mytable ALTER COLUMN myfield SET NOT NULL;
209 ALTER TABLE mytable ALTER COLUMN myfield TYPE character varying(25)],
210 'Alter field works');
212 my $field1_complex = SQL::Translator::Schema::Field->new(
213 name => 'my_complex_field',
215 data_type => 'VARCHAR',
217 default_value => undef,
218 is_auto_increment => 0,
224 my $field2_complex = SQL::Translator::Schema::Field->new(
225 name => 'my_altered_field',
227 data_type => 'VARCHAR',
229 default_value => 'whatever',
230 is_auto_increment => 0,
236 my $alter_field_complex = SQL::Translator::Producer::PostgreSQL::alter_field($field1_complex, $field2_complex);
238 $alter_field_complex,
239 q{ALTER TABLE mytable RENAME COLUMN my_complex_field TO my_altered_field;
240 ALTER TABLE mytable ALTER COLUMN my_altered_field TYPE character varying(60);
241 ALTER TABLE mytable ALTER COLUMN my_altered_field SET DEFAULT 'whatever'},
242 'Complex Alter field works'
245 $field1->name('field3');
246 my $add_field = SQL::Translator::Producer::PostgreSQL::add_field($field1);
248 is($add_field, 'ALTER TABLE mytable ADD COLUMN field3 character varying(10)', 'Add field works');
250 my $drop_field = SQL::Translator::Producer::PostgreSQL::drop_field($field2);
251 is($drop_field, 'ALTER TABLE mytable DROP COLUMN myfield', 'Drop field works');
253 my $field_serial = SQL::Translator::Schema::Field->new( name => 'serial_field',
256 is_auto_increment => 1,
259 my $field_serial_sql = SQL::Translator::Producer::PostgreSQL::create_field($field_serial);
261 is($field_serial_sql, 'serial_field serial NOT NULL', 'Create serial field works');
263 my $field_bigserial = SQL::Translator::Schema::Field->new( name => 'bigserial_field',
265 data_type => 'BIGINT',
266 is_auto_increment => 1,
269 my $field_bigserial_sql = SQL::Translator::Producer::PostgreSQL::create_field($field_bigserial);
271 is($field_bigserial_sql, 'bigserial_field bigserial NOT NULL', 'Create bigserial field works (from bigint type)');
273 $field_bigserial = SQL::Translator::Schema::Field->new( name => 'bigserial_field',
276 is_auto_increment => 1,
280 $field_bigserial_sql = SQL::Translator::Producer::PostgreSQL::create_field($field_bigserial);
282 is($field_bigserial_sql, 'bigserial_field bigserial NOT NULL', 'Create bigserial field works (based on size)');
284 my $field3 = SQL::Translator::Schema::Field->new( name => 'time_field',
287 default_value => undef,
288 is_auto_increment => 0,
293 my $field3_sql = SQL::Translator::Producer::PostgreSQL::create_field($field3);
295 is($field3_sql, 'time_field time NOT NULL', 'Create time field works');
297 my $field3_datetime_with_TZ = SQL::Translator::Schema::Field->new(
298 name => 'datetime_with_TZ',
300 data_type => 'timestamp with time zone',
304 my $field3_datetime_with_TZ_sql =
305 SQL::Translator::Producer::PostgreSQL::create_field(
306 $field3_datetime_with_TZ
310 $field3_datetime_with_TZ_sql,
311 'datetime_with_TZ timestamp(6) with time zone',
312 'Create time field with time zone and size, works'
315 my $field3_time_without_TZ = SQL::Translator::Schema::Field->new(
316 name => 'time_without_TZ',
318 data_type => 'time without time zone',
322 my $field3_time_without_TZ_sql
323 = SQL::Translator::Producer::PostgreSQL::create_field(
324 $field3_time_without_TZ
328 $field3_time_without_TZ_sql,
329 'time_without_TZ time(2) without time zone',
330 'Create time field without time zone but with size, works'
333 my $field_num = SQL::Translator::Schema::Field->new( name => 'num',
335 data_type => 'numeric',
338 my $fieldnum_sql = SQL::Translator::Producer::PostgreSQL::create_field($field_num);
340 is($fieldnum_sql, 'num numeric(10,2)', 'Create numeric field works');
343 my $field4 = SQL::Translator::Schema::Field->new( name => 'bytea_field',
345 data_type => 'bytea',
347 default_value => undef,
348 is_auto_increment => 0,
353 my $field4_sql = SQL::Translator::Producer::PostgreSQL::create_field($field4);
355 is($field4_sql, 'bytea_field bytea NOT NULL', 'Create bytea field works');
357 my $field5 = SQL::Translator::Schema::Field->new( name => 'enum_field',
360 extra => { list => [ 'Foo', 'Bar', 'Ba\'z' ] },
361 is_auto_increment => 0,
366 my $field5_types = {};
367 my $field5_sql = SQL::Translator::Producer::PostgreSQL::create_field(
370 postgres_version => 8.3,
371 type_defs => $field5_types,
375 is($field5_sql, 'enum_field mytable_enum_field_type NOT NULL', 'Create real enum field works');
378 { mytable_enum_field_type =>
379 "DROP TYPE IF EXISTS mytable_enum_field_type CASCADE;\n" .
380 "CREATE TYPE mytable_enum_field_type AS ENUM ('Foo', 'Bar', 'Ba''z')"
382 'Create real enum type works'
385 my $field6 = SQL::Translator::Schema::Field->new(
388 data_type => 'character',
390 default_value => 'foobar',
391 is_auto_increment => 0,
396 my $field7 = SQL::Translator::Schema::Field->new(
399 data_type => 'character',
401 default_value => undef,
402 is_auto_increment => 0,
407 $alter_field = SQL::Translator::Producer::PostgreSQL::alter_field($field6,
410 is($alter_field, q(ALTER TABLE mytable ALTER COLUMN character DROP DEFAULT), 'DROP DEFAULT');
412 $field7->default_value(q(foo'bar'));
414 $alter_field = SQL::Translator::Producer::PostgreSQL::alter_field($field6,
417 is($alter_field, q(ALTER TABLE mytable ALTER COLUMN character SET DEFAULT 'foo''bar'''), 'DEFAULT with escaping');
419 $field7->default_value(\q(foobar));
421 $alter_field = SQL::Translator::Producer::PostgreSQL::alter_field($field6,
424 is($alter_field, q(ALTER TABLE mytable ALTER COLUMN character SET DEFAULT foobar), 'DEFAULT unescaped if scalarref');
426 $field7->is_nullable(1);
427 $field7->default_value(q(foobar));
429 $alter_field = SQL::Translator::Producer::PostgreSQL::alter_field($field6,
432 is($alter_field, q(ALTER TABLE mytable ALTER COLUMN character DROP NOT NULL), 'DROP NOT NULL');
434 my $field8 = SQL::Translator::Schema::Field->new( name => 'ts_field',
436 data_type => 'timestamp with time zone',
438 is_auto_increment => 0,
443 my $field8_sql = SQL::Translator::Producer::PostgreSQL::create_field($field8,{ postgres_version => 8.3 });
445 is($field8_sql, 'ts_field timestamp(6) with time zone NOT NULL', 'timestamp with precision');
447 my $field9 = SQL::Translator::Schema::Field->new( name => 'time_field',
449 data_type => 'time with time zone',
451 is_auto_increment => 0,
456 my $field9_sql = SQL::Translator::Producer::PostgreSQL::create_field($field9,{ postgres_version => 8.3 });
458 is($field9_sql, 'time_field time(6) with time zone NOT NULL', 'time with precision');
460 my $field10 = SQL::Translator::Schema::Field->new( name => 'interval_field',
462 data_type => 'interval',
464 is_auto_increment => 0,
469 my $field10_sql = SQL::Translator::Producer::PostgreSQL::create_field($field10,{ postgres_version => 8.3 });
471 is($field10_sql, 'interval_field interval(6) NOT NULL', 'time with precision');
474 my $field11 = SQL::Translator::Schema::Field->new( name => 'time_field',
476 data_type => 'time without time zone',
478 is_auto_increment => 0,
483 my $field11_sql = SQL::Translator::Producer::PostgreSQL::create_field($field11,{ postgres_version => 8.3 });
485 is($field11_sql, 'time_field time(6) without time zone NOT NULL', 'time with precision');
489 my $field12 = SQL::Translator::Schema::Field->new( name => 'time_field',
491 data_type => 'timestamp',
492 is_auto_increment => 0,
497 my $field12_sql = SQL::Translator::Producer::PostgreSQL::create_field($field12,{ postgres_version => 8.3 });
499 is($field12_sql, 'time_field timestamp NOT NULL', 'time with precision');
501 my $field13 = SQL::Translator::Schema::Field->new( name => 'enum_field_with_type_name',
504 extra => { list => [ 'Foo', 'Bar', 'Ba\'z' ],
505 custom_type_name => 'real_enum_type' },
506 is_auto_increment => 0,
511 my $field13_types = {};
512 my $field13_sql = SQL::Translator::Producer::PostgreSQL::create_field(
515 postgres_version => 8.3,
516 type_defs => $field13_types,
520 is($field13_sql, 'enum_field_with_type_name real_enum_type NOT NULL', 'Create real enum field works');
524 "DROP TYPE IF EXISTS real_enum_type CASCADE;\n" .
525 "CREATE TYPE real_enum_type AS ENUM ('Foo', 'Bar', 'Ba''z')"
527 'Create real enum type works'
532 # let's test default values! -- rjbs, 2008-09-30
535 data_type => 'VARCHAR',
537 is_auto_increment => 0,
544 my $simple_default = SQL::Translator::Schema::Field->new(
546 name => 'str_default',
547 default_value => 'foo',
551 $PRODUCER->($simple_default),
552 q{str_default character varying(10) DEFAULT 'foo'},
558 my $null_default = SQL::Translator::Schema::Field->new(
560 name => 'null_default',
561 default_value => \'NULL',
565 $PRODUCER->($null_default),
566 q{null_default character varying(10) DEFAULT NULL},
572 my $null_default = SQL::Translator::Schema::Field->new(
574 name => 'null_default_2',
575 default_value => 'NULL', # XXX: this should go away
579 $PRODUCER->($null_default),
580 q{null_default_2 character varying(10) DEFAULT NULL},
581 'default null from special cased string',
586 my $func_default = SQL::Translator::Schema::Field->new(
588 name => 'func_default',
589 default_value => \'func(funky)',
593 $PRODUCER->($func_default),
594 q{func_default character varying(10) DEFAULT func(funky)},
595 'unquoted default from scalar ref',
601 my $view1 = SQL::Translator::Schema::View->new(
603 fields => [qw/id name/],
604 sql => 'SELECT id, name FROM thing',
606 my $create_opts = { add_replace_view => 1, no_comments => 1 };
607 my $view1_sql1 = SQL::Translator::Producer::PostgreSQL::create_view($view1, $create_opts);
609 my $view_sql_replace = "CREATE VIEW view_foo ( id, name ) AS
610 SELECT id, name FROM thing
612 is($view1_sql1, $view_sql_replace, 'correct "CREATE OR REPLACE VIEW" SQL');
614 my $view2 = SQL::Translator::Schema::View->new(
616 sql => 'SELECT id, name FROM thing',
619 'check_option' => 'cascaded',
622 my $create2_opts = { add_replace_view => 1, no_comments => 1 };
623 my $view2_sql1 = SQL::Translator::Producer::PostgreSQL::create_view($view2, $create2_opts);
625 my $view2_sql_replace = "CREATE TEMPORARY VIEW view_foo2 AS
626 SELECT id, name FROM thing
627 WITH CASCADED CHECK OPTION";
628 is($view2_sql1, $view2_sql_replace, 'correct "CREATE OR REPLACE VIEW" SQL 2');
631 my $table = SQL::Translator::Schema::Table->new( name => 'foobar', fields => [qw( foo bar )] );
632 my $quote = { quote_table_names => '"' };
635 my $index = $table->add_index(name => 'myindex', fields => ['foo']);
636 my ($def) = SQL::Translator::Producer::PostgreSQL::create_index($index);
637 is($def, "CREATE INDEX myindex on foobar (foo)", 'index created');
638 ($def) = SQL::Translator::Producer::PostgreSQL::create_index($index, $quote);
639 is($def, 'CREATE INDEX "myindex" on "foobar" ("foo")', 'index created w/ quotes');
643 my $index = $table->add_index(name => 'myindex', fields => ['lower(foo)']);
644 my ($def) = SQL::Translator::Producer::PostgreSQL::create_index($index);
645 is($def, "CREATE INDEX myindex on foobar (lower(foo))", 'index created');
646 ($def) = SQL::Translator::Producer::PostgreSQL::create_index($index, $quote);
647 is($def, 'CREATE INDEX "myindex" on "foobar" (lower(foo))', 'index created w/ quotes');
651 my $index = $table->add_index(name => 'myindex', fields => ['bar', 'lower(foo)']);
652 my ($def) = SQL::Translator::Producer::PostgreSQL::create_index($index);
653 is($def, "CREATE INDEX myindex on foobar (bar, lower(foo))", 'index created');
654 ($def) = SQL::Translator::Producer::PostgreSQL::create_index($index, $quote);
655 is($def, 'CREATE INDEX "myindex" on "foobar" ("bar", lower(foo))', 'index created w/ quotes');
659 my $constr = $table->add_constraint(name => 'constr', type => UNIQUE, fields => ['foo']);
660 my ($def) = SQL::Translator::Producer::PostgreSQL::create_constraint($constr);
661 is($def->[0], 'CONSTRAINT constr UNIQUE (foo)', 'constraint created');
662 ($def) = SQL::Translator::Producer::PostgreSQL::create_constraint($constr, $quote);
663 is($def->[0], 'CONSTRAINT "constr" UNIQUE ("foo")', 'constraint created w/ quotes');
667 my $constr = $table->add_constraint(name => 'constr', type => UNIQUE, fields => ['lower(foo)']);
668 my ($def) = SQL::Translator::Producer::PostgreSQL::create_constraint($constr);
669 is($def->[0], 'CONSTRAINT constr UNIQUE (lower(foo))', 'constraint created');
670 ($def) = SQL::Translator::Producer::PostgreSQL::create_constraint($constr, $quote);
671 is($def->[0], 'CONSTRAINT "constr" UNIQUE (lower(foo))', 'constraint created w/ quotes');
675 my $constr = $table->add_constraint(name => 'constr', type => UNIQUE, fields => ['bar', 'lower(foo)']);
676 my ($def) = SQL::Translator::Producer::PostgreSQL::create_constraint($constr);
677 is($def->[0], 'CONSTRAINT constr UNIQUE (bar, lower(foo))', 'constraint created');
678 ($def) = SQL::Translator::Producer::PostgreSQL::create_constraint($constr, $quote);
679 is($def->[0], 'CONSTRAINT "constr" UNIQUE ("bar", lower(foo))', 'constraint created w/ quotes');
683 my $index = $table->add_index(name => 'myindex', options => [{using => 'hash'}, {where => "upper(foo) = 'bar' AND bar = 'foo'"}], fields => ['bar', 'lower(foo)']);
684 my ($def) = SQL::Translator::Producer::PostgreSQL::create_index($index);
685 is($def, "CREATE INDEX myindex on foobar USING hash (bar, lower(foo)) WHERE upper(foo) = 'bar' AND bar = 'foo'", 'index using & where created');
686 ($def) = SQL::Translator::Producer::PostgreSQL::create_index($index, $quote);
687 is($def, 'CREATE INDEX "myindex" on "foobar" USING hash ("bar", lower(foo)) WHERE upper(foo) = \'bar\' AND bar = \'foo\'', 'index using & where created w/ quotes');
691 my $drop_view_opts1 = { add_drop_view => 1, no_comments => 1, postgres_version => 8.001 };
692 my $drop_view_8_1_produced = SQL::Translator::Producer::PostgreSQL::create_view($view1, $drop_view_opts1);
694 my $drop_view_8_1_expected = "DROP VIEW view_foo;
695 CREATE VIEW view_foo ( id, name ) AS
696 SELECT id, name FROM thing
699 is($drop_view_8_1_produced, $drop_view_8_1_expected, "My DROP VIEW statement for 8.1 is correct");
701 my $drop_view_opts2 = { add_drop_view => 1, no_comments => 1, postgres_version => 9.001 };
702 my $drop_view_9_1_produced = SQL::Translator::Producer::PostgreSQL::create_view($view1, $drop_view_opts2);
704 my $drop_view_9_1_expected = "DROP VIEW IF EXISTS view_foo;
705 CREATE VIEW view_foo ( id, name ) AS
706 SELECT id, name FROM thing
709 is($drop_view_9_1_produced, $drop_view_9_1_expected, "My DROP VIEW statement for 9.1 is correct");