Commit | Line | Data |
8c4efd11 |
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 | |
10 | use Data::Dumper; |
11 | use FindBin qw/$Bin/; |
12 | |
13 | # Testing 1,2,3,4... |
14 | #============================================================================= |
15 | |
16 | BEGIN { |
ad258776 |
17 | maybe_plan(22, |
8c4efd11 |
18 | 'SQL::Translator::Producer::PostgreSQL', |
19 | 'Test::Differences', |
20 | ) |
21 | } |
22 | use Test::Differences; |
23 | use SQL::Translator; |
24 | |
bc8e2aa1 |
25 | my $PRODUCER = \&SQL::Translator::Producer::PostgreSQL::create_field; |
8c4efd11 |
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); |
3406fd5b |
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)], |
8c4efd11 |
57 | 'Alter field works'); |
58 | |
59 | $field1->name('field3'); |
60 | my $add_field = SQL::Translator::Producer::PostgreSQL::add_field($field1); |
61 | |
3406fd5b |
62 | is($add_field, 'ALTER TABLE mytable ADD COLUMN field3 character varying(10)', 'Add field works'); |
8c4efd11 |
63 | |
64 | my $drop_field = SQL::Translator::Producer::PostgreSQL::drop_field($field2); |
3406fd5b |
65 | is($drop_field, 'ALTER TABLE mytable DROP COLUMN myfield', 'Drop field works'); |
8c4efd11 |
66 | |
e56dabb7 |
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 $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 | |
90 | my $field4_sql = SQL::Translator::Producer::PostgreSQL::create_field($field4); |
8c4efd11 |
91 | |
e56dabb7 |
92 | is($field4_sql, 'bytea_field bytea NOT NULL', 'Create bytea field works'); |
5342f5c1 |
93 | |
94 | my $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 | |
103 | my $field5_sql = SQL::Translator::Producer::PostgreSQL::create_field($field5,{ postgres_version => 8.3 }); |
104 | |
105 | is($field5_sql, 'enum_field mytable_enum_field_type NOT NULL', 'Create real enum field works'); |
106 | |
90726ffd |
107 | |
108 | |
109 | |
110 | my $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 | |
121 | my $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 | |
135 | is($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 | |
142 | is($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 | |
149 | is($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 | |
157 | is($alter_field, q(ALTER TABLE mytable ALTER COLUMN character DROP NOT NULL), 'DROP NOT NULL'); |
158 | |
ad258776 |
159 | my $field8 = SQL::Translator::Schema::Field->new( name => 'ts_field', |
160 | table => $table, |
161 | data_type => 'timestamp with time zone', |
162 | size => 6, |
163 | is_auto_increment => 0, |
164 | is_nullable => 0, |
165 | is_foreign_key => 0, |
166 | is_unique => 0 ); |
167 | |
168 | my $field8_sql = SQL::Translator::Producer::PostgreSQL::create_field($field8,{ postgres_version => 8.3 }); |
169 | |
170 | is($field8_sql, 'ts_field timestamp(6) with time zone NOT NULL', 'timestamp with precision'); |
171 | |
172 | my $field9 = SQL::Translator::Schema::Field->new( name => 'time_field', |
173 | table => $table, |
174 | data_type => 'time with time zone', |
175 | size => 6, |
176 | is_auto_increment => 0, |
177 | is_nullable => 0, |
178 | is_foreign_key => 0, |
179 | is_unique => 0 ); |
180 | |
181 | my $field9_sql = SQL::Translator::Producer::PostgreSQL::create_field($field9,{ postgres_version => 8.3 }); |
182 | |
183 | is($field9_sql, 'time_field time(6) with time zone NOT NULL', 'time with precision'); |
184 | |
185 | my $field10 = SQL::Translator::Schema::Field->new( name => 'interval_field', |
186 | table => $table, |
187 | data_type => 'interval', |
188 | size => 6, |
189 | is_auto_increment => 0, |
190 | is_nullable => 0, |
191 | is_foreign_key => 0, |
192 | is_unique => 0 ); |
193 | |
194 | my $field10_sql = SQL::Translator::Producer::PostgreSQL::create_field($field10,{ postgres_version => 8.3 }); |
195 | |
196 | is($field10_sql, 'interval_field interval(6) NOT NULL', 'time with precision'); |
197 | |
198 | |
199 | my $field11 = SQL::Translator::Schema::Field->new( name => 'time_field', |
200 | table => $table, |
201 | data_type => 'time without time zone', |
202 | size => 6, |
203 | is_auto_increment => 0, |
204 | is_nullable => 0, |
205 | is_foreign_key => 0, |
206 | is_unique => 0 ); |
207 | |
208 | my $field11_sql = SQL::Translator::Producer::PostgreSQL::create_field($field11,{ postgres_version => 8.3 }); |
209 | |
210 | is($field11_sql, 'time_field time(6) without time zone NOT NULL', 'time with precision'); |
211 | |
212 | |
213 | |
214 | my $field12 = SQL::Translator::Schema::Field->new( name => 'time_field', |
215 | table => $table, |
216 | data_type => 'timestamp', |
217 | is_auto_increment => 0, |
218 | is_nullable => 0, |
219 | is_foreign_key => 0, |
220 | is_unique => 0 ); |
221 | |
222 | my $field12_sql = SQL::Translator::Producer::PostgreSQL::create_field($field12,{ postgres_version => 8.3 }); |
223 | |
224 | is($field12_sql, 'time_field timestamp NOT NULL', 'time with precision'); |
225 | |
90726ffd |
226 | |
bc8e2aa1 |
227 | { |
228 | # let's test default values! -- rjbs, 2008-09-30 |
229 | my %field = ( |
230 | table => $table, |
231 | data_type => 'VARCHAR', |
232 | size => 10, |
233 | is_auto_increment => 0, |
234 | is_nullable => 1, |
235 | is_foreign_key => 0, |
236 | is_unique => 0, |
237 | ); |
238 | |
239 | { |
240 | my $simple_default = SQL::Translator::Schema::Field->new( |
241 | %field, |
242 | name => 'str_default', |
243 | default_value => 'foo', |
244 | ); |
245 | |
246 | is( |
247 | $PRODUCER->($simple_default), |
248 | q{str_default character varying(10) DEFAULT 'foo'}, |
249 | 'default str', |
250 | ); |
251 | } |
252 | |
253 | { |
254 | my $null_default = SQL::Translator::Schema::Field->new( |
255 | %field, |
256 | name => 'null_default', |
257 | default_value => \'NULL', |
258 | ); |
259 | |
260 | is( |
261 | $PRODUCER->($null_default), |
262 | q{null_default character varying(10) DEFAULT NULL}, |
263 | 'default null', |
264 | ); |
265 | } |
266 | |
267 | { |
268 | my $null_default = SQL::Translator::Schema::Field->new( |
269 | %field, |
270 | name => 'null_default_2', |
271 | default_value => 'NULL', # XXX: this should go away |
272 | ); |
273 | |
274 | is( |
275 | $PRODUCER->($null_default), |
276 | q{null_default_2 character varying(10) DEFAULT NULL}, |
277 | 'default null from special cased string', |
278 | ); |
279 | } |
280 | |
281 | { |
282 | my $func_default = SQL::Translator::Schema::Field->new( |
283 | %field, |
284 | name => 'func_default', |
285 | default_value => \'func(funky)', |
286 | ); |
287 | |
288 | is( |
289 | $PRODUCER->($func_default), |
290 | q{func_default character varying(10) DEFAULT func(funky)}, |
291 | 'unquoted default from scalar ref', |
292 | ); |
293 | } |
294 | } |
295 | |
296 | |
296c2701 |
297 | my $view1 = SQL::Translator::Schema::View->new( |
298 | name => 'view_foo', |
299 | fields => [qw/id name/], |
300 | sql => 'SELECT id, name FROM thing', |
301 | ); |
302 | my $create_opts = { add_replace_view => 1, no_comments => 1 }; |
303 | my $view1_sql1 = SQL::Translator::Producer::PostgreSQL::create_view($view1, $create_opts); |
304 | |
f59b2c0e |
305 | my $view_sql_replace = "CREATE VIEW view_foo ( id, name ) AS |
296c2701 |
306 | SELECT id, name FROM thing |
f59b2c0e |
307 | "; |
296c2701 |
308 | is($view1_sql1, $view_sql_replace, 'correct "CREATE OR REPLACE VIEW" SQL'); |
309 | |
310 | my $view2 = SQL::Translator::Schema::View->new( |
311 | name => 'view_foo2', |
312 | sql => 'SELECT id, name FROM thing', |
313 | extra => { |
314 | 'temporary' => '1', |
315 | 'check_option' => 'cascaded', |
316 | }, |
317 | ); |
318 | my $create2_opts = { add_replace_view => 1, no_comments => 1 }; |
319 | my $view2_sql1 = SQL::Translator::Producer::PostgreSQL::create_view($view2, $create2_opts); |
320 | |
f59b2c0e |
321 | my $view2_sql_replace = "CREATE TEMPORARY VIEW view_foo2 AS |
296c2701 |
322 | SELECT id, name FROM thing |
f59b2c0e |
323 | WITH CASCADED CHECK OPTION"; |
296c2701 |
324 | is($view2_sql1, $view2_sql_replace, 'correct "CREATE OR REPLACE VIEW" SQL 2'); |