Flyway
It looks like the Flyway database migration tool offers the functionality that I was looking for - at least if you feel like hacking :) Since I required the feature just for integration testing anyway it is good enough for me. Let me share my code.
Add Flyway and Oracle driver dependency
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
<version>5.1.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc7</artifactId>
<version>12.1.0.2</version>
<scope>provided</scope>
</dependency>
Main class to execute scripts and cleanup db
public class ExecuteScriptsAgainstOracle {
public static void main(String args[]) throws Exception, SQLException {
Flyway flyway = new Flyway();
flyway.setDataSource("jdbc:oracle:thin:@xxx:1521:yyy", "zzz", "zzz");
flyway.setTable("auto_schema_setup_flyway");
Database<?> database = DatabaseFactory.createDatabase(flyway.getConfiguration(), false);
// clean the database (remove all objects of the schema)
flyway.clean();
flyway.setSkipDefaultResolvers(true);
flyway.setResolvers(new MyMigrationResolver(database, flyway.getConfiguration()));
flyway.migrate();
}
}
The MigrationResolver that lists the to be executed files
package org.flywaydb.core.internal.resolver.sql;
public class MyMigrationResolver implements MigrationResolver {
private Database<?> database;
private Configuration configuration;
private int order = 0;
public MyMigrationResolver(Database<?> database, Configuration configuration) {
this.database = database;
this.configuration = configuration;
}
@Override
public Collection<ResolvedMigration> resolveMigrations() {
Collection<ResolvedMigration> scripts = new LinkedList<ResolvedMigration>();
scripts.add(script(folder1\\somescript.dml"));
scripts.add(script("folder2\\someOtherScript.sql"));
return scripts;
}
private ResolvedMigrationImpl script(String scriptName) {
String baseFolder = "target\\scripts\\";
order++;
ResolvedMigrationImpl migration = new ResolvedMigrationImpl();
migration.setScript(baseFolder+scriptName);
migration.setType(MigrationType.SQL);
migration.setDescription(""+String.format("%03d",order)+" "+scriptName);
migration.setExecutor(new SqlMigrationExecutor(database,
new FileSystemResource(migration.getScript(), configuration.getEncoding()),
new PlaceholderReplacer() {
@Override
public String replacePlaceholders(String input) {
// just remove parts of the sql that flyway can't deal with
input = StringUtils.replace(input, "WHENEVER SQLERROR EXIT SQL.SQLCODE ROLLBACK;", "");
input = StringUtils.replace(input, "SET DEFINE OFF;", "");
return input;
}
@Override
public Map<String, String> getPlaceholderReplacements() {
return null;
}
}
, configuration));
return migration;
}
}