added more tests for multi-schema support in 72pg.t
[dbsrgits/DBIx-Class.git] / t / 72pg.t
CommitLineData
70350518 1use strict;
4617b792 2use warnings;
70350518 3
4use Test::More;
9ba627e3 5use Test::Exception;
70350518 6use lib qw(t/lib);
7use DBICTest;
8
89add887 9{
10 package DBICTest::Schema::Casecheck;
11
12 use strict;
13 use warnings;
14 use base 'DBIx::Class';
15
3ff5b740 16 __PACKAGE__->load_components(qw/Core/);
9ba627e3 17 __PACKAGE__->table('testschema.casecheck');
c3af542a 18 __PACKAGE__->add_columns(qw/id name NAME uc_name storecolumn/);
d9916234 19 __PACKAGE__->column_info_from_storage(1);
89add887 20 __PACKAGE__->set_primary_key('id');
21
ece2b6ec 22 sub store_column {
c3af542a 23 my ($self, $name, $value) = @_;
24 $value = '#'.$value if($name eq "storecolumn");
25 $self->maybe::next::method($name, $value);
ece2b6ec 26 }
89add887 27}
28
9ba627e3 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
0567538f 44my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_PG_${_}" } qw/DSN USER PASS/};
45
9ba627e3 46plan 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\''.
f2b70f86 49 ' as well as following schemas: \'testschema\',\'anothertestschema\'!)'
64d5427c 50 unless ($dsn && $user);
0567538f 51
0567538f 52
4617b792 53plan tests => 45;
9ba627e3 54
55DBICTest::Schema->load_classes( 'Casecheck', 'ArrayTest' );
f2b70f86 56my $schema = DBICTest::Schema->connect($dsn, $user, $pass,);
0567538f 57
114780ee 58# Check that datetime_parser returns correctly before we explicitly connect.
59SKIP: {
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
3ff5b740 70my $dbh = $schema->storage->dbh;
71$schema->source("Artist")->name("testschema.artist");
39b8d119 72$schema->source("SequenceTest")->name("testschema.sequence_test");
bb452615 73{
74 local $SIG{__WARN__} = sub {};
7603d2a5 75 _cleanup ($dbh);
76
f2b70f86 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)
85EOS
bb452615 86 $dbh->do("CREATE SCHEMA testschema;");
f2b70f86 87 $dbh->do("CREATE TABLE testschema.artist $artist_table_def;");
bb452615 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");
c3af542a 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');
9ba627e3 93 ok ( $dbh->do('CREATE TABLE testschema.array_test (id serial PRIMARY KEY, arrayfield INTEGER[]);'), 'Creation of array_test table');
f2b70f86 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');
bb452615 99}
0567538f 100
c3af542a 101# store_column is called once for create() for non sequence columns
102
103ok(my $storecolumn = $schema->resultset('Casecheck')->create({'storecolumn' => 'a'}));
104
105is($storecolumn->storecolumn, '#a'); # was '##a'
106
107
3ff5b740 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');
0567538f 110
46bb5b38 111cmp_ok( $schema->resultset('Artist')->count, '==', 0, 'this should start with an empty artist table');
f2b70f86 112
46bb5b38 113{ # test that auto-pk also works with the defined search path by
114 # un-schema-qualifying the table name
f2b70f86 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
4617b792 125 #test with anothertestschema
126 $schema->source('Artist')->name('anothertestschema.artist');
127 my $another_new = $schema->resultset('Artist')->create({ name => 'ribasushi'});
128 is( $another_new->artistid,1, 'got correct artistid for yetanotherschema');
129
130 #test with yetanothertestschema
131 $schema->source('Artist')->name('yetanothertestschema.artist');
132 my $yetanother_new = $schema->resultset('Artist')->create({ name => 'ribasushi'});
133 is( $yetanother_new->artistid,1, 'got correct artistid for yetanotherschema');
134 is( $yetanother_new->artistid,1, 'got correct artistid for yetanotherschema');
135
f2b70f86 136 $schema->source("Artist")->name($artist_name_save);
137}
138
3ff5b740 139my $new = $schema->resultset('Artist')->create({ name => 'foo' });
0567538f 140
f2b70f86 141is($new->artistid, 2, "Auto-PK worked");
b6b65a3e 142
3ff5b740 143$new = $schema->resultset('Artist')->create({ name => 'bar' });
b6b65a3e 144
f2b70f86 145is($new->artistid, 3, "Auto-PK worked");
146
b6b65a3e 147
a953d8d9 148my $test_type_info = {
149 'artistid' => {
103e3e03 150 'data_type' => 'integer',
151 'is_nullable' => 0,
fc22fbac 152 'size' => 4,
a953d8d9 153 },
154 'name' => {
103e3e03 155 'data_type' => 'character varying',
156 'is_nullable' => 1,
ae515736 157 'size' => 100,
fc22fbac 158 'default_value' => undef,
103e3e03 159 },
85df19d7 160 'rank' => {
161 'data_type' => 'integer',
162 'is_nullable' => 0,
163 'size' => 4,
164 'default_value' => 13,
165
166 },
103e3e03 167 'charfield' => {
168 'data_type' => 'character',
a953d8d9 169 'is_nullable' => 1,
fc22fbac 170 'size' => 10,
171 'default_value' => undef,
103e3e03 172 },
9ba627e3 173 'arrayfield' => {
174 'data_type' => 'integer[]',
175 'is_nullable' => 1,
176 'size' => undef,
177 'default_value' => undef,
178 },
a953d8d9 179};
180
fc22fbac 181
3ff5b740 182my $type_info = $schema->storage->columns_info_for('testschema.artist');
fc22fbac 183my $artistid_defval = delete $type_info->{artistid}->{default_value};
184like($artistid_defval,
4d272ce5 185 qr/^nextval\('([^\.]*\.){0,1}artist_artistid_seq'::(?:text|regclass)\)/,
fc22fbac 186 'columns_info_for - sequence matches Pg get_autoinc_seq expectations');
187is_deeply($type_info, $test_type_info,
188 'columns_info_for - column data types');
a953d8d9 189
ac45262b 190SKIP: {
191 skip "Need DBD::Pg 2.9.2 or newer for array tests", 4 if $DBD::Pg::VERSION < 2.009002;
192
9ba627e3 193 lives_ok {
194 $schema->resultset('ArrayTest')->create({
195 arrayfield => [1, 2],
196 });
197 } 'inserting arrayref as pg array data';
198
199 lives_ok {
200 $schema->resultset('ArrayTest')->update({
201 arrayfield => [3, 4],
202 });
203 } 'updating arrayref as pg array data';
c224117b 204
205 $schema->resultset('ArrayTest')->create({
206 arrayfield => [5, 6],
207 });
208
209 my $count;
210 lives_ok {
211 $count = $schema->resultset('ArrayTest')->search({
aab0d3b7 212 arrayfield => \[ '= ?' => [arrayfield => [3, 4]] ], #Todo anything less ugly than this?
c224117b 213 })->count;
214 } 'comparing arrayref to pg array data does not blow up';
215 is($count, 1, 'comparing arrayref to pg array data gives correct result');
9ba627e3 216}
217
218
3ff5b740 219my $name_info = $schema->source('Casecheck')->column_info( 'name' );
ae515736 220is( $name_info->{size}, 1, "Case sensitive matching info for 'name'" );
221
3ff5b740 222my $NAME_info = $schema->source('Casecheck')->column_info( 'NAME' );
ae515736 223is( $NAME_info->{size}, 2, "Case sensitive matching info for 'NAME'" );
224
3ff5b740 225my $uc_name_info = $schema->source('Casecheck')->column_info( 'uc_name' );
ae515736 226is( $uc_name_info->{size}, 3, "Case insensitive matching info for 'uc_name'" );
227
95ba7ee4 228# Test SELECT ... FOR UPDATE
229my $HaveSysSigAction = eval "require Sys::SigAction" && !$@;
230if ($HaveSysSigAction) {
231 Sys::SigAction->import( 'set_sig_handler' );
232}
233
234SKIP: {
235 skip "Sys::SigAction is not available", 3 unless $HaveSysSigAction;
236 # create a new schema
237 my $schema2 = DBICTest::Schema->connect($dsn, $user, $pass);
238 $schema2->source("Artist")->name("testschema.artist");
239
240 $schema->txn_do( sub {
241 my $artist = $schema->resultset('Artist')->search(
242 {
243 artistid => 1
244 },
245 {
246 for => 'update'
247 }
248 )->first;
249 is($artist->artistid, 1, "select for update returns artistid = 1");
250
251 my $artist_from_schema2;
252 my $error_ok = 0;
253 eval {
254 my $h = set_sig_handler( 'ALRM', sub { die "DBICTestTimeout" } );
255 alarm(2);
256 $artist_from_schema2 = $schema2->resultset('Artist')->find(1);
257 $artist_from_schema2->name('fooey');
258 $artist_from_schema2->update;
259 alarm(0);
260 };
261 if (my $e = $@) {
262 $error_ok = $e =~ /DBICTestTimeout/;
263 }
264
265 # Make sure that an error was raised, and that the update failed
266 ok($error_ok, "update from second schema times out");
267 ok($artist_from_schema2->is_column_changed('name'), "'name' column is still dirty from second schema");
268 });
269}
270
271SKIP: {
272 skip "Sys::SigAction is not available", 3 unless $HaveSysSigAction;
273 # create a new schema
274 my $schema2 = DBICTest::Schema->connect($dsn, $user, $pass);
275 $schema2->source("Artist")->name("testschema.artist");
276
277 $schema->txn_do( sub {
278 my $artist = $schema->resultset('Artist')->search(
279 {
280 artistid => 1
281 },
282 )->first;
283 is($artist->artistid, 1, "select for update returns artistid = 1");
284
285 my $artist_from_schema2;
286 my $error_ok = 0;
287 eval {
288 my $h = set_sig_handler( 'ALRM', sub { die "DBICTestTimeout" } );
289 alarm(2);
290 $artist_from_schema2 = $schema2->resultset('Artist')->find(1);
291 $artist_from_schema2->name('fooey');
292 $artist_from_schema2->update;
293 alarm(0);
294 };
295 if (my $e = $@) {
296 $error_ok = $e =~ /DBICTestTimeout/;
297 }
298
299 # Make sure that an error was NOT raised, and that the update succeeded
300 ok(! $error_ok, "update from second schema DOES NOT timeout");
301 ok(! $artist_from_schema2->is_column_changed('name'), "'name' column is NOT dirty from second schema");
302 });
303}
304
d093d3eb 305for (1..5) {
39b8d119 306 my $st = $schema->resultset('SequenceTest')->create({ name => 'foo' });
307 is($st->pkid1, $_, "Oracle Auto-PK without trigger: First primary key");
308 is($st->pkid2, $_ + 9, "Oracle Auto-PK without trigger: Second primary key");
309 is($st->nonpkid, $_ + 19, "Oracle Auto-PK without trigger: Non-primary key");
d093d3eb 310}
311my $st = $schema->resultset('SequenceTest')->create({ name => 'foo', pkid1 => 55 });
312is($st->pkid1, 55, "Oracle Auto-PK without trigger: First primary key set manually");
313
7603d2a5 314sub _cleanup {
315 my $dbh = shift or return;
316
317 for my $stat (
318 'DROP TABLE testschema.artist',
319 'DROP TABLE testschema.casecheck',
320 'DROP TABLE testschema.sequence_test',
321 'DROP TABLE testschema.array_test',
322 'DROP SEQUENCE pkid1_seq',
323 'DROP SEQUENCE pkid2_seq',
324 'DROP SEQUENCE nonpkid_seq',
325 'DROP SCHEMA testschema',
f2b70f86 326 'DROP TABLE anothertestschema.artist',
327 'DROP SCHEMA anothertestschema',
328 'DROP TABLE yetanothertestschema.artist',
329 'DROP SCHEMA yetanothertestschema',
7603d2a5 330 ) {
331 eval { $dbh->do ($stat) };
332 }
3ff5b740 333}
7603d2a5 334
335END { _cleanup($dbh) }