use of org.flywaydb.core.internal.util.StopWatch 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.internal.util.StopWatch 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.StopWatch 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.StopWatch 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.StopWatch 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();
}
}
Aggregations