Search in sources :

Example 6 with FlywayCallback

use of org.flywaydb.core.api.callback.FlywayCallback in project flyway by flyway.

the class FlywayCallbackSmallTest method migrateTest.

@Test
public void migrateTest() {
    Properties properties = createProperties("migrate");
    FlywayCallbackImpl callbackImpl = new FlywayCallbackImpl();
    FlywayCallback[] callbacks = new FlywayCallback[] { callbackImpl };
    final Flyway flyway = new Flyway();
    flyway.configure(properties);
    flyway.setCallbacks(callbacks);
    assertNotNull(flyway.getDataSource());
    assertFalse(callbackImpl.isBeforeMigrate());
    assertFalse(callbackImpl.isAfterMigrate());
    flyway.migrate();
    assertFalse(callbackImpl.isBeforeClean());
    assertFalse(callbackImpl.isAfterClean());
    assertTrue(callbackImpl.isAfterEachMigrate());
    assertFalse(callbackImpl.isAfterInfo());
    assertFalse(callbackImpl.isAfterBaseline());
    assertTrue(callbackImpl.isAfterMigrate());
    assertFalse(callbackImpl.isAfterRepair());
    assertFalse(callbackImpl.isAfterValidate());
    assertTrue(callbackImpl.isBeforeEachMigrate());
    assertFalse(callbackImpl.isBeforeInfo());
    assertFalse(callbackImpl.isBeforeBaseline());
    assertTrue(callbackImpl.isBeforeMigrate());
    assertFalse(callbackImpl.isBeforeRepair());
    assertFalse(callbackImpl.isBeforeValidate());
}
Also used : FlywayCallback(org.flywaydb.core.api.callback.FlywayCallback) BaseFlywayCallback(org.flywaydb.core.api.callback.BaseFlywayCallback) Properties(java.util.Properties) Test(org.junit.Test)

Example 7 with FlywayCallback

use of org.flywaydb.core.api.callback.FlywayCallback in project flyway by flyway.

the class FlywayCallbackSmallTest method validateTest.

@Test
public void validateTest() {
    Properties properties = createProperties("validate");
    FlywayCallbackImpl callbackImpl = new FlywayCallbackImpl();
    FlywayCallback[] callbacks = new FlywayCallback[] { callbackImpl };
    final Flyway flyway = new Flyway();
    flyway.configure(properties);
    flyway.migrate();
    flyway.setCallbacks(callbacks);
    assertNotNull(flyway.getDataSource());
    assertFalse(callbackImpl.isBeforeValidate());
    assertFalse(callbackImpl.isAfterValidate());
    flyway.validate();
    assertFalse(callbackImpl.isBeforeClean());
    assertFalse(callbackImpl.isAfterClean());
    assertFalse(callbackImpl.isAfterEachMigrate());
    assertFalse(callbackImpl.isAfterInfo());
    assertFalse(callbackImpl.isAfterBaseline());
    assertFalse(callbackImpl.isAfterMigrate());
    assertFalse(callbackImpl.isAfterRepair());
    assertTrue(callbackImpl.isAfterValidate());
    assertFalse(callbackImpl.isBeforeEachMigrate());
    assertFalse(callbackImpl.isBeforeInfo());
    assertFalse(callbackImpl.isBeforeBaseline());
    assertFalse(callbackImpl.isBeforeMigrate());
    assertFalse(callbackImpl.isBeforeRepair());
    assertTrue(callbackImpl.isBeforeValidate());
}
Also used : FlywayCallback(org.flywaydb.core.api.callback.FlywayCallback) BaseFlywayCallback(org.flywaydb.core.api.callback.BaseFlywayCallback) Properties(java.util.Properties) Test(org.junit.Test)

Example 8 with FlywayCallback

use of org.flywaydb.core.api.callback.FlywayCallback in project flyway by flyway.

the class Flyway method execute.

/**
     * Executes this command with proper resource handling and cleanup.
     *
     * @param command The command to execute.
     * @param <T>     The type of the result.
     * @return The result of the command.
     */
/*private -> testing*/
<T> T execute(Command<T> command) {
    T result;
    VersionPrinter.printVersion();
    Connection connectionMetaDataTable = null;
    try {
        if (dataSource == null) {
            throw new FlywayException("Unable to connect to the database. Configure the url, user and password!");
        }
        connectionMetaDataTable = JdbcUtils.openConnection(dataSource);
        DbSupport dbSupport = DbSupportFactory.createDbSupport(connectionMetaDataTable, !dbConnectionInfoPrinted);
        dbConnectionInfoPrinted = true;
        LOG.debug("DDL Transactions Supported: " + dbSupport.supportsDdlTransactions());
        if (schemaNames.length == 0) {
            Schema currentSchema = dbSupport.getOriginalSchema();
            if (currentSchema == null) {
                throw new FlywayException("Unable to determine schema for the metadata table." + " Set a default schema for the connection or specify one using the schemas property!");
            }
            setSchemas(currentSchema.getName());
        }
        if (schemaNames.length == 1) {
            LOG.debug("Schema: " + schemaNames[0]);
        } else {
            LOG.debug("Schemas: " + StringUtils.arrayToCommaDelimitedString(schemaNames));
        }
        Schema[] schemas = new Schema[schemaNames.length];
        for (int i = 0; i < schemaNames.length; i++) {
            schemas[i] = dbSupport.getSchema(schemaNames[i]);
        }
        Scanner scanner = new Scanner(classLoader);
        MigrationResolver migrationResolver = createMigrationResolver(dbSupport, scanner);
        if (!skipDefaultCallbacks) {
            Set<FlywayCallback> flywayCallbacks = new LinkedHashSet<FlywayCallback>(Arrays.asList(callbacks));
            flywayCallbacks.add(new SqlScriptFlywayCallback(dbSupport, scanner, locations, createPlaceholderReplacer(), this));
            callbacks = flywayCallbacks.toArray(new FlywayCallback[flywayCallbacks.size()]);
        }
        for (FlywayCallback callback : callbacks) {
            ConfigurationInjectionUtils.injectFlywayConfiguration(callback, this);
        }
        MetaDataTable metaDataTable = new MetaDataTableImpl(dbSupport, schemas[0].getTable(table), installedBy);
        if (metaDataTable.upgradeIfNecessary()) {
            new DbRepair(dbSupport, connectionMetaDataTable, schemas[0], migrationResolver, metaDataTable, callbacks).repairChecksumsAndDescriptions();
            LOG.info("Metadata table " + table + " successfully upgraded to the Flyway 4.0 format.");
        }
        result = command.execute(connectionMetaDataTable, migrationResolver, metaDataTable, dbSupport, schemas, callbacks);
    } finally {
        JdbcUtils.closeConnection(connectionMetaDataTable);
    }
    return result;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) FlywayException(org.flywaydb.core.api.FlywayException) Scanner(org.flywaydb.core.internal.util.scanner.Scanner) MetaDataTable(org.flywaydb.core.internal.metadatatable.MetaDataTable) Schema(org.flywaydb.core.internal.dbsupport.Schema) Connection(java.sql.Connection) DbRepair(org.flywaydb.core.internal.command.DbRepair) MetaDataTableImpl(org.flywaydb.core.internal.metadatatable.MetaDataTableImpl) SqlScriptFlywayCallback(org.flywaydb.core.internal.callback.SqlScriptFlywayCallback) FlywayCallback(org.flywaydb.core.api.callback.FlywayCallback) SqlScriptFlywayCallback(org.flywaydb.core.internal.callback.SqlScriptFlywayCallback) DbSupport(org.flywaydb.core.internal.dbsupport.DbSupport) MigrationResolver(org.flywaydb.core.api.resolver.MigrationResolver) CompositeMigrationResolver(org.flywaydb.core.internal.resolver.CompositeMigrationResolver)

Example 9 with FlywayCallback

use of org.flywaydb.core.api.callback.FlywayCallback in project killbill by killbill.

the class DbMigrateWithDryRun method doMigrate.

private void doMigrate(final MigrationInfo migration, final MigrationExecutor migrationExecutor, final String migrationText) throws SQLException {
    for (final FlywayCallback callback : callbacks) {
        dbSupportUserObjects.changeCurrentSchemaTo(schema);
        callback.beforeEachMigrate(connectionUserObjects, migration);
    }
    dbSupportUserObjects.changeCurrentSchemaTo(schema);
    migrationExecutor.execute(connectionUserObjects);
    LOG.debug("Successfully completed migration of " + migrationText);
    for (final FlywayCallback callback : callbacks) {
        dbSupportUserObjects.changeCurrentSchemaTo(schema);
        callback.afterEachMigrate(connectionUserObjects, migration);
    }
}
Also used : FlywayCallback(org.flywaydb.core.api.callback.FlywayCallback)

Example 10 with FlywayCallback

use of org.flywaydb.core.api.callback.FlywayCallback in project killbill by killbill.

the class DbMigrateWithDryRun method dryRunMigrate.

public int dryRunMigrate() throws FlywayException {
    try {
        for (final FlywayCallback callback : callbacks) {
            new TransactionTemplate(connectionUserObjects).execute(new TransactionCallback<Object>() {

                @Override
                public Object doInTransaction() throws SQLException {
                    dbSupportUserObjects.changeCurrentSchemaTo(schema);
                    callback.beforeMigrate(connectionUserObjects);
                    return null;
                }
            });
        }
        // PIERRE: perform a single query to the metadata table
        final MigrationInfoServiceImpl infoService = new MigrationInfoServiceImpl(migrationResolver, metaDataTableForDryRun, target, outOfOrder, true, true);
        infoService.refresh();
        final MigrationInfoImpl[] pendingMigrations = infoService.pending();
        new TransactionTemplate(connectionMetaDataTable, false).execute(new TransactionCallback<Boolean>() {

            public Boolean doInTransaction() {
                int i = 1;
                for (final MigrationInfoImpl migrationInfo : pendingMigrations) {
                    applyMigration(i, migrationInfo);
                    i++;
                }
                return true;
            }
        });
        for (final FlywayCallback callback : callbacks) {
            new TransactionTemplate(connectionUserObjects).execute(new TransactionCallback<Object>() {

                @Override
                public Object doInTransaction() throws SQLException {
                    dbSupportUserObjects.changeCurrentSchemaTo(schema);
                    callback.afterMigrate(connectionUserObjects);
                    return null;
                }
            });
        }
        return pendingMigrations.length;
    } finally {
        dbSupportUserObjects.restoreCurrentSchema();
    }
}
Also used : MigrationInfoServiceImpl(org.flywaydb.core.internal.info.MigrationInfoServiceImpl) FlywayCallback(org.flywaydb.core.api.callback.FlywayCallback) SQLException(java.sql.SQLException) MigrationInfoImpl(org.flywaydb.core.internal.info.MigrationInfoImpl) TransactionTemplate(org.flywaydb.core.internal.util.jdbc.TransactionTemplate)

Aggregations

FlywayCallback (org.flywaydb.core.api.callback.FlywayCallback)15 Test (org.junit.Test)10 Properties (java.util.Properties)8 BaseFlywayCallback (org.flywaydb.core.api.callback.BaseFlywayCallback)8 Connection (java.sql.Connection)5 MigrationResolver (org.flywaydb.core.api.resolver.MigrationResolver)3 DbSupport (org.flywaydb.core.internal.dbsupport.DbSupport)3 Schema (org.flywaydb.core.internal.dbsupport.Schema)3 MetaDataTable (org.flywaydb.core.internal.metadatatable.MetaDataTable)3 SqlScriptFlywayCallback (org.flywaydb.core.internal.callback.SqlScriptFlywayCallback)2 MigrationInfoServiceImpl (org.flywaydb.core.internal.info.MigrationInfoServiceImpl)2 CompositeMigrationResolver (org.flywaydb.core.internal.resolver.CompositeMigrationResolver)2 TransactionTemplate (org.flywaydb.core.internal.util.jdbc.TransactionTemplate)2 SQLException (java.sql.SQLException)1 LinkedHashSet (java.util.LinkedHashSet)1 Callable (java.util.concurrent.Callable)1 Flyway (org.flywaydb.core.Flyway)1 FlywayException (org.flywaydb.core.api.FlywayException)1 MigrationInfoService (org.flywaydb.core.api.MigrationInfoService)1 DbRepair (org.flywaydb.core.internal.command.DbRepair)1