9 package DBICTest::Schema::Casecheck;
13 use base 'DBIx::Class';
15 __PACKAGE__->load_components(qw/Core/);
16 __PACKAGE__->table('casecheck');
17 __PACKAGE__->add_columns(qw/id name NAME uc_name/);
18 __PACKAGE__->column_info_from_storage(1);
19 __PACKAGE__->set_primary_key('id');
23 my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_PG_${_}" } qw/DSN USER PASS/};
25 #warn "$dsn $user $pass";
27 plan skip_all => 'Set $ENV{DBICTEST_PG_DSN}, _USER and _PASS to run this test'
28 . ' (note: creates and drops tables named artist and casecheck!)' unless ($dsn && $user);
32 DBICTest::Schema->load_classes( 'Casecheck' );
33 my $schema = DBICTest::Schema->connect($dsn, $user, $pass);
35 # Check that datetime_parser returns correctly before we explicitly connect.
37 eval { require DateTime::Format::Pg };
38 skip "DateTime::Format::Pg required", 2 if $@;
40 my $store = ref $schema->storage;
41 is($store, 'DBIx::Class::Storage::DBI', 'Started with generic storage');
43 my $parser = $schema->storage->datetime_parser;
44 is( $parser, 'DateTime::Format::Pg', 'datetime_parser is as expected');
47 my $dbh = $schema->storage->dbh;
48 $schema->source("Artist")->name("testschema.artist");
49 $schema->source("SequenceTest")->name("testschema.sequence_test");
50 $dbh->do("CREATE SCHEMA testschema;");
51 $dbh->do("CREATE TABLE testschema.artist (artistid serial PRIMARY KEY, name VARCHAR(100), charfield CHAR(10));");
52 $dbh->do("CREATE TABLE testschema.sequence_test (pkid1 integer, pkid2 integer, nonpkid integer, name VARCHAR(100), CONSTRAINT pk PRIMARY KEY(pkid1, pkid2));");
53 $dbh->do("CREATE SEQUENCE pkid1_seq START 1 MAXVALUE 999999 MINVALUE 0");
54 $dbh->do("CREATE SEQUENCE pkid2_seq START 10 MAXVALUE 999999 MINVALUE 0");
55 $dbh->do("CREATE SEQUENCE nonpkid_seq START 20 MAXVALUE 999999 MINVALUE 0");
56 ok ( $dbh->do('CREATE TABLE testschema.casecheck (id serial PRIMARY KEY, "name" VARCHAR(1), "NAME" VARCHAR(2), "UC_NAME" VARCHAR(3));'), 'Creation of casecheck table');
58 # This is in Core now, but it's here just to test that it doesn't break
59 $schema->class('Artist')->load_components('PK::Auto');
61 my $new = $schema->resultset('Artist')->create({ name => 'foo' });
63 is($new->artistid, 1, "Auto-PK worked");
65 $new = $schema->resultset('Artist')->create({ name => 'bar' });
67 is($new->artistid, 2, "Auto-PK worked");
69 my $test_type_info = {
71 'data_type' => 'integer',
76 'data_type' => 'character varying',
79 'default_value' => undef,
82 'data_type' => 'character',
85 'default_value' => undef,
90 my $type_info = $schema->storage->columns_info_for('testschema.artist');
91 my $artistid_defval = delete $type_info->{artistid}->{default_value};
92 like($artistid_defval,
93 qr/^nextval\('([^\.]*\.){0,1}artist_artistid_seq'::(?:text|regclass)\)/,
94 'columns_info_for - sequence matches Pg get_autoinc_seq expectations');
95 is_deeply($type_info, $test_type_info,
96 'columns_info_for - column data types');
98 my $name_info = $schema->source('Casecheck')->column_info( 'name' );
99 is( $name_info->{size}, 1, "Case sensitive matching info for 'name'" );
101 my $NAME_info = $schema->source('Casecheck')->column_info( 'NAME' );
102 is( $NAME_info->{size}, 2, "Case sensitive matching info for 'NAME'" );
104 my $uc_name_info = $schema->source('Casecheck')->column_info( 'uc_name' );
105 is( $uc_name_info->{size}, 3, "Case insensitive matching info for 'uc_name'" );
107 # Test SELECT ... FOR UPDATE
108 my $HaveSysSigAction = eval "require Sys::SigAction" && !$@;
109 if ($HaveSysSigAction) {
110 Sys::SigAction->import( 'set_sig_handler' );
114 skip "Sys::SigAction is not available", 3 unless $HaveSysSigAction;
115 # create a new schema
116 my $schema2 = DBICTest::Schema->connect($dsn, $user, $pass);
117 $schema2->source("Artist")->name("testschema.artist");
119 $schema->txn_do( sub {
120 my $artist = $schema->resultset('Artist')->search(
128 is($artist->artistid, 1, "select for update returns artistid = 1");
130 my $artist_from_schema2;
133 my $h = set_sig_handler( 'ALRM', sub { die "DBICTestTimeout" } );
135 $artist_from_schema2 = $schema2->resultset('Artist')->find(1);
136 $artist_from_schema2->name('fooey');
137 $artist_from_schema2->update;
141 $error_ok = $e =~ /DBICTestTimeout/;
144 # Make sure that an error was raised, and that the update failed
145 ok($error_ok, "update from second schema times out");
146 ok($artist_from_schema2->is_column_changed('name'), "'name' column is still dirty from second schema");
151 skip "Sys::SigAction is not available", 3 unless $HaveSysSigAction;
152 # create a new schema
153 my $schema2 = DBICTest::Schema->connect($dsn, $user, $pass);
154 $schema2->source("Artist")->name("testschema.artist");
156 $schema->txn_do( sub {
157 my $artist = $schema->resultset('Artist')->search(
162 is($artist->artistid, 1, "select for update returns artistid = 1");
164 my $artist_from_schema2;
167 my $h = set_sig_handler( 'ALRM', sub { die "DBICTestTimeout" } );
169 $artist_from_schema2 = $schema2->resultset('Artist')->find(1);
170 $artist_from_schema2->name('fooey');
171 $artist_from_schema2->update;
175 $error_ok = $e =~ /DBICTestTimeout/;
178 # Make sure that an error was NOT raised, and that the update succeeded
179 ok(! $error_ok, "update from second schema DOES NOT timeout");
180 ok(! $artist_from_schema2->is_column_changed('name'), "'name' column is NOT dirty from second schema");
185 skip "Oracle Auto-PK tests are broken", 16;
187 # test auto increment using sequences WITHOUT triggers
189 my $st = $schema->resultset('SequenceTest')->create({ name => 'foo' });
190 is($st->pkid1, $_, "Oracle Auto-PK without trigger: First primary key");
191 is($st->pkid2, $_ + 9, "Oracle Auto-PK without trigger: Second primary key");
192 is($st->nonpkid, $_ + 19, "Oracle Auto-PK without trigger: Non-primary key");
194 my $st = $schema->resultset('SequenceTest')->create({ name => 'foo', pkid1 => 55 });
195 is($st->pkid1, 55, "Oracle Auto-PK without trigger: First primary key set manually");
200 $dbh->do("DROP TABLE testschema.artist;");
201 $dbh->do("DROP TABLE testschema.casecheck;");
202 $dbh->do("DROP TABLE testschema.sequence_test;");
203 $dbh->do("DROP SEQUENCE pkid1_seq");
204 $dbh->do("DROP SEQUENCE pkid2_seq");
205 $dbh->do("DROP SEQUENCE nonpkid_seq");
206 $dbh->do("DROP SCHEMA testschema;");