use of org.flywaydb.core.internal.util.jdbc.TransactionTemplate in project flyway by flyway.
the class DbClean method dropSchema.
/**
* Drops this schema.
*
* @param schema The schema to drop.
* @throws FlywayException when the drop failed.
*/
private void dropSchema(final Schema schema) {
LOG.debug("Dropping schema " + schema + " ...");
StopWatch stopWatch = new StopWatch();
stopWatch.start();
new TransactionTemplate(connection).execute(new Callable<Object>() {
@Override
public Void call() {
schema.drop();
return null;
}
});
stopWatch.stop();
LOG.info(String.format("Successfully dropped schema %s (execution time %s)", schema, TimeFormat.format(stopWatch.getTotalTimeMillis())));
}
use of org.flywaydb.core.internal.util.jdbc.TransactionTemplate in project flyway by flyway.
the class DbClean method cleanSchema.
/**
* Cleans this schema of all objects.
*
* @param schema The schema to clean.
* @throws FlywayException when clean failed.
*/
private void cleanSchema(final Schema schema) {
LOG.debug("Cleaning schema " + schema + " ...");
StopWatch stopWatch = new StopWatch();
stopWatch.start();
new TransactionTemplate(connection).execute(new Callable<Object>() {
@Override
public Void call() {
schema.clean();
return null;
}
});
stopWatch.stop();
LOG.info(String.format("Successfully cleaned schema %s (execution time %s)", schema, TimeFormat.format(stopWatch.getTotalTimeMillis())));
}
use of org.flywaydb.core.internal.util.jdbc.TransactionTemplate in project flyway by flyway.
the class DbMigrate method migrate.
/**
* Starts the actual migration.
*
* @return The number of successfully applied migrations.
* @throws FlywayException when migration failed.
*/
public int migrate() throws FlywayException {
try {
for (final FlywayCallback callback : configuration.getCallbacks()) {
new TransactionTemplate(connectionUserObjects).execute(new Callable<Object>() {
@Override
public Object call() throws SQLException {
dbSupportUserObjects.changeCurrentSchemaTo(schema);
callback.beforeMigrate(connectionUserObjects);
return null;
}
});
}
StopWatch stopWatch = new StopWatch();
stopWatch.start();
int migrationSuccessCount = 0;
while (true) {
final boolean firstRun = migrationSuccessCount == 0;
boolean done = metaDataTable.lock(new Callable<Boolean>() {
@Override
public Boolean call() {
MigrationInfoServiceImpl infoService = new MigrationInfoServiceImpl(migrationResolver, metaDataTable, configuration.getTarget(), configuration.isOutOfOrder(), true, true, true);
infoService.refresh();
MigrationVersion currentSchemaVersion = MigrationVersion.EMPTY;
if (infoService.current() != null) {
currentSchemaVersion = infoService.current().getVersion();
}
if (firstRun) {
LOG.info("Current version of schema " + schema + ": " + currentSchemaVersion);
if (configuration.isOutOfOrder()) {
LOG.warn("outOfOrder mode is active. Migration of schema " + schema + " may not be reproducible.");
}
}
MigrationInfo[] future = infoService.future();
if (future.length > 0) {
MigrationInfo[] resolved = infoService.resolved();
if (resolved.length == 0) {
LOG.warn("Schema " + schema + " has version " + currentSchemaVersion + ", but no migration could be resolved in the configured locations !");
} else {
int offset = resolved.length - 1;
while (resolved[offset].getVersion() == null) {
// Skip repeatable migrations
offset--;
}
LOG.warn("Schema " + schema + " has a version (" + currentSchemaVersion + ") that is newer than the latest available migration (" + resolved[offset].getVersion() + ") !");
}
}
MigrationInfo[] failed = infoService.failed();
if (failed.length > 0) {
if ((failed.length == 1) && (failed[0].getState() == MigrationState.FUTURE_FAILED) && (configuration.isIgnoreFutureMigrations() || ignoreFailedFutureMigration)) {
LOG.warn("Schema " + schema + " contains a failed future migration to version " + failed[0].getVersion() + " !");
} else {
if (failed[0].getVersion() == null) {
throw new FlywayException("Schema " + schema + " contains a failed repeatable migration (" + failed[0].getDescription() + ") !");
}
throw new FlywayException("Schema " + schema + " contains a failed migration to version " + failed[0].getVersion() + " !");
}
}
MigrationInfoImpl[] pendingMigrations = infoService.pending();
if (pendingMigrations.length == 0) {
return true;
}
boolean isOutOfOrder = pendingMigrations[0].getVersion() != null && pendingMigrations[0].getVersion().compareTo(currentSchemaVersion) < 0;
return applyMigration(pendingMigrations[0], isOutOfOrder);
}
});
if (done) {
// No further migrations available
break;
}
migrationSuccessCount++;
}
stopWatch.stop();
logSummary(migrationSuccessCount, stopWatch.getTotalTimeMillis());
for (final FlywayCallback callback : configuration.getCallbacks()) {
new TransactionTemplate(connectionUserObjects).execute(new Callable<Object>() {
@Override
public Object call() throws SQLException {
dbSupportUserObjects.changeCurrentSchemaTo(schema);
callback.afterMigrate(connectionUserObjects);
return null;
}
});
}
return migrationSuccessCount;
} finally {
dbSupportUserObjects.restoreCurrentSchema();
}
}
use of org.flywaydb.core.internal.util.jdbc.TransactionTemplate in project flyway by flyway.
the class DbRepair method repair.
/**
* Repairs the metadata table.
*/
public void repair() {
try {
for (final FlywayCallback callback : callbacks) {
new TransactionTemplate(connection).execute(new Callable<Object>() {
@Override
public Object call() throws SQLException {
dbSupport.changeCurrentSchemaTo(schema);
callback.beforeRepair(connection);
return null;
}
});
}
StopWatch stopWatch = new StopWatch();
stopWatch.start();
new TransactionTemplate(connection).execute(new Callable<Object>() {
public Void call() {
dbSupport.changeCurrentSchemaTo(schema);
metaDataTable.removeFailedMigrations();
repairChecksumsAndDescriptions();
return null;
}
});
stopWatch.stop();
LOG.info("Successfully repaired metadata table " + metaDataTable + " (execution time " + TimeFormat.format(stopWatch.getTotalTimeMillis()) + ").");
if (!dbSupport.supportsDdlTransactions()) {
LOG.info("Manual cleanup of the remaining effects the failed migration may still be required.");
}
for (final FlywayCallback callback : callbacks) {
new TransactionTemplate(connection).execute(new Callable<Object>() {
@Override
public Object call() throws SQLException {
dbSupport.changeCurrentSchemaTo(schema);
callback.afterRepair(connection);
return null;
}
});
}
} finally {
dbSupport.restoreCurrentSchema();
}
}
use of org.flywaydb.core.internal.util.jdbc.TransactionTemplate in project flyway by flyway.
the class DbValidate method validate.
/**
* Starts the actual migration.
*
* @return The validation error, if any.
*/
public String validate() {
if (!schema.exists()) {
if (!migrationResolver.resolveMigrations().isEmpty() && !pending) {
return "Schema " + schema + " doesn't exist yet";
}
return null;
}
try {
for (final FlywayCallback callback : callbacks) {
new TransactionTemplate(connection).execute(new Callable<Object>() {
@Override
public Object call() throws SQLException {
dbSupport.changeCurrentSchemaTo(schema);
callback.beforeValidate(connection);
return null;
}
});
}
LOG.debug("Validating migrations ...");
StopWatch stopWatch = new StopWatch();
stopWatch.start();
Pair<Integer, String> result = new TransactionTemplate(connection).execute(new Callable<Pair<Integer, String>>() {
@Override
public Pair<Integer, String> call() {
dbSupport.changeCurrentSchemaTo(schema);
MigrationInfoServiceImpl migrationInfoService = new MigrationInfoServiceImpl(migrationResolver, metaDataTable, target, outOfOrder, pending, missing, future);
migrationInfoService.refresh();
int count = migrationInfoService.all().length;
String validationError = migrationInfoService.validate();
return Pair.of(count, validationError);
}
});
stopWatch.stop();
String error = result.getRight();
if (error == null) {
int count = result.getLeft();
if (count == 1) {
LOG.info(String.format("Successfully validated 1 migration (execution time %s)", TimeFormat.format(stopWatch.getTotalTimeMillis())));
} else {
LOG.info(String.format("Successfully validated %d migrations (execution time %s)", count, TimeFormat.format(stopWatch.getTotalTimeMillis())));
}
}
for (final FlywayCallback callback : callbacks) {
new TransactionTemplate(connection).execute(new Callable<Object>() {
@Override
public Object call() throws SQLException {
dbSupport.changeCurrentSchemaTo(schema);
callback.afterValidate(connection);
return null;
}
});
}
return error;
} finally {
dbSupport.restoreCurrentSchema();
}
}
Aggregations