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