File outputFile = File.createTempFile("recovery",".sql");
Database db = new Database("failure_recovery_test");
db.createSchemaVersionTable();
DbDeploy dbDeploy = new DbDeploy();
db.applyDatabaseSettingsTo(dbDeploy);
dbDeploy.setScriptdirectory(findScriptDirectory("src/it/db/invalid_deltas"));
dbDeploy.setOutputfile(outputFile);
dbDeploy.go();
try {
db.applyScript(outputFile);
} catch (SQLException ex) {
//expected
assertThat(ex.getMessage(), startsWith("Column count does not match in statement"));
}
// script 2 failed, so it should not be considered applied to the database
assertThat(db.getChangelogEntries(), hasItems(1L));
assertThat(db.getChangelogEntries(), not(hasItems(2L)));
List<Object[]> results = db.executeQuery("select id from Test");
assertThat(results.size(), is(0));
// now run dbdeploy again with valid scripts, should recover
dbDeploy.setScriptdirectory(findScriptDirectory("src/it/db/deltas"));
dbDeploy.setOutputfile(outputFile);
dbDeploy.go();
db.applyScript(outputFile);
assertThat(db.getChangelogEntries(), hasItems(1L, 2L));