7 use Time::HiRes 'time';
12 use DBIx::Class::_Util qw( sigwarn_silencer modver_gt_or_eq modver_gt_or_eq_and_lt );
14 # check that we work somewhat OK with braindead SQLite transaction handling
16 # As per https://metacpan.org/source/ADAMK/DBD-SQLite-1.37/lib/DBD/SQLite.pm#L921
17 # SQLite does *not* try to synchronize
19 # However DBD::SQLite 1.38_02 seems to fix this, with an accompanying test:
20 # https://metacpan.org/source/ADAMK/DBD-SQLite-1.38_02/t/54_literal_txn.t
23 my $lit_txn_todo = modver_gt_or_eq('DBD::SQLite', '1.38_02')
25 : "DBD::SQLite before 1.38_02 is retarded wrt detecting literal BEGIN/COMMIT statements"
28 for my $prefix_comment (qw/Begin_only Commit_only Begin_and_Commit/) {
29 note "Testing with comment prefixes on $prefix_comment";
31 # FIXME warning won't help us for the time being
32 # perhaps when (if ever) DBD::SQLite gets fixed,
33 # we can do something extra here
34 local $SIG{__WARN__} = sigwarn_silencer( qr/Internal transaction state .+? does not seem to match/ )
35 if ( $lit_txn_todo && !$ENV{TEST_VERBOSE} );
37 my ($c_begin, $c_commit) = map { $prefix_comment =~ $_ ? 1 : 0 } (qr/Begin/, qr/Commit/);
39 my $schema = DBICTest->init_schema( no_deploy => 1 );
40 my $ars = $schema->resultset('Artist');
42 ok (! $schema->storage->connected, 'No connection yet');
44 $schema->storage->dbh->do(<<'DDL');
46 artistid INTEGER PRIMARY KEY NOT NULL,
48 rank integer DEFAULT 13,
49 charfield char(10) NULL
53 my $artist = $ars->create({ name => 'Artist_' . time() });
54 is ($ars->count, 1, 'Inserted artist ' . $artist->name);
56 ok ($schema->storage->connected, 'Connected');
57 ok ($schema->storage->_dbh->{AutoCommit}, 'DBD not in txn yet');
59 $schema->storage->dbh->do(join "\n",
60 $c_begin ? '-- comment' : (),
63 ok ($schema->storage->connected, 'Still connected');
65 local $TODO = $lit_txn_todo if $c_begin;
66 ok (! $schema->storage->_dbh->{AutoCommit}, "DBD aware of txn begin with comments on $prefix_comment");
69 $schema->storage->dbh->do(join "\n",
70 $c_commit ? '-- comment' : (),
73 ok ($schema->storage->connected, 'Still connected');
75 local $TODO = $lit_txn_todo if $c_commit and ! $c_begin;
76 ok ($schema->storage->_dbh->{AutoCommit}, "DBD aware txn ended with comments on $prefix_comment");
79 is ($ars->count, 1, 'Inserted artists still there');
82 # this never worked in the 1st place
83 local $TODO = $lit_txn_todo if ! $c_begin and $c_commit;
85 # odd argument passing, because such nested crefs leak on 5.8
87 $schema->storage->txn_do (sub {
88 ok ($_[0]->find({ name => $_[1] }), "Artist still where we left it after cycle with comments on $prefix_comment");
89 }, $ars, $artist->name );
90 } "Succesfull transaction with comments on $prefix_comment";
94 # test blank begin/svp/commit/begin cycle
96 my $schema = DBICTest->init_schema( no_populate => 1 );
97 my $rs = $schema->resultset('Artist');
98 is ($rs->count, 0, 'Start with empty table');
100 for my $do_commit (1, 0) {
103 $schema->svp_rollback;
106 $schema->svp_rollback;
108 $schema->svp_release;
112 $schema->txn_rollback;
116 $schema->svp_rollback;
119 $schema->svp_rollback;
121 $schema->svp_release;
125 $do_commit ? $schema->txn_commit : $schema->txn_rollback;
127 is_deeply $schema->storage->savepoints, [], 'Savepoint names cleared away'
130 $schema->txn_do(sub {
131 ok (1, 'all seems fine');
133 } [], 'No warnings emitted';
135 my $schema = DBICTest->init_schema();
137 # make sure the side-effects of RT#67581 do not result in data loss
139 warnings_exist { $row = $schema->resultset('Artist')->create ({ name => 'alpha rank', rank => 'abc' }) }
140 [qr/Non-integer value supplied for column 'rank' despite the integer datatype/],
141 'proper warning on string insertion into an numeric column'
143 $row->discard_changes;
144 is ($row->rank, 'abc', 'proper rank inserted into database');
146 # and make sure we do not lose actual bigints
149 skip "Not testing bigint handling on known broken DBD::SQLite trial versions", 1
150 if modver_gt_or_eq_and_lt( 'DBD::SQLite', '1.45', '1.45_03' );
153 package DBICTest::BigIntArtist;
154 use base 'DBICTest::Schema::Artist';
155 __PACKAGE__->table('artist');
156 __PACKAGE__->add_column(bigint => { data_type => 'bigint' });
158 $schema->register_class(BigIntArtist => 'DBICTest::BigIntArtist');
159 $schema->storage->dbh_do(sub {
160 $_[1]->do('ALTER TABLE artist ADD COLUMN bigint BIGINT');
163 my $sqlite_broken_bigint = modver_gt_or_eq_and_lt( 'DBD::SQLite', '1.34', '1.37' );
166 my $many_bits = (Math::BigInt->new(2) ** 62);
168 # test upper/lower boundaries for sqlite and some values inbetween
169 # range is -(2**63) .. 2**63 - 1
171 # Not testing -0 - it seems to overflow to ~0 on some combinations,
172 # thus not triggering the >32 bit guards
173 # interesting read: https://en.wikipedia.org/wiki/Signed_zero#Representations
212 # these values cause exceptions even with all workarounds in place on these
213 # fucked DBD::SQLite versions *regardless* of ivsize >.<
214 $sqlite_broken_bigint
216 : ( '2147483648', '2147483649' )
218 # unsigned 32 bit ints have a range of −2,147,483,648 to 2,147,483,647
219 # alternatively expressed as the hexadecimal numbers below
220 # the comparison math will come out right regardless of ivsize, since
221 # we are operating within 31 bits
222 # P.S. 31 because one bit is lost for the sign
223 my $v_bits = ($bi > 0x7fff_ffff || $bi < -0x8000_0000) ? 64 : 32;
225 my $v_desc = sprintf '%s (%d bit signed int)', $bi, $v_bits;
228 local $SIG{__WARN__} = sub {
229 if ($_[0] =~ /datatype mismatch/) {
232 elsif ($_[0] =~ /An integer value occupying more than 32 bits was supplied .+ can not bind properly so DBIC will treat it as a string instead/ ) {
233 # do nothing, this warning will pop up here and there depending on
234 # DBD/bitness combination
235 # we don't want to test for it explicitly, we are just interested
236 # in the results matching at the end
243 # some combinations of SQLite 1.35 and older 5.8 faimly is wonky
244 # instead of a warning we get a full exception. Sod it
246 $row = $schema->resultset('BigIntArtist')->create({ bigint => $bi });
248 fail("Exception on inserting $v_desc") unless $sqlite_broken_bigint;
252 # explicitly using eq, to make sure we did not nummify the argument
253 # which can be an issue on 32 bit ivsize
254 cmp_ok ($row->bigint, 'eq', $bi, "value in object correct ($v_desc)");
256 $row->discard_changes;
261 # the test will not pass an == if we are running under 32 bit ivsize
262 # use 'eq' on the numified (and possibly "scientificied") returned value
263 (DBIx::Class::_ENV_::IV_SIZE < 8 and $v_bits > 32) ? 'eq' : '==',
265 # in 1.37 DBD::SQLite switched to proper losless representation of bigints
266 # regardless of ivize
267 # before this use 'eq' (from above) on the numified (and possibly
268 # "scientificied") returned value
269 (DBIx::Class::_ENV_::IV_SIZE < 8 and ! modver_gt_or_eq('DBD::SQLite', '1.37')) ? $bi+0 : $bi,
271 "value in database correct ($v_desc)"
274 # check if math works
275 # start by adding/subtracting a 50 bit integer, and then divide by 2 for good measure
276 my ($sqlop, $expect) = $bi < 0
277 ? ( '(bigint + ? )', ($bi + $many_bits) )
278 : ( '(bigint - ? )', ($bi - $many_bits) )
281 $expect = ($expect + ($expect % 2)) / 2;
283 # read https://en.wikipedia.org/wiki/Modulo_operation#Common_pitfalls
284 # and check the tables on the right side of the article for an
285 # enlightening journey on why a mere bigint % 2 won't work
286 $sqlop = "( $sqlop + ( ((bigint % 2)+2)%2 ) ) / 2";
288 for my $dtype (undef, \'int', \'bigint') {
290 # FIXME - the double-load should not be needed
291 # will fix in the future
292 $row->update({ bigint => $bi });
293 $row->discard_changes;
294 $row->update({ bigint => \[ $sqlop, [ $dtype => $many_bits ] ] });
295 $row->discard_changes;
297 # can't use cmp_ok - will not engage the M::BI overload of $many_bits
303 (DBIx::Class::_ENV_::IV_SIZE < 8 and ! modver_gt_or_eq('DBD::SQLite', '1.37')) ? $expect->bstr + 0 : $expect
304 , "simple integer math with@{[ $dtype ? '' : 'out' ]} bindtype in database correct (base $v_desc)")
305 or diag sprintf '%s != %s', $row->bigint, $expect;
308 is_deeply (\@w, [], "No mismatch warnings on bigint operations ($v_desc)" );