Add parenthesis into the VIEW definition to make sure the pg parser still can deal...
[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 {
90726ffd 17 maybe_plan(17,
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
80my $field4 = SQL::Translator::Schema::Field->new( name => 'bytea_field',
81 table => $table,
82 data_type => 'bytea',
83 size => '16777215',
84 default_value => undef,
85 is_auto_increment => 0,
86 is_nullable => 0,
87 is_foreign_key => 0,
88 is_unique => 0 );
89
90my $field4_sql = SQL::Translator::Producer::PostgreSQL::create_field($field4);
8c4efd11 91
e56dabb7 92is($field4_sql, 'bytea_field bytea NOT NULL', 'Create bytea field works');
5342f5c1 93
94my $field5 = SQL::Translator::Schema::Field->new( name => 'enum_field',
95 table => $table,
96 data_type => 'enum',
97 extra => { list => [ 'Foo', 'Bar' ] },
98 is_auto_increment => 0,
99 is_nullable => 0,
100 is_foreign_key => 0,
101 is_unique => 0 );
102
103my $field5_sql = SQL::Translator::Producer::PostgreSQL::create_field($field5,{ postgres_version => 8.3 });
104
105is($field5_sql, 'enum_field mytable_enum_field_type NOT NULL', 'Create real enum field works');
106
90726ffd 107
108
109
110my $field6 = SQL::Translator::Schema::Field->new(
111 name => 'character',
112 table => $table,
113 data_type => 'character',
114 size => '123',
115 default_value => 'foobar',
116 is_auto_increment => 0,
117 is_nullable => 0,
118 is_foreign_key => 0,
119 is_unique => 0);
120
121my $field7 = SQL::Translator::Schema::Field->new(
122 name => 'character',
123 table => $table,
124 data_type => 'character',
125 size => '123',
126 default_value => undef,
127 is_auto_increment => 0,
128 is_nullable => 0,
129 is_foreign_key => 0,
130 is_unique => 0);
131
132$alter_field = SQL::Translator::Producer::PostgreSQL::alter_field($field6,
133 $field7);
134
135is($alter_field, q(ALTER TABLE mytable ALTER COLUMN character DROP DEFAULT), 'DROP DEFAULT');
136
137$field7->default_value(q(foo'bar'));
138
139$alter_field = SQL::Translator::Producer::PostgreSQL::alter_field($field6,
140 $field7);
141
142is($alter_field, q(ALTER TABLE mytable ALTER COLUMN character SET DEFAULT 'foo''bar'''), 'DEFAULT with escaping');
143
144$field7->default_value(\q(foobar));
145
146$alter_field = SQL::Translator::Producer::PostgreSQL::alter_field($field6,
147 $field7);
148
149is($alter_field, q(ALTER TABLE mytable ALTER COLUMN character SET DEFAULT foobar), 'DEFAULT unescaped if scalarref');
150
151$field7->is_nullable(1);
152$field7->default_value(q(foobar));
153
154$alter_field = SQL::Translator::Producer::PostgreSQL::alter_field($field6,
155 $field7);
156
157is($alter_field, q(ALTER TABLE mytable ALTER COLUMN character DROP NOT NULL), 'DROP NOT NULL');
158
159
bc8e2aa1 160{
161 # let's test default values! -- rjbs, 2008-09-30
162 my %field = (
163 table => $table,
164 data_type => 'VARCHAR',
165 size => 10,
166 is_auto_increment => 0,
167 is_nullable => 1,
168 is_foreign_key => 0,
169 is_unique => 0,
170 );
171
172 {
173 my $simple_default = SQL::Translator::Schema::Field->new(
174 %field,
175 name => 'str_default',
176 default_value => 'foo',
177 );
178
179 is(
180 $PRODUCER->($simple_default),
181 q{str_default character varying(10) DEFAULT 'foo'},
182 'default str',
183 );
184 }
185
186 {
187 my $null_default = SQL::Translator::Schema::Field->new(
188 %field,
189 name => 'null_default',
190 default_value => \'NULL',
191 );
192
193 is(
194 $PRODUCER->($null_default),
195 q{null_default character varying(10) DEFAULT NULL},
196 'default null',
197 );
198 }
199
200 {
201 my $null_default = SQL::Translator::Schema::Field->new(
202 %field,
203 name => 'null_default_2',
204 default_value => 'NULL', # XXX: this should go away
205 );
206
207 is(
208 $PRODUCER->($null_default),
209 q{null_default_2 character varying(10) DEFAULT NULL},
210 'default null from special cased string',
211 );
212 }
213
214 {
215 my $func_default = SQL::Translator::Schema::Field->new(
216 %field,
217 name => 'func_default',
218 default_value => \'func(funky)',
219 );
220
221 is(
222 $PRODUCER->($func_default),
223 q{func_default character varying(10) DEFAULT func(funky)},
224 'unquoted default from scalar ref',
225 );
226 }
227}
228
229
296c2701 230my $view1 = SQL::Translator::Schema::View->new(
231 name => 'view_foo',
232 fields => [qw/id name/],
233 sql => 'SELECT id, name FROM thing',
234);
235my $create_opts = { add_replace_view => 1, no_comments => 1 };
236my $view1_sql1 = SQL::Translator::Producer::PostgreSQL::create_view($view1, $create_opts);
237
a25ac5d2 238my $view_sql_replace = "CREATE VIEW view_foo ( id, name ) AS (
296c2701 239 SELECT id, name FROM thing
3406fd5b 240 )";
296c2701 241is($view1_sql1, $view_sql_replace, 'correct "CREATE OR REPLACE VIEW" SQL');
242
243my $view2 = SQL::Translator::Schema::View->new(
244 name => 'view_foo2',
245 sql => 'SELECT id, name FROM thing',
246 extra => {
247 'temporary' => '1',
248 'check_option' => 'cascaded',
249 },
250);
251my $create2_opts = { add_replace_view => 1, no_comments => 1 };
252my $view2_sql1 = SQL::Translator::Producer::PostgreSQL::create_view($view2, $create2_opts);
253
a25ac5d2 254my $view2_sql_replace = "CREATE TEMPORARY VIEW view_foo2 AS (
296c2701 255 SELECT id, name FROM thing
3406fd5b 256 ) WITH CASCADED CHECK OPTION";
296c2701 257is($view2_sql1, $view2_sql_replace, 'correct "CREATE OR REPLACE VIEW" SQL 2');