Flyway is a great tool to manage your database migrations. You can create incremental database migrations which will be applied on database. The concept is similar to Rails ActiveRecord migrations

But unlike Rails ActiveRecord migrations, there is no built in create migration file feature present in Flyway Gradle plugin and it is not a priority for plugin developers.

I have created following custom Gradle task to generate flyway db migration file. I have kept timestamped migration file name format similar to Rails database migration file name format

Include following snippet in build.gradle.

import java.time.ZoneOffset
import java.time.ZonedDateTime
import java.time.format.DateTimeFormatter

class GenerateMigration extends DefaultTask {
    String migrationDir

    @TaskAction
    void createMigration() {
        def now = ZonedDateTime.now(ZoneOffset.UTC)
        def timeFormatter = DateTimeFormatter.ofPattern("uuuuMMddhhmmss")
        def timePart = now.format(timeFormatter)

        def namePart = new StringBuilder()
        getProject().property('desc').eachWithIndex {
            c, i -> if(c.toUpperCase() == c) {
                if (i != 0) { namePart.append("_") }
                namePart.append("${c.toLowerCase()}")
            } else {
                namePart.append(c)
            }
        }

        def filename = "V${timePart}__$namePart"
        def file = "${getProject().getProjectDir()}/$migrationDir/${filename}.sql"
        println()
        print("Creating $file : ")
        def success = new File(file).createNewFile()
        print(success ? "Success" : "Failed")

    }
}

task genmig (type: GenerateMigration) {
    group 'project'
    description 'Creates a flyway db migration file'
    // Specify your db migration directory here
    migrationDir 'src/main/resources/db/migration'
}

How to use?

Just run genmig task with desc property.

gradle genmig -Pdesc=TestForGenMig
> Task :genmig 

Creating /home/user/mysrc/gradle-project/src/main/resources/db/migration/V20180818043331__test_for_gen_mig.sql : Success

It will create new db migration SQL file src/main/resources/db/migration/V20180818043331__test_for_gen_mig.sql.

I hope someone finds this useful.