10 package DBICTest::Schema::Casecheck;
14 use base 'DBIx::Class';
16 __PACKAGE__->load_components(qw/Core/);
17 __PACKAGE__->table('casecheck');
18 __PACKAGE__->add_columns(qw/id name NAME uc_name/);
19 __PACKAGE__->column_info_from_storage(1);
20 __PACKAGE__->set_primary_key('id');
24 my ($dsn, $user, $pass) = @ENV{map { "DBICTEST_PG_${_}" } qw/DSN USER PASS/};
26 #warn "$dsn $user $pass";
28 plan skip_all => 'Set $ENV{DBICTEST_PG_DSN}, _USER and _PASS to run this test'
29 . ' (note: creates and drops tables named artist and casecheck!)' unless ($dsn && $user);
33 DBICTest::Schema->load_classes( 'Casecheck' );
34 my $schema = DBICTest::Schema->connect($dsn, $user, $pass, { auto_savepoint => 1});
36 # Check that datetime_parser returns correctly before we explicitly connect.
38 eval { require DateTime::Format::Pg };
39 skip "DateTime::Format::Pg required", 2 if $@;
41 my $store = ref $schema->storage;
42 is($store, 'DBIx::Class::Storage::DBI', 'Started with generic storage');
44 my $parser = $schema->storage->datetime_parser;
45 is( $parser, 'DateTime::Format::Pg', 'datetime_parser is as expected');
48 my $dbh = $schema->storage->dbh;
49 my $stats = new DBICTest::Stats();
50 $schema->storage->debugobj($stats);
51 $schema->storage->debug(1);
53 $schema->source("Artist")->name("testschema.artist");
54 $schema->source("SequenceTest")->name("testschema.sequence_test");
55 $dbh->do("CREATE SCHEMA testschema;");
56 $dbh->do("CREATE TABLE testschema.artist (artistid serial PRIMARY KEY, name VARCHAR(100), charfield CHAR(10));");
57 $dbh->do("CREATE TABLE testschema.sequence_test (pkid1 integer, pkid2 integer, nonpkid integer, name VARCHAR(100), CONSTRAINT pk PRIMARY KEY(pkid1, pkid2));");
58 $dbh->do("CREATE SEQUENCE pkid1_seq START 1 MAXVALUE 999999 MINVALUE 0");
59 $dbh->do("CREATE SEQUENCE pkid2_seq START 10 MAXVALUE 999999 MINVALUE 0");
60 $dbh->do("CREATE SEQUENCE nonpkid_seq START 20 MAXVALUE 999999 MINVALUE 0");
61 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');
63 # This is in Core now, but it's here just to test that it doesn't break
64 $schema->class('Artist')->load_components('PK::Auto');
66 my $new = $schema->resultset('Artist')->create({ name => 'foo' });
68 is($new->artistid, 1, "Auto-PK worked");
70 $new = $schema->resultset('Artist')->create({ name => 'bar' });
72 is($new->artistid, 2, "Auto-PK worked");
74 my $test_type_info = {
76 'data_type' => 'integer',
81 'data_type' => 'character varying',
84 'default_value' => undef,
87 'data_type' => 'character',
90 'default_value' => undef,
95 my $type_info = $schema->storage->columns_info_for('testschema.artist');
96 my $artistid_defval = delete $type_info->{artistid}->{default_value};
97 like($artistid_defval,
98 qr/^nextval\('([^\.]*\.){0,1}artist_artistid_seq'::(?:text|regclass)\)/,
99 'columns_info_for - sequence matches Pg get_autoinc_seq expectations');
100 is_deeply($type_info, $test_type_info,
101 'columns_info_for - column data types');
103 my $name_info = $schema->source('Casecheck')->column_info( 'name' );
104 is( $name_info->{size}, 1, "Case sensitive matching info for 'name'" );
106 my $NAME_info = $schema->source('Casecheck')->column_info( 'NAME' );
107 is( $NAME_info->{size}, 2, "Case sensitive matching info for 'NAME'" );
109 my $uc_name_info = $schema->source('Casecheck')->column_info( 'uc_name' );
110 is( $uc_name_info->{size}, 3, "Case insensitive matching info for 'uc_name'" );
112 # Test SELECT ... FOR UPDATE
113 my $HaveSysSigAction = eval "require Sys::SigAction" && !$@;
114 if ($HaveSysSigAction) {
115 Sys::SigAction->import( 'set_sig_handler' );
119 skip "Sys::SigAction is not available", 3 unless $HaveSysSigAction;
120 # create a new schema
121 my $schema2 = DBICTest::Schema->connect($dsn, $user, $pass);
122 $schema2->source("Artist")->name("testschema.artist");
124 $schema->txn_do( sub {
125 my $artist = $schema->resultset('Artist')->search(
133 is($artist->artistid, 1, "select for update returns artistid = 1");
135 my $artist_from_schema2;
138 my $h = set_sig_handler( 'ALRM', sub { die "DBICTestTimeout" } );
140 $artist_from_schema2 = $schema2->resultset('Artist')->find(1);
141 $artist_from_schema2->name('fooey');
142 $artist_from_schema2->update;
146 $error_ok = $e =~ /DBICTestTimeout/;
149 # Make sure that an error was raised, and that the update failed
150 ok($error_ok, "update from second schema times out");
151 ok($artist_from_schema2->is_column_changed('name'), "'name' column is still dirty from second schema");
156 skip "Sys::SigAction is not available", 3 unless $HaveSysSigAction;
157 # create a new schema
158 my $schema2 = DBICTest::Schema->connect($dsn, $user, $pass);
159 $schema2->source("Artist")->name("testschema.artist");
161 $schema->txn_do( sub {
162 my $artist = $schema->resultset('Artist')->search(
167 is($artist->artistid, 1, "select for update returns artistid = 1");
169 my $artist_from_schema2;
172 my $h = set_sig_handler( 'ALRM', sub { die "DBICTestTimeout" } );
174 $artist_from_schema2 = $schema2->resultset('Artist')->find(1);
175 $artist_from_schema2->name('fooey');
176 $artist_from_schema2->update;
180 $error_ok = $e =~ /DBICTestTimeout/;
183 # Make sure that an error was NOT raised, and that the update succeeded
184 ok(! $error_ok, "update from second schema DOES NOT timeout");
185 ok(! $artist_from_schema2->is_column_changed('name'), "'name' column is NOT dirty from second schema");
190 skip "Oracle Auto-PK tests are broken", 16;
191 # test auto increment using sequences WITHOUT triggers
194 my $st = $schema->resultset('SequenceTest')->create({ name => 'foo' });
195 is($st->pkid1, $_, "Oracle Auto-PK without trigger: First primary key");
196 is($st->pkid2, $_ + 9, "Oracle Auto-PK without trigger: Second primary key");
197 is($st->nonpkid, $_ + 19, "Oracle Auto-PK without trigger: Non-primary key");
199 my $st = $schema->resultset('SequenceTest')->create({ name => 'foo', pkid1 => 55 });
200 is($st->pkid1, 55, "Oracle Auto-PK without trigger: First primary key set manually");
203 $schema->txn_begin();
205 my $arty = $schema->resultset('Artist')->find(1);
207 my $name = $arty->name();
209 $schema->svp_begin('savepoint1');
211 cmp_ok($stats->{'SVP_BEGIN'}, '==', 1, 'Statistics svp_begin tickled');
213 $arty->update({ name => 'Jheephizzy' });
215 $arty->discard_changes();
217 cmp_ok($arty->name(), 'eq', 'Jheephizzy', 'Name changed');
219 $schema->svp_rollback('savepoint1');
221 cmp_ok($stats->{'SVP_ROLLBACK'}, '==', 1, 'Statistics svp_rollback tickled');
223 $arty->discard_changes();
225 cmp_ok($arty->name(), 'eq', $name, 'Name rolled back');
227 $schema->txn_commit();
229 $schema->txn_do (sub {
230 $schema->txn_do (sub {
231 $arty->name ('Muff');
237 $schema->txn_do (sub {
238 $arty->name ('Moff');
242 $arty->discard_changes;
244 is($arty->name,'Moff','Value updated in nested transaction');
246 $schema->storage->dbh->do ("GUARANTEED TO PHAIL");
250 ok ($@,'Nested transaction failed (good)');
252 $arty->discard_changes;
254 is($arty->name,'Muff','auto_savepoint rollback worked');
256 $arty->name ('Miff');
261 $arty->discard_changes;
263 is($arty->name,'Miff','auto_savepoint worked');
265 cmp_ok($stats->{'SVP_BEGIN'},'==',3,'Correct number of savepoints created');
267 cmp_ok($stats->{'SVP_RELEASE'},'==',2,'Correct number of savepoints released');
269 cmp_ok($stats->{'SVP_ROLLBACK'},'==',2,'Correct number of savepoint rollbacks');
273 $dbh->do("DROP TABLE testschema.artist;");
274 $dbh->do("DROP TABLE testschema.casecheck;");
275 $dbh->do("DROP TABLE testschema.sequence_test;");
276 $dbh->do("DROP SEQUENCE pkid1_seq");
277 $dbh->do("DROP SEQUENCE pkid2_seq");
278 $dbh->do("DROP SEQUENCE nonpkid_seq");
279 $dbh->do("DROP SCHEMA testschema;");