use of org.flywaydb.core.api.resolver.MigrationExecutor in project flyway by flyway.
the class MyCustomMigrationResolver method resolveMigrations.
@Override
public List<ResolvedMigration> resolveMigrations() {
List<ResolvedMigration> resolvedMigrations = new ArrayList<ResolvedMigration>();
resolvedMigrations.add(new ResolvedMigration() {
@Override
public MigrationVersion getVersion() {
return MigrationVersion.fromVersion("1.9");
}
@Override
public String getDescription() {
return "Virtual Migration";
}
@Override
public String getScript() {
return "VirtualScript 1.9";
}
@Override
public Integer getChecksum() {
return 19;
}
@Override
public MigrationType getType() {
return MigrationType.CUSTOM;
}
@Override
public String getPhysicalLocation() {
return "virtual://loaction";
}
@Override
public MigrationExecutor getExecutor() {
return new MigrationExecutor() {
@Override
public void execute(Connection connection) {
System.out.println("Executed !");
}
@Override
public boolean executeInTransaction() {
return true;
}
};
}
});
return resolvedMigrations;
}
use of org.flywaydb.core.api.resolver.MigrationExecutor in project flyway by flyway.
the class DbMigrate method applyMigration.
/**
* Applies this migration to the database. The migration state and the execution time are updated accordingly.
*
* @param migration The migration to apply.
* @param isOutOfOrder If this migration is being applied out of order.
* @return The result of the migration.
*/
private Boolean applyMigration(final MigrationInfoImpl migration, boolean isOutOfOrder) {
MigrationVersion version = migration.getVersion();
final MigrationExecutor migrationExecutor = migration.getResolvedMigration().getExecutor();
final String migrationText;
if (version != null) {
migrationText = "schema " + schema + " to version " + version + " - " + migration.getDescription() + (isOutOfOrder ? " [out of order]" : "") + (migrationExecutor.executeInTransaction() ? "" : " [non-transactional]");
} else {
migrationText = "schema " + schema + " with repeatable migration " + migration.getDescription() + (migrationExecutor.executeInTransaction() ? "" : " [non-transactional]");
}
LOG.info("Migrating " + migrationText);
StopWatch stopWatch = new StopWatch();
stopWatch.start();
try {
if (migrationExecutor.executeInTransaction()) {
new TransactionTemplate(connectionUserObjects).execute(new Callable<Object>() {
@Override
public Object call() throws SQLException {
doMigrate(migration, migrationExecutor, migrationText);
return null;
}
});
} else {
try {
doMigrate(migration, migrationExecutor, migrationText);
} catch (SQLException e) {
throw new FlywaySqlException("Unable to apply migration", e);
}
}
} catch (FlywayException e) {
String failedMsg = "Migration of " + migrationText + " failed!";
if (dbSupport.supportsDdlTransactions() && migrationExecutor.executeInTransaction()) {
LOG.error(failedMsg + " Changes successfully rolled back.");
} else {
LOG.error(failedMsg + " Please restore backups and roll back database and code!");
stopWatch.stop();
int executionTime = (int) stopWatch.getTotalTimeMillis();
AppliedMigration appliedMigration = new AppliedMigration(version, migration.getDescription(), migration.getType(), migration.getScript(), migration.getResolvedMigration().getChecksum(), executionTime, false);
metaDataTable.addAppliedMigration(appliedMigration);
}
throw e;
}
stopWatch.stop();
int executionTime = (int) stopWatch.getTotalTimeMillis();
AppliedMigration appliedMigration = new AppliedMigration(version, migration.getDescription(), migration.getType(), migration.getScript(), migration.getResolvedMigration().getChecksum(), executionTime, true);
metaDataTable.addAppliedMigration(appliedMigration);
return false;
}
use of org.flywaydb.core.api.resolver.MigrationExecutor in project flyway by flyway.
the class SampleResolver method resolveMigrations.
@Override
public Collection<ResolvedMigration> resolveMigrations() {
return Arrays.asList((ResolvedMigration) new ResolvedMigration() {
@Override
public MigrationVersion getVersion() {
return MigrationVersion.fromVersion("2.0");
}
@Override
public String getDescription() {
return "Custom Resolved";
}
@Override
public String getScript() {
return "R1";
}
@Override
public Integer getChecksum() {
return 20;
}
@Override
public MigrationType getType() {
return MigrationType.CUSTOM;
}
@Override
public String getPhysicalLocation() {
return null;
}
@Override
public MigrationExecutor getExecutor() {
return new MigrationExecutor() {
@Override
public void execute(Connection connection) throws SQLException {
Statement statement = null;
try {
statement = connection.createStatement();
statement.execute("INSERT INTO test_user (name) VALUES ('Resolvix')");
} finally {
JdbcUtils.closeStatement(statement);
}
}
@Override
public boolean executeInTransaction() {
return true;
}
};
}
});
}
use of org.flywaydb.core.api.resolver.MigrationExecutor in project killbill by killbill.
the class DbMigrateWithDryRun method applyMigration.
private void applyMigration(final int installedRnk, final MigrationInfoImpl migration) {
final MigrationVersion version = migration.getVersion();
final String migrationText;
if (version != null) {
migrationText = "schema " + schema + " to version " + version + " - " + migration.getDescription();
} else {
migrationText = "schema " + schema + " with repeatable migration " + migration.getDescription();
}
LOG.info("Migrating " + migrationText);
// PIERRE: override the executor to capture the SQL
final FileSystemResource sqlScriptResource = new FileSystemResource(migration.getResolvedMigration().getPhysicalLocation());
final MigrationExecutor migrationExecutor = new CapturingSqlMigrationExecutor(sqlStatements, dbSupport, sqlScriptResource, placeholderReplacer, encoding);
try {
doMigrate(migration, migrationExecutor, migrationText);
} catch (final SQLException e) {
throw new FlywayException("Unable to apply migration", e);
}
final AppliedMigration appliedMigration = new AppliedMigration(installedRnk, version, migration.getDescription(), migration.getType(), migration.getScript(), migration.getResolvedMigration().getChecksum(), null, null, -1, true);
metaDataTableForDryRun.addAppliedMigration(appliedMigration);
}
Aggregations