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;
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 $schema->storage->_dbh->disconnect;
25 # Try the operation again - What should happen here is:
26 # 1. S::DBI blindly attempts the SELECT, which throws an exception
27 # 2. It catches the exception, checks ->{Active}/->ping, sees the disconnected state...
28 # 3. Reconnects, and retries the operation
30 my @art_two = $schema->resultset("Artist")->search({ }, { order_by => 'name DESC'});
31 cmp_ok(@art_two, '==', 3, "Three artists returned");
33 ### Now, disconnect the dbh, and move the db file;
34 # create a new one and chmod 000 to prevent SQLite from connecting.
35 $schema->storage->_dbh->disconnect;
36 move( $db_orig, $db_tmp );
37 open DBFILE, '>', $db_orig;
38 print DBFILE 'THIS IS NOT A REAL DATABASE';
42 ### Try the operation again... it should fail, since there's no db
44 my @art_three = $schema->resultset("Artist")->search( {}, { order_by => 'name DESC' } );
46 ok( $@, 'The operation failed' );
48 ### Now, move the db file back to the correct name
50 move( $db_tmp, $db_orig );
53 skip "Cannot reconnect if original connection didn't fail", 2
54 if ( $@ =~ /encrypted or is not a database/ );
56 ### Try the operation again... this time, it should succeed
59 @art_four = $schema->resultset("Artist")->search( {}, { order_by => 'name DESC' } );
61 ok( !$@, 'The operation succeeded' );
62 cmp_ok( @art_four, '==', 3, "Three artists returned" );