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