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 my $field = SQL::Translator::Schema::Field->new( name => 'baz',
31 data_type => 'VARCHAR',
33 default_value => 'quux',
34 is_auto_increment => 0,
38 $table->add_field($field);
39 my ($create, $fks) = SQL::Translator::Producer::PostgreSQL::create_table($table, { quote_table_names => q{"} });
40 is($table->name, 'foo.bar');
42 my $expected = "--\n-- Table: foo.bar\n--\nCREATE TABLE \"foo\".\"bar\" (\n \"baz\" character varying(10) DEFAULT 'quux' NOT NULL\n)";
43 is($create, $expected);
46 my $table = SQL::Translator::Schema::Table->new( name => 'mytable');
48 my $field1 = SQL::Translator::Schema::Field->new( name => 'myfield',
50 data_type => 'VARCHAR',
52 default_value => undef,
53 is_auto_increment => 0,
58 my $field1_sql = SQL::Translator::Producer::PostgreSQL::create_field($field1);
60 is($field1_sql, 'myfield character varying(10)', 'Create field works');
62 my $field_array = SQL::Translator::Schema::Field->new( name => 'myfield',
64 data_type => 'character varying[]',
66 default_value => undef,
67 is_auto_increment => 0,
72 my $field_array_sql = SQL::Translator::Producer::PostgreSQL::create_field($field_array);
74 is($field_array_sql, 'myfield character varying(10)[]', 'Create field works');
76 my $field2 = SQL::Translator::Schema::Field->new( name => 'myfield',
78 data_type => 'VARCHAR',
80 default_value => undef,
81 is_auto_increment => 0,
86 my $pk_constraint = SQL::Translator::Schema::Constraint->new(
89 fields => [qw(myfield)],
90 type => 'PRIMARY_KEY',
93 my ($pk_constraint_def_ref, $pk_constraint_fk_ref ) = SQL::Translator::Producer::PostgreSQL::create_constraint($pk_constraint);
94 ok(@{$pk_constraint_def_ref} == 1 && @{$pk_constraint_fk_ref} == 0, 'precheck of create_Primary Key constraint');
95 is($pk_constraint_def_ref->[0], 'CONSTRAINT foo PRIMARY KEY (myfield)', 'Create Primary Key Constraint works');
97 my $alter_pk_constraint = SQL::Translator::Producer::PostgreSQL::alter_drop_constraint($pk_constraint);
98 is($alter_pk_constraint, 'ALTER TABLE mytable DROP CONSTRAINT foo', 'Alter drop Primary Key constraint works');
100 my $table2 = SQL::Translator::Schema::Table->new( name => 'mytable2');
102 my $field1_2 = SQL::Translator::Schema::Field->new( name => 'myfield_2',
104 data_type => 'VARCHAR',
106 default_value => undef,
107 is_auto_increment => 0,
112 # check named, and unnamed foreign keys
113 for my $name ( 'foo', undef ) {
114 my $fk_constraint = SQL::Translator::Schema::Constraint->new(
117 fields => [qw(myfield)],
118 type => 'FOREIGN_KEY',
119 reference_table => $table2,
120 reference_fields => [qw(myfield_2)],
122 my $fk_constraint_2 = SQL::Translator::Schema::Constraint->new(
125 fields => [qw(myfield)],
126 type => 'FOREIGN_KEY',
127 reference_table => $table2,
128 reference_fields => [qw(myfield_2)],
131 my ($fk_constraint_def_ref, $fk_constraint_fk_ref ) = SQL::Translator::Producer::PostgreSQL::create_constraint($fk_constraint);
133 ok(@{$fk_constraint_def_ref} == 0 && @{$fk_constraint_fk_ref} == 1, 'precheck of create_Foreign Key constraint');
136 is($fk_constraint_fk_ref->[0], "ALTER TABLE mytable ADD CONSTRAINT $name FOREIGN KEY (myfield)
137 REFERENCES mytable2 (myfield_2) DEFERRABLE", 'Create Foreign Key Constraint works');
139 # ToDo: may we should check if the constraint name was valid, or if next
140 # unused_name created has choosen a different one
141 my $alter_fk_constraint = SQL::Translator::Producer::PostgreSQL::alter_drop_constraint($fk_constraint);
142 is($alter_fk_constraint, "ALTER TABLE mytable DROP CONSTRAINT $name", 'Alter drop Foreign Key constraint works');
145 is($fk_constraint_fk_ref->[0], 'ALTER TABLE mytable ADD FOREIGN KEY (myfield)
146 REFERENCES mytable2 (myfield_2) DEFERRABLE', 'Create un-named Foreign Key Constraint works');
148 my $alter_fk_constraint = SQL::Translator::Producer::PostgreSQL::alter_drop_constraint($fk_constraint);
149 is($alter_fk_constraint, 'ALTER TABLE mytable DROP CONSTRAINT mytable_myfield_fkey', 'Alter drop un-named Foreign Key constraint works');
153 # check named, and unnamed primary keys
154 for my $name ( 'foo', undef ) {
155 my $pk_constraint = SQL::Translator::Schema::Constraint->new(
158 fields => [qw(myfield)],
159 type => 'PRIMARY_KEY',
161 my $pk_constraint_2 = SQL::Translator::Schema::Constraint->new(
164 fields => [qw(myfield)],
165 type => 'PRIMARY_KEY',
168 my ($pk_constraint_def_ref, $pk_constraint_pk_ref ) = SQL::Translator::Producer::PostgreSQL::create_constraint($pk_constraint);
171 is($pk_constraint_def_ref->[0], "CONSTRAINT $name PRIMARY KEY (myfield)", 'Create Primary Key Constraint works');
173 # ToDo: may we should check if the constraint name was valid, or if next
174 # unused_name created has choosen a different one
175 my $alter_pk_constraint = SQL::Translator::Producer::PostgreSQL::alter_drop_constraint($pk_constraint);
176 is($alter_pk_constraint, "ALTER TABLE mytable DROP CONSTRAINT $name", 'Alter drop Primary Key constraint works');
179 is($pk_constraint_def_ref->[0], 'PRIMARY KEY (myfield)', 'Create un-named Primary Key Constraint works');
181 my $alter_pk_constraint = SQL::Translator::Producer::PostgreSQL::alter_drop_constraint($pk_constraint);
182 is($alter_pk_constraint, 'ALTER TABLE mytable DROP CONSTRAINT mytable_pkey', 'Alter drop un-named Foreign Key constraint works');
186 my $alter_field = SQL::Translator::Producer::PostgreSQL::alter_field($field1,
188 is($alter_field, qq[ALTER TABLE mytable ALTER COLUMN myfield SET NOT NULL;
189 ALTER TABLE mytable ALTER COLUMN myfield TYPE character varying(25)],
190 'Alter field works');
192 my $field1_complex = SQL::Translator::Schema::Field->new(
193 name => 'my_complex_field',
195 data_type => 'VARCHAR',
197 default_value => undef,
198 is_auto_increment => 0,
204 my $field2_complex = SQL::Translator::Schema::Field->new(
205 name => 'my_altered_field',
207 data_type => 'VARCHAR',
209 default_value => 'whatever',
210 is_auto_increment => 0,
216 my $alter_field_complex = SQL::Translator::Producer::PostgreSQL::alter_field($field1_complex, $field2_complex);
218 $alter_field_complex,
219 q{ALTER TABLE mytable RENAME COLUMN my_complex_field TO my_altered_field;
220 ALTER TABLE mytable ALTER COLUMN my_altered_field TYPE character varying(60);
221 ALTER TABLE mytable ALTER COLUMN my_altered_field SET DEFAULT 'whatever'},
222 'Complex Alter field works'
225 $field1->name('field3');
226 my $add_field = SQL::Translator::Producer::PostgreSQL::add_field($field1);
228 is($add_field, 'ALTER TABLE mytable ADD COLUMN field3 character varying(10)', 'Add field works');
230 my $drop_field = SQL::Translator::Producer::PostgreSQL::drop_field($field2);
231 is($drop_field, 'ALTER TABLE mytable DROP COLUMN myfield', 'Drop field works');
233 my $field3 = SQL::Translator::Schema::Field->new( name => 'time_field',
236 default_value => undef,
237 is_auto_increment => 0,
242 my $field3_sql = SQL::Translator::Producer::PostgreSQL::create_field($field3);
244 is($field3_sql, 'time_field time NOT NULL', 'Create time field works');
246 my $field3_datetime_with_TZ = SQL::Translator::Schema::Field->new(
247 name => 'datetime_with_TZ',
249 data_type => 'timestamp with time zone',
253 my $field3_datetime_with_TZ_sql =
254 SQL::Translator::Producer::PostgreSQL::create_field(
255 $field3_datetime_with_TZ
259 $field3_datetime_with_TZ_sql,
260 'datetime_with_TZ timestamp(6) with time zone',
261 'Create time field with time zone and size, works'
264 my $field3_time_without_TZ = SQL::Translator::Schema::Field->new(
265 name => 'time_without_TZ',
267 data_type => 'time without time zone',
271 my $field3_time_without_TZ_sql
272 = SQL::Translator::Producer::PostgreSQL::create_field(
273 $field3_time_without_TZ
277 $field3_time_without_TZ_sql,
278 'time_without_TZ time(2) without time zone',
279 'Create time field without time zone but with size, works'
282 my $field_num = SQL::Translator::Schema::Field->new( name => 'num',
284 data_type => 'numeric',
287 my $fieldnum_sql = SQL::Translator::Producer::PostgreSQL::create_field($field_num);
289 is($fieldnum_sql, 'num numeric(10,2)', 'Create numeric field works');
292 my $field4 = SQL::Translator::Schema::Field->new( name => 'bytea_field',
294 data_type => 'bytea',
296 default_value => undef,
297 is_auto_increment => 0,
302 my $field4_sql = SQL::Translator::Producer::PostgreSQL::create_field($field4);
304 is($field4_sql, 'bytea_field bytea NOT NULL', 'Create bytea field works');
306 my $field5 = SQL::Translator::Schema::Field->new( name => 'enum_field',
309 extra => { list => [ 'Foo', 'Bar' ] },
310 is_auto_increment => 0,
315 my $field5_sql = SQL::Translator::Producer::PostgreSQL::create_field($field5,{ postgres_version => 8.3 });
317 is($field5_sql, 'enum_field mytable_enum_field_type NOT NULL', 'Create real enum field works');
322 my $field6 = SQL::Translator::Schema::Field->new(
325 data_type => 'character',
327 default_value => 'foobar',
328 is_auto_increment => 0,
333 my $field7 = SQL::Translator::Schema::Field->new(
336 data_type => 'character',
338 default_value => undef,
339 is_auto_increment => 0,
344 $alter_field = SQL::Translator::Producer::PostgreSQL::alter_field($field6,
347 is($alter_field, q(ALTER TABLE mytable ALTER COLUMN character DROP DEFAULT), 'DROP DEFAULT');
349 $field7->default_value(q(foo'bar'));
351 $alter_field = SQL::Translator::Producer::PostgreSQL::alter_field($field6,
354 is($alter_field, q(ALTER TABLE mytable ALTER COLUMN character SET DEFAULT 'foo''bar'''), 'DEFAULT with escaping');
356 $field7->default_value(\q(foobar));
358 $alter_field = SQL::Translator::Producer::PostgreSQL::alter_field($field6,
361 is($alter_field, q(ALTER TABLE mytable ALTER COLUMN character SET DEFAULT foobar), 'DEFAULT unescaped if scalarref');
363 $field7->is_nullable(1);
364 $field7->default_value(q(foobar));
366 $alter_field = SQL::Translator::Producer::PostgreSQL::alter_field($field6,
369 is($alter_field, q(ALTER TABLE mytable ALTER COLUMN character DROP NOT NULL), 'DROP NOT NULL');
371 my $field8 = SQL::Translator::Schema::Field->new( name => 'ts_field',
373 data_type => 'timestamp with time zone',
375 is_auto_increment => 0,
380 my $field8_sql = SQL::Translator::Producer::PostgreSQL::create_field($field8,{ postgres_version => 8.3 });
382 is($field8_sql, 'ts_field timestamp(6) with time zone NOT NULL', 'timestamp with precision');
384 my $field9 = SQL::Translator::Schema::Field->new( name => 'time_field',
386 data_type => 'time with time zone',
388 is_auto_increment => 0,
393 my $field9_sql = SQL::Translator::Producer::PostgreSQL::create_field($field9,{ postgres_version => 8.3 });
395 is($field9_sql, 'time_field time(6) with time zone NOT NULL', 'time with precision');
397 my $field10 = SQL::Translator::Schema::Field->new( name => 'interval_field',
399 data_type => 'interval',
401 is_auto_increment => 0,
406 my $field10_sql = SQL::Translator::Producer::PostgreSQL::create_field($field10,{ postgres_version => 8.3 });
408 is($field10_sql, 'interval_field interval(6) NOT NULL', 'time with precision');
411 my $field11 = SQL::Translator::Schema::Field->new( name => 'time_field',
413 data_type => 'time without time zone',
415 is_auto_increment => 0,
420 my $field11_sql = SQL::Translator::Producer::PostgreSQL::create_field($field11,{ postgres_version => 8.3 });
422 is($field11_sql, 'time_field time(6) without time zone NOT NULL', 'time with precision');
426 my $field12 = SQL::Translator::Schema::Field->new( name => 'time_field',
428 data_type => 'timestamp',
429 is_auto_increment => 0,
434 my $field12_sql = SQL::Translator::Producer::PostgreSQL::create_field($field12,{ postgres_version => 8.3 });
436 is($field12_sql, 'time_field timestamp NOT NULL', 'time with precision');
438 my $field13 = SQL::Translator::Schema::Field->new( name => 'enum_field_with_type_name',
441 extra => { list => [ 'Foo', 'Bar' ],
442 custom_type_name => 'real_enum_type' },
443 is_auto_increment => 0,
448 my $field13_sql = SQL::Translator::Producer::PostgreSQL::create_field($field13,{ postgres_version => 8.3 });
450 is($field13_sql, 'enum_field_with_type_name real_enum_type NOT NULL', 'Create real enum field works');
454 # let's test default values! -- rjbs, 2008-09-30
457 data_type => 'VARCHAR',
459 is_auto_increment => 0,
466 my $simple_default = SQL::Translator::Schema::Field->new(
468 name => 'str_default',
469 default_value => 'foo',
473 $PRODUCER->($simple_default),
474 q{str_default character varying(10) DEFAULT 'foo'},
480 my $null_default = SQL::Translator::Schema::Field->new(
482 name => 'null_default',
483 default_value => \'NULL',
487 $PRODUCER->($null_default),
488 q{null_default character varying(10) DEFAULT NULL},
494 my $null_default = SQL::Translator::Schema::Field->new(
496 name => 'null_default_2',
497 default_value => 'NULL', # XXX: this should go away
501 $PRODUCER->($null_default),
502 q{null_default_2 character varying(10) DEFAULT NULL},
503 'default null from special cased string',
508 my $func_default = SQL::Translator::Schema::Field->new(
510 name => 'func_default',
511 default_value => \'func(funky)',
515 $PRODUCER->($func_default),
516 q{func_default character varying(10) DEFAULT func(funky)},
517 'unquoted default from scalar ref',
523 my $view1 = SQL::Translator::Schema::View->new(
525 fields => [qw/id name/],
526 sql => 'SELECT id, name FROM thing',
528 my $create_opts = { add_replace_view => 1, no_comments => 1 };
529 my $view1_sql1 = SQL::Translator::Producer::PostgreSQL::create_view($view1, $create_opts);
531 my $view_sql_replace = "CREATE VIEW view_foo ( id, name ) AS
532 SELECT id, name FROM thing
534 is($view1_sql1, $view_sql_replace, 'correct "CREATE OR REPLACE VIEW" SQL');
536 my $view2 = SQL::Translator::Schema::View->new(
538 sql => 'SELECT id, name FROM thing',
541 'check_option' => 'cascaded',
544 my $create2_opts = { add_replace_view => 1, no_comments => 1 };
545 my $view2_sql1 = SQL::Translator::Producer::PostgreSQL::create_view($view2, $create2_opts);
547 my $view2_sql_replace = "CREATE TEMPORARY VIEW view_foo2 AS
548 SELECT id, name FROM thing
549 WITH CASCADED CHECK OPTION";
550 is($view2_sql1, $view2_sql_replace, 'correct "CREATE OR REPLACE VIEW" SQL 2');
553 my $table = SQL::Translator::Schema::Table->new( name => 'foobar', fields => [qw( foo bar )] );
554 my $quote = { quote_table_names => '"', quote_field_names => '"' };
557 my $index = $table->add_index(name => 'myindex', fields => ['foo']);
558 my ($def) = SQL::Translator::Producer::PostgreSQL::create_index($index);
559 is($def, "CREATE INDEX myindex on foobar (foo)", 'index created');
560 ($def) = SQL::Translator::Producer::PostgreSQL::create_index($index, $quote);
561 is($def, 'CREATE INDEX "myindex" on "foobar" ("foo")', 'index created w/ quotes');
565 my $index = $table->add_index(name => 'myindex', fields => ['lower(foo)']);
566 my ($def) = SQL::Translator::Producer::PostgreSQL::create_index($index);
567 is($def, "CREATE INDEX myindex on foobar (lower(foo))", 'index created');
568 ($def) = SQL::Translator::Producer::PostgreSQL::create_index($index, $quote);
569 is($def, 'CREATE INDEX "myindex" on "foobar" (lower(foo))', 'index created w/ quotes');
573 my $index = $table->add_index(name => 'myindex', fields => ['bar', 'lower(foo)']);
574 my ($def) = SQL::Translator::Producer::PostgreSQL::create_index($index);
575 is($def, "CREATE INDEX myindex on foobar (bar, lower(foo))", 'index created');
576 ($def) = SQL::Translator::Producer::PostgreSQL::create_index($index, $quote);
577 is($def, 'CREATE INDEX "myindex" on "foobar" ("bar", lower(foo))', 'index created w/ quotes');
581 my $constr = $table->add_constraint(name => 'constr', type => UNIQUE, fields => ['foo']);
582 my ($def) = SQL::Translator::Producer::PostgreSQL::create_constraint($constr);
583 is($def->[0], 'CONSTRAINT constr UNIQUE (foo)', 'constraint created');
584 ($def) = SQL::Translator::Producer::PostgreSQL::create_constraint($constr, $quote);
585 is($def->[0], 'CONSTRAINT "constr" UNIQUE ("foo")', 'constraint created w/ quotes');
589 my $constr = $table->add_constraint(name => 'constr', type => UNIQUE, fields => ['lower(foo)']);
590 my ($def) = SQL::Translator::Producer::PostgreSQL::create_constraint($constr);
591 is($def->[0], 'CONSTRAINT constr UNIQUE (lower(foo))', 'constraint created');
592 ($def) = SQL::Translator::Producer::PostgreSQL::create_constraint($constr, $quote);
593 is($def->[0], 'CONSTRAINT "constr" UNIQUE (lower(foo))', 'constraint created w/ quotes');
597 my $constr = $table->add_constraint(name => 'constr', type => UNIQUE, fields => ['bar', 'lower(foo)']);
598 my ($def) = SQL::Translator::Producer::PostgreSQL::create_constraint($constr);
599 is($def->[0], 'CONSTRAINT constr UNIQUE (bar, lower(foo))', 'constraint created');
600 ($def) = SQL::Translator::Producer::PostgreSQL::create_constraint($constr, $quote);
601 is($def->[0], 'CONSTRAINT "constr" UNIQUE ("bar", lower(foo))', 'constraint created w/ quotes');
605 my $drop_view_opts1 = { add_drop_view => 1, no_comments => 1, postgres_version => 8.001 };
606 my $drop_view_8_1_produced = SQL::Translator::Producer::PostgreSQL::create_view($view1, $drop_view_opts1);
608 my $drop_view_8_1_expected = "DROP VIEW view_foo;
609 CREATE VIEW view_foo ( id, name ) AS
610 SELECT id, name FROM thing
613 is($drop_view_8_1_produced, $drop_view_8_1_expected, "My DROP VIEW statement for 8.1 is correct");
615 my $drop_view_opts2 = { add_drop_view => 1, no_comments => 1, postgres_version => 9.001 };
616 my $drop_view_9_1_produced = SQL::Translator::Producer::PostgreSQL::create_view($view1, $drop_view_opts2);
618 my $drop_view_9_1_expected = "DROP VIEW IF EXISTS view_foo;
619 CREATE VIEW view_foo ( id, name ) AS
620 SELECT id, name FROM thing
623 is($drop_view_9_1_produced, $drop_view_9_1_expected, "My DROP VIEW statement for 9.1 is correct");