Fix RT49301
[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);
9
10use Data::Dumper;
11use FindBin qw/$Bin/;
12
13# Testing 1,2,3,4...
14#=============================================================================
15
16BEGIN {
c3bddac9 17 maybe_plan(25,
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
43my $field2 = SQL::Translator::Schema::Field->new( name => 'myfield',
44 table => $table,
45 data_type => 'VARCHAR',
46 size => 25,
47 default_value => undef,
48 is_auto_increment => 0,
49 is_nullable => 0,
50 is_foreign_key => 0,
51 is_unique => 0 );
52
53my $alter_field = SQL::Translator::Producer::PostgreSQL::alter_field($field1,
54 $field2);
3406fd5b 55is($alter_field, qq[ALTER TABLE mytable ALTER COLUMN myfield SET NOT NULL
56ALTER TABLE mytable ALTER COLUMN myfield TYPE character varying(25)],
8c4efd11 57 'Alter field works');
58
59$field1->name('field3');
60my $add_field = SQL::Translator::Producer::PostgreSQL::add_field($field1);
61
3406fd5b 62is($add_field, 'ALTER TABLE mytable ADD COLUMN field3 character varying(10)', 'Add field works');
8c4efd11 63
64my $drop_field = SQL::Translator::Producer::PostgreSQL::drop_field($field2);
3406fd5b 65is($drop_field, 'ALTER TABLE mytable DROP COLUMN myfield', 'Drop field works');
8c4efd11 66
e56dabb7 67my $field3 = SQL::Translator::Schema::Field->new( name => 'time_field',
68 table => $table,
69 data_type => 'TIME',
70 default_value => undef,
71 is_auto_increment => 0,
72 is_nullable => 0,
73 is_foreign_key => 0,
74 is_unique => 0 );
75
76my $field3_sql = SQL::Translator::Producer::PostgreSQL::create_field($field3);
77
78is($field3_sql, 'time_field time NOT NULL', 'Create time field works');
79
621cb859 80my $field3_datetime_with_TZ = SQL::Translator::Schema::Field->new(
81 name => 'datetime_with_TZ',
82 table => $table,
83 data_type => 'timestamp with time zone',
84 size => 7,
85);
86
87my $field3_datetime_with_TZ_sql =
88 SQL::Translator::Producer::PostgreSQL::create_field(
89 $field3_datetime_with_TZ
90 );
91
92is(
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'
96);
97
98my $field3_time_without_TZ = SQL::Translator::Schema::Field->new(
99 name => 'time_without_TZ',
100 table => $table,
101 data_type => 'time without time zone',
102 size => 2,
103);
104
105my $field3_time_without_TZ_sql
106 = SQL::Translator::Producer::PostgreSQL::create_field(
107 $field3_time_without_TZ
108 );
109
110is(
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'
114);
115
c3bddac9 116my $field_num = SQL::Translator::Schema::Field->new( name => 'num',
117 table => $table,
118 data_type => 'numeric',
119 size => [10,2],
120 );
121my $fieldnum_sql = SQL::Translator::Producer::PostgreSQL::create_field($field_num);
122
123is($fieldnum_sql, 'num numeric(10,2)', 'Create numeric field works');
124
125
e56dabb7 126my $field4 = SQL::Translator::Schema::Field->new( name => 'bytea_field',
127 table => $table,
128 data_type => 'bytea',
129 size => '16777215',
130 default_value => undef,
131 is_auto_increment => 0,
132 is_nullable => 0,
133 is_foreign_key => 0,
134 is_unique => 0 );
135
136my $field4_sql = SQL::Translator::Producer::PostgreSQL::create_field($field4);
8c4efd11 137
e56dabb7 138is($field4_sql, 'bytea_field bytea NOT NULL', 'Create bytea field works');
5342f5c1 139
140my $field5 = SQL::Translator::Schema::Field->new( name => 'enum_field',
141 table => $table,
142 data_type => 'enum',
143 extra => { list => [ 'Foo', 'Bar' ] },
144 is_auto_increment => 0,
145 is_nullable => 0,
146 is_foreign_key => 0,
147 is_unique => 0 );
148
149my $field5_sql = SQL::Translator::Producer::PostgreSQL::create_field($field5,{ postgres_version => 8.3 });
150
151is($field5_sql, 'enum_field mytable_enum_field_type NOT NULL', 'Create real enum field works');
152
90726ffd 153
154
155
156my $field6 = SQL::Translator::Schema::Field->new(
157 name => 'character',
158 table => $table,
159 data_type => 'character',
160 size => '123',
161 default_value => 'foobar',
162 is_auto_increment => 0,
163 is_nullable => 0,
164 is_foreign_key => 0,
165 is_unique => 0);
166
167my $field7 = SQL::Translator::Schema::Field->new(
168 name => 'character',
169 table => $table,
170 data_type => 'character',
171 size => '123',
172 default_value => undef,
173 is_auto_increment => 0,
174 is_nullable => 0,
175 is_foreign_key => 0,
176 is_unique => 0);
177
178$alter_field = SQL::Translator::Producer::PostgreSQL::alter_field($field6,
179 $field7);
180
181is($alter_field, q(ALTER TABLE mytable ALTER COLUMN character DROP DEFAULT), 'DROP DEFAULT');
182
183$field7->default_value(q(foo'bar'));
184
185$alter_field = SQL::Translator::Producer::PostgreSQL::alter_field($field6,
186 $field7);
187
188is($alter_field, q(ALTER TABLE mytable ALTER COLUMN character SET DEFAULT 'foo''bar'''), 'DEFAULT with escaping');
189
190$field7->default_value(\q(foobar));
191
192$alter_field = SQL::Translator::Producer::PostgreSQL::alter_field($field6,
193 $field7);
194
195is($alter_field, q(ALTER TABLE mytable ALTER COLUMN character SET DEFAULT foobar), 'DEFAULT unescaped if scalarref');
196
197$field7->is_nullable(1);
198$field7->default_value(q(foobar));
199
200$alter_field = SQL::Translator::Producer::PostgreSQL::alter_field($field6,
201 $field7);
202
203is($alter_field, q(ALTER TABLE mytable ALTER COLUMN character DROP NOT NULL), 'DROP NOT NULL');
204
ad258776 205my $field8 = SQL::Translator::Schema::Field->new( name => 'ts_field',
206 table => $table,
207 data_type => 'timestamp with time zone',
208 size => 6,
209 is_auto_increment => 0,
210 is_nullable => 0,
211 is_foreign_key => 0,
212 is_unique => 0 );
213
214my $field8_sql = SQL::Translator::Producer::PostgreSQL::create_field($field8,{ postgres_version => 8.3 });
215
216is($field8_sql, 'ts_field timestamp(6) with time zone NOT NULL', 'timestamp with precision');
217
218my $field9 = SQL::Translator::Schema::Field->new( name => 'time_field',
219 table => $table,
220 data_type => 'time with time zone',
221 size => 6,
222 is_auto_increment => 0,
223 is_nullable => 0,
224 is_foreign_key => 0,
225 is_unique => 0 );
226
227my $field9_sql = SQL::Translator::Producer::PostgreSQL::create_field($field9,{ postgres_version => 8.3 });
228
229is($field9_sql, 'time_field time(6) with time zone NOT NULL', 'time with precision');
230
231my $field10 = SQL::Translator::Schema::Field->new( name => 'interval_field',
232 table => $table,
233 data_type => 'interval',
234 size => 6,
235 is_auto_increment => 0,
236 is_nullable => 0,
237 is_foreign_key => 0,
238 is_unique => 0 );
239
240my $field10_sql = SQL::Translator::Producer::PostgreSQL::create_field($field10,{ postgres_version => 8.3 });
241
242is($field10_sql, 'interval_field interval(6) NOT NULL', 'time with precision');
243
244
245my $field11 = SQL::Translator::Schema::Field->new( name => 'time_field',
246 table => $table,
247 data_type => 'time without time zone',
248 size => 6,
249 is_auto_increment => 0,
250 is_nullable => 0,
251 is_foreign_key => 0,
252 is_unique => 0 );
253
254my $field11_sql = SQL::Translator::Producer::PostgreSQL::create_field($field11,{ postgres_version => 8.3 });
255
256is($field11_sql, 'time_field time(6) without time zone NOT NULL', 'time with precision');
257
258
259
260my $field12 = SQL::Translator::Schema::Field->new( name => 'time_field',
261 table => $table,
262 data_type => 'timestamp',
263 is_auto_increment => 0,
264 is_nullable => 0,
265 is_foreign_key => 0,
266 is_unique => 0 );
267
268my $field12_sql = SQL::Translator::Producer::PostgreSQL::create_field($field12,{ postgres_version => 8.3 });
269
270is($field12_sql, 'time_field timestamp NOT NULL', 'time with precision');
271
90726ffd 272
bc8e2aa1 273{
274 # let's test default values! -- rjbs, 2008-09-30
275 my %field = (
276 table => $table,
277 data_type => 'VARCHAR',
278 size => 10,
279 is_auto_increment => 0,
280 is_nullable => 1,
281 is_foreign_key => 0,
282 is_unique => 0,
283 );
284
285 {
286 my $simple_default = SQL::Translator::Schema::Field->new(
287 %field,
288 name => 'str_default',
289 default_value => 'foo',
290 );
291
292 is(
293 $PRODUCER->($simple_default),
294 q{str_default character varying(10) DEFAULT 'foo'},
295 'default str',
296 );
297 }
298
299 {
300 my $null_default = SQL::Translator::Schema::Field->new(
301 %field,
302 name => 'null_default',
303 default_value => \'NULL',
304 );
305
306 is(
307 $PRODUCER->($null_default),
308 q{null_default character varying(10) DEFAULT NULL},
309 'default null',
310 );
311 }
312
313 {
314 my $null_default = SQL::Translator::Schema::Field->new(
315 %field,
316 name => 'null_default_2',
317 default_value => 'NULL', # XXX: this should go away
318 );
319
320 is(
321 $PRODUCER->($null_default),
322 q{null_default_2 character varying(10) DEFAULT NULL},
323 'default null from special cased string',
324 );
325 }
326
327 {
328 my $func_default = SQL::Translator::Schema::Field->new(
329 %field,
330 name => 'func_default',
331 default_value => \'func(funky)',
332 );
333
334 is(
335 $PRODUCER->($func_default),
336 q{func_default character varying(10) DEFAULT func(funky)},
337 'unquoted default from scalar ref',
338 );
339 }
340}
341
342
296c2701 343my $view1 = SQL::Translator::Schema::View->new(
344 name => 'view_foo',
345 fields => [qw/id name/],
346 sql => 'SELECT id, name FROM thing',
347);
348my $create_opts = { add_replace_view => 1, no_comments => 1 };
349my $view1_sql1 = SQL::Translator::Producer::PostgreSQL::create_view($view1, $create_opts);
350
f59b2c0e 351my $view_sql_replace = "CREATE VIEW view_foo ( id, name ) AS
296c2701 352 SELECT id, name FROM thing
f59b2c0e 353";
296c2701 354is($view1_sql1, $view_sql_replace, 'correct "CREATE OR REPLACE VIEW" SQL');
355
356my $view2 = SQL::Translator::Schema::View->new(
357 name => 'view_foo2',
358 sql => 'SELECT id, name FROM thing',
359 extra => {
360 'temporary' => '1',
361 'check_option' => 'cascaded',
362 },
363);
364my $create2_opts = { add_replace_view => 1, no_comments => 1 };
365my $view2_sql1 = SQL::Translator::Producer::PostgreSQL::create_view($view2, $create2_opts);
366
f59b2c0e 367my $view2_sql_replace = "CREATE TEMPORARY VIEW view_foo2 AS
296c2701 368 SELECT id, name FROM thing
f59b2c0e 369 WITH CASCADED CHECK OPTION";
296c2701 370is($view2_sql1, $view2_sql_replace, 'correct "CREATE OR REPLACE VIEW" SQL 2');