Quote table names in 'SHOW CREATE TABLE' in Parser::DBI::MySQL
[dbsrgits/SQL-Translator.git] / t / 14postgres-parser.t
CommitLineData
f4eb75f3 1#!/usr/bin/perl
fbc0552f 2# vim: set ft=perl:
f4eb75f3 3
4use strict;
fbc0552f 5use Test::More;
f4eb75f3 6use SQL::Translator;
07b82495 7use SQL::Translator::Schema::Constants;
2d691ec1 8use Test::SQL::Translator qw(maybe_plan);
fbc0552f 9
2d691ec1 10BEGIN {
9f03b4c2 11 maybe_plan(154, 'SQL::Translator::Parser::PostgreSQL');
fbc0552f 12 SQL::Translator::Parser::PostgreSQL->import('parse');
fbc0552f 13}
f4eb75f3 14
15my $t = SQL::Translator->new( trace => 0 );
c96cd4a8 16my $sql = q{
fcc393cd 17 -- comment on t_test1
f4eb75f3 18 create table t_test1 (
fcc393cd 19 -- this is the primary key
07b82495 20 f_serial serial NOT NULL default '0' primary key,
f4eb75f3 21 f_varchar character varying (255),
22 f_double double precision,
07b82495 23 f_bigint bigint not null,
88ad8255 24 f_char character(10) default 'FOO'::character(10),
f4eb75f3 25 f_bool boolean,
07b82495 26 f_bin bytea,
88ad8255 27 f_tz timestamp default '1970-01-01 00:00:00'::TIMESTAMP,
f4eb75f3 28 f_text text,
29 f_fk1 integer not null references t_test2 (f_id),
08d91aad 30 f_dropped text,
31 f_timestamp timestamp(0) with time zone,
9f03b4c2 32 f_timestamp2 timestamp without time zone,
33 f_json json,
34 f_hstore hstore
f4eb75f3 35 );
36
37 create table t_test2 (
38 f_id integer NOT NULL,
39 f_varchar varchar(25),
668c5039 40 f_int smallint,
a68deb7d 41 f_smallint smallint default (0)::smallint,
668c5039 42 primary key (f_id),
43 check (f_int between 1 and 5)
f4eb75f3 44 );
45
3e98f7d9 46 CREATE TABLE products_1 (
47 product_no integer,
48 name text,
49 price numeric
50 );
c96cd4a8 51
3e98f7d9 52 CREATE TEMP TABLE products_2 (
53 product_no integer,
54 name text,
55 price numeric
56 );
57
58 CREATE TEMPORARY TABLE products_3 (
59 product_no integer,
60 name text,
61 price numeric
62 );
63
c96cd4a8 64 CREATE TRIGGER test_trigger
65 BEFORE INSERT OR UPDATE OR DELETE
66 ON products_1
67 FOR EACH ROW
68 EXECUTE PROCEDURE foo();
69
b3abc007 70 alter table t_test1 add f_fk2 integer;
71
f4eb75f3 72 alter table only t_test1 add constraint c_u1 unique (f_varchar);
73
b3abc007 74 alter table t_test1 add constraint "c_fk2" foreign key (f_fk2)
840447a5 75 references t_test2 (f_id) match simple
76 on update no action on delete cascade deferrable;
77
b3abc007 78
79 alter table t_test1 drop column f_dropped restrict;
80
81 alter table t_test1 alter column f_fk2 set default 'FOO';
82
83 alter table t_test1 alter column f_char drop default;
84
aee4b66e 85 -- The following are allowed by the grammar
08d91aad 86 -- but won\'t do anything... - ky
b3abc007 87
88 alter table t_text1 alter column f_char set not null;
89
90 alter table t_text1 alter column f_char drop not null;
91
92 alter table t_test1 alter f_char set statistics 10;
93
94 alter table t_test1 alter f_text set storage extended;
c96cd4a8 95
b3abc007 96 alter table t_test1 rename column f_text to foo;
97
98 alter table t_test1 rename to foo;
99
100 alter table only t_test1 drop constraint foo cascade;
101
102 alter table t_test1 owner to foo;
1f5b2625 103
104 commit;
c96cd4a8 105};
f4eb75f3 106
107$| = 1;
108
07b82495 109my $data = parse( $t, $sql );
110my $schema = $t->schema;
111
112isa_ok( $schema, 'SQL::Translator::Schema', 'Schema object' );
113my @tables = $schema->get_tables;
3e98f7d9 114is( scalar @tables, 5, 'Five tables' );
07b82495 115
116my $t1 = shift @tables;
117is( $t1->name, 't_test1', 'Table t_test1 exists' );
118
fcc393cd 119is( $t1->comments, 'comment on t_test1', 'Table comment exists' );
120
07b82495 121my @t1_fields = $t1->get_fields;
9f03b4c2 122is( scalar @t1_fields, 15, '15 fields in t_test1' );
07b82495 123
124my $f1 = shift @t1_fields;
125is( $f1->name, 'f_serial', 'First field is "f_serial"' );
126is( $f1->data_type, 'integer', 'Field is an integer' );
127is( $f1->is_nullable, 0, 'Field cannot be null' );
9bcfe960 128is( $f1->size, 11, 'Size is "11"' );
07b82495 129is( $f1->default_value, '0', 'Default value is "0"' );
130is( $f1->is_primary_key, 1, 'Field is PK' );
fcc393cd 131is( $f1->comments, 'this is the primary key', 'Comment' );
6d278664 132is( $f1->is_auto_increment, 1, 'Field is auto increment' );
07b82495 133
134my $f2 = shift @t1_fields;
135is( $f2->name, 'f_varchar', 'Second field is "f_varchar"' );
136is( $f2->data_type, 'varchar', 'Field is a varchar' );
137is( $f2->is_nullable, 1, 'Field can be null' );
138is( $f2->size, 255, 'Size is "255"' );
139is( $f2->default_value, undef, 'Default value is undefined' );
140is( $f2->is_primary_key, 0, 'Field is not PK' );
6d278664 141is( $f2->is_auto_increment, 0, 'Field is not auto increment' );
07b82495 142
143my $f3 = shift @t1_fields;
144is( $f3->name, 'f_double', 'Third field is "f_double"' );
145is( $f3->data_type, 'float', 'Field is a float' );
146is( $f3->is_nullable, 1, 'Field can be null' );
9bcfe960 147is( $f3->size, 20, 'Size is "20"' );
07b82495 148is( $f3->default_value, undef, 'Default value is undefined' );
149is( $f3->is_primary_key, 0, 'Field is not PK' );
150
151my $f4 = shift @t1_fields;
152is( $f4->name, 'f_bigint', 'Fourth field is "f_bigint"' );
153is( $f4->data_type, 'integer', 'Field is an integer' );
154is( $f4->is_nullable, 0, 'Field cannot be null' );
9bcfe960 155is( $f4->size, 20, 'Size is "20"' );
07b82495 156is( $f4->default_value, undef, 'Default value is undefined' );
157is( $f4->is_primary_key, 0, 'Field is not PK' );
158
159my $f5 = shift @t1_fields;
160is( $f5->name, 'f_char', 'Fifth field is "f_char"' );
161is( $f5->data_type, 'char', 'Field is char' );
162is( $f5->is_nullable, 1, 'Field can be null' );
163is( $f5->size, 10, 'Size is "10"' );
164is( $f5->default_value, undef, 'Default value is undefined' );
165is( $f5->is_primary_key, 0, 'Field is not PK' );
166
167my $f6 = shift @t1_fields;
168is( $f6->name, 'f_bool', 'Sixth field is "f_bool"' );
169is( $f6->data_type, 'boolean', 'Field is a boolean' );
170is( $f6->is_nullable, 1, 'Field can be null' );
171is( $f6->size, 0, 'Size is "0"' );
172is( $f6->default_value, undef, 'Default value is undefined' );
173is( $f6->is_primary_key, 0, 'Field is not PK' );
174
175my $f7 = shift @t1_fields;
176is( $f7->name, 'f_bin', 'Seventh field is "f_bin"' );
177is( $f7->data_type, 'bytea', 'Field is bytea' );
178is( $f7->is_nullable, 1, 'Field can be null' );
179is( $f7->size, 0, 'Size is "0"' );
180is( $f7->default_value, undef, 'Default value is undefined' );
181is( $f7->is_primary_key, 0, 'Field is not PK' );
182
183my $f8 = shift @t1_fields;
184is( $f8->name, 'f_tz', 'Eighth field is "f_tz"' );
185is( $f8->data_type, 'timestamp', 'Field is a timestamp' );
186is( $f8->is_nullable, 1, 'Field can be null' );
187is( $f8->size, 0, 'Size is "0"' );
88ad8255 188is( $f8->default_value, '1970-01-01 00:00:00', 'Default value is 1970-01-01 00:00:00' );
07b82495 189is( $f8->is_primary_key, 0, 'Field is not PK' );
190
191my $f9 = shift @t1_fields;
192is( $f9->name, 'f_text', 'Ninth field is "f_text"' );
193is( $f9->data_type, 'text', 'Field is text' );
194is( $f9->is_nullable, 1, 'Field can be null' );
60e3ded0 195is( $f9->size, 64000, 'Size is "64,000"' );
07b82495 196is( $f9->default_value, undef, 'Default value is undefined' );
197is( $f9->is_primary_key, 0, 'Field is not PK' );
198
199my $f10 = shift @t1_fields;
200is( $f10->name, 'f_fk1', 'Tenth field is "f_fk1"' );
201is( $f10->data_type, 'integer', 'Field is an integer' );
202is( $f10->is_nullable, 0, 'Field cannot be null' );
9bcfe960 203is( $f10->size, 10, 'Size is "10"' );
07b82495 204is( $f10->default_value, undef, 'Default value is undefined' );
205is( $f10->is_primary_key, 0, 'Field is not PK' );
206is( $f10->is_foreign_key, 1, 'Field is a FK' );
207my $fk_ref1 = $f10->foreign_key_reference;
208isa_ok( $fk_ref1, 'SQL::Translator::Schema::Constraint', 'FK' );
209is( $fk_ref1->reference_table, 't_test2', 'FK is to "t_test2" table' );
210
211my $f11 = shift @t1_fields;
08d91aad 212is( $f11->name, 'f_timestamp', 'Eleventh field is "f_timestamp"' );
b5a782a0 213is( $f11->data_type, 'timestamp with time zone', 'Field is a timestamp with time zone' );
07b82495 214is( $f11->is_nullable, 1, 'Field can be null' );
08d91aad 215is( $f11->size, 0, 'Size is "0"' );
216is( $f11->default_value, undef, 'Default value is "undef"' );
07b82495 217is( $f11->is_primary_key, 0, 'Field is not PK' );
08d91aad 218is( $f11->is_foreign_key, 0, 'Field is not FK' );
b5a782a0 219
220my $f12 = shift @t1_fields;
221is( $f12->name, 'f_timestamp2', '12th field is "f_timestamp2"' );
222is( $f12->data_type, 'timestamp without time zone', 'Field is a timestamp without time zone' );
223is( $f12->is_nullable, 1, 'Field can be null' );
224is( $f12->size, 0, 'Size is "0"' );
225is( $f12->default_value, undef, 'Default value is "undef"' );
226is( $f12->is_primary_key, 0, 'Field is not PK' );
227is( $f12->is_foreign_key, 0, 'Field is not FK' );
228
9f03b4c2 229my $f13 = shift @t1_fields;
230is( $f13->name, 'f_json', '13th field is "f_json"' );
231is( $f13->data_type, 'json', 'Field is Json' );
232is( $f13->is_nullable, 1, 'Field can be null' );
233is( $f13->size, 0, 'Size is "0"' );
234is( $f13->default_value, undef, 'Default value is "undef"' );
235is( $f13->is_primary_key, 0, 'Field is not PK' );
236is( $f13->is_foreign_key, 0, 'Field is not FK' );
237
238my $f14 = shift @t1_fields;
239is( $f14->name, 'f_hstore', '14th field is "f_hstore"' );
240is( $f14->data_type, 'hstore', 'Field is hstore' );
241is( $f14->is_nullable, 1, 'Field can be null' );
242is( $f14->size, 0, 'Size is "0"' );
243is( $f14->default_value, undef, 'Default value is "undef"' );
244is( $f14->is_primary_key, 0, 'Field is not PK' );
245is( $f14->is_foreign_key, 0, 'Field is not FK' );
246
08d91aad 247# my $fk_ref2 = $f11->foreign_key_reference;
248# isa_ok( $fk_ref2, 'SQL::Translator::Schema::Constraint', 'FK' );
249# is( $fk_ref2->reference_table, 't_test2', 'FK is to "t_test2" table' );
07b82495 250
251my @t1_constraints = $t1->get_constraints;
128e4af7 252is( scalar @t1_constraints, 8, '8 constraints on t_test1' );
07b82495 253
254my $c1 = $t1_constraints[0];
255is( $c1->type, PRIMARY_KEY, 'First constraint is PK' );
256is( join(',', $c1->fields), 'f_serial', 'Constraint is on field "f_serial"' );
257
8b714ee5 258my $c2 = $t1_constraints[4];
07b82495 259is( $c2->type, FOREIGN_KEY, 'Second constraint is foreign key' );
260is( join(',', $c2->fields), 'f_fk1', 'Constraint is on field "f_fk1"' );
261is( $c2->reference_table, 't_test2', 'Constraint is to table "t_test2"' );
262is( join(',', $c2->reference_fields), 'f_id', 'Constraint is to field "f_id"' );
263
8b714ee5 264my $c3 = $t1_constraints[5];
07b82495 265is( $c3->type, UNIQUE, 'Third constraint is unique' );
266is( join(',', $c3->fields), 'f_varchar', 'Constraint is on field "f_varchar"' );
267
8b714ee5 268my $c4 = $t1_constraints[6];
07b82495 269is( $c4->type, FOREIGN_KEY, 'Fourth constraint is foreign key' );
270is( join(',', $c4->fields), 'f_fk2', 'Constraint is on field "f_fk2"' );
271is( $c4->reference_table, 't_test2', 'Constraint is to table "t_test2"' );
272is( join(',', $c4->reference_fields), 'f_id', 'Constraint is to field "f_id"' );
273is( $c4->on_delete, 'cascade', 'On delete: cascade' );
274is( $c4->on_update, 'no_action', 'On delete: no action' );
840447a5 275is( $c4->match_type, 'simple', 'Match type: simple' );
276is( $c4->deferrable, 1, 'Deferrable detected' );
07b82495 277
278my $t2 = shift @tables;
279is( $t2->name, 't_test2', 'Table t_test2 exists' );
280
281my @t2_fields = $t2->get_fields;
a68deb7d 282is( scalar @t2_fields, 4, '4 fields in t_test2' );
07b82495 283
284my $t2_f1 = shift @t2_fields;
285is( $t2_f1->name, 'f_id', 'First field is "f_id"' );
286is( $t2_f1->data_type, 'integer', 'Field is an integer' );
287is( $t2_f1->is_nullable, 0, 'Field cannot be null' );
9bcfe960 288is( $t2_f1->size, 10, 'Size is "10"' );
07b82495 289is( $t2_f1->default_value, undef, 'Default value is undefined' );
290is( $t2_f1->is_primary_key, 1, 'Field is PK' );
291
292my $t2_f2 = shift @t2_fields;
668c5039 293is( $t2_f2->name, 'f_varchar', 'Second field is "f_varchar"' );
07b82495 294is( $t2_f2->data_type, 'varchar', 'Field is an varchar' );
295is( $t2_f2->is_nullable, 1, 'Field can be null' );
296is( $t2_f2->size, 25, 'Size is "25"' );
297is( $t2_f2->default_value, undef, 'Default value is undefined' );
298is( $t2_f2->is_primary_key, 0, 'Field is not PK' );
668c5039 299
300my $t2_f3 = shift @t2_fields;
301is( $t2_f3->name, 'f_int', 'Third field is "f_int"' );
302is( $t2_f3->data_type, 'integer', 'Field is an integer' );
303is( $t2_f3->is_nullable, 1, 'Field can be null' );
304is( $t2_f3->size, 5, 'Size is "5"' );
305is( $t2_f3->default_value, undef, 'Default value is undefined' );
306is( $t2_f3->is_primary_key, 0, 'Field is not PK' );
307
a68deb7d 308my $t2_f4 = shift @t2_fields;
309is( $t2_f4->name, 'f_smallint', 'Fourth field is "f_smallint"' );
310is( $t2_f4->data_type, 'integer', 'Field is an integer' );
311is( $t2_f4->is_nullable, 1, 'Field can be null' );
312is( $t2_f4->size, 5, 'Size is "5"' );
313is( $t2_f4->default_value, 0, 'Default value is 0' );
314is( $t2_f4->is_primary_key, 0, 'Field is not PK' );
315
316
668c5039 317my @t2_constraints = $t2->get_constraints;
318is( scalar @t2_constraints, 3, "Three constraints on table" );
319
320my $t2_c1 = shift @t2_constraints;
321is( $t2_c1->type, NOT_NULL, "Constraint is NOT NULL" );
322
323my $t2_c2 = shift @t2_constraints;
324is( $t2_c2->type, PRIMARY_KEY, "Constraint is a PK" );
325
326my $t2_c3 = shift @t2_constraints;
327is( $t2_c3->type, CHECK_C, "Constraint is a 'CHECK'" );
3e98f7d9 328
329# test temporary tables
330is( exists $schema->get_table('products_1')->extra()->{'temporary'}, "", "Table is NOT temporary");
331is( $schema->get_table('products_2')->extra('temporary'), 1,"Table is TEMP");
332is( $schema->get_table('products_3')->extra('temporary'), 1,"Table is TEMPORARY");
c96cd4a8 333
334# test trigger
335my $trigger = $schema->get_trigger('test_trigger');
336is( $trigger->on_table, 'products_1', "Trigger is on correct table");
337is_deeply( scalar $trigger->database_events, [qw(insert update delete)], "Correct events for trigger");
338
339is( $trigger->perform_action_when, 'before', "Correct time for trigger");
340is( $trigger->scope, 'row', "Correct scope for trigger");
341is( $trigger->action, 'EXECUTE PROCEDURE foo()', "Correct action for trigger");