Search in sources :

Example 1 with Callback

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

the class ClassicConfiguration method loadCallbackLocation.

/**
 * Scan this location for classes that implement Callback.
 *
 * @param path The path to scan.
 * @param errorOnNotFound Whether to show an error if the location is not found.
 */
public void loadCallbackLocation(String path, boolean errorOnNotFound) {
    List<String> callbackClasses = classScanner.scanForType(path, Callback.class, errorOnNotFound);
    for (String callback : callbackClasses) {
        Class<? extends Callback> callbackClass;
        try {
            callbackClass = ClassUtils.loadClass(Callback.class, callback, classLoader);
        } catch (Throwable e) {
            Throwable rootCause = ExceptionUtils.getRootCause(e);
            LOG.warn("Skipping " + Callback.class + ": " + ClassUtils.formatThrowable(e) + (rootCause == e ? "" : " caused by " + ClassUtils.formatThrowable(rootCause) + " at " + ExceptionUtils.getThrowLocation(rootCause)));
            callbackClass = null;
        }
        if (callbackClass != null) {
            // Filter out abstract classes
            Callback callbackObj = ClassUtils.instantiate(callback, classLoader);
            this.callbacks.add(callbackObj);
        }
    }
}
Also used : Callback(org.flywaydb.core.api.callback.Callback)

Example 2 with Callback

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

the class FlywayExecutor method prepareCallbacks.

private List<Callback> prepareCallbacks(Database database, ResourceProvider resourceProvider, JdbcConnectionFactory jdbcConnectionFactory, SqlScriptFactory sqlScriptFactory, StatementInterceptor statementInterceptor, Schema schema, ParsingContext parsingContext) {
    List<Callback> effectiveCallbacks = new ArrayList<>();
    CallbackExecutor callbackExecutor = NoopCallbackExecutor.INSTANCE;
    effectiveCallbacks.addAll(Arrays.asList(configuration.getCallbacks()));
    if (!configuration.isSkipDefaultCallbacks()) {
        SqlScriptExecutorFactory sqlScriptExecutorFactory = jdbcConnectionFactory.getDatabaseType().createSqlScriptExecutorFactory(jdbcConnectionFactory, callbackExecutor, statementInterceptor);
        effectiveCallbacks.addAll(new SqlScriptCallbackFactory(resourceProvider, sqlScriptExecutorFactory, sqlScriptFactory, configuration).getCallbacks());
    }
    return effectiveCallbacks;
}
Also used : SqlScriptExecutorFactory(org.flywaydb.core.internal.sqlscript.SqlScriptExecutorFactory) Callback(org.flywaydb.core.api.callback.Callback) ArrayList(java.util.ArrayList)

Example 3 with Callback

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

the class Flyway6xAutoConfigurationTests method callbacksAreConfiguredAndOrdered.

@Test
void callbacksAreConfiguredAndOrdered() {
    this.contextRunner.withUserConfiguration(DataSourceAutoConfiguration.class, CallbackConfiguration.class).run((context) -> {
        assertThat(context).hasSingleBean(Flyway.class);
        Flyway flyway = context.getBean(Flyway.class);
        Callback callbackOne = context.getBean("callbackOne", Callback.class);
        Callback callbackTwo = context.getBean("callbackTwo", Callback.class);
        assertThat(flyway.getConfiguration().getCallbacks()).hasSize(2);
        assertThat(flyway.getConfiguration().getCallbacks()).containsExactly(callbackTwo, callbackOne);
        InOrder orderedCallbacks = inOrder(callbackOne, callbackTwo);
        orderedCallbacks.verify(callbackTwo).handle(any(Event.class), any(Context.class));
        orderedCallbacks.verify(callbackOne).handle(any(Event.class), any(Context.class));
    });
}
Also used : Context(org.flywaydb.core.api.callback.Context) Flyway(org.flywaydb.core.Flyway) Callback(org.flywaydb.core.api.callback.Callback) InOrder(org.mockito.InOrder) Event(org.flywaydb.core.api.callback.Event) DataSourceAutoConfiguration(org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration) Test(org.junit.jupiter.api.Test)

Example 4 with Callback

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

the class FlywayAutoConfigurationTests method callbacksAreConfiguredAndOrderedByName.

@Test
void callbacksAreConfiguredAndOrderedByName() {
    this.contextRunner.withUserConfiguration(EmbeddedDataSourceConfiguration.class, CallbackConfiguration.class).run((context) -> {
        assertThat(context).hasSingleBean(Flyway.class);
        Flyway flyway = context.getBean(Flyway.class);
        Callback callbackOne = context.getBean("callbackOne", Callback.class);
        Callback callbackTwo = context.getBean("callbackTwo", Callback.class);
        assertThat(flyway.getConfiguration().getCallbacks()).hasSize(2);
        InOrder orderedCallbacks = inOrder(callbackOne, callbackTwo);
        orderedCallbacks.verify(callbackTwo).handle(any(Event.class), any(Context.class));
        orderedCallbacks.verify(callbackOne).handle(any(Event.class), any(Context.class));
    });
}
Also used : DefaultDSLContext(org.jooq.impl.DefaultDSLContext) DSLContext(org.jooq.DSLContext) AssertableApplicationContext(org.springframework.boot.test.context.assertj.AssertableApplicationContext) Context(org.flywaydb.core.api.callback.Context) EmbeddedDataSourceConfiguration(org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration) Flyway(org.flywaydb.core.Flyway) Callback(org.flywaydb.core.api.callback.Callback) InOrder(org.mockito.InOrder) Event(org.flywaydb.core.api.callback.Event) Test(org.junit.jupiter.api.Test)

Example 5 with Callback

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

the class Flyway7xAutoConfigurationTests method callbacksAreConfigured.

@Test
void callbacksAreConfigured() {
    this.contextRunner.withUserConfiguration(DataSourceAutoConfiguration.class, CallbackConfiguration.class).run((context) -> {
        assertThat(context).hasSingleBean(Flyway.class);
        Flyway flyway = context.getBean(Flyway.class);
        Callback callbackOne = context.getBean("callbackOne", Callback.class);
        Callback callbackTwo = context.getBean("callbackTwo", Callback.class);
        assertThat(flyway.getConfiguration().getCallbacks()).hasSize(2);
        assertThat(flyway.getConfiguration().getCallbacks()).containsExactlyInAnyOrder(callbackTwo, callbackOne);
        then(callbackOne).should(atLeastOnce()).handle(any(Event.class), any(Context.class));
        then(callbackTwo).should(atLeastOnce()).handle(any(Event.class), any(Context.class));
    });
}
Also used : Context(org.flywaydb.core.api.callback.Context) Flyway(org.flywaydb.core.Flyway) Callback(org.flywaydb.core.api.callback.Callback) Event(org.flywaydb.core.api.callback.Event) DataSourceAutoConfiguration(org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration) Test(org.junit.jupiter.api.Test)

Aggregations

Callback (org.flywaydb.core.api.callback.Callback)5 Flyway (org.flywaydb.core.Flyway)3 Context (org.flywaydb.core.api.callback.Context)3 Event (org.flywaydb.core.api.callback.Event)3 Test (org.junit.jupiter.api.Test)3 InOrder (org.mockito.InOrder)2 DataSourceAutoConfiguration (org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration)2 ArrayList (java.util.ArrayList)1 SqlScriptExecutorFactory (org.flywaydb.core.internal.sqlscript.SqlScriptExecutorFactory)1 DSLContext (org.jooq.DSLContext)1 DefaultDSLContext (org.jooq.impl.DefaultDSLContext)1 EmbeddedDataSourceConfiguration (org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration)1 AssertableApplicationContext (org.springframework.boot.test.context.assertj.AssertableApplicationContext)1