Spring Boot will automatically run Flyway migrations on startup just by detecting the presence of the flyway-core dependency. I assumed since Grails 3 is based on spring boot that it might work the same way, but it doesn't exactly. Here's a quick overview of the steps to set up flyway with Grails 3

Flyway dependency

Exactly the same as spring boot, include the dependency in your build.gradle file:

compile 'org.flywaydb:flyway-core:3.2.1'  

Where to put migrations

Not much surprise here, but by default your migration (.sql) files should be put in conf/db/migration

Flyway configuration

You can put your flyway configuration in your application.yml. For example:

  enabled: true
  baselineOnMigrate: true

Set up to run automatically on startup

There is a gradle plugin that provides tasks to run flyway migrations, but I prefer the simplicity of just having them run when the app starts up. This simplifies production deploys since there's one less thing to worry about forgetting.

The automatic behavior happens out of the box when Spring Boot detects the flyway-core dependency, but it doesn't happen with Grails 3. In Grails, you'll need to configure it in your resources.groovy and make sure that the migrations run before Grails tries to initialize the sessionFactory.

beans = {

    if (application.config.flyway.enabled) {

        flyway(Flyway) { bean ->
            bean.initMethod = 'migrate'
            dataSource = ref('dataSource')

        def sessionFactoryBeanDef = getBeanDefinition('sessionFactory')
        if (sessionFactoryBeanDef) {
            // make it depend on flyway
            sessionFactoryBeanDef.dependsOn = ['flyway'] as String[]

Hat tip to this StackOverflow question

Starting a new app

When initially building out my data model I like to leave flyway out of the picture and just use GORM's dbCreate options to automatically build out the tables. When I'm at a solid point I'll introduce flyway into the picture:

  • Set GORM's dbCreate to validate
  • Set up Flyway as described above
  • Make sure Flyway can set up your full schema from the beginning:
    • Use your db tool of choice to generate the CREATE TABLE statements for all of your app's existing tables. You can then put these into the initial flyway migration script.
    • Drop all your tables and start up the app and confirm that everything was created.