7 use Time::HiRes 'time';
12 use DBIx::Class::_Util qw(sigwarn_silencer modver_gt_or_eq);
16 my $schema = DBICTest->init_schema(auto_savepoint => 1);
18 my $ars = $schema->resultset('Artist');
20 # test two-phase commit and inner transaction rollback from nested transactions
22 $ars->create({ name => 'in_outer_transaction' });
24 $ars->create({ name => 'in_inner_transaction' });
26 ok($ars->search({ name => 'in_inner_transaction' })->first,
27 'commit from inner transaction visible in outer transaction');
30 $ars->create({ name => 'in_inner_transaction_rolling_back' });
31 die 'rolling back inner transaction';
33 } qr/rolling back inner transaction/, 'inner transaction rollback executed';
34 $ars->create({ name => 'in_outer_transaction2' });
37 ok($ars->search({ name => 'in_outer_transaction' })->first,
38 'commit from outer transaction');
39 ok($ars->search({ name => 'in_outer_transaction2' })->first,
40 'second commit from outer transaction');
41 ok($ars->search({ name => 'in_inner_transaction' })->first,
42 'commit from inner transaction');
43 is $ars->search({ name => 'in_inner_transaction_rolling_back' })->first,
45 'rollback from inner transaction';
48 # check that we work somewhat OK with braindead SQLite transaction handling
50 # As per https://metacpan.org/source/ADAMK/DBD-SQLite-1.37/lib/DBD/SQLite.pm#L921
51 # SQLite does *not* try to synchronize
53 # However DBD::SQLite 1.38_02 seems to fix this, with an accompanying test:
54 # https://metacpan.org/source/ADAMK/DBD-SQLite-1.38_02/t/54_literal_txn.t
56 my $lit_txn_todo = modver_gt_or_eq('DBD::SQLite', '1.38_02')
58 : "DBD::SQLite before 1.38_02 is retarded wrt detecting literal BEGIN/COMMIT statements"
61 for my $prefix_comment (qw/Begin_only Commit_only Begin_and_Commit/) {
62 note "Testing with comment prefixes on $prefix_comment";
64 # FIXME warning won't help us for the time being
65 # perhaps when (if ever) DBD::SQLite gets fixed,
66 # we can do something extra here
67 local $SIG{__WARN__} = sigwarn_silencer( qr/Internal transaction state .+? does not seem to match/ )
68 if ( $lit_txn_todo && !$ENV{TEST_VERBOSE} );
70 my ($c_begin, $c_commit) = map { $prefix_comment =~ $_ ? 1 : 0 } (qr/Begin/, qr/Commit/);
72 my $schema = DBICTest->init_schema( no_deploy => 1 );
73 my $ars = $schema->resultset('Artist');
75 ok (! $schema->storage->connected, 'No connection yet');
77 $schema->storage->dbh->do(<<'DDL');
79 artistid INTEGER PRIMARY KEY NOT NULL,
81 rank integer DEFAULT 13,
82 charfield char(10) NULL
86 my $artist = $ars->create({ name => 'Artist_' . time() });
87 is ($ars->count, 1, 'Inserted artist ' . $artist->name);
89 ok ($schema->storage->connected, 'Connected');
90 ok ($schema->storage->_dbh->{AutoCommit}, 'DBD not in txn yet');
92 $schema->storage->dbh->do(join "\n",
93 $c_begin ? '-- comment' : (),
96 ok ($schema->storage->connected, 'Still connected');
98 local $TODO = $lit_txn_todo if $c_begin;
99 ok (! $schema->storage->_dbh->{AutoCommit}, "DBD aware of txn begin with comments on $prefix_comment");
102 $schema->storage->dbh->do(join "\n",
103 $c_commit ? '-- comment' : (),
106 ok ($schema->storage->connected, 'Still connected');
108 local $TODO = $lit_txn_todo if $c_commit and ! $c_begin;
109 ok ($schema->storage->_dbh->{AutoCommit}, "DBD aware txn ended with comments on $prefix_comment");
112 is ($ars->count, 1, 'Inserted artists still there');
115 # this never worked in the 1st place
116 local $TODO = $lit_txn_todo if ! $c_begin and $c_commit;
118 # odd argument passing, because such nested crefs leak on 5.8
120 $schema->storage->txn_do (sub {
121 ok ($_[0]->find({ name => $_[1] }), "Artist still where we left it after cycle with comments on $prefix_comment");
122 }, $ars, $artist->name );
123 } "Succesfull transaction with comments on $prefix_comment";
128 my $schema = DBICTest->init_schema();
130 # make sure the side-effects of RT#67581 do not result in data loss
132 warnings_exist { $row = $schema->resultset('Artist')->create ({ name => 'alpha rank', rank => 'abc' }) }
133 [qr/Non-integer value supplied for column 'rank' despite the integer datatype/],
134 'proper warning on string insertion into an numeric column'
136 $row->discard_changes;
137 is ($row->rank, 'abc', 'proper rank inserted into database');
139 # and make sure we do not lose actual bigints
141 package DBICTest::BigIntArtist;
142 use base 'DBICTest::Schema::Artist';
143 __PACKAGE__->table('artist');
144 __PACKAGE__->add_column(bigint => { data_type => 'bigint' });
146 $schema->register_class(BigIntArtist => 'DBICTest::BigIntArtist');
147 $schema->storage->dbh_do(sub {
148 $_[1]->do('ALTER TABLE artist ADD COLUMN bigint BIGINT');
151 my $sqlite_broken_bigint = (
152 modver_gt_or_eq('DBD::SQLite', '1.34') and ! modver_gt_or_eq('DBD::SQLite', '1.37')
156 my $many_bits = (Math::BigInt->new(2) ** 62);
158 # test upper/lower boundaries for sqlite and some values inbetween
159 # range is -(2**63) .. 2**63 - 1
161 # Not testing -0 - it seems to overflow to ~0 on some combinations,
162 # thus not triggering the >32 bit guards
163 # interesting read: https://en.wikipedia.org/wiki/Signed_zero#Representations
202 # these values cause exceptions even with all workarounds in place on these
203 # fucked DBD::SQLite versions *regardless* of ivsize >.<
204 $sqlite_broken_bigint
206 : ( '2147483648', '2147483649' )
208 # unsigned 32 bit ints have a range of −2,147,483,648 to 2,147,483,647
209 # alternatively expressed as the hexadecimal numbers below
210 # the comparison math will come out right regardless of ivsize, since
211 # we are operating within 31 bits
212 # P.S. 31 because one bit is lost for the sign
213 my $v_bits = ($bi > 0x7fff_ffff || $bi < -0x8000_0000) ? 64 : 32;
215 my $v_desc = sprintf '%s (%d bit signed int)', $bi, $v_bits;
218 local $SIG{__WARN__} = sub { $_[0] =~ /datatype mismatch/ ? push @w, @_ : warn @_ };
220 # some combinations of SQLite 1.35 and older 5.8 faimly is wonky
221 # instead of a warning we get a full exception. Sod it
223 $row = $schema->resultset('BigIntArtist')->create({ bigint => $bi });
225 fail("Exception on inserting $v_desc") unless $sqlite_broken_bigint;
229 # explicitly using eq, to make sure we did not nummify the argument
230 # which can be an issue on 32 bit ivsize
231 cmp_ok ($row->bigint, 'eq', $bi, "value in object correct ($v_desc)");
233 $row->discard_changes;
238 # the test will not pass an == if we are running under 32 bit ivsize
239 # use 'eq' on the numified (and possibly "scientificied") returned value
240 (DBIx::Class::_ENV_::IV_SIZE < 8 and $v_bits > 32) ? 'eq' : '==',
242 # in 1.37 DBD::SQLite switched to proper losless representation of bigints
243 # regardless of ivize
244 # before this use 'eq' (from above) on the numified (and possibly
245 # "scientificied") returned value
246 (DBIx::Class::_ENV_::IV_SIZE < 8 and ! modver_gt_or_eq('DBD::SQLite', '1.37')) ? $bi+0 : $bi,
248 "value in database correct ($v_desc)"
251 # FIXME - temporary smoke-only escape
253 skip 'Potential for false negatives - investigation pending', 1
254 if DBICTest::RunMode->is_plain;
256 # check if math works
257 # start by adding/subtracting a 50 bit integer, and then divide by 2 for good measure
258 my ($sqlop, $expect) = $bi < 0
259 ? ( '(bigint + ? )', ($bi + $many_bits) )
260 : ( '(bigint - ? )', ($bi - $many_bits) )
263 $expect = ($expect + ($expect % 2)) / 2;
265 # read https://en.wikipedia.org/wiki/Modulo_operation#Common_pitfalls
266 # and check the tables on the right side of the article for an
267 # enlightening journey on why a mere bigint % 2 won't work
268 $sqlop = "( $sqlop + ( ((bigint % 2)+2)%2 ) ) / 2";
270 for my $dtype (undef, \'int', \'bigint') {
272 # FIXME - the double-load should not be needed
273 # will fix in the future
274 $row->update({ bigint => $bi });
275 $row->discard_changes;
276 $row->update({ bigint => \[ $sqlop, [ $dtype => $many_bits ] ] });
277 $row->discard_changes;
279 # can't use cmp_ok - will not engage the M::BI overload of $many_bits
285 (DBIx::Class::_ENV_::IV_SIZE < 8 and ! modver_gt_or_eq('DBD::SQLite', '1.37')) ? $expect->bstr + 0 : $expect
286 , "simple integer math with@{[ $dtype ? '' : 'out' ]} bindtype in database correct (base $v_desc)")
287 or diag sprintf '%s != %s', $row->bigint, $expect;
292 is_deeply (\@w, [], "No mismatch warnings on bigint operations ($v_desc)" );