8 use Test::SQL::Translator qw(maybe_plan);
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 $field2 = SQL::Translator::Schema::Field->new( name => 'myfield',
45 data_type => 'VARCHAR',
47 default_value => undef,
48 is_auto_increment => 0,
53 my $alter_field = SQL::Translator::Producer::PostgreSQL::alter_field($field1,
55 is($alter_field, qq[ALTER TABLE mytable ALTER COLUMN myfield SET NOT NULL
56 ALTER TABLE mytable ALTER COLUMN myfield TYPE character varying(25)],
59 $field1->name('field3');
60 my $add_field = SQL::Translator::Producer::PostgreSQL::add_field($field1);
62 is($add_field, 'ALTER TABLE mytable ADD COLUMN field3 character varying(10)', 'Add field works');
64 my $drop_field = SQL::Translator::Producer::PostgreSQL::drop_field($field2);
65 is($drop_field, 'ALTER TABLE mytable DROP COLUMN myfield', 'Drop field works');
67 my $field3 = SQL::Translator::Schema::Field->new( name => 'time_field',
70 default_value => undef,
71 is_auto_increment => 0,
76 my $field3_sql = SQL::Translator::Producer::PostgreSQL::create_field($field3);
78 is($field3_sql, 'time_field time NOT NULL', 'Create time field works');
80 my $field3_datetime_with_TZ = SQL::Translator::Schema::Field->new(
81 name => 'datetime_with_TZ',
83 data_type => 'timestamp with time zone',
87 my $field3_datetime_with_TZ_sql =
88 SQL::Translator::Producer::PostgreSQL::create_field(
89 $field3_datetime_with_TZ
93 $field3_datetime_with_TZ_sql,
94 'datetime_with_TZ timestamp(6) with time zone',
95 'Create time field with time zone and size, works'
98 my $field3_time_without_TZ = SQL::Translator::Schema::Field->new(
99 name => 'time_without_TZ',
101 data_type => 'time without time zone',
105 my $field3_time_without_TZ_sql
106 = SQL::Translator::Producer::PostgreSQL::create_field(
107 $field3_time_without_TZ
111 $field3_time_without_TZ_sql,
112 'time_without_TZ time(2) without time zone',
113 'Create time field without time zone but with size, works'
116 my $field_num = SQL::Translator::Schema::Field->new( name => 'num',
118 data_type => 'numeric',
121 my $fieldnum_sql = SQL::Translator::Producer::PostgreSQL::create_field($field_num);
123 is($fieldnum_sql, 'num numeric(10,2)', 'Create numeric field works');
126 my $field4 = SQL::Translator::Schema::Field->new( name => 'bytea_field',
128 data_type => 'bytea',
130 default_value => undef,
131 is_auto_increment => 0,
136 my $field4_sql = SQL::Translator::Producer::PostgreSQL::create_field($field4);
138 is($field4_sql, 'bytea_field bytea NOT NULL', 'Create bytea field works');
140 my $field5 = SQL::Translator::Schema::Field->new( name => 'enum_field',
143 extra => { list => [ 'Foo', 'Bar' ] },
144 is_auto_increment => 0,
149 my $field5_sql = SQL::Translator::Producer::PostgreSQL::create_field($field5,{ postgres_version => 8.3 });
151 is($field5_sql, 'enum_field mytable_enum_field_type NOT NULL', 'Create real enum field works');
156 my $field6 = SQL::Translator::Schema::Field->new(
159 data_type => 'character',
161 default_value => 'foobar',
162 is_auto_increment => 0,
167 my $field7 = SQL::Translator::Schema::Field->new(
170 data_type => 'character',
172 default_value => undef,
173 is_auto_increment => 0,
178 $alter_field = SQL::Translator::Producer::PostgreSQL::alter_field($field6,
181 is($alter_field, q(ALTER TABLE mytable ALTER COLUMN character DROP DEFAULT), 'DROP DEFAULT');
183 $field7->default_value(q(foo'bar'));
185 $alter_field = SQL::Translator::Producer::PostgreSQL::alter_field($field6,
188 is($alter_field, q(ALTER TABLE mytable ALTER COLUMN character SET DEFAULT 'foo''bar'''), 'DEFAULT with escaping');
190 $field7->default_value(\q(foobar));
192 $alter_field = SQL::Translator::Producer::PostgreSQL::alter_field($field6,
195 is($alter_field, q(ALTER TABLE mytable ALTER COLUMN character SET DEFAULT foobar), 'DEFAULT unescaped if scalarref');
197 $field7->is_nullable(1);
198 $field7->default_value(q(foobar));
200 $alter_field = SQL::Translator::Producer::PostgreSQL::alter_field($field6,
203 is($alter_field, q(ALTER TABLE mytable ALTER COLUMN character DROP NOT NULL), 'DROP NOT NULL');
205 my $field8 = SQL::Translator::Schema::Field->new( name => 'ts_field',
207 data_type => 'timestamp with time zone',
209 is_auto_increment => 0,
214 my $field8_sql = SQL::Translator::Producer::PostgreSQL::create_field($field8,{ postgres_version => 8.3 });
216 is($field8_sql, 'ts_field timestamp(6) with time zone NOT NULL', 'timestamp with precision');
218 my $field9 = SQL::Translator::Schema::Field->new( name => 'time_field',
220 data_type => 'time with time zone',
222 is_auto_increment => 0,
227 my $field9_sql = SQL::Translator::Producer::PostgreSQL::create_field($field9,{ postgres_version => 8.3 });
229 is($field9_sql, 'time_field time(6) with time zone NOT NULL', 'time with precision');
231 my $field10 = SQL::Translator::Schema::Field->new( name => 'interval_field',
233 data_type => 'interval',
235 is_auto_increment => 0,
240 my $field10_sql = SQL::Translator::Producer::PostgreSQL::create_field($field10,{ postgres_version => 8.3 });
242 is($field10_sql, 'interval_field interval(6) NOT NULL', 'time with precision');
245 my $field11 = SQL::Translator::Schema::Field->new( name => 'time_field',
247 data_type => 'time without time zone',
249 is_auto_increment => 0,
254 my $field11_sql = SQL::Translator::Producer::PostgreSQL::create_field($field11,{ postgres_version => 8.3 });
256 is($field11_sql, 'time_field time(6) without time zone NOT NULL', 'time with precision');
260 my $field12 = SQL::Translator::Schema::Field->new( name => 'time_field',
262 data_type => 'timestamp',
263 is_auto_increment => 0,
268 my $field12_sql = SQL::Translator::Producer::PostgreSQL::create_field($field12,{ postgres_version => 8.3 });
270 is($field12_sql, 'time_field timestamp NOT NULL', 'time with precision');
274 # let's test default values! -- rjbs, 2008-09-30
277 data_type => 'VARCHAR',
279 is_auto_increment => 0,
286 my $simple_default = SQL::Translator::Schema::Field->new(
288 name => 'str_default',
289 default_value => 'foo',
293 $PRODUCER->($simple_default),
294 q{str_default character varying(10) DEFAULT 'foo'},
300 my $null_default = SQL::Translator::Schema::Field->new(
302 name => 'null_default',
303 default_value => \'NULL',
307 $PRODUCER->($null_default),
308 q{null_default character varying(10) DEFAULT NULL},
314 my $null_default = SQL::Translator::Schema::Field->new(
316 name => 'null_default_2',
317 default_value => 'NULL', # XXX: this should go away
321 $PRODUCER->($null_default),
322 q{null_default_2 character varying(10) DEFAULT NULL},
323 'default null from special cased string',
328 my $func_default = SQL::Translator::Schema::Field->new(
330 name => 'func_default',
331 default_value => \'func(funky)',
335 $PRODUCER->($func_default),
336 q{func_default character varying(10) DEFAULT func(funky)},
337 'unquoted default from scalar ref',
343 my $view1 = SQL::Translator::Schema::View->new(
345 fields => [qw/id name/],
346 sql => 'SELECT id, name FROM thing',
348 my $create_opts = { add_replace_view => 1, no_comments => 1 };
349 my $view1_sql1 = SQL::Translator::Producer::PostgreSQL::create_view($view1, $create_opts);
351 my $view_sql_replace = "CREATE VIEW view_foo ( id, name ) AS
352 SELECT id, name FROM thing
354 is($view1_sql1, $view_sql_replace, 'correct "CREATE OR REPLACE VIEW" SQL');
356 my $view2 = SQL::Translator::Schema::View->new(
358 sql => 'SELECT id, name FROM thing',
361 'check_option' => 'cascaded',
364 my $create2_opts = { add_replace_view => 1, no_comments => 1 };
365 my $view2_sql1 = SQL::Translator::Producer::PostgreSQL::create_view($view2, $create2_opts);
367 my $view2_sql_replace = "CREATE TEMPORARY VIEW view_foo2 AS
368 SELECT id, name FROM thing
369 WITH CASCADED CHECK OPTION";
370 is($view2_sql1, $view2_sql_replace, 'correct "CREATE OR REPLACE VIEW" SQL 2');