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;
27 my $table = SQL::Translator::Schema::Table->new( name => 'mytable');
29 my $field1 = SQL::Translator::Schema::Field->new( name => 'myfield',
31 data_type => 'VARCHAR',
33 default_value => undef,
34 is_auto_increment => 0,
39 my $field1_sql = SQL::Translator::Producer::PostgreSQL::create_field($field1);
41 is($field1_sql, 'myfield character varying(10)', 'Create field works');
43 my $field_array = SQL::Translator::Schema::Field->new( name => 'myfield',
45 data_type => 'character varying[]',
47 default_value => undef,
48 is_auto_increment => 0,
53 my $field_array_sql = SQL::Translator::Producer::PostgreSQL::create_field($field_array);
55 is($field_array_sql, 'myfield character varying(10)[]', 'Create field works');
57 my $field2 = SQL::Translator::Schema::Field->new( name => 'myfield',
59 data_type => 'VARCHAR',
61 default_value => undef,
62 is_auto_increment => 0,
67 my $pk_constraint = SQL::Translator::Schema::Constraint->new(
70 fields => [qw(myfield)],
71 type => 'PRIMARY_KEY',
74 my ($pk_constraint_def_ref, $pk_constraint_fk_ref ) = SQL::Translator::Producer::PostgreSQL::create_constraint($pk_constraint);
75 ok(@{$pk_constraint_def_ref} == 1 && @{$pk_constraint_fk_ref} == 0, 'precheck of create_Primary Key constraint');
76 is($pk_constraint_def_ref->[0], 'CONSTRAINT foo PRIMARY KEY (myfield)', 'Create Primary Key Constraint works');
78 my $alter_pk_constraint = SQL::Translator::Producer::PostgreSQL::alter_drop_constraint($pk_constraint);
79 is($alter_pk_constraint, 'ALTER TABLE mytable DROP CONSTRAINT foo', 'Alter drop Primary Key constraint works');
81 my $table2 = SQL::Translator::Schema::Table->new( name => 'mytable2');
83 my $field1_2 = SQL::Translator::Schema::Field->new( name => 'myfield_2',
85 data_type => 'VARCHAR',
87 default_value => undef,
88 is_auto_increment => 0,
93 # check named, and unnamed foreign keys
94 for my $name ( 'foo', undef ) {
95 my $fk_constraint = SQL::Translator::Schema::Constraint->new(
98 fields => [qw(myfield)],
99 type => 'FOREIGN_KEY',
100 reference_table => $table2,
101 reference_fields => [qw(myfield_2)],
103 my $fk_constraint_2 = SQL::Translator::Schema::Constraint->new(
106 fields => [qw(myfield)],
107 type => 'FOREIGN_KEY',
108 reference_table => $table2,
109 reference_fields => [qw(myfield_2)],
112 my ($fk_constraint_def_ref, $fk_constraint_fk_ref ) = SQL::Translator::Producer::PostgreSQL::create_constraint($fk_constraint);
114 ok(@{$fk_constraint_def_ref} == 0 && @{$fk_constraint_fk_ref} == 1, 'precheck of create_Foreign Key constraint');
117 is($fk_constraint_fk_ref->[0], "ALTER TABLE mytable ADD CONSTRAINT $name FOREIGN KEY (myfield)
118 REFERENCES mytable2 (myfield_2) DEFERRABLE", 'Create Foreign Key Constraint works');
120 # ToDo: may we should check if the constraint name was valid, or if next
121 # unused_name created has choosen a different one
122 my $alter_fk_constraint = SQL::Translator::Producer::PostgreSQL::alter_drop_constraint($fk_constraint);
123 is($alter_fk_constraint, "ALTER TABLE mytable DROP CONSTRAINT $name", 'Alter drop Foreign Key constraint works');
126 is($fk_constraint_fk_ref->[0], 'ALTER TABLE mytable ADD FOREIGN KEY (myfield)
127 REFERENCES mytable2 (myfield_2) DEFERRABLE', 'Create named Foreign Key Constraint works');
129 my $alter_fk_constraint = SQL::Translator::Producer::PostgreSQL::alter_drop_constraint($fk_constraint);
130 is($alter_fk_constraint, 'ALTER TABLE mytable DROP CONSTRAINT mytable_myfield_fkey', 'Alter drop named Foreign Key constraint works');
135 my $alter_field = SQL::Translator::Producer::PostgreSQL::alter_field($field1,
137 is($alter_field, qq[ALTER TABLE mytable ALTER COLUMN myfield SET NOT NULL;
138 ALTER TABLE mytable ALTER COLUMN myfield TYPE character varying(25)],
139 'Alter field works');
141 my $field1_complex = SQL::Translator::Schema::Field->new(
142 name => 'my_complex_field',
144 data_type => 'VARCHAR',
146 default_value => undef,
147 is_auto_increment => 0,
153 my $field2_complex = SQL::Translator::Schema::Field->new(
154 name => 'my_altered_field',
156 data_type => 'VARCHAR',
158 default_value => 'whatever',
159 is_auto_increment => 0,
165 my $alter_field_complex = SQL::Translator::Producer::PostgreSQL::alter_field($field1_complex, $field2_complex);
167 $alter_field_complex,
168 q{ALTER TABLE mytable RENAME COLUMN my_complex_field TO my_altered_field;
169 ALTER TABLE mytable ALTER COLUMN my_altered_field TYPE character varying(60);
170 ALTER TABLE mytable ALTER COLUMN my_altered_field SET DEFAULT 'whatever'},
171 'Complex Alter field works'
174 $field1->name('field3');
175 my $add_field = SQL::Translator::Producer::PostgreSQL::add_field($field1);
177 is($add_field, 'ALTER TABLE mytable ADD COLUMN field3 character varying(10)', 'Add field works');
179 my $drop_field = SQL::Translator::Producer::PostgreSQL::drop_field($field2);
180 is($drop_field, 'ALTER TABLE mytable DROP COLUMN myfield', 'Drop field works');
182 my $field3 = SQL::Translator::Schema::Field->new( name => 'time_field',
185 default_value => undef,
186 is_auto_increment => 0,
191 my $field3_sql = SQL::Translator::Producer::PostgreSQL::create_field($field3);
193 is($field3_sql, 'time_field time NOT NULL', 'Create time field works');
195 my $field3_datetime_with_TZ = SQL::Translator::Schema::Field->new(
196 name => 'datetime_with_TZ',
198 data_type => 'timestamp with time zone',
202 my $field3_datetime_with_TZ_sql =
203 SQL::Translator::Producer::PostgreSQL::create_field(
204 $field3_datetime_with_TZ
208 $field3_datetime_with_TZ_sql,
209 'datetime_with_TZ timestamp(6) with time zone',
210 'Create time field with time zone and size, works'
213 my $field3_time_without_TZ = SQL::Translator::Schema::Field->new(
214 name => 'time_without_TZ',
216 data_type => 'time without time zone',
220 my $field3_time_without_TZ_sql
221 = SQL::Translator::Producer::PostgreSQL::create_field(
222 $field3_time_without_TZ
226 $field3_time_without_TZ_sql,
227 'time_without_TZ time(2) without time zone',
228 'Create time field without time zone but with size, works'
231 my $field_num = SQL::Translator::Schema::Field->new( name => 'num',
233 data_type => 'numeric',
236 my $fieldnum_sql = SQL::Translator::Producer::PostgreSQL::create_field($field_num);
238 is($fieldnum_sql, 'num numeric(10,2)', 'Create numeric field works');
241 my $field4 = SQL::Translator::Schema::Field->new( name => 'bytea_field',
243 data_type => 'bytea',
245 default_value => undef,
246 is_auto_increment => 0,
251 my $field4_sql = SQL::Translator::Producer::PostgreSQL::create_field($field4);
253 is($field4_sql, 'bytea_field bytea NOT NULL', 'Create bytea field works');
255 my $field5 = SQL::Translator::Schema::Field->new( name => 'enum_field',
258 extra => { list => [ 'Foo', 'Bar' ] },
259 is_auto_increment => 0,
264 my $field5_sql = SQL::Translator::Producer::PostgreSQL::create_field($field5,{ postgres_version => 8.3 });
266 is($field5_sql, 'enum_field mytable_enum_field_type NOT NULL', 'Create real enum field works');
271 my $field6 = SQL::Translator::Schema::Field->new(
274 data_type => 'character',
276 default_value => 'foobar',
277 is_auto_increment => 0,
282 my $field7 = SQL::Translator::Schema::Field->new(
285 data_type => 'character',
287 default_value => undef,
288 is_auto_increment => 0,
293 $alter_field = SQL::Translator::Producer::PostgreSQL::alter_field($field6,
296 is($alter_field, q(ALTER TABLE mytable ALTER COLUMN character DROP DEFAULT), 'DROP DEFAULT');
298 $field7->default_value(q(foo'bar'));
300 $alter_field = SQL::Translator::Producer::PostgreSQL::alter_field($field6,
303 is($alter_field, q(ALTER TABLE mytable ALTER COLUMN character SET DEFAULT 'foo''bar'''), 'DEFAULT with escaping');
305 $field7->default_value(\q(foobar));
307 $alter_field = SQL::Translator::Producer::PostgreSQL::alter_field($field6,
310 is($alter_field, q(ALTER TABLE mytable ALTER COLUMN character SET DEFAULT foobar), 'DEFAULT unescaped if scalarref');
312 $field7->is_nullable(1);
313 $field7->default_value(q(foobar));
315 $alter_field = SQL::Translator::Producer::PostgreSQL::alter_field($field6,
318 is($alter_field, q(ALTER TABLE mytable ALTER COLUMN character DROP NOT NULL), 'DROP NOT NULL');
320 my $field8 = SQL::Translator::Schema::Field->new( name => 'ts_field',
322 data_type => 'timestamp with time zone',
324 is_auto_increment => 0,
329 my $field8_sql = SQL::Translator::Producer::PostgreSQL::create_field($field8,{ postgres_version => 8.3 });
331 is($field8_sql, 'ts_field timestamp(6) with time zone NOT NULL', 'timestamp with precision');
333 my $field9 = SQL::Translator::Schema::Field->new( name => 'time_field',
335 data_type => 'time with time zone',
337 is_auto_increment => 0,
342 my $field9_sql = SQL::Translator::Producer::PostgreSQL::create_field($field9,{ postgres_version => 8.3 });
344 is($field9_sql, 'time_field time(6) with time zone NOT NULL', 'time with precision');
346 my $field10 = SQL::Translator::Schema::Field->new( name => 'interval_field',
348 data_type => 'interval',
350 is_auto_increment => 0,
355 my $field10_sql = SQL::Translator::Producer::PostgreSQL::create_field($field10,{ postgres_version => 8.3 });
357 is($field10_sql, 'interval_field interval(6) NOT NULL', 'time with precision');
360 my $field11 = SQL::Translator::Schema::Field->new( name => 'time_field',
362 data_type => 'time without time zone',
364 is_auto_increment => 0,
369 my $field11_sql = SQL::Translator::Producer::PostgreSQL::create_field($field11,{ postgres_version => 8.3 });
371 is($field11_sql, 'time_field time(6) without time zone NOT NULL', 'time with precision');
375 my $field12 = SQL::Translator::Schema::Field->new( name => 'time_field',
377 data_type => 'timestamp',
378 is_auto_increment => 0,
383 my $field12_sql = SQL::Translator::Producer::PostgreSQL::create_field($field12,{ postgres_version => 8.3 });
385 is($field12_sql, 'time_field timestamp NOT NULL', 'time with precision');
387 my $field13 = SQL::Translator::Schema::Field->new( name => 'enum_field_with_type_name',
390 extra => { list => [ 'Foo', 'Bar' ],
391 custom_type_name => 'real_enum_type' },
392 is_auto_increment => 0,
397 my $field13_sql = SQL::Translator::Producer::PostgreSQL::create_field($field13,{ postgres_version => 8.3 });
399 is($field13_sql, 'enum_field_with_type_name real_enum_type NOT NULL', 'Create real enum field works');
403 # let's test default values! -- rjbs, 2008-09-30
406 data_type => 'VARCHAR',
408 is_auto_increment => 0,
415 my $simple_default = SQL::Translator::Schema::Field->new(
417 name => 'str_default',
418 default_value => 'foo',
422 $PRODUCER->($simple_default),
423 q{str_default character varying(10) DEFAULT 'foo'},
429 my $null_default = SQL::Translator::Schema::Field->new(
431 name => 'null_default',
432 default_value => \'NULL',
436 $PRODUCER->($null_default),
437 q{null_default character varying(10) DEFAULT NULL},
443 my $null_default = SQL::Translator::Schema::Field->new(
445 name => 'null_default_2',
446 default_value => 'NULL', # XXX: this should go away
450 $PRODUCER->($null_default),
451 q{null_default_2 character varying(10) DEFAULT NULL},
452 'default null from special cased string',
457 my $func_default = SQL::Translator::Schema::Field->new(
459 name => 'func_default',
460 default_value => \'func(funky)',
464 $PRODUCER->($func_default),
465 q{func_default character varying(10) DEFAULT func(funky)},
466 'unquoted default from scalar ref',
472 my $view1 = SQL::Translator::Schema::View->new(
474 fields => [qw/id name/],
475 sql => 'SELECT id, name FROM thing',
477 my $create_opts = { add_replace_view => 1, no_comments => 1 };
478 my $view1_sql1 = SQL::Translator::Producer::PostgreSQL::create_view($view1, $create_opts);
480 my $view_sql_replace = "CREATE VIEW view_foo ( id, name ) AS
481 SELECT id, name FROM thing
483 is($view1_sql1, $view_sql_replace, 'correct "CREATE OR REPLACE VIEW" SQL');
485 my $view2 = SQL::Translator::Schema::View->new(
487 sql => 'SELECT id, name FROM thing',
490 'check_option' => 'cascaded',
493 my $create2_opts = { add_replace_view => 1, no_comments => 1 };
494 my $view2_sql1 = SQL::Translator::Producer::PostgreSQL::create_view($view2, $create2_opts);
496 my $view2_sql_replace = "CREATE TEMPORARY VIEW view_foo2 AS
497 SELECT id, name FROM thing
498 WITH CASCADED CHECK OPTION";
499 is($view2_sql1, $view2_sql_replace, 'correct "CREATE OR REPLACE VIEW" SQL 2');
502 my $table = SQL::Translator::Schema::Table->new( name => 'foobar', fields => [qw( foo bar )] );
503 my $quote = { quote_table_names => '"', quote_field_names => '"' };
506 my $index = $table->add_index(name => 'myindex', fields => ['foo']);
507 my ($def) = SQL::Translator::Producer::PostgreSQL::create_index($index);
508 is($def, "CREATE INDEX myindex on foobar (foo)", 'index created');
509 ($def) = SQL::Translator::Producer::PostgreSQL::create_index($index, $quote);
510 is($def, 'CREATE INDEX "myindex" on "foobar" ("foo")', 'index created w/ quotes');
514 my $index = $table->add_index(name => 'myindex', fields => ['lower(foo)']);
515 my ($def) = SQL::Translator::Producer::PostgreSQL::create_index($index);
516 is($def, "CREATE INDEX myindex on foobar (lower(foo))", 'index created');
517 ($def) = SQL::Translator::Producer::PostgreSQL::create_index($index, $quote);
518 is($def, 'CREATE INDEX "myindex" on "foobar" (lower(foo))', 'index created w/ quotes');
522 my $index = $table->add_index(name => 'myindex', fields => ['bar', 'lower(foo)']);
523 my ($def) = SQL::Translator::Producer::PostgreSQL::create_index($index);
524 is($def, "CREATE INDEX myindex on foobar (bar, lower(foo))", 'index created');
525 ($def) = SQL::Translator::Producer::PostgreSQL::create_index($index, $quote);
526 is($def, 'CREATE INDEX "myindex" on "foobar" ("bar", lower(foo))', 'index created w/ quotes');
530 my $constr = $table->add_constraint(name => 'constr', type => UNIQUE, fields => ['foo']);
531 my ($def) = SQL::Translator::Producer::PostgreSQL::create_constraint($constr);
532 is($def->[0], 'CONSTRAINT constr UNIQUE (foo)', 'constraint created');
533 ($def) = SQL::Translator::Producer::PostgreSQL::create_constraint($constr, $quote);
534 is($def->[0], 'CONSTRAINT "constr" UNIQUE ("foo")', 'constraint created w/ quotes');
538 my $constr = $table->add_constraint(name => 'constr', type => UNIQUE, fields => ['lower(foo)']);
539 my ($def) = SQL::Translator::Producer::PostgreSQL::create_constraint($constr);
540 is($def->[0], 'CONSTRAINT constr UNIQUE (lower(foo))', 'constraint created');
541 ($def) = SQL::Translator::Producer::PostgreSQL::create_constraint($constr, $quote);
542 is($def->[0], 'CONSTRAINT "constr" UNIQUE (lower(foo))', 'constraint created w/ quotes');
546 my $constr = $table->add_constraint(name => 'constr', type => UNIQUE, fields => ['bar', 'lower(foo)']);
547 my ($def) = SQL::Translator::Producer::PostgreSQL::create_constraint($constr);
548 is($def->[0], 'CONSTRAINT constr UNIQUE (bar, lower(foo))', 'constraint created');
549 ($def) = SQL::Translator::Producer::PostgreSQL::create_constraint($constr, $quote);
550 is($def->[0], 'CONSTRAINT "constr" UNIQUE ("bar", lower(foo))', 'constraint created w/ quotes');
554 my $drop_view_opts1 = { add_drop_view => 1, no_comments => 1, postgres_version => 8.001 };
555 my $drop_view_8_1_produced = SQL::Translator::Producer::PostgreSQL::create_view($view1, $drop_view_opts1);
557 my $drop_view_8_1_expected = "DROP VIEW view_foo;
558 CREATE VIEW view_foo ( id, name ) AS
559 SELECT id, name FROM thing
562 is($drop_view_8_1_produced, $drop_view_8_1_expected, "My DROP VIEW statement for 8.1 is correct");
564 my $drop_view_opts2 = { add_drop_view => 1, no_comments => 1, postgres_version => 9.001 };
565 my $drop_view_9_1_produced = SQL::Translator::Producer::PostgreSQL::create_view($view1, $drop_view_opts2);
567 my $drop_view_9_1_expected = "DROP VIEW IF EXISTS view_foo;
568 CREATE VIEW view_foo ( id, name ) AS
569 SELECT id, name FROM thing
572 is($drop_view_9_1_produced, $drop_view_9_1_expected, "My DROP VIEW statement for 9.1 is correct");