Baseline your database with Flyway and Gradle
From time to time it is necessary to baseline your database, like when you are introducing Flyway to an existing database or when some changes happened to the DB that were not managed by Flyway.
Baselining a DB at a specific version tells Flyway to ignore all migrations up to and including that version when doing a migration. Flyway will now ensure that only migrations after the baseline version are applied whenever it is invoked.
Baseline using Flyway task in Gradle
Step 1: Set the baseline version in the Flyway Gradle task definition
The task definition might look like this. The important property for this post is “baselineVersion”. If you’re introducing Flyway for the first time the default value of “1” is sufficient and you don’t need to specify it. In that case your first Flyway migration will be version “2”, for example “V2__My_first_flyway_migration.sql”.
flyway {
url = "jdbc:postgresql://localhost:5432/mydatabase?user=username&password=supersecret"
locations = ["db/migrations"]
baselineVersion = 17
}
Step 2: Drop the existing 'flyway_schema_history’
table
DROP TABLE IF EXISTS flyway_schema_history;
Step 3: Execute baseline task
./gradlew flywayBaseline