7 use Scalar::Util 'weaken';
13 my $db_orig = DBICTest->_sqlite_dbfilename;
14 my $db_tmp = "$db_orig.tmp";
16 # Set up the "usual" sqlite for DBICTest
17 my $schema = DBICTest->init_schema( sqlite_use_file => 1 );
19 my $exception_action_count;
20 $schema->exception_action(sub {
21 $exception_action_count++;
25 # Make sure we're connected by doing something
26 my @art = $schema->resultset("Artist")->search({ }, { order_by => { -desc => 'name' }});
27 cmp_ok(@art, '==', 3, "Three artists returned");
29 # Disconnect the dbh, and be sneaky about it
30 # Also test if DBD::SQLite finaly knows how to ->disconnect properly
33 local $SIG{__WARN__} = sub { $w = shift };
34 $schema->storage->_dbh->disconnect;
35 ok ($w !~ /active statement handles/, 'SQLite can disconnect properly');
38 # Try the operation again - What should happen here is:
39 # 1. S::DBI blindly attempts the SELECT, which throws an exception
40 # 2. It catches the exception, checks ->{Active}/->ping, sees the disconnected state...
41 # 3. Reconnects, and retries the operation
43 my @art_two = $schema->resultset("Artist")->search({ }, { order_by => { -desc => 'name' }});
44 cmp_ok(@art_two, '==', 3, "Three artists returned");
46 ### Now, disconnect the dbh, and move the db file;
47 # create a new one full of garbage, prevent SQLite from connecting.
48 $schema->storage->_dbh->disconnect;
49 move( $db_orig, $db_tmp )
50 or die "failed to move $db_orig to $db_tmp: $!";
51 open my $db_file, '>', $db_orig;
52 print $db_file 'THIS IS NOT A REAL DATABASE';
55 ### Try the operation again... it should fail, since there's no valid db
57 # Catch the DBI connection error
58 local $SIG{__WARN__} = sub {};
60 $schema->resultset("Artist")->create({ name => 'not gonna happen' });
61 } qr/not a database/, 'The operation failed';
64 ok (! $schema->storage->connected, 'We are not connected' );
66 ### Now, move the db file back to the correct name
67 unlink($db_orig) or die "could not delete $db_orig: $!";
68 move( $db_tmp, $db_orig )
69 or die "could not move $db_tmp to $db_orig: $!";
71 ### Try the operation again... this time, it should succeed
74 @art_four = $schema->resultset("Artist")->search( {}, { order_by => { -desc => 'name' } } );
75 } 'The operation succeeded';
76 cmp_ok( @art_four, '==', 3, "Three artists returned" );
78 # check that reconnection contexts are preserved in txn_do / dbh_do
84 invoke => sub { shift->(); 1 },
88 invoke => sub { my $foo = shift->() },
92 invoke => sub { my @foo = shift->() },
97 for my $ctx (keys %$ctx_map) {
99 # start disconnected and then connected
100 $schema->storage->disconnect;
101 $exception_action_count = 0;
106 $ctx_map->{$ctx}{invoke}->(sub { $schema->txn_do(sub {
107 is_deeply (\@_, $args, 'Args propagated correctly' );
109 is (wantarray(), $ctx_map->{$ctx}{wa}, "Correct $ctx context");
111 # this will cause a retry
112 $schema->storage->_dbh->disconnect unless $disarmed++;
114 isa_ok ($schema->resultset('Artist')->next, 'DBICTest::Artist');
118 is( $exception_action_count, 0, 'exception_action never called' );
121 # make sure RT#110429 does not recur on manual DBI-side disconnect
126 my $g = $schema->txn_scope_guard;
128 is( $schema->storage->transaction_depth, 1, "Expected txn depth" );
130 $schema->storage->_dbh->disconnect;
132 $schema->storage->dbh_do(sub { $_[1]->do('SELECT 1') } );
136 $schema->txn_do(sub {
137 $schema->storage->_dbh->disconnect
142 $schema->txn_do(sub {
143 $schema->storage->disconnect;
149 note( "Testing with " . B::Deparse->new->coderef2text($cref) );
151 $schema->storage->disconnect;
152 $exception_action_count = 0;
154 ok( !$schema->storage->connected, 'Not connected' );
156 is( $schema->storage->transaction_depth, undef, "Start with unknown txn depth" );
158 # messages vary depending on version and whether txn or do, whatever
161 } 'Threw *something*';
163 ok( !$schema->storage->connected, 'Not connected as a result of failed rollback' );
165 is( $schema->storage->transaction_depth, undef, "Depth expectedly unknown after failed rollbacks" );
167 is( $exception_action_count, 1, "exception_action called only once" );
170 # check exception_action under tenacious disconnect
172 $schema->storage->disconnect;
173 $exception_action_count = 0;
175 throws_ok { $schema->txn_do(sub {
176 $schema->storage->_dbh->disconnect;
178 $schema->resultset('Artist')->next;
179 })} qr/prepare on inactive database handle/;
181 is( $exception_action_count, 1, "exception_action called only once" );
184 # check that things aren't crazy with a non-violent disconnect
186 my $schema = DBICTest->init_schema( sqlite_use_file => 0, no_deploy => 1 );
187 weaken( my $ws = $schema );
189 $schema->is_executed_sql_bind( sub {
190 $ws->txn_do(sub { $ws->storage->disconnect } );
191 }, [ [ 'BEGIN' ] ], 'Only one BEGIN statement' );
193 $schema->is_executed_sql_bind( sub {
194 my $g = $ws->txn_scope_guard;
195 $ws->storage->disconnect;
196 }, [ [ 'BEGIN' ] ], 'Only one BEGIN statement' );