11 my $db_orig = DBICTest->_sqlite_dbfilename;
12 my $db_tmp = "$db_orig.tmp";
14 # Set up the "usual" sqlite for DBICTest
15 my $schema = DBICTest->init_schema( sqlite_use_file => 1 );
17 # Make sure we're connected by doing something
18 my @art = $schema->resultset("Artist")->search({ }, { order_by => { -desc => 'name' }});
19 cmp_ok(@art, '==', 3, "Three artists returned");
21 # Disconnect the dbh, and be sneaky about it
22 # Also test if DBD::SQLite finaly knows how to ->disconnect properly
25 local $SIG{__WARN__} = sub { $w = shift };
26 $schema->storage->_dbh->disconnect;
27 ok ($w !~ /active statement handles/, 'SQLite can disconnect properly');
30 # Try the operation again - What should happen here is:
31 # 1. S::DBI blindly attempts the SELECT, which throws an exception
32 # 2. It catches the exception, checks ->{Active}/->ping, sees the disconnected state...
33 # 3. Reconnects, and retries the operation
35 my @art_two = $schema->resultset("Artist")->search({ }, { order_by => { -desc => 'name' }});
36 cmp_ok(@art_two, '==', 3, "Three artists returned");
38 ### Now, disconnect the dbh, and move the db file;
39 # create a new one full of garbage, prevent SQLite from connecting.
40 $schema->storage->_dbh->disconnect;
41 move( $db_orig, $db_tmp )
42 or die "failed to move $db_orig to $db_tmp: $!";
43 open my $db_file, '>', $db_orig;
44 print $db_file 'THIS IS NOT A REAL DATABASE';
47 ### Try the operation again... it should fail, since there's no valid db
49 # Catch the DBI connection error
50 local $SIG{__WARN__} = sub {};
52 my @art_three = $schema->resultset("Artist")->search( {}, { order_by => { -desc => 'name' } } );
53 } qr/not a database/, 'The operation failed';
56 ok (! $schema->storage->connected, 'We are not connected' );
58 ### Now, move the db file back to the correct name
59 unlink($db_orig) or die "could not delete $db_orig: $!";
60 move( $db_tmp, $db_orig )
61 or die "could not move $db_tmp to $db_orig: $!";
63 ### Try the operation again... this time, it should succeed
66 @art_four = $schema->resultset("Artist")->search( {}, { order_by => { -desc => 'name' } } );
67 } 'The operation succeeded';
68 cmp_ok( @art_four, '==', 3, "Three artists returned" );
70 # check that reconnection contexts are preserved in txn_do / dbh_do
76 invoke => sub { shift->(); 1 },
80 invoke => sub { my $foo = shift->() },
84 invoke => sub { my @foo = shift->() },
89 for my $ctx (keys %$ctx_map) {
91 # start disconnected and then connected
92 $schema->storage->disconnect;
96 $ctx_map->{$ctx}{invoke}->(sub { $schema->txn_do(sub {
97 is_deeply (\@_, $args, 'Args propagated correctly' );
99 is (wantarray(), $ctx_map->{$ctx}{wa}, "Correct $ctx context");
101 # this will cause a retry
102 $schema->storage->_dbh->disconnect unless $disarmed++;
104 isa_ok ($schema->resultset('Artist')->next, 'DBICTest::Artist');