Fixed bug where the "is_auto_increment" flag wasn't being set for serial
[dbsrgits/SQL-Translator.git] / t / 14postgres-parser.t
CommitLineData
f4eb75f3 1#!/usr/bin/perl
2
3use strict;
07b82495 4use Test::More tests => 105;
f4eb75f3 5use SQL::Translator;
07b82495 6use SQL::Translator::Schema::Constants;
f4eb75f3 7use SQL::Translator::Parser::PostgreSQL qw(parse);
8
9my $t = SQL::Translator->new( trace => 0 );
10my $sql = q[
11 create table t_test1 (
07b82495 12 f_serial serial NOT NULL default '0' primary key,
f4eb75f3 13 f_varchar character varying (255),
14 f_double double precision,
07b82495 15 f_bigint bigint not null,
f4eb75f3 16 f_char character(10),
17 f_bool boolean,
07b82495 18 f_bin bytea,
f4eb75f3 19 f_tz timestamp,
20 f_text text,
21 f_fk1 integer not null references t_test2 (f_id),
22 f_fk2 integer
23 );
24
25 create table t_test2 (
26 f_id integer NOT NULL,
27 f_varchar varchar(25),
28 primary key (f_id)
29 );
30
31 alter table only t_test1 add constraint c_u1 unique (f_varchar);
32
07b82495 33 alter table only t_test1 add constraint "c_fk2" foreign key (f_fk2)
f4eb75f3 34 references t_test2 (f_id) on update no action on delete cascade;
35];
36
37$| = 1;
38
07b82495 39my $data = parse( $t, $sql );
40my $schema = $t->schema;
41
42isa_ok( $schema, 'SQL::Translator::Schema', 'Schema object' );
43my @tables = $schema->get_tables;
44is( scalar @tables, 2, 'Two tables' );
45
46my $t1 = shift @tables;
47is( $t1->name, 't_test1', 'Table t_test1 exists' );
48
49my @t1_fields = $t1->get_fields;
50is( scalar @t1_fields, 11, '11 fields in t_test1' );
51
52my $f1 = shift @t1_fields;
53is( $f1->name, 'f_serial', 'First field is "f_serial"' );
54is( $f1->data_type, 'integer', 'Field is an integer' );
55is( $f1->is_nullable, 0, 'Field cannot be null' );
56is( $f1->size, 4, 'Size is "4"' );
57is( $f1->default_value, '0', 'Default value is "0"' );
58is( $f1->is_primary_key, 1, 'Field is PK' );
59
60my $f2 = shift @t1_fields;
61is( $f2->name, 'f_varchar', 'Second field is "f_varchar"' );
62is( $f2->data_type, 'varchar', 'Field is a varchar' );
63is( $f2->is_nullable, 1, 'Field can be null' );
64is( $f2->size, 255, 'Size is "255"' );
65is( $f2->default_value, undef, 'Default value is undefined' );
66is( $f2->is_primary_key, 0, 'Field is not PK' );
67
68my $f3 = shift @t1_fields;
69is( $f3->name, 'f_double', 'Third field is "f_double"' );
70is( $f3->data_type, 'float', 'Field is a float' );
71is( $f3->is_nullable, 1, 'Field can be null' );
72is( $f3->size, 8, 'Size is "8"' );
73is( $f3->default_value, undef, 'Default value is undefined' );
74is( $f3->is_primary_key, 0, 'Field is not PK' );
75
76my $f4 = shift @t1_fields;
77is( $f4->name, 'f_bigint', 'Fourth field is "f_bigint"' );
78is( $f4->data_type, 'integer', 'Field is an integer' );
79is( $f4->is_nullable, 0, 'Field cannot be null' );
80is( $f4->size, 8, 'Size is "8"' );
81is( $f4->default_value, undef, 'Default value is undefined' );
82is( $f4->is_primary_key, 0, 'Field is not PK' );
83
84my $f5 = shift @t1_fields;
85is( $f5->name, 'f_char', 'Fifth field is "f_char"' );
86is( $f5->data_type, 'char', 'Field is char' );
87is( $f5->is_nullable, 1, 'Field can be null' );
88is( $f5->size, 10, 'Size is "10"' );
89is( $f5->default_value, undef, 'Default value is undefined' );
90is( $f5->is_primary_key, 0, 'Field is not PK' );
91
92my $f6 = shift @t1_fields;
93is( $f6->name, 'f_bool', 'Sixth field is "f_bool"' );
94is( $f6->data_type, 'boolean', 'Field is a boolean' );
95is( $f6->is_nullable, 1, 'Field can be null' );
96is( $f6->size, 0, 'Size is "0"' );
97is( $f6->default_value, undef, 'Default value is undefined' );
98is( $f6->is_primary_key, 0, 'Field is not PK' );
99
100my $f7 = shift @t1_fields;
101is( $f7->name, 'f_bin', 'Seventh field is "f_bin"' );
102is( $f7->data_type, 'bytea', 'Field is bytea' );
103is( $f7->is_nullable, 1, 'Field can be null' );
104is( $f7->size, 0, 'Size is "0"' );
105is( $f7->default_value, undef, 'Default value is undefined' );
106is( $f7->is_primary_key, 0, 'Field is not PK' );
107
108my $f8 = shift @t1_fields;
109is( $f8->name, 'f_tz', 'Eighth field is "f_tz"' );
110is( $f8->data_type, 'timestamp', 'Field is a timestamp' );
111is( $f8->is_nullable, 1, 'Field can be null' );
112is( $f8->size, 0, 'Size is "0"' );
113is( $f8->default_value, undef, 'Default value is undefined' );
114is( $f8->is_primary_key, 0, 'Field is not PK' );
115
116my $f9 = shift @t1_fields;
117is( $f9->name, 'f_text', 'Ninth field is "f_text"' );
118is( $f9->data_type, 'text', 'Field is text' );
119is( $f9->is_nullable, 1, 'Field can be null' );
120is( $f9->size, 0, 'Size is "0"' );
121is( $f9->default_value, undef, 'Default value is undefined' );
122is( $f9->is_primary_key, 0, 'Field is not PK' );
123
124my $f10 = shift @t1_fields;
125is( $f10->name, 'f_fk1', 'Tenth field is "f_fk1"' );
126is( $f10->data_type, 'integer', 'Field is an integer' );
127is( $f10->is_nullable, 0, 'Field cannot be null' );
128is( $f10->size, 4, 'Size is "4"' );
129is( $f10->default_value, undef, 'Default value is undefined' );
130is( $f10->is_primary_key, 0, 'Field is not PK' );
131is( $f10->is_foreign_key, 1, 'Field is a FK' );
132my $fk_ref1 = $f10->foreign_key_reference;
133isa_ok( $fk_ref1, 'SQL::Translator::Schema::Constraint', 'FK' );
134is( $fk_ref1->reference_table, 't_test2', 'FK is to "t_test2" table' );
135
136my $f11 = shift @t1_fields;
137is( $f11->name, 'f_fk2', 'Eleventh field is "f_fk2"' );
138is( $f11->data_type, 'integer', 'Field is an integer' );
139is( $f11->is_nullable, 1, 'Field can be null' );
140is( $f11->size, 4, 'Size is "4"' );
141is( $f11->default_value, undef, 'Default value is undefined' );
142is( $f11->is_primary_key, 0, 'Field is not PK' );
143is( $f11->is_foreign_key, 1, 'Field is a FK' );
144my $fk_ref2 = $f11->foreign_key_reference;
145isa_ok( $fk_ref2, 'SQL::Translator::Schema::Constraint', 'FK' );
146is( $fk_ref2->reference_table, 't_test2', 'FK is to "t_test2" table' );
147
148my @t1_constraints = $t1->get_constraints;
8b714ee5 149is( scalar @t1_constraints, 7, '7 constraints on t_test1' );
07b82495 150
151my $c1 = $t1_constraints[0];
152is( $c1->type, PRIMARY_KEY, 'First constraint is PK' );
153is( join(',', $c1->fields), 'f_serial', 'Constraint is on field "f_serial"' );
154
8b714ee5 155my $c2 = $t1_constraints[4];
07b82495 156is( $c2->type, FOREIGN_KEY, 'Second constraint is foreign key' );
157is( join(',', $c2->fields), 'f_fk1', 'Constraint is on field "f_fk1"' );
158is( $c2->reference_table, 't_test2', 'Constraint is to table "t_test2"' );
159is( join(',', $c2->reference_fields), 'f_id', 'Constraint is to field "f_id"' );
160
8b714ee5 161my $c3 = $t1_constraints[5];
07b82495 162is( $c3->type, UNIQUE, 'Third constraint is unique' );
163is( join(',', $c3->fields), 'f_varchar', 'Constraint is on field "f_varchar"' );
164
8b714ee5 165my $c4 = $t1_constraints[6];
07b82495 166is( $c4->type, FOREIGN_KEY, 'Fourth constraint is foreign key' );
167is( join(',', $c4->fields), 'f_fk2', 'Constraint is on field "f_fk2"' );
168is( $c4->reference_table, 't_test2', 'Constraint is to table "t_test2"' );
169is( join(',', $c4->reference_fields), 'f_id', 'Constraint is to field "f_id"' );
170is( $c4->on_delete, 'cascade', 'On delete: cascade' );
171is( $c4->on_update, 'no_action', 'On delete: no action' );
172
173my $t2 = shift @tables;
174is( $t2->name, 't_test2', 'Table t_test2 exists' );
175
176my @t2_fields = $t2->get_fields;
177is( scalar @t2_fields, 2, '2 fields in t_test2' );
178
179my $t2_f1 = shift @t2_fields;
180is( $t2_f1->name, 'f_id', 'First field is "f_id"' );
181is( $t2_f1->data_type, 'integer', 'Field is an integer' );
182is( $t2_f1->is_nullable, 0, 'Field cannot be null' );
183is( $t2_f1->size, 4, 'Size is "4"' );
184is( $t2_f1->default_value, undef, 'Default value is undefined' );
185is( $t2_f1->is_primary_key, 1, 'Field is PK' );
186
187my $t2_f2 = shift @t2_fields;
188is( $t2_f2->name, 'f_varchar', 'First field is "f_varchar"' );
189is( $t2_f2->data_type, 'varchar', 'Field is an varchar' );
190is( $t2_f2->is_nullable, 1, 'Field can be null' );
191is( $t2_f2->size, 25, 'Size is "25"' );
192is( $t2_f2->default_value, undef, 'Default value is undefined' );
193is( $t2_f2->is_primary_key, 0, 'Field is not PK' );