7 use Time::HiRes 'time';
11 use DBIx::Class::_Util qw(sigwarn_silencer modver_gt_or_eq);
15 my $schema = DBICTest->init_schema(auto_savepoint => 1);
17 my $ars = $schema->resultset('Artist');
19 # test two-phase commit and inner transaction rollback from nested transactions
21 $ars->create({ name => 'in_outer_transaction' });
23 $ars->create({ name => 'in_inner_transaction' });
25 ok($ars->search({ name => 'in_inner_transaction' })->first,
26 'commit from inner transaction visible in outer transaction');
29 $ars->create({ name => 'in_inner_transaction_rolling_back' });
30 die 'rolling back inner transaction';
32 } qr/rolling back inner transaction/, 'inner transaction rollback executed';
33 $ars->create({ name => 'in_outer_transaction2' });
36 ok($ars->search({ name => 'in_outer_transaction' })->first,
37 'commit from outer transaction');
38 ok($ars->search({ name => 'in_outer_transaction2' })->first,
39 'second commit from outer transaction');
40 ok($ars->search({ name => 'in_inner_transaction' })->first,
41 'commit from inner transaction');
42 is $ars->search({ name => 'in_inner_transaction_rolling_back' })->first,
44 'rollback from inner transaction';
47 # check that we work somewhat OK with braindead SQLite transaction handling
49 # As per https://metacpan.org/source/ADAMK/DBD-SQLite-1.37/lib/DBD/SQLite.pm#L921
50 # SQLite does *not* try to synchronize
52 # However DBD::SQLite 1.38_02 seems to fix this, with an accompanying test:
53 # https://metacpan.org/source/ADAMK/DBD-SQLite-1.38_02/t/54_literal_txn.t
55 my $lit_txn_todo = modver_gt_or_eq('DBD::SQLite', '1.38_02')
57 : "DBD::SQLite before 1.38_02 is retarded wrt detecting literal BEGIN/COMMIT statements"
60 for my $prefix_comment (qw/Begin_only Commit_only Begin_and_Commit/) {
61 note "Testing with comment prefixes on $prefix_comment";
63 # FIXME warning won't help us for the time being
64 # perhaps when (if ever) DBD::SQLite gets fixed,
65 # we can do something extra here
66 local $SIG{__WARN__} = sigwarn_silencer( qr/Internal transaction state .+? does not seem to match/ )
67 if ( $lit_txn_todo && !$ENV{TEST_VERBOSE} );
69 my ($c_begin, $c_commit) = map { $prefix_comment =~ $_ ? 1 : 0 } (qr/Begin/, qr/Commit/);
71 my $schema = DBICTest->init_schema( no_deploy => 1 );
72 my $ars = $schema->resultset('Artist');
74 ok (! $schema->storage->connected, 'No connection yet');
76 $schema->storage->dbh->do(<<'DDL');
78 artistid INTEGER PRIMARY KEY NOT NULL,
80 rank integer DEFAULT 13,
81 charfield char(10) NULL
85 my $artist = $ars->create({ name => 'Artist_' . time() });
86 is ($ars->count, 1, 'Inserted artist ' . $artist->name);
88 ok ($schema->storage->connected, 'Connected');
89 ok ($schema->storage->_dbh->{AutoCommit}, 'DBD not in txn yet');
91 $schema->storage->dbh->do(join "\n",
92 $c_begin ? '-- comment' : (),
95 ok ($schema->storage->connected, 'Still connected');
97 local $TODO = $lit_txn_todo if $c_begin;
98 ok (! $schema->storage->_dbh->{AutoCommit}, "DBD aware of txn begin with comments on $prefix_comment");
101 $schema->storage->dbh->do(join "\n",
102 $c_commit ? '-- comment' : (),
105 ok ($schema->storage->connected, 'Still connected');
107 local $TODO = $lit_txn_todo if $c_commit and ! $c_begin;
108 ok ($schema->storage->_dbh->{AutoCommit}, "DBD aware txn ended with comments on $prefix_comment");
111 is ($ars->count, 1, 'Inserted artists still there');
114 # this never worked in the 1st place
115 local $TODO = $lit_txn_todo if ! $c_begin and $c_commit;
117 # odd argument passing, because such nested crefs leak on 5.8
119 $schema->storage->txn_do (sub {
120 ok ($_[0]->find({ name => $_[1] }), "Artist still where we left it after cycle with comments on $prefix_comment");
121 }, $ars, $artist->name );
122 } "Succesfull transaction with comments on $prefix_comment";
127 my $schema = DBICTest->init_schema();
129 # make sure the side-effects of RT#67581 do not result in data loss
131 warnings_exist { $row = $schema->resultset('Artist')->create ({ name => 'alpha rank', rank => 'abc' }) }
132 [qr/Non-integer value supplied for column 'rank' despite the integer datatype/],
133 'proper warning on string insertion into an numeric column'
135 $row->discard_changes;
136 is ($row->rank, 'abc', 'proper rank inserted into database');
138 # and make sure we do not lose actual bigints
140 package DBICTest::BigIntArtist;
141 use base 'DBICTest::Schema::Artist';
142 __PACKAGE__->table('artist');
143 __PACKAGE__->add_column(bigint => { data_type => 'bigint' });
145 $schema->register_class(BigIntArtist => 'DBICTest::BigIntArtist');
146 $schema->storage->dbh_do(sub {
147 $_[1]->do('ALTER TABLE artist ADD COLUMN bigint BIGINT');
150 # test upper/lower boundaries for sqlite and some values inbetween
151 # range is -(2**63) .. 2**63 - 1
183 # these values cause exceptions even with all workarounds in place on these
184 # fucked DBD::SQLite versions *regardless* of ivsize >.<
185 ( modver_gt_or_eq('DBD::SQLite', '1.34') and ! modver_gt_or_eq('DBD::SQLite', '1.37') )
187 : ( '2147483648', '2147483649' )
189 # unsigned 32 bit ints have a range of −2,147,483,648 to 2,147,483,647
190 # alternatively expressed as the hexadecimal numbers below
191 # the comparison math will come out right regardless of ivsize, since
192 # we are operating within 31 bits
193 # P.S. 31 because one bit is lost for the sign
194 my $v_bits = ($bi > 0x7fff_ffff || $bi < -0x8000_0000) ? 64 : 32;
196 my $v_desc = sprintf '%s (%d bit signed int)', $bi, $v_bits;
199 local $SIG{__WARN__} = sub { $_[0] =~ /datatype mismatch/ ? push @w, @_ : warn @_ };
202 $row = $schema->resultset('BigIntArtist')->create({ bigint => $bi });
203 } "Insering value ($v_desc)" or next;
205 # explicitly using eq, to make sure we did not nummify the argument
206 # which can be an issue on 32 bit ivsize
207 cmp_ok ($row->bigint, 'eq', $bi, "value in object correct ($v_desc)");
209 $row->discard_changes;
214 # the test will not pass an == if we are running under 32 bit ivsize
215 # use 'eq' on the numified (and possibly "scientificied") returned value
216 DBIx::Class::_ENV_::IV_SIZE < 8 ? 'eq' : '==',
218 # in 1.37 DBD::SQLite switched to proper losless representation of bigints
219 # regardless of ivize
220 # before this use 'eq' (from above) on the numified (and possibly
221 # "scientificied") returned value
222 (DBIx::Class::_ENV_::IV_SIZE < 8 and ! modver_gt_or_eq('DBD::SQLite', '1.37')) ? $bi+0 : $bi,
224 "value in database correct ($v_desc)"
227 is_deeply (\@w, [], 'No mismatch warnings on bigint operations' );