zonkyio / embedded-database-spring-test

A library for creating isolated embedded databases for Spring-powered integration tests.
Apache License 2.0
399 stars 37 forks source link

For Flyway 9.9+ JavaMigration beans are not applied #264

Closed gaksenov-cen57237 closed 4 months ago

gaksenov-cen57237 commented 4 months ago

When used with Flyway 9.9+,

Actual: Beans of type org.flywaydb.core.api.migration.JavaMigration are not applied during migration phase.

Expected: Flyway is properly configured, so that beans of type org.flywaydb.core.api.migration.JavaMigration are applied during migration phase.

Investigation: io.zonky.test.db.flyway.FlywayWrapper#getConfig picks up a modernConfig in Flyway's config, which doesn't contain an array of JavaMigration objects, therefore missing them. These objects are still present in config property and accessible, though. I don't think JavaMigration was ever deprecated, so the missing javaMigrations property in modernConfig seems like an issue of Flyway itself. However, this particular bug may be fixed using existing Flyway's API.

gaksenov-cen57237 commented 4 months ago

As workaround, Flyway's integration can be disabled by creating a no-op bean of type FlywayDatabaseExtension with name "flywayDatabaseExtension"

class NoOpFlywayDatabaseExtension() : FlywayDatabaseExtension() {
    public override fun postProcessBeforeInitialization(bean: Any, beanName: String): Any {
        return bean
    }

    public override fun postProcessAfterInitialization(bean: Any, beanName: String): Any {
        return bean
    }

    fun processPendingOperations() {
        // do nothing
    }
}

Then the Flyway migrations are applied ok, which raises a question - what is the purpose of Flyway's integration in embedded-database-spring-test?

tomix26 commented 4 months ago

Thanks for the report. I'll try to investigate it more, but If it works with the flyway database extension disabled, then it's definitely a bug in the library.

The primary purpose of the flyway extension lies in performance optimizations. It offers a more effective background bootstrapping mode, reusing template databases among multiple Spring contexts, and, probably the most important optimization, is the handling of the @FlywayTest annotation. Without FlywayDatabaseExtension and OptimizedFlywayTestExecutionListener, this annotation would trigger a complete cleanup of the database before applying new migrations. The flyway extension can take advantage of template databases to speed it up.

tomix26 commented 4 months ago

Ok, just to clarify, how do you register Java migrations with Flyway? Do you do it manually using ClassicConfiguration or FluentConfiguration, and not using the locations property, am I right?

gaksenov-cen57237 commented 4 months ago

Thank You @tomix26, looks fixed