Search in sources :

Example 6 with AppliedMigration

use of org.flywaydb.core.internal.metadatatable.AppliedMigration in project flyway by flyway.

the class DbBaselineTest method sameBaselineMarkerPresentWithoutMigrations.

@Test
public void sameBaselineMarkerPresentWithoutMigrations() {
    // 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(false);
    // 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 7 with AppliedMigration

use of org.flywaydb.core.internal.metadatatable.AppliedMigration in project flyway by flyway.

the class DbBaseline method baseline.

/**
     * Baselines the database.
     */
public void baseline() {
    try {
        for (final FlywayCallback callback : callbacks) {
            new TransactionTemplate(connection).execute(new Callable<Object>() {

                @Override
                public Object call() throws SQLException {
                    dbSupport.changeCurrentSchemaTo(schema);
                    callback.beforeBaseline(connection);
                    return null;
                }
            });
        }
        new TransactionTemplate(connection).execute(new Callable<Object>() {

            @Override
            public Void call() {
                dbSupport.changeCurrentSchemaTo(schema);
                if (metaDataTable.hasBaselineMarker()) {
                    AppliedMigration baselineMarker = metaDataTable.getBaselineMarker();
                    if (baselineVersion.equals(baselineMarker.getVersion()) && baselineDescription.equals(baselineMarker.getDescription())) {
                        LOG.info("Metadata table " + metaDataTable + " already initialized with (" + baselineVersion + "," + baselineDescription + "). Skipping.");
                        return null;
                    }
                    throw new FlywayException("Unable to baseline metadata table " + metaDataTable + " with (" + baselineVersion + "," + baselineDescription + ") as it has already been initialized with (" + baselineMarker.getVersion() + "," + baselineMarker.getDescription() + ")");
                }
                if (metaDataTable.hasSchemasMarker() && baselineVersion.equals(MigrationVersion.fromVersion("0"))) {
                    throw new FlywayException("Unable to baseline metadata table " + metaDataTable + " with version 0 as this version was used for schema creation");
                }
                if (metaDataTable.hasAppliedMigrations()) {
                    throw new FlywayException("Unable to baseline metadata table " + metaDataTable + " as it already contains migrations");
                }
                metaDataTable.addBaselineMarker(baselineVersion, baselineDescription);
                return null;
            }
        });
        LOG.info("Successfully baselined schema with version: " + baselineVersion);
        for (final FlywayCallback callback : callbacks) {
            new TransactionTemplate(connection).execute(new Callable<Object>() {

                @Override
                public Object call() throws SQLException {
                    dbSupport.changeCurrentSchemaTo(schema);
                    callback.afterBaseline(connection);
                    return null;
                }
            });
        }
    } finally {
        dbSupport.restoreCurrentSchema();
    }
}
Also used : FlywayException(org.flywaydb.core.api.FlywayException) FlywayCallback(org.flywaydb.core.api.callback.FlywayCallback) SQLException(java.sql.SQLException) AppliedMigration(org.flywaydb.core.internal.metadatatable.AppliedMigration) TransactionTemplate(org.flywaydb.core.internal.util.jdbc.TransactionTemplate)

Example 8 with AppliedMigration

use of org.flywaydb.core.internal.metadatatable.AppliedMigration in project flyway by flyway.

the class DbRepair method repairChecksumsAndDescriptions.

public void repairChecksumsAndDescriptions() {
    migrationInfoService.refresh();
    for (MigrationInfo migrationInfo : migrationInfoService.all()) {
        MigrationInfoImpl migrationInfoImpl = (MigrationInfoImpl) migrationInfo;
        ResolvedMigration resolved = migrationInfoImpl.getResolvedMigration();
        AppliedMigration applied = migrationInfoImpl.getAppliedMigration();
        if (resolved != null && applied != null && resolved.getVersion() != null) {
            if (!ObjectUtils.nullSafeEquals(resolved.getChecksum(), applied.getChecksum()) || !ObjectUtils.nullSafeEquals(resolved.getDescription(), applied.getDescription())) {
                metaDataTable.update(migrationInfoImpl.getVersion(), resolved.getDescription(), resolved.getChecksum());
            }
        }
    }
}
Also used : MigrationInfo(org.flywaydb.core.api.MigrationInfo) MigrationInfoImpl(org.flywaydb.core.internal.info.MigrationInfoImpl) AppliedMigration(org.flywaydb.core.internal.metadatatable.AppliedMigration) ResolvedMigration(org.flywaydb.core.api.resolver.ResolvedMigration)

Example 9 with AppliedMigration

use of org.flywaydb.core.internal.metadatatable.AppliedMigration in project flyway by flyway.

the class MigrationInfoDumperSmallTest method createMetaDataTable.

/**
     * Creates a metadata table for testing.
     *
     * @return The metadata table.
     */
private MetaDataTable createMetaDataTable() {
    MetaDataTable metaDataTable = mock(MetaDataTable.class);
    when(metaDataTable.allAppliedMigrations()).thenReturn(new ArrayList<AppliedMigration>());
    return metaDataTable;
}
Also used : MetaDataTable(org.flywaydb.core.internal.metadatatable.MetaDataTable) AppliedMigration(org.flywaydb.core.internal.metadatatable.AppliedMigration)

Example 10 with AppliedMigration

use of org.flywaydb.core.internal.metadatatable.AppliedMigration in project flyway by flyway.

the class MigrationInfoImplSmallTest method validateFuture.

@Test
public void validateFuture() {
    MigrationVersion version = MigrationVersion.fromVersion("1");
    String description = "test";
    MigrationType type = MigrationType.SQL;
    AppliedMigration appliedMigration = new AppliedMigration(1, version, description, type, null, 123, new Date(), "abc", 0, true);
    MigrationInfoImpl migrationInfo = new MigrationInfoImpl(null, appliedMigration, new MigrationInfoContext(), false);
    String message = migrationInfo.validate();
    assertTrue(message, message.contains("not resolved"));
}
Also used : MigrationVersion(org.flywaydb.core.api.MigrationVersion) AppliedMigration(org.flywaydb.core.internal.metadatatable.AppliedMigration) MigrationType(org.flywaydb.core.api.MigrationType) Date(java.util.Date) Test(org.junit.Test)

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