use of org.flywaydb.core.internal.metadatatable.AppliedMigration in project flyway by flyway.
the class DbBaselineTest method differentBaselineMarkerVersionPresent.
@Test
public void differentBaselineMarkerVersionPresent() {
// arrange
MigrationVersion baselineVersion = MigrationVersion.fromVersion("3.0.0");
AppliedMigration baseline = new AppliedMigration(baselineVersion, TEST_BASELINE_DESCRIPTION, MigrationType.BASELINE, "V2.0.0__test-migration.sql", 12345, 100, true);
when(this.metaDataTable.hasBaselineMarker()).thenReturn(true);
when(this.metaDataTable.getBaselineMarker()).thenReturn(baseline);
// assert
this.expectedException.expect(FlywayException.class);
this.expectedException.expectMessage(TEST_BASELINE_VERSION.toString());
this.expectedException.expectMessage(TEST_BASELINE_DESCRIPTION.toString());
this.expectedException.expectMessage(baselineVersion.toString());
// act
this.testBaseline.baseline();
}
use of org.flywaydb.core.internal.metadatatable.AppliedMigration in project flyway by flyway.
the class DbBaselineTest method differentBaselineMarkerDescriptionPresent.
@Test
public void differentBaselineMarkerDescriptionPresent() {
// arrange
String baselineDescription = "Differen description";
AppliedMigration baseline = new AppliedMigration(TEST_BASELINE_VERSION, baselineDescription, MigrationType.BASELINE, "V2.0.0__test-migration.sql", 12345, 100, true);
when(this.metaDataTable.hasBaselineMarker()).thenReturn(true);
when(this.metaDataTable.getBaselineMarker()).thenReturn(baseline);
// assert
this.expectedException.expect(FlywayException.class);
this.expectedException.expectMessage(TEST_BASELINE_VERSION.toString());
this.expectedException.expectMessage(TEST_BASELINE_DESCRIPTION.toString());
this.expectedException.expectMessage(baselineDescription);
// act
this.testBaseline.baseline();
}
use of org.flywaydb.core.internal.metadatatable.AppliedMigration in project flyway by flyway.
the class DbBaselineTest method sameBaselineMarkerPresentWithMigrations.
@Test
public void sameBaselineMarkerPresentWithMigrations() {
// arrange
AppliedMigration baseline = new AppliedMigration(TEST_BASELINE_VERSION, TEST_BASELINE_DESCRIPTION, MigrationType.BASELINE, "V2.0.0__test-migration.sql", 12345, 100, true);
when(this.metaDataTable.hasBaselineMarker()).thenReturn(true);
when(this.metaDataTable.getBaselineMarker()).thenReturn(baseline);
when(this.metaDataTable.hasAppliedMigrations()).thenReturn(true);
// act
this.testBaseline.baseline();
// assert
verify(metaDataTable, never()).addBaselineMarker(Mockito.<MigrationVersion>anyObject(), anyString());
}
use of org.flywaydb.core.internal.metadatatable.AppliedMigration in project flyway by flyway.
the class MigrationInfoServiceImpl method refresh.
/**
* Refreshes the info about all known migrations from both the classpath and the DB.
*/
public void refresh() {
Collection<ResolvedMigration> availableMigrations = migrationResolver.resolveMigrations();
List<AppliedMigration> appliedMigrations = metaDataTable.allAppliedMigrations();
MigrationInfoContext context = new MigrationInfoContext();
context.outOfOrder = outOfOrder;
context.pending = pending;
context.missing = missing;
context.future = future;
context.target = target;
Map<MigrationVersion, ResolvedMigration> resolvedMigrationsMap = new TreeMap<MigrationVersion, ResolvedMigration>();
Map<String, ResolvedMigration> resolvedRepeatableMigrationsMap = new TreeMap<String, ResolvedMigration>();
for (ResolvedMigration resolvedMigration : availableMigrations) {
MigrationVersion version = resolvedMigration.getVersion();
if (version != null) {
if (version.compareTo(context.lastResolved) > 0) {
context.lastResolved = version;
}
resolvedMigrationsMap.put(version, resolvedMigration);
} else {
resolvedRepeatableMigrationsMap.put(resolvedMigration.getDescription(), resolvedMigration);
}
}
Map<MigrationVersion, Pair<AppliedMigration, Boolean>> appliedMigrationsMap = new TreeMap<MigrationVersion, Pair<AppliedMigration, Boolean>>();
List<AppliedMigration> appliedRepeatableMigrations = new ArrayList<AppliedMigration>();
for (AppliedMigration appliedMigration : appliedMigrations) {
MigrationVersion version = appliedMigration.getVersion();
boolean outOfOrder1 = false;
if (version != null) {
if (version.compareTo(context.lastApplied) > 0) {
context.lastApplied = version;
} else {
outOfOrder1 = true;
}
}
if (appliedMigration.getType() == MigrationType.SCHEMA) {
context.schema = version;
}
if (appliedMigration.getType() == MigrationType.BASELINE) {
context.baseline = version;
}
if (version != null) {
appliedMigrationsMap.put(version, Pair.of(appliedMigration, outOfOrder1));
} else {
appliedRepeatableMigrations.add(appliedMigration);
}
}
if (MigrationVersion.CURRENT == target) {
context.target = context.lastApplied;
}
Set<MigrationVersion> allVersions = new HashSet<MigrationVersion>();
allVersions.addAll(resolvedMigrationsMap.keySet());
allVersions.addAll(appliedMigrationsMap.keySet());
List<MigrationInfoImpl> migrationInfos1 = new ArrayList<MigrationInfoImpl>();
for (MigrationVersion version : allVersions) {
ResolvedMigration resolvedMigration = resolvedMigrationsMap.get(version);
Pair<AppliedMigration, Boolean> appliedMigrationInfo = appliedMigrationsMap.get(version);
if (appliedMigrationInfo == null) {
migrationInfos1.add(new MigrationInfoImpl(resolvedMigration, null, context, false));
} else {
migrationInfos1.add(new MigrationInfoImpl(resolvedMigration, appliedMigrationInfo.getLeft(), context, appliedMigrationInfo.getRight()));
}
}
for (AppliedMigration appliedRepeatableMigration : appliedRepeatableMigrations) {
if (!context.latestRepeatableRuns.containsKey(appliedRepeatableMigration.getDescription()) || (appliedRepeatableMigration.getInstalledRank() > context.latestRepeatableRuns.get(appliedRepeatableMigration.getDescription()))) {
context.latestRepeatableRuns.put(appliedRepeatableMigration.getDescription(), appliedRepeatableMigration.getInstalledRank());
}
}
Set<ResolvedMigration> pendingResolvedRepeatableMigrations = new HashSet<ResolvedMigration>(resolvedRepeatableMigrationsMap.values());
for (AppliedMigration appliedRepeatableMigration : appliedRepeatableMigrations) {
ResolvedMigration resolvedMigration = resolvedRepeatableMigrationsMap.get(appliedRepeatableMigration.getDescription());
int latestRank = context.latestRepeatableRuns.get(appliedRepeatableMigration.getDescription());
if (resolvedMigration != null && appliedRepeatableMigration.getInstalledRank() == latestRank && ObjectUtils.nullSafeEquals(appliedRepeatableMigration.getChecksum(), resolvedMigration.getChecksum())) {
pendingResolvedRepeatableMigrations.remove(resolvedMigration);
}
migrationInfos1.add(new MigrationInfoImpl(resolvedMigration, appliedRepeatableMigration, context, false));
}
for (ResolvedMigration pendingResolvedRepeatableMigration : pendingResolvedRepeatableMigrations) {
migrationInfos1.add(new MigrationInfoImpl(pendingResolvedRepeatableMigration, null, context, false));
}
Collections.sort(migrationInfos1);
migrationInfos = migrationInfos1;
}
use of org.flywaydb.core.internal.metadatatable.AppliedMigration 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;
}
Aggregations