Refactor ::DBIHacks::_extract_fixed_condition_columns (sequel to 8d005ad9)
[dbsrgits/DBIx-Class.git] / t / 752sqlite.t
CommitLineData
86a51471 1use strict;
2use warnings;
3
4use Test::More;
5use Test::Exception;
632d1e0f 6use Test::Warn;
2aeb3c7f 7use Time::HiRes 'time';
f3b1224b 8use Math::BigInt;
67b35a45 9
86a51471 10use lib qw(t/lib);
11use DBICTest;
052a832c 12use DBIx::Class::_Util qw(sigwarn_silencer modver_gt_or_eq);
86a51471 13
2aeb3c7f 14# check that we work somewhat OK with braindead SQLite transaction handling
15#
16# As per https://metacpan.org/source/ADAMK/DBD-SQLite-1.37/lib/DBD/SQLite.pm#L921
17# SQLite does *not* try to synchronize
ab0b0a09 18#
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
21
56270bba 22require DBD::SQLite;
b1dbf716 23my $lit_txn_todo = modver_gt_or_eq('DBD::SQLite', '1.38_02')
ab0b0a09 24 ? undef
25 : "DBD::SQLite before 1.38_02 is retarded wrt detecting literal BEGIN/COMMIT statements"
26;
2aeb3c7f 27
28for my $prefix_comment (qw/Begin_only Commit_only Begin_and_Commit/) {
29 note "Testing with comment prefixes on $prefix_comment";
30
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
052a832c 34 local $SIG{__WARN__} = sigwarn_silencer( qr/Internal transaction state .+? does not seem to match/ )
ab0b0a09 35 if ( $lit_txn_todo && !$ENV{TEST_VERBOSE} );
2aeb3c7f 36
37 my ($c_begin, $c_commit) = map { $prefix_comment =~ $_ ? 1 : 0 } (qr/Begin/, qr/Commit/);
38
39 my $schema = DBICTest->init_schema( no_deploy => 1 );
40 my $ars = $schema->resultset('Artist');
41
42 ok (! $schema->storage->connected, 'No connection yet');
43
44 $schema->storage->dbh->do(<<'DDL');
45CREATE TABLE artist (
46 artistid INTEGER PRIMARY KEY NOT NULL,
47 name varchar(100),
48 rank integer DEFAULT 13,
49 charfield char(10) NULL
50);
51DDL
52
53 my $artist = $ars->create({ name => 'Artist_' . time() });
54 is ($ars->count, 1, 'Inserted artist ' . $artist->name);
55
56 ok ($schema->storage->connected, 'Connected');
57 ok ($schema->storage->_dbh->{AutoCommit}, 'DBD not in txn yet');
58
59 $schema->storage->dbh->do(join "\n",
60 $c_begin ? '-- comment' : (),
61 'BEGIN TRANSACTION'
62 );
63 ok ($schema->storage->connected, 'Still connected');
64 {
ab0b0a09 65 local $TODO = $lit_txn_todo if $c_begin;
2aeb3c7f 66 ok (! $schema->storage->_dbh->{AutoCommit}, "DBD aware of txn begin with comments on $prefix_comment");
67 }
68
69 $schema->storage->dbh->do(join "\n",
70 $c_commit ? '-- comment' : (),
71 'COMMIT'
72 );
73 ok ($schema->storage->connected, 'Still connected');
74 {
ab0b0a09 75 local $TODO = $lit_txn_todo if $c_commit and ! $c_begin;
2aeb3c7f 76 ok ($schema->storage->_dbh->{AutoCommit}, "DBD aware txn ended with comments on $prefix_comment");
77 }
78
79 is ($ars->count, 1, 'Inserted artists still there');
80
81 {
82 # this never worked in the 1st place
ab0b0a09 83 local $TODO = $lit_txn_todo if ! $c_begin and $c_commit;
2aeb3c7f 84
08615cfb 85 # odd argument passing, because such nested crefs leak on 5.8
2aeb3c7f 86 lives_ok {
87 $schema->storage->txn_do (sub {
08615cfb 88 ok ($_[0]->find({ name => $_[1] }), "Artist still where we left it after cycle with comments on $prefix_comment");
89 }, $ars, $artist->name );
2aeb3c7f 90 } "Succesfull transaction with comments on $prefix_comment";
91 }
92}
93
398215b1 94# test blank begin/svp/commit/begin cycle
95warnings_are {
96 my $schema = DBICTest->init_schema( no_populate => 1 );
97 my $rs = $schema->resultset('Artist');
98 is ($rs->count, 0, 'Start with empty table');
99
100 for my $do_commit (1, 0) {
101 $schema->txn_begin;
102 $schema->svp_begin;
103 $schema->svp_rollback;
104
105 $schema->svp_begin;
106 $schema->svp_rollback;
107
108 $schema->svp_release;
109
110 $schema->svp_begin;
111
112 $schema->txn_rollback;
113
114 $schema->txn_begin;
115 $schema->svp_begin;
116 $schema->svp_rollback;
117
118 $schema->svp_begin;
119 $schema->svp_rollback;
120
121 $schema->svp_release;
122
123 $schema->svp_begin;
124
125 $do_commit ? $schema->txn_commit : $schema->txn_rollback;
126
127 is_deeply $schema->storage->savepoints, [], 'Savepoint names cleared away'
128 }
129
130 $schema->txn_do(sub {
131 ok (1, 'all seems fine');
132 });
133} [], 'No warnings emitted';
2aeb3c7f 134
67b35a45 135my $schema = DBICTest->init_schema();
86a51471 136
632d1e0f 137# make sure the side-effects of RT#67581 do not result in data loss
138my $row;
67b35a45 139warnings_exist { $row = $schema->resultset('Artist')->create ({ name => 'alpha rank', rank => 'abc' }) }
445bc0cd 140 [qr/Non-integer value supplied for column 'rank' despite the integer datatype/],
632d1e0f 141 'proper warning on string insertion into an numeric column'
142;
143$row->discard_changes;
144is ($row->rank, 'abc', 'proper rank inserted into database');
145
67b35a45 146# and make sure we do not lose actual bigints
147{
148 package DBICTest::BigIntArtist;
149 use base 'DBICTest::Schema::Artist';
150 __PACKAGE__->table('artist');
151 __PACKAGE__->add_column(bigint => { data_type => 'bigint' });
152}
153$schema->register_class(BigIntArtist => 'DBICTest::BigIntArtist');
154$schema->storage->dbh_do(sub {
155 $_[1]->do('ALTER TABLE artist ADD COLUMN bigint BIGINT');
156});
157
f3b1224b 158my $sqlite_broken_bigint = (
159 modver_gt_or_eq('DBD::SQLite', '1.34') and ! modver_gt_or_eq('DBD::SQLite', '1.37')
160);
161
162# 63 bit integer
163my $many_bits = (Math::BigInt->new(2) ** 62);
164
67b35a45 165# test upper/lower boundaries for sqlite and some values inbetween
166# range is -(2**63) .. 2**63 - 1
f3b1224b 167#
168# Not testing -0 - it seems to overflow to ~0 on some combinations,
169# thus not triggering the >32 bit guards
170# interesting read: https://en.wikipedia.org/wiki/Signed_zero#Representations
04ab4eb1 171for my $bi ( qw(
f3b1224b 172 -2
173 -1
174 0
175 +0
176 1
177 2
178
04ab4eb1 179 -9223372036854775808
180 -9223372036854775807
181 -8694837494948124658
182 -6848440844435891639
183 -5664812265578554454
184 -5380388020020483213
185 -2564279463598428141
186 2442753333597784273
187 4790993557925631491
188 6773854980030157393
189 7627910776496326154
190 8297530189347439311
191 9223372036854775806
192 9223372036854775807
193
194 4294967295
195 4294967296
196
197 -4294967296
198 -4294967295
199 -4294967294
200
201 -2147483649
202 -2147483648
203 -2147483647
204 -2147483646
205
206 2147483646
207 2147483647
208),
209 # these values cause exceptions even with all workarounds in place on these
210 # fucked DBD::SQLite versions *regardless* of ivsize >.<
f3b1224b 211 $sqlite_broken_bigint
04ab4eb1 212 ? ()
213 : ( '2147483648', '2147483649' )
214) {
215 # unsigned 32 bit ints have a range of −2,147,483,648 to 2,147,483,647
216 # alternatively expressed as the hexadecimal numbers below
217 # the comparison math will come out right regardless of ivsize, since
218 # we are operating within 31 bits
219 # P.S. 31 because one bit is lost for the sign
220 my $v_bits = ($bi > 0x7fff_ffff || $bi < -0x8000_0000) ? 64 : 32;
221
222 my $v_desc = sprintf '%s (%d bit signed int)', $bi, $v_bits;
223
1363f0f5 224 my @w;
113322e5 225 local $SIG{__WARN__} = sub {
226 if ($_[0] =~ /datatype mismatch/) {
227 push @w, @_;
228 }
229 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/ ) {
230 # do nothing, this warning will pop up here and there depending on
231 # DBD/bitness combination
232 # we don't want to test for it explicitly, we are just interested
233 # in the results matching at the end
234 }
235 else {
236 warn @_;
237 }
238 };
1363f0f5 239
f3b1224b 240 # some combinations of SQLite 1.35 and older 5.8 faimly is wonky
241 # instead of a warning we get a full exception. Sod it
242 eval {
2e092442 243 $row = $schema->resultset('BigIntArtist')->create({ bigint => $bi });
f3b1224b 244 } or do {
245 fail("Exception on inserting $v_desc") unless $sqlite_broken_bigint;
246 next;
247 };
04ab4eb1 248
249 # explicitly using eq, to make sure we did not nummify the argument
250 # which can be an issue on 32 bit ivsize
251 cmp_ok ($row->bigint, 'eq', $bi, "value in object correct ($v_desc)");
252
253 $row->discard_changes;
254
255 cmp_ok (
256 $row->bigint,
257
258 # the test will not pass an == if we are running under 32 bit ivsize
259 # use 'eq' on the numified (and possibly "scientificied") returned value
f3b1224b 260 (DBIx::Class::_ENV_::IV_SIZE < 8 and $v_bits > 32) ? 'eq' : '==',
04ab4eb1 261
262 # in 1.37 DBD::SQLite switched to proper losless representation of bigints
263 # regardless of ivize
264 # before this use 'eq' (from above) on the numified (and possibly
265 # "scientificied") returned value
266 (DBIx::Class::_ENV_::IV_SIZE < 8 and ! modver_gt_or_eq('DBD::SQLite', '1.37')) ? $bi+0 : $bi,
267
268 "value in database correct ($v_desc)"
269 );
1363f0f5 270
f3b1224b 271# FIXME - temporary smoke-only escape
272SKIP: {
273 skip 'Potential for false negatives - investigation pending', 1
274 if DBICTest::RunMode->is_plain;
275
276 # check if math works
277 # start by adding/subtracting a 50 bit integer, and then divide by 2 for good measure
278 my ($sqlop, $expect) = $bi < 0
279 ? ( '(bigint + ? )', ($bi + $many_bits) )
280 : ( '(bigint - ? )', ($bi - $many_bits) )
281 ;
282
283 $expect = ($expect + ($expect % 2)) / 2;
284
285 # read https://en.wikipedia.org/wiki/Modulo_operation#Common_pitfalls
286 # and check the tables on the right side of the article for an
287 # enlightening journey on why a mere bigint % 2 won't work
288 $sqlop = "( $sqlop + ( ((bigint % 2)+2)%2 ) ) / 2";
289
290 for my $dtype (undef, \'int', \'bigint') {
291
292 # FIXME - the double-load should not be needed
293 # will fix in the future
294 $row->update({ bigint => $bi });
295 $row->discard_changes;
296 $row->update({ bigint => \[ $sqlop, [ $dtype => $many_bits ] ] });
297 $row->discard_changes;
298
299 # can't use cmp_ok - will not engage the M::BI overload of $many_bits
300 ok (
301 $row->bigint
302
303 ==
304
305 (DBIx::Class::_ENV_::IV_SIZE < 8 and ! modver_gt_or_eq('DBD::SQLite', '1.37')) ? $expect->bstr + 0 : $expect
306 , "simple integer math with@{[ $dtype ? '' : 'out' ]} bindtype in database correct (base $v_desc)")
307 or diag sprintf '%s != %s', $row->bigint, $expect;
308 }
309# end of fixme
310}
311
312 is_deeply (\@w, [], "No mismatch warnings on bigint operations ($v_desc)" );
67b35a45 313}
314
86a51471 315done_testing;
316
317# vim:sts=2 sw=2: