0fc3f3d1b0448c0a97d46d67849ad0f39dca4d7b
[dbsrgits/DBIx-Class.git] / t / 72pg.t
1 use strict;
2 use warnings;  
3
4 use Test::More;
5 use Test::Exception;
6 use lib qw(t/lib);
7 use DBICTest;
8
9 {
10   package DBICTest::Schema::Casecheck;
11
12   use strict;
13   use warnings;
14   use base 'DBIx::Class';
15
16   __PACKAGE__->load_components(qw/Core/);
17   __PACKAGE__->table('testschema.casecheck');
18   __PACKAGE__->add_columns(qw/id name NAME uc_name storecolumn/);
19   __PACKAGE__->column_info_from_storage(1);
20   __PACKAGE__->set_primary_key('id');
21
22   sub store_column {
23     my ($self, $name, $value) = @_;
24     $value = '#'.$value if($name eq "storecolumn");
25     $self->maybe::next::method($name, $value);
26   }
27 }
28
29 {
30   package DBICTest::Schema::ArrayTest;
31
32   use strict;
33   use warnings;
34   use base 'DBIx::Class';
35
36   __PACKAGE__->load_components(qw/Core/);
37   __PACKAGE__->table('testschema.array_test');
38   __PACKAGE__->add_columns(qw/id arrayfield/);
39   __PACKAGE__->column_info_from_storage(1);
40   __PACKAGE__->set_primary_key('id');
41
42 }
43
44 my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_PG_${_}" } qw/DSN USER PASS/};
45
46 plan skip_all => 'Set $ENV{DBICTEST_PG_DSN}, _USER and _PASS to run this test '.
47   '(note: This test drops and creates tables called \'artist\', \'casecheck\', \'array_test\' and \'sequence_test\''.
48   ' as well as following sequences: \'pkid1_seq\', \'pkid2_seq\' and \'nonpkid_seq\''.
49   ' as well as following schemas: \'testschema\',\'anothertestschema\'!)'
50     unless ($dsn && $user);
51
52
53 plan tests => 42;
54
55 DBICTest::Schema->load_classes( 'Casecheck', 'ArrayTest' );
56 my $schema = DBICTest::Schema->connect($dsn, $user, $pass,);
57
58 # Check that datetime_parser returns correctly before we explicitly connect.
59 SKIP: {
60     eval { require DateTime::Format::Pg };
61     skip "DateTime::Format::Pg required", 2 if $@;
62
63     my $store = ref $schema->storage;
64     is($store, 'DBIx::Class::Storage::DBI', 'Started with generic storage');
65
66     my $parser = $schema->storage->datetime_parser;
67     is( $parser, 'DateTime::Format::Pg', 'datetime_parser is as expected');
68 }
69
70 my $dbh = $schema->storage->dbh;
71 $schema->source("Artist")->name("testschema.artist");
72 $schema->source("SequenceTest")->name("testschema.sequence_test");
73 {
74     local $SIG{__WARN__} = sub {};
75     _cleanup ($dbh);
76
77     my $artist_table_def = <<EOS;
78 (
79   artistid serial PRIMARY KEY
80   , name VARCHAR(100)
81   , rank INTEGER NOT NULL DEFAULT '13'
82   , charfield CHAR(10)
83   , arrayfield INTEGER[]
84 )
85 EOS
86     $dbh->do("CREATE SCHEMA testschema;");
87     $dbh->do("CREATE TABLE testschema.artist $artist_table_def;");
88     $dbh->do("CREATE TABLE testschema.sequence_test (pkid1 integer, pkid2 integer, nonpkid integer, name VARCHAR(100), CONSTRAINT pk PRIMARY KEY(pkid1, pkid2));");
89     $dbh->do("CREATE SEQUENCE pkid1_seq START 1 MAXVALUE 999999 MINVALUE 0");
90     $dbh->do("CREATE SEQUENCE pkid2_seq START 10 MAXVALUE 999999 MINVALUE 0");
91     $dbh->do("CREATE SEQUENCE nonpkid_seq START 20 MAXVALUE 999999 MINVALUE 0");
92     ok ( $dbh->do('CREATE TABLE testschema.casecheck (id serial PRIMARY KEY, "name" VARCHAR(1), "NAME" VARCHAR(2), "UC_NAME" VARCHAR(3), "storecolumn" VARCHAR(10));'), 'Creation of casecheck table');
93     ok ( $dbh->do('CREATE TABLE testschema.array_test (id serial PRIMARY KEY, arrayfield INTEGER[]);'), 'Creation of array_test table');
94     $dbh->do("CREATE SCHEMA anothertestschema;");
95     $dbh->do("CREATE TABLE anothertestschema.artist $artist_table_def;");
96     $dbh->do("CREATE SCHEMA yetanothertestschema;");
97     $dbh->do("CREATE TABLE yetanothertestschema.artist $artist_table_def;");
98     $dbh->do('set search_path=testschema,public');
99 }
100
101 # store_column is called once for create() for non sequence columns
102
103 ok(my $storecolumn = $schema->resultset('Casecheck')->create({'storecolumn' => 'a'}));
104
105 is($storecolumn->storecolumn, '#a'); # was '##a'
106
107
108 # This is in Core now, but it's here just to test that it doesn't break
109 $schema->class('Artist')->load_components('PK::Auto');
110
111 cmp_ok( $schema->resultset('Artist')->count, '==', 0, 'this should start with an empty artist table');
112
113 { # test that auto-pk also works with the defined search path by
114   # un-schema-qualifying the table name
115   my $artist_name_save = $schema->source("Artist")->name;
116   $schema->source("Artist")->name("artist");
117
118   my $unq_new;
119   lives_ok {
120       $unq_new = $schema->resultset('Artist')->create({ name => 'baz' });
121   } 'insert into unqualified, shadowed table succeeds';
122
123   is($unq_new && $unq_new->artistid, 1, "and got correct artistid");
124
125   $schema->source("Artist")->name($artist_name_save);
126 }
127
128 my $new = $schema->resultset('Artist')->create({ name => 'foo' });
129
130 is($new->artistid, 2, "Auto-PK worked");
131
132 $new = $schema->resultset('Artist')->create({ name => 'bar' });
133
134 is($new->artistid, 3, "Auto-PK worked");
135
136
137 my $test_type_info = {
138     'artistid' => {
139         'data_type' => 'integer',
140         'is_nullable' => 0,
141         'size' => 4,
142     },
143     'name' => {
144         'data_type' => 'character varying',
145         'is_nullable' => 1,
146         'size' => 100,
147         'default_value' => undef,
148     },
149     'rank' => {
150         'data_type' => 'integer',
151         'is_nullable' => 0,
152         'size' => 4,
153         'default_value' => 13,
154
155     },
156     'charfield' => {
157         'data_type' => 'character',
158         'is_nullable' => 1,
159         'size' => 10,
160         'default_value' => undef,
161     },
162     'arrayfield' => {
163         'data_type' => 'integer[]',
164         'is_nullable' => 1,
165         'size' => undef,
166         'default_value' => undef,
167     },
168 };
169
170
171 my $type_info = $schema->storage->columns_info_for('testschema.artist');
172 my $artistid_defval = delete $type_info->{artistid}->{default_value};
173 like($artistid_defval,
174      qr/^nextval\('([^\.]*\.){0,1}artist_artistid_seq'::(?:text|regclass)\)/,
175      'columns_info_for - sequence matches Pg get_autoinc_seq expectations');
176 is_deeply($type_info, $test_type_info,
177           'columns_info_for - column data types');
178
179 SKIP: {
180   skip "Need DBD::Pg 2.9.2 or newer for array tests", 4 if $DBD::Pg::VERSION < 2.009002;
181
182   lives_ok {
183     $schema->resultset('ArrayTest')->create({
184       arrayfield => [1, 2],
185     });
186   } 'inserting arrayref as pg array data';
187
188   lives_ok {
189     $schema->resultset('ArrayTest')->update({
190       arrayfield => [3, 4],
191     });
192   } 'updating arrayref as pg array data';
193
194   $schema->resultset('ArrayTest')->create({
195     arrayfield => [5, 6],
196   });
197
198   my $count;
199   lives_ok {
200     $count = $schema->resultset('ArrayTest')->search({
201       arrayfield => \[ '= ?' => [arrayfield => [3, 4]] ],   #Todo anything less ugly than this?
202     })->count;
203   } 'comparing arrayref to pg array data does not blow up';
204   is($count, 1, 'comparing arrayref to pg array data gives correct result');
205 }
206
207
208 my $name_info = $schema->source('Casecheck')->column_info( 'name' );
209 is( $name_info->{size}, 1, "Case sensitive matching info for 'name'" );
210
211 my $NAME_info = $schema->source('Casecheck')->column_info( 'NAME' );
212 is( $NAME_info->{size}, 2, "Case sensitive matching info for 'NAME'" );
213
214 my $uc_name_info = $schema->source('Casecheck')->column_info( 'uc_name' );
215 is( $uc_name_info->{size}, 3, "Case insensitive matching info for 'uc_name'" );
216
217 # Test SELECT ... FOR UPDATE
218 my $HaveSysSigAction = eval "require Sys::SigAction" && !$@;
219 if ($HaveSysSigAction) {
220     Sys::SigAction->import( 'set_sig_handler' );
221 }
222
223 SKIP: {
224     skip "Sys::SigAction is not available", 3 unless $HaveSysSigAction;
225     # create a new schema
226     my $schema2 = DBICTest::Schema->connect($dsn, $user, $pass);
227     $schema2->source("Artist")->name("testschema.artist");
228
229     $schema->txn_do( sub {
230         my $artist = $schema->resultset('Artist')->search(
231             {
232                 artistid => 1
233             },
234             {
235                 for => 'update'
236             }
237         )->first;
238         is($artist->artistid, 1, "select for update returns artistid = 1");
239
240         my $artist_from_schema2;
241         my $error_ok = 0;
242         eval {
243             my $h = set_sig_handler( 'ALRM', sub { die "DBICTestTimeout" } );
244             alarm(2);
245             $artist_from_schema2 = $schema2->resultset('Artist')->find(1);
246             $artist_from_schema2->name('fooey');
247             $artist_from_schema2->update;
248             alarm(0);
249         };
250         if (my $e = $@) {
251             $error_ok = $e =~ /DBICTestTimeout/;
252         }
253
254         # Make sure that an error was raised, and that the update failed
255         ok($error_ok, "update from second schema times out");
256         ok($artist_from_schema2->is_column_changed('name'), "'name' column is still dirty from second schema");
257     });
258 }
259
260 SKIP: {
261     skip "Sys::SigAction is not available", 3 unless $HaveSysSigAction;
262     # create a new schema
263     my $schema2 = DBICTest::Schema->connect($dsn, $user, $pass);
264     $schema2->source("Artist")->name("testschema.artist");
265
266     $schema->txn_do( sub {
267         my $artist = $schema->resultset('Artist')->search(
268             {
269                 artistid => 1
270             },
271         )->first;
272         is($artist->artistid, 1, "select for update returns artistid = 1");
273
274         my $artist_from_schema2;
275         my $error_ok = 0;
276         eval {
277             my $h = set_sig_handler( 'ALRM', sub { die "DBICTestTimeout" } );
278             alarm(2);
279             $artist_from_schema2 = $schema2->resultset('Artist')->find(1);
280             $artist_from_schema2->name('fooey');
281             $artist_from_schema2->update;
282             alarm(0);
283         };
284         if (my $e = $@) {
285             $error_ok = $e =~ /DBICTestTimeout/;
286         }
287
288         # Make sure that an error was NOT raised, and that the update succeeded
289         ok(! $error_ok, "update from second schema DOES NOT timeout");
290         ok(! $artist_from_schema2->is_column_changed('name'), "'name' column is NOT dirty from second schema");
291     });
292 }
293
294 for (1..5) {
295     my $st = $schema->resultset('SequenceTest')->create({ name => 'foo' });
296     is($st->pkid1, $_, "Oracle Auto-PK without trigger: First primary key");
297     is($st->pkid2, $_ + 9, "Oracle Auto-PK without trigger: Second primary key");
298     is($st->nonpkid, $_ + 19, "Oracle Auto-PK without trigger: Non-primary key");
299 }
300 my $st = $schema->resultset('SequenceTest')->create({ name => 'foo', pkid1 => 55 });
301 is($st->pkid1, 55, "Oracle Auto-PK without trigger: First primary key set manually");
302
303 sub _cleanup {
304   my $dbh = shift or return;
305
306   for my $stat (
307     'DROP TABLE testschema.artist',
308     'DROP TABLE testschema.casecheck',
309     'DROP TABLE testschema.sequence_test',
310     'DROP TABLE testschema.array_test',
311     'DROP SEQUENCE pkid1_seq',
312     'DROP SEQUENCE pkid2_seq',
313     'DROP SEQUENCE nonpkid_seq',
314     'DROP SCHEMA testschema',
315     'DROP TABLE anothertestschema.artist',
316     'DROP SCHEMA anothertestschema',
317     'DROP TABLE yetanothertestschema.artist',
318     'DROP SCHEMA yetanothertestschema',
319   ) {
320     eval { $dbh->do ($stat) };
321   }
322 }
323
324 END { _cleanup($dbh) }