Fix index issue in Parser::DBI::PostgreSQL
[dbsrgits/SQL-Translator.git] / t / 66-postgres-dbi-parser.t
1 #!/usr/bin/perl
2 # vim: set ft=perl:
3
4 use strict;
5 use Test::More;
6 use SQL::Translator;
7 use SQL::Translator::Schema::Constants;
8 use Test::SQL::Translator qw(maybe_plan table_ok);
9
10 BEGIN {
11     maybe_plan(56, 'SQL::Translator::Parser::DBI::PostgreSQL');
12     SQL::Translator::Parser::DBI::PostgreSQL->import('parse');
13 }
14
15 use_ok('SQL::Translator::Parser::DBI::PostgreSQL');
16
17 my @dsn =
18   $ENV{DBICTEST_PG_DSN} ? @ENV{ map { "DBICTEST_PG_$_" } qw/DSN USER PASS/ }
19 : $ENV{DBI_DSN} ? @ENV{ map { "DBI_$_" } qw/DSN USER PASS/ }
20 : ( "dbi:Pg:dbname=postgres", '', '' );
21
22 my $dbh = eval {
23   DBI->connect(@dsn, {AutoCommit => 1, RaiseError=>1,PrintError => 1} );
24 };
25
26 SKIP: {
27     if (my $err = ($@ || $DBI::err )) {
28       chomp $err;
29       skip "No connection to test db. DBI says '$err'", 55;
30     }
31
32     ok($dbh, "dbh setup correctly");
33     $dbh->do('SET client_min_messages=WARNING');
34
35 my $t   = SQL::Translator->new( trace => 0 );
36 my $sql = q[
37     drop table if exists sqlt_test2;
38     drop table if exists sqlt_test1;
39     drop table if exists sqlt_products_1;
40
41     create table sqlt_test1 (
42         f_serial serial NOT NULL primary key,
43         f_varchar character varying(255),
44         f_text text default 'FOO',
45         f_to_drop integer,
46         f_last text
47     );
48
49     create index sqlt_test1_f_last_idx on sqlt_test1 (f_last);
50
51     create table sqlt_test2 (
52         f_id integer NOT NULL,
53         f_int smallint,
54         primary key (f_id),
55         f_fk1 integer NOT NULL references sqlt_test1 (f_serial)
56     );
57
58     CREATE TABLE sqlt_products_1 (
59         product_no integer,
60         name text,
61         price numeric
62     );
63
64     -- drop a column, to not have a linear id 
65     -- When the table t_test1 is created, f_last get id 5 but
66     -- after this drop, there is only 4 columns.
67     alter table sqlt_test1 drop column f_to_drop;
68 ];
69
70 $| = 1;
71
72 $dbh->do($sql);
73
74 my $data   = SQL::Translator::Parser::DBI::PostgreSQL::parse( $t, $dbh );
75 my $schema = $t->schema;
76
77 isa_ok( $schema, 'SQL::Translator::Schema', 'Schema object' );
78 my @tables = $schema->get_tables;
79
80 my $t1 = $schema->get_table("sqlt_test1");
81 is( $t1->name, 'sqlt_test1', 'Table sqlt_test1 exists' );
82
83 my @t1_fields = $t1->get_fields;
84 is( scalar @t1_fields, 4, '4 fields in sqlt_test1' );
85
86 my $f1 = shift @t1_fields;
87 is( $f1->name, 'f_serial', 'First field is "f_serial"' );
88 is( $f1->data_type, 'integer', 'Field is an integer' );
89 is( $f1->is_nullable, 0, 'Field cannot be null' );
90 is( $f1->default_value, "nextval('sqlt_test1_f_serial_seq'::regclass)", 'Default value is nextval()' );
91 is( $f1->is_primary_key, 1, 'Field is PK' );
92 #FIXME: not set to auto-increment? maybe we can guess auto-increment behavior by looking at the default_value (i.e. it call function nextval() )
93 #is( $f1->is_auto_increment, 1, 'Field is auto increment' );
94
95 my $f2 = shift @t1_fields;
96 is( $f2->name, 'f_varchar', 'Second field is "f_varchar"' );
97 is( $f2->data_type, 'character varying(255)', 'Field is a character varying(255)' );
98 is( $f2->is_nullable, 1, 'Field can be null' );
99 #FIXME: should not be 255?
100 is( $f2->size, 259, 'Size is "259"' );
101 is( $f2->default_value, undef, 'Default value is undefined' );
102 is( $f2->is_primary_key, 0, 'Field is not PK' );
103 is( $f2->is_auto_increment, 0, 'Field is not auto increment' );
104
105 my $f3 = shift @t1_fields;
106 is( $f3->name, 'f_text', 'Third field is "f_text"' );
107 is( $f3->data_type, 'text', 'Field is a text' );
108 is( $f3->is_nullable, 1, 'Field can be null' );
109 is( $f3->size, 0, 'Size is 0' );
110 is( $f3->default_value, "'FOO'::text", 'Default value is "FOO"' );
111 is( $f3->is_primary_key, 0, 'Field is not PK' );
112 is( $f3->is_auto_increment, 0, 'Field is not auto increment' );
113
114 my $f4 = shift @t1_fields;
115 is( $f4->name, 'f_last', 'Fouth field is "f_last"' );
116 is( $f4->data_type, 'text', 'Field is a text' );
117 is( $f4->is_nullable, 1, 'Field can be null' );
118 is( $f4->size, 0, 'Size is 0' );
119 is( $f4->default_value, undef, 'No default value' );
120 is( $f4->is_primary_key, 0, 'Field is not PK' );
121 is( $f4->is_auto_increment, 0, 'Field is not auto increment' );
122
123 #TODO: no 'NOT NULL' constraint not set
124
125 my $t2 = $schema->get_table("sqlt_test2");
126 is( $t2->name, 'sqlt_test2', 'Table sqlt_test2 exists' );
127
128 my @t2_fields = $t2->get_fields;
129 is( scalar @t2_fields, 3, '3 fields in sqlt_test2' );
130
131 my $t2_f1 = shift @t2_fields;
132 is( $t2_f1->name, 'f_id', 'First field is "f_id"' );
133 is( $t2_f1->data_type, 'integer', 'Field is an integer' );
134 is( $t2_f1->is_nullable, 0, 'Field cannot be null' );
135 is( $t2_f1->size, 0, 'Size is "0"' );
136 is( $t2_f1->default_value, undef, 'Default value is undefined' );
137 is( $t2_f1->is_primary_key, 1, 'Field is PK' );
138
139 my $t2_f2= shift @t2_fields;
140 is( $t2_f2->name, 'f_int', 'Third field is "f_int"' );
141 is( $t2_f2->data_type, 'smallint', 'Field is an smallint' );
142 is( $t2_f2->is_nullable, 1, 'Field can be null' );
143 is( $t2_f2->size, 0, 'Size is "0"' );
144 is( $t2_f2->default_value, undef, 'Default value is undefined' );
145 is( $t2_f2->is_primary_key, 0, 'Field is not PK' );
146
147 my $t2_f3 = shift @t2_fields;
148 is( $t2_f3->name, 'f_fk1', 'Third field is "f_fk1"' );
149 is( $t2_f3->data_type, 'integer', 'Field is an integer' );
150 is( $t2_f3->is_nullable, 0, 'Field cannot be null' );
151 is( $t2_f3->size, 0, 'Size is "0"' );
152 is( $t2_f3->default_value, undef, 'Default value is undefined' );
153 is( $t2_f3->is_primary_key, 0, 'Field is not PK' );
154 is( $t2_f3->is_foreign_key, 1, 'Field is a FK' );
155 my $fk_ref1 = $t2_f3->foreign_key_reference;
156 isa_ok( $fk_ref1, 'SQL::Translator::Schema::Constraint', 'FK' );
157 is( $fk_ref1->reference_table, 'sqlt_test1', 'FK is to "sqlt_test1" table' );
158
159 my @t2_constraints = $t2->get_constraints;
160 is( scalar @t2_constraints, 1, "One constraint on table" );
161
162 my $t2_c1 = shift @t2_constraints;
163 is( $t2_c1->type, FOREIGN_KEY, "Constraint is a FK" );
164
165 $dbh->disconnect;
166 } # end of SKIP block
167
168 END {
169   if ($dbh) {
170     for (
171       'drop table if exists sqlt_test2',
172       'drop table if exists sqlt_test1',
173       'drop table if exists sqlt_products_1',
174     ) {
175       eval { $dbh->do($_) };
176     }
177   }
178 }