Search in sources :

Example 1 with AppliedMigration

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();
}
Also used : MigrationVersion(org.flywaydb.core.api.MigrationVersion) AppliedMigration(org.flywaydb.core.internal.metadatatable.AppliedMigration) Test(org.junit.Test)

Example 2 with AppliedMigration

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();
}
Also used : AppliedMigration(org.flywaydb.core.internal.metadatatable.AppliedMigration) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Example 3 with AppliedMigration

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());
}
Also used : AppliedMigration(org.flywaydb.core.internal.metadatatable.AppliedMigration) Test(org.junit.Test)

Example 4 with AppliedMigration

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;
}
Also used : ArrayList(java.util.ArrayList) TreeMap(java.util.TreeMap) MigrationVersion(org.flywaydb.core.api.MigrationVersion) AppliedMigration(org.flywaydb.core.internal.metadatatable.AppliedMigration) ResolvedMigration(org.flywaydb.core.api.resolver.ResolvedMigration) Pair(org.flywaydb.core.internal.util.Pair) HashSet(java.util.HashSet)

Example 5 with AppliedMigration

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;
}
Also used : FlywayException(org.flywaydb.core.api.FlywayException) FlywaySqlException(org.flywaydb.core.internal.dbsupport.FlywaySqlException) SQLException(java.sql.SQLException) MigrationExecutor(org.flywaydb.core.api.resolver.MigrationExecutor) TransactionTemplate(org.flywaydb.core.internal.util.jdbc.TransactionTemplate) StopWatch(org.flywaydb.core.internal.util.StopWatch) MigrationVersion(org.flywaydb.core.api.MigrationVersion) AppliedMigration(org.flywaydb.core.internal.metadatatable.AppliedMigration)

Aggregations

AppliedMigration (org.flywaydb.core.internal.metadatatable.AppliedMigration)12 MigrationVersion (org.flywaydb.core.api.MigrationVersion)6 Test (org.junit.Test)6 SQLException (java.sql.SQLException)3 FlywayException (org.flywaydb.core.api.FlywayException)3 Date (java.util.Date)2 MigrationType (org.flywaydb.core.api.MigrationType)2 MigrationExecutor (org.flywaydb.core.api.resolver.MigrationExecutor)2 ResolvedMigration (org.flywaydb.core.api.resolver.ResolvedMigration)2 TransactionTemplate (org.flywaydb.core.internal.util.jdbc.TransactionTemplate)2 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 TreeMap (java.util.TreeMap)1 MigrationInfo (org.flywaydb.core.api.MigrationInfo)1 FlywayCallback (org.flywaydb.core.api.callback.FlywayCallback)1 FlywaySqlException (org.flywaydb.core.internal.dbsupport.FlywaySqlException)1 MigrationInfoImpl (org.flywaydb.core.internal.info.MigrationInfoImpl)1 MetaDataTable (org.flywaydb.core.internal.metadatatable.MetaDataTable)1 ResolvedMigrationImpl (org.flywaydb.core.internal.resolver.ResolvedMigrationImpl)1 Pair (org.flywaydb.core.internal.util.Pair)1