Search code examples
database-migrationflywaydata-migrationschema-migration

Run Flyway Migration Asynchronously


By design the flyway migrations run in transactions and synchronously pretty early during startup of an application. This is usually desired, to ensure before business logic starts to execute the database is in a consistent state (migrated) or the migration fails and the app crashes.

In some cases I'd really like to be able to start the application without it waiting for some migrations to be completed (long running migrations, creating indexes or materialized views, etc.). This might also be needed when deploying from a CI-Server and using deploy-timeouts / health-checks (that can not be raised indefinitely) to ensure the deployment worked as expected.

Is there any configuration / convention / best practice to enable async migrations?
(i.e. naming the migration A2_00__UpdateSthLong.sql instead of V2 (standard) or R2 (Repeatable migration).


Solution

  • Seems like its not possible (yet):

    • There is an issue requesting this on github: https://github.com/flyway/flyway/issues/950

    • Flyway for micronaut, seems to already support this: https://github.com/flyway/flyway/issues/950

    • A temp solution until support is integrated could be to use Java Migrations and spawn an async-Task from within the migrate method yourself. The migration wouldn't be transactional then of course :(

    • Another solution might be to do the migration before actually starting up the application (i.e. by using the maven task).