8 use Test::SQL::Translator qw(maybe_plan);
14 #=============================================================================
18 'SQL::Translator::Producer::PostgreSQL',
22 use Test::Differences;
25 my $PRODUCER = \&SQL::Translator::Producer::PostgreSQL::create_field;
27 my $table = SQL::Translator::Schema::Table->new( name => 'mytable');
29 my $field1 = SQL::Translator::Schema::Field->new( name => 'myfield',
31 data_type => 'VARCHAR',
33 default_value => undef,
34 is_auto_increment => 0,
39 my $field1_sql = SQL::Translator::Producer::PostgreSQL::create_field($field1);
41 is($field1_sql, 'myfield character varying(10)', 'Create field works');
43 my $field2 = SQL::Translator::Schema::Field->new( name => 'myfield',
45 data_type => 'VARCHAR',
47 default_value => undef,
48 is_auto_increment => 0,
53 my $alter_field = SQL::Translator::Producer::PostgreSQL::alter_field($field1,
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)],
59 $field1->name('field3');
60 my $add_field = SQL::Translator::Producer::PostgreSQL::add_field($field1);
62 is($add_field, 'ALTER TABLE mytable ADD COLUMN field3 character varying(10)', 'Add field works');
64 my $drop_field = SQL::Translator::Producer::PostgreSQL::drop_field($field2);
65 is($drop_field, 'ALTER TABLE mytable DROP COLUMN myfield', 'Drop field works');
67 my $field3 = SQL::Translator::Schema::Field->new( name => 'time_field',
70 default_value => undef,
71 is_auto_increment => 0,
76 my $field3_sql = SQL::Translator::Producer::PostgreSQL::create_field($field3);
78 is($field3_sql, 'time_field time NOT NULL', 'Create time field works');
80 my $field4 = SQL::Translator::Schema::Field->new( name => 'bytea_field',
84 default_value => undef,
85 is_auto_increment => 0,
90 my $field4_sql = SQL::Translator::Producer::PostgreSQL::create_field($field4);
92 is($field4_sql, 'bytea_field bytea NOT NULL', 'Create bytea field works');
94 my $field5 = SQL::Translator::Schema::Field->new( name => 'enum_field',
97 extra => { list => [ 'Foo', 'Bar' ] },
98 is_auto_increment => 0,
103 my $field5_sql = SQL::Translator::Producer::PostgreSQL::create_field($field5,{ postgres_version => 8.3 });
105 is($field5_sql, 'enum_field mytable_enum_field_type NOT NULL', 'Create real enum field works');
110 my $field6 = SQL::Translator::Schema::Field->new(
113 data_type => 'character',
115 default_value => 'foobar',
116 is_auto_increment => 0,
121 my $field7 = SQL::Translator::Schema::Field->new(
124 data_type => 'character',
126 default_value => undef,
127 is_auto_increment => 0,
132 $alter_field = SQL::Translator::Producer::PostgreSQL::alter_field($field6,
135 is($alter_field, q(ALTER TABLE mytable ALTER COLUMN character DROP DEFAULT), 'DROP DEFAULT');
137 $field7->default_value(q(foo'bar'));
139 $alter_field = SQL::Translator::Producer::PostgreSQL::alter_field($field6,
142 is($alter_field, q(ALTER TABLE mytable ALTER COLUMN character SET DEFAULT 'foo''bar'''), 'DEFAULT with escaping');
144 $field7->default_value(\q(foobar));
146 $alter_field = SQL::Translator::Producer::PostgreSQL::alter_field($field6,
149 is($alter_field, q(ALTER TABLE mytable ALTER COLUMN character SET DEFAULT foobar), 'DEFAULT unescaped if scalarref');
151 $field7->is_nullable(1);
152 $field7->default_value(q(foobar));
154 $alter_field = SQL::Translator::Producer::PostgreSQL::alter_field($field6,
157 is($alter_field, q(ALTER TABLE mytable ALTER COLUMN character DROP NOT NULL), 'DROP NOT NULL');
161 # let's test default values! -- rjbs, 2008-09-30
164 data_type => 'VARCHAR',
166 is_auto_increment => 0,
173 my $simple_default = SQL::Translator::Schema::Field->new(
175 name => 'str_default',
176 default_value => 'foo',
180 $PRODUCER->($simple_default),
181 q{str_default character varying(10) DEFAULT 'foo'},
187 my $null_default = SQL::Translator::Schema::Field->new(
189 name => 'null_default',
190 default_value => \'NULL',
194 $PRODUCER->($null_default),
195 q{null_default character varying(10) DEFAULT NULL},
201 my $null_default = SQL::Translator::Schema::Field->new(
203 name => 'null_default_2',
204 default_value => 'NULL', # XXX: this should go away
208 $PRODUCER->($null_default),
209 q{null_default_2 character varying(10) DEFAULT NULL},
210 'default null from special cased string',
215 my $func_default = SQL::Translator::Schema::Field->new(
217 name => 'func_default',
218 default_value => \'func(funky)',
222 $PRODUCER->($func_default),
223 q{func_default character varying(10) DEFAULT func(funky)},
224 'unquoted default from scalar ref',
230 my $view1 = SQL::Translator::Schema::View->new(
232 fields => [qw/id name/],
233 sql => 'SELECT id, name FROM thing',
235 my $create_opts = { add_replace_view => 1, no_comments => 1 };
236 my $view1_sql1 = SQL::Translator::Producer::PostgreSQL::create_view($view1, $create_opts);
238 my $view_sql_replace = "CREATE VIEW view_foo ( id, name ) AS (
239 SELECT id, name FROM thing
241 is($view1_sql1, $view_sql_replace, 'correct "CREATE OR REPLACE VIEW" SQL');
243 my $view2 = SQL::Translator::Schema::View->new(
245 sql => 'SELECT id, name FROM thing',
248 'check_option' => 'cascaded',
251 my $create2_opts = { add_replace_view => 1, no_comments => 1 };
252 my $view2_sql1 = SQL::Translator::Producer::PostgreSQL::create_view($view2, $create2_opts);
254 my $view2_sql_replace = "CREATE TEMPORARY VIEW view_foo2 AS (
255 SELECT id, name FROM thing
256 ) WITH CASCADED CHECK OPTION";
257 is($view2_sql1, $view2_sql_replace, 'correct "CREATE OR REPLACE VIEW" SQL 2');