Search in sources :

Example 11 with FlywayCallback

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

the class FlywayCallbackSmallTest method cleanTest.

@Test
public void cleanTest() {
    Properties properties = createProperties("clean");
    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.isBeforeClean());
    assertFalse(callbackImpl.isAfterClean());
    flyway.clean();
    assertTrue(callbackImpl.isBeforeClean());
    assertTrue(callbackImpl.isAfterClean());
    //make sure no other lifecycle events were fired
    assertFalse(callbackImpl.isAfterEachMigrate());
    assertFalse(callbackImpl.isAfterInfo());
    assertFalse(callbackImpl.isAfterBaseline());
    assertFalse(callbackImpl.isAfterMigrate());
    assertFalse(callbackImpl.isAfterRepair());
    assertFalse(callbackImpl.isAfterValidate());
    assertFalse(callbackImpl.isBeforeEachMigrate());
    assertFalse(callbackImpl.isBeforeInfo());
    assertFalse(callbackImpl.isBeforeBaseline());
    assertFalse(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 12 with FlywayCallback

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

the class FlywaySmallTest method configure.

@Test
public void configure() {
    Properties properties = new Properties();
    properties.setProperty("flyway.user", "sa");
    properties.setProperty("flyway.password", "");
    properties.setProperty("flyway.url", "jdbc:h2:mem:flyway_test;DB_CLOSE_DELAY=-1");
    properties.setProperty("flyway.driver", "org.h2.Driver");
    final Flyway flyway = new Flyway();
    flyway.configure(properties);
    assertNotNull(flyway.getDataSource());
    flyway.execute(new Flyway.Command<Void>() {

        public Void execute(Connection connectionMetaDataTable, MigrationResolver migrationResolver, MetaDataTable metaDataTable, DbSupport dbSupport, Schema[] schemas, FlywayCallback[] flywayCallbacks) {
            assertEquals("PUBLIC", flyway.getSchemas()[0]);
            return null;
        }
    });
}
Also used : FlywayCallback(org.flywaydb.core.api.callback.FlywayCallback) MetaDataTable(org.flywaydb.core.internal.metadatatable.MetaDataTable) Schema(org.flywaydb.core.internal.dbsupport.Schema) Connection(java.sql.Connection) DbSupport(org.flywaydb.core.internal.dbsupport.DbSupport) Properties(java.util.Properties) MigrationResolver(org.flywaydb.core.api.resolver.MigrationResolver) MyCustomMigrationResolver(org.flywaydb.core.internal.resolver.MyCustomMigrationResolver) Test(org.junit.Test)

Example 13 with FlywayCallback

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

the class DbMigrate method doMigrate.

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

Example 14 with FlywayCallback

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

the class Flyway method info.

/**
     * <p>Retrieves the complete information about all the migrations including applied, pending and current migrations with
     * details and status.</p>
     * <img src="https://flywaydb.org/assets/balsamiq/command-info.png" alt="info">
     *
     * @return All migrations sorted by version, oldest first.
     * @throws FlywayException when the info retrieval failed.
     */
public MigrationInfoService info() {
    return execute(new Command<MigrationInfoService>() {

        public MigrationInfoService execute(final Connection connectionMetaDataTable, MigrationResolver migrationResolver, MetaDataTable metaDataTable, final DbSupport dbSupport, final Schema[] schemas, FlywayCallback[] flywayCallbacks) {
            try {
                for (final FlywayCallback callback : flywayCallbacks) {
                    new TransactionTemplate(connectionMetaDataTable).execute(new Callable<Object>() {

                        @Override
                        public Object call() throws SQLException {
                            dbSupport.changeCurrentSchemaTo(schemas[0]);
                            callback.beforeInfo(connectionMetaDataTable);
                            return null;
                        }
                    });
                }
                MigrationInfoServiceImpl migrationInfoService = new MigrationInfoServiceImpl(migrationResolver, metaDataTable, target, outOfOrder, true, true, true);
                migrationInfoService.refresh();
                for (final FlywayCallback callback : flywayCallbacks) {
                    new TransactionTemplate(connectionMetaDataTable).execute(new Callable<Object>() {

                        @Override
                        public Object call() throws SQLException {
                            dbSupport.changeCurrentSchemaTo(schemas[0]);
                            callback.afterInfo(connectionMetaDataTable);
                            return null;
                        }
                    });
                }
                return migrationInfoService;
            } finally {
                dbSupport.restoreCurrentSchema();
            }
        }
    });
}
Also used : MigrationInfoServiceImpl(org.flywaydb.core.internal.info.MigrationInfoServiceImpl) MigrationInfoService(org.flywaydb.core.api.MigrationInfoService) MetaDataTable(org.flywaydb.core.internal.metadatatable.MetaDataTable) Schema(org.flywaydb.core.internal.dbsupport.Schema) Connection(java.sql.Connection) TransactionTemplate(org.flywaydb.core.internal.util.jdbc.TransactionTemplate) Callable(java.util.concurrent.Callable) 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 15 with FlywayCallback

use of org.flywaydb.core.api.callback.FlywayCallback in project spring-boot by spring-projects.

the class FlywayAutoConfigurationTests method callbacksAreConfiguredAndOrdered.

@Test
public void callbacksAreConfiguredAndOrdered() throws Exception {
    registerAndRefresh(EmbeddedDataSourceConfiguration.class, FlywayAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class, CallbackConfiguration.class);
    assertThat(this.context.getBeansOfType(Flyway.class)).hasSize(1);
    Flyway flyway = this.context.getBean(Flyway.class);
    FlywayCallback callbackOne = this.context.getBean("callbackOne", FlywayCallback.class);
    FlywayCallback callbackTwo = this.context.getBean("callbackTwo", FlywayCallback.class);
    assertThat(flyway.getCallbacks()).containsExactly(callbackTwo, callbackOne);
    InOrder orderedCallbacks = inOrder(callbackOne, callbackTwo);
    orderedCallbacks.verify(callbackTwo).beforeMigrate(any(Connection.class));
    orderedCallbacks.verify(callbackOne).beforeMigrate(any(Connection.class));
}
Also used : Flyway(org.flywaydb.core.Flyway) InOrder(org.mockito.InOrder) FlywayCallback(org.flywaydb.core.api.callback.FlywayCallback) Connection(java.sql.Connection) Test(org.junit.Test)

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