Make Pg producer consistent with the rest in terms of quoting
[dbsrgits/SQL-Translator.git] / t / 47postgres-producer.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More;
7 use Test::Exception;
8 use Test::SQL::Translator qw(maybe_plan);
9 use SQL::Translator::Schema::Constants;
10 use Data::Dumper;
11 use FindBin qw/$Bin/;
12
13 # Testing 1,2,3,4...
14 #=============================================================================
15
16 BEGIN {
17     maybe_plan(38,
18         'SQL::Translator::Producer::PostgreSQL',
19         'Test::Differences',
20     )
21 }
22 use Test::Differences;
23 use SQL::Translator;
24
25 my $PRODUCER = \&SQL::Translator::Producer::PostgreSQL::create_field;
26
27 my $table = SQL::Translator::Schema::Table->new( name => 'mytable');
28
29 my $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
39 my $field1_sql = SQL::Translator::Producer::PostgreSQL::create_field($field1);
40
41 is($field1_sql, 'myfield character varying(10)', 'Create field works');
42
43 my $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
53 my $alter_field = SQL::Translator::Producer::PostgreSQL::alter_field($field1,
54                                                                 $field2);
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)],
57  'Alter field works');
58
59 $field1->name('field3');
60 my $add_field = SQL::Translator::Producer::PostgreSQL::add_field($field1);
61
62 is($add_field, 'ALTER TABLE mytable ADD COLUMN field3 character varying(10)', 'Add field works');
63
64 my $drop_field = SQL::Translator::Producer::PostgreSQL::drop_field($field2);
65 is($drop_field, 'ALTER TABLE mytable DROP COLUMN myfield', 'Drop field works');
66
67 my $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
76 my $field3_sql = SQL::Translator::Producer::PostgreSQL::create_field($field3);
77
78 is($field3_sql, 'time_field time NOT NULL', 'Create time field works');
79
80 my $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
87 my $field3_datetime_with_TZ_sql = 
88     SQL::Translator::Producer::PostgreSQL::create_field(
89         $field3_datetime_with_TZ
90     );
91
92 is(
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
98 my $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
105 my $field3_time_without_TZ_sql 
106     = SQL::Translator::Producer::PostgreSQL::create_field(
107         $field3_time_without_TZ
108     );
109
110 is(
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
116 my $field_num = SQL::Translator::Schema::Field->new( name => 'num',
117                                                   table => $table,
118                                                   data_type => 'numeric',
119                                                   size => [10,2],
120                                                   );
121 my $fieldnum_sql = SQL::Translator::Producer::PostgreSQL::create_field($field_num);
122
123 is($fieldnum_sql, 'num numeric(10,2)', 'Create numeric field works');
124
125
126 my $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
136 my $field4_sql = SQL::Translator::Producer::PostgreSQL::create_field($field4);
137
138 is($field4_sql, 'bytea_field bytea NOT NULL', 'Create bytea field works');
139
140 my $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
149 my $field5_sql = SQL::Translator::Producer::PostgreSQL::create_field($field5,{ postgres_version => 8.3 });
150
151 is($field5_sql, 'enum_field mytable_enum_field_type NOT NULL', 'Create real enum field works');
152
153
154
155
156 my $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
167 my $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
181 is($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
188 is($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
195 is($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
203 is($alter_field, q(ALTER TABLE mytable ALTER COLUMN character DROP NOT NULL), 'DROP NOT NULL');
204
205 my $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
214 my $field8_sql = SQL::Translator::Producer::PostgreSQL::create_field($field8,{ postgres_version => 8.3 });
215
216 is($field8_sql, 'ts_field timestamp(6) with time zone NOT NULL', 'timestamp with precision');
217
218 my $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
227 my $field9_sql = SQL::Translator::Producer::PostgreSQL::create_field($field9,{ postgres_version => 8.3 });
228
229 is($field9_sql, 'time_field time(6) with time zone NOT NULL', 'time with precision');
230
231 my $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
240 my $field10_sql = SQL::Translator::Producer::PostgreSQL::create_field($field10,{ postgres_version => 8.3 });
241
242 is($field10_sql, 'interval_field interval(6) NOT NULL', 'time with precision');
243
244
245 my $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
254 my $field11_sql = SQL::Translator::Producer::PostgreSQL::create_field($field11,{ postgres_version => 8.3 });
255
256 is($field11_sql, 'time_field time(6) without time zone NOT NULL', 'time with precision');
257
258
259
260 my $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
268 my $field12_sql = SQL::Translator::Producer::PostgreSQL::create_field($field12,{ postgres_version => 8.3 });
269
270 is($field12_sql, 'time_field timestamp NOT NULL', 'time with precision');
271
272 my $field13 = SQL::Translator::Schema::Field->new( name => 'enum_field_with_type_name',
273                                                    table => $table,
274                                                    data_type => 'enum',
275                                                    extra => { list => [ 'Foo', 'Bar' ],
276                                                               custom_type_name => 'real_enum_type' },
277                                                    is_auto_increment => 0,
278                                                    is_nullable => 0,
279                                                    is_foreign_key => 0,
280                                                    is_unique => 0 );
281
282 my $field13_sql = SQL::Translator::Producer::PostgreSQL::create_field($field13,{ postgres_version => 8.3 });
283
284 is($field13_sql, 'enum_field_with_type_name real_enum_type NOT NULL', 'Create real enum field works');
285
286
287 {
288     # let's test default values! -- rjbs, 2008-09-30
289     my %field = (
290         table => $table,
291         data_type => 'VARCHAR',
292         size => 10,
293         is_auto_increment => 0,
294         is_nullable => 1,
295         is_foreign_key => 0,
296         is_unique => 0,
297     );
298
299     {
300         my $simple_default = SQL::Translator::Schema::Field->new(
301             %field,
302             name => 'str_default',
303             default_value => 'foo',
304         );
305
306         is(
307             $PRODUCER->($simple_default),
308             q{str_default character varying(10) DEFAULT 'foo'},
309             'default str',
310         );
311     }
312
313     {
314         my $null_default = SQL::Translator::Schema::Field->new(
315             %field,
316             name => 'null_default',
317             default_value => \'NULL',
318         );
319
320         is(
321             $PRODUCER->($null_default),
322             q{null_default character varying(10) DEFAULT NULL},
323             'default null',
324         );
325     }
326
327     {
328         my $null_default = SQL::Translator::Schema::Field->new(
329             %field,
330             name => 'null_default_2',
331             default_value => 'NULL', # XXX: this should go away
332         );
333
334         is(
335             $PRODUCER->($null_default),
336             q{null_default_2 character varying(10) DEFAULT NULL},
337             'default null from special cased string',
338         );
339     }
340
341     {
342         my $func_default = SQL::Translator::Schema::Field->new(
343             %field,
344             name => 'func_default',
345             default_value => \'func(funky)',
346         );
347
348         is(
349             $PRODUCER->($func_default),
350             q{func_default character varying(10) DEFAULT func(funky)},
351             'unquoted default from scalar ref',
352         );
353     }
354 }
355
356
357 my $view1 = SQL::Translator::Schema::View->new(
358     name   => 'view_foo',
359     fields => [qw/id name/],
360     sql    => 'SELECT id, name FROM thing',
361 );
362 my $create_opts = { add_replace_view => 1, no_comments => 1 };
363 my $view1_sql1 = SQL::Translator::Producer::PostgreSQL::create_view($view1, $create_opts);
364
365 my $view_sql_replace = "CREATE VIEW view_foo ( id, name ) AS
366     SELECT id, name FROM thing
367 ";
368 is($view1_sql1, $view_sql_replace, 'correct "CREATE OR REPLACE VIEW" SQL');
369
370 my $view2 = SQL::Translator::Schema::View->new(
371     name   => 'view_foo2',
372     sql    => 'SELECT id, name FROM thing',
373     extra  => {
374       'temporary'    => '1',
375       'check_option' => 'cascaded',
376     },
377 );
378 my $create2_opts = { add_replace_view => 1, no_comments => 1 };
379 my $view2_sql1 = SQL::Translator::Producer::PostgreSQL::create_view($view2, $create2_opts);
380
381 my $view2_sql_replace = "CREATE TEMPORARY VIEW view_foo2 AS
382     SELECT id, name FROM thing
383  WITH CASCADED CHECK OPTION";
384 is($view2_sql1, $view2_sql_replace, 'correct "CREATE OR REPLACE VIEW" SQL 2');
385
386 {
387     my $table = SQL::Translator::Schema::Table->new( name => 'foobar', fields => [qw( foo  bar )] );
388     my $quote = { quote_table_names => '"', quote_field_names => '"' };
389
390     {
391         my $index = $table->add_index(name => 'myindex', fields => ['foo']);
392         my ($def) = SQL::Translator::Producer::PostgreSQL::create_index($index);
393         is($def, "CREATE INDEX myindex on foobar (foo)", 'index created');
394         ($def) = SQL::Translator::Producer::PostgreSQL::create_index($index, $quote);
395         is($def, 'CREATE INDEX "myindex" on "foobar" ("foo")', 'index created w/ quotes');
396     }
397
398     {
399         my $index = $table->add_index(name => 'myindex', fields => ['lower(foo)']);
400         my ($def) = SQL::Translator::Producer::PostgreSQL::create_index($index);
401         is($def, "CREATE INDEX myindex on foobar (lower(foo))", 'index created');
402         ($def) = SQL::Translator::Producer::PostgreSQL::create_index($index, $quote);
403         is($def, 'CREATE INDEX "myindex" on "foobar" (lower(foo))', 'index created w/ quotes');
404     }
405
406     {
407         my $index = $table->add_index(name => 'myindex', fields => ['bar', 'lower(foo)']);
408         my ($def) = SQL::Translator::Producer::PostgreSQL::create_index($index);
409         is($def, "CREATE INDEX myindex on foobar (bar, lower(foo))", 'index created');
410         ($def) = SQL::Translator::Producer::PostgreSQL::create_index($index, $quote);
411         is($def, 'CREATE INDEX "myindex" on "foobar" ("bar", lower(foo))', 'index created w/ quotes');
412     }
413
414     {
415         my $constr = $table->add_constraint(name => 'constr', type => UNIQUE, fields => ['foo']);
416         my ($def) = SQL::Translator::Producer::PostgreSQL::create_constraint($constr);
417         is($def->[0], 'CONSTRAINT constr UNIQUE (foo)', 'constraint created');
418         ($def) = SQL::Translator::Producer::PostgreSQL::create_constraint($constr, $quote);
419         is($def->[0], 'CONSTRAINT "constr" UNIQUE ("foo")', 'constraint created w/ quotes');
420     }
421
422     {
423         my $constr = $table->add_constraint(name => 'constr', type => UNIQUE, fields => ['lower(foo)']);
424         my ($def) = SQL::Translator::Producer::PostgreSQL::create_constraint($constr);
425         is($def->[0], 'CONSTRAINT constr UNIQUE (lower(foo))', 'constraint created');
426         ($def) = SQL::Translator::Producer::PostgreSQL::create_constraint($constr, $quote);
427         is($def->[0], 'CONSTRAINT "constr" UNIQUE (lower(foo))', 'constraint created w/ quotes');
428     }
429
430     {
431         my $constr = $table->add_constraint(name => 'constr', type => UNIQUE, fields => ['bar', 'lower(foo)']);
432         my ($def) = SQL::Translator::Producer::PostgreSQL::create_constraint($constr);
433         is($def->[0], 'CONSTRAINT constr UNIQUE (bar, lower(foo))', 'constraint created');
434         ($def) = SQL::Translator::Producer::PostgreSQL::create_constraint($constr, $quote);
435         is($def->[0], 'CONSTRAINT "constr" UNIQUE ("bar", lower(foo))', 'constraint created w/ quotes');
436     }
437 }