Really fix the DBD::SQLite ping() issue
[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';
67b35a45 8use Config;
9
86a51471 10use lib qw(t/lib);
11use DBICTest;
12
67b35a45 13# savepoints test
14{
15 my $schema = DBICTest->init_schema(auto_savepoint => 1);
86a51471 16
67b35a45 17 my $ars = $schema->resultset('Artist');
86a51471 18
67b35a45 19 # test two-phase commit and inner transaction rollback from nested transactions
86a51471 20 $schema->txn_do(sub {
67b35a45 21 $ars->create({ name => 'in_outer_transaction' });
86a51471 22 $schema->txn_do(sub {
67b35a45 23 $ars->create({ name => 'in_inner_transaction' });
86a51471 24 });
67b35a45 25 ok($ars->search({ name => 'in_inner_transaction' })->first,
26 'commit from inner transaction visible in outer transaction');
27 throws_ok {
28 $schema->txn_do(sub {
29 $ars->create({ name => 'in_inner_transaction_rolling_back' });
30 die 'rolling back inner transaction';
31 });
32 } qr/rolling back inner transaction/, 'inner transaction rollback executed';
33 $ars->create({ name => 'in_outer_transaction2' });
34 });
35
36 ok($ars->search({ name => 'in_outer_transaction' })->first,
37 'commit from outer transaction');
38 ok($ars->search({ name => 'in_outer_transaction2' })->first,
39 'second commit from outer transaction');
40 ok($ars->search({ name => 'in_inner_transaction' })->first,
41 'commit from inner transaction');
42 is $ars->search({ name => 'in_inner_transaction_rolling_back' })->first,
43 undef,
44 'rollback from inner transaction';
45}
632d1e0f 46
2aeb3c7f 47# check that we work somewhat OK with braindead SQLite transaction handling
48#
49# As per https://metacpan.org/source/ADAMK/DBD-SQLite-1.37/lib/DBD/SQLite.pm#L921
50# SQLite does *not* try to synchronize
51
52for my $prefix_comment (qw/Begin_only Commit_only Begin_and_Commit/) {
53 note "Testing with comment prefixes on $prefix_comment";
54
55 # FIXME warning won't help us for the time being
56 # perhaps when (if ever) DBD::SQLite gets fixed,
57 # we can do something extra here
58 local $SIG{__WARN__} = sub { warn @_ if $_[0] !~ /Internal transaction state .+? does not seem to match/ }
59 unless $ENV{TEST_VERBOSE};
60
61 my ($c_begin, $c_commit) = map { $prefix_comment =~ $_ ? 1 : 0 } (qr/Begin/, qr/Commit/);
62
63 my $schema = DBICTest->init_schema( no_deploy => 1 );
64 my $ars = $schema->resultset('Artist');
65
66 ok (! $schema->storage->connected, 'No connection yet');
67
68 $schema->storage->dbh->do(<<'DDL');
69CREATE TABLE artist (
70 artistid INTEGER PRIMARY KEY NOT NULL,
71 name varchar(100),
72 rank integer DEFAULT 13,
73 charfield char(10) NULL
74);
75DDL
76
77 my $artist = $ars->create({ name => 'Artist_' . time() });
78 is ($ars->count, 1, 'Inserted artist ' . $artist->name);
79
80 ok ($schema->storage->connected, 'Connected');
81 ok ($schema->storage->_dbh->{AutoCommit}, 'DBD not in txn yet');
82
83 $schema->storage->dbh->do(join "\n",
84 $c_begin ? '-- comment' : (),
85 'BEGIN TRANSACTION'
86 );
87 ok ($schema->storage->connected, 'Still connected');
88 {
89 local $TODO = 'SQLite is retarded wrt detecting BEGIN' if $c_begin;
90 ok (! $schema->storage->_dbh->{AutoCommit}, "DBD aware of txn begin with comments on $prefix_comment");
91 }
92
93 $schema->storage->dbh->do(join "\n",
94 $c_commit ? '-- comment' : (),
95 'COMMIT'
96 );
97 ok ($schema->storage->connected, 'Still connected');
98 {
99 local $TODO = 'SQLite is retarded wrt detecting COMMIT' if $c_commit;
100 ok ($schema->storage->_dbh->{AutoCommit}, "DBD aware txn ended with comments on $prefix_comment");
101 }
102
103 is ($ars->count, 1, 'Inserted artists still there');
104
105 {
106 # this never worked in the 1st place
107 local $TODO = 'SQLite is retarded wrt detecting COMMIT' if ! $c_begin and $c_commit;
108
109 lives_ok {
110 $schema->storage->txn_do (sub {
111 ok ($ars->find({ name => $artist->name }), "Artist still where we left it after cycle with comments on $prefix_comment");
112 });
113 } "Succesfull transaction with comments on $prefix_comment";
114 }
115}
116
117
67b35a45 118my $schema = DBICTest->init_schema();
86a51471 119
632d1e0f 120# make sure the side-effects of RT#67581 do not result in data loss
121my $row;
67b35a45 122warnings_exist { $row = $schema->resultset('Artist')->create ({ name => 'alpha rank', rank => 'abc' }) }
632d1e0f 123 [qr/Non-numeric value supplied for column 'rank' despite the numeric datatype/],
124 'proper warning on string insertion into an numeric column'
125;
126$row->discard_changes;
127is ($row->rank, 'abc', 'proper rank inserted into database');
128
67b35a45 129# and make sure we do not lose actual bigints
130{
131 package DBICTest::BigIntArtist;
132 use base 'DBICTest::Schema::Artist';
133 __PACKAGE__->table('artist');
134 __PACKAGE__->add_column(bigint => { data_type => 'bigint' });
135}
136$schema->register_class(BigIntArtist => 'DBICTest::BigIntArtist');
137$schema->storage->dbh_do(sub {
138 $_[1]->do('ALTER TABLE artist ADD COLUMN bigint BIGINT');
139});
140
141# test upper/lower boundaries for sqlite and some values inbetween
142# range is -(2**63) .. 2**63 - 1
2e092442 143SKIP: {
144 skip 'This perl does not seem to have 64bit int support - DBI roundtrip of large int will fail with DBD::SQLite < 1.37', 1
145 if ($Config{ivsize} < 8 and ! eval { DBD::SQLite->VERSION(1.37); 1 });
67b35a45 146
2e092442 147 for my $bi (qw/
148 -9223372036854775808
149 -9223372036854775807
150 -8694837494948124658
151 -6848440844435891639
152 -5664812265578554454
153 -5380388020020483213
154 -2564279463598428141
155 2442753333597784273
156 4790993557925631491
157 6773854980030157393
158 7627910776496326154
159 8297530189347439311
160 9223372036854775806
161 9223372036854775807
162 /) {
163 $row = $schema->resultset('BigIntArtist')->create({ bigint => $bi });
164 is ($row->bigint, $bi, "value in object correct ($bi)");
67b35a45 165
166 $row->discard_changes;
167 is ($row->bigint, $bi, "value in database correct ($bi)");
168 }
169}
170
86a51471 171done_testing;
172
173# vim:sts=2 sw=2: