12 my $db_orig = "$FindBin::Bin/var/DBIxClass.db";
13 my $db_tmp = "$db_orig.tmp";
15 # Set up the "usual" sqlite for DBICTest
16 my $schema = DBICTest->init_schema( sqlite_use_file => 1 );
18 # Make sure we're connected by doing something
19 my @art = $schema->resultset("Artist")->search({ }, { order_by => 'name DESC'});
20 cmp_ok(@art, '==', 3, "Three artists returned");
22 # Disconnect the dbh, and be sneaky about it
23 # Also test if DBD::SQLite finaly knows how to ->disconnect properly
25 local $TODO = 'SQLite is evil/braindead. Once this test starts passing, remove the related atrocity from DBIx::Class::Storage::DBI::SQLite';
27 local $SIG{__WARN__} = sub { $w = shift };
28 $schema->storage->_dbh->disconnect;
29 ok ($w !~ /active statement handles/, 'SQLite can disconnect properly \o/');
32 # Try the operation again - What should happen here is:
33 # 1. S::DBI blindly attempts the SELECT, which throws an exception
34 # 2. It catches the exception, checks ->{Active}/->ping, sees the disconnected state...
35 # 3. Reconnects, and retries the operation
37 my @art_two = $schema->resultset("Artist")->search({ }, { order_by => 'name DESC'});
38 cmp_ok(@art_two, '==', 3, "Three artists returned");
40 ### Now, disconnect the dbh, and move the db file;
41 # create a new one and chmod 000 to prevent SQLite from connecting.
42 $schema->storage->_dbh->disconnect;
43 move( $db_orig, $db_tmp );
44 open DBFILE, '>', $db_orig;
45 print DBFILE 'THIS IS NOT A REAL DATABASE';
49 ### Try the operation again... it should fail, since there's no db
51 # Catch the DBI connection error
52 local $SIG{__WARN__} = sub {};
54 my @art_three = $schema->resultset("Artist")->search( {}, { order_by => 'name DESC' } );
56 ok( $@, 'The operation failed' );
59 ### Now, move the db file back to the correct name
61 move( $db_tmp, $db_orig );
64 skip "Cannot reconnect if original connection didn't fail", 2
65 if ( $@ =~ /encrypted or is not a database/ );
67 ### Try the operation again... this time, it should succeed
70 @art_four = $schema->resultset("Artist")->search( {}, { order_by => 'name DESC' } );
72 ok( !$@, 'The operation succeeded' );
73 cmp_ok( @art_four, '==', 3, "Three artists returned" );