Sub topic 6
Configuring Flyway with spring.flyway properties
This chapter assumes you can add Flyway to a Spring Boot project, write a versioned migration such as V1__create_app_user.sql, and read application.yml. Spring Boot auto-configures Flyway as soon as flyway-core is on the classpath. The spring.flyway.* properties let you change that default behavior without writing any Java.
Where the properties fit
Spring Boot binds every spring.flyway.* property to the auto-configured Flyway instance. It then runs the migration before the JPA EntityManagerFactory is created, so the schema is ready when Hibernate starts.
A baseline configuration
The following configuration makes the defaults explicit. It is a good starting point for a new project.
spring:
flyway:
enabled: true
locations: classpath:db/migration
validate-on-migrate: true
table: flyway_schema_history
schemas: public
default-schema: publicSpring Boot uses the application's spring.datasource settings unless you tell Flyway otherwise.
The properties you will use most
| Property | Default | Purpose |
|---|---|---|
| `spring.flyway.enabled` | `true` | Turns the startup migration on or off. |
| `spring.flyway.locations` | `classpath:db/migration` | Where Flyway looks for migration files. Accepts a comma-separated list. |
| `spring.flyway.validate-on-migrate` | `true` | Checks the checksums of applied migrations before applying new ones. |
| `spring.flyway.table` | `flyway_schema_history` | Name of the history table. |
| `spring.flyway.schemas` | none | Schemas Flyway manages. Flyway creates them if they are missing. |
| `spring.flyway.baseline-on-migrate` | `false` | Baselines a non-empty database that has no history table. |
| `spring.flyway.baseline-version` | `1` | Version assigned to the baseline. |
| `spring.flyway.out-of-order` | `false` | Allows applying a lower version than one already applied. |
| `spring.flyway.clean-disabled` | `true` | Blocks the destructive `clean` command. |
Leave clean-disabled at true everywhere except throwaway local databases. The clean command drops every object in the managed schemas.
Using a separate datasource
By default Flyway reuses the application's datasource. Applications often connect with a restricted user that cannot run DDL. In that case give Flyway its own credentials:
spring:
datasource:
url: jdbc:postgresql://localhost:5432/appdb
username: app_runtime
password: ${APP_DB_PASSWORD}
flyway:
url: jdbc:postgresql://localhost:5432/appdb
user: app_migrator
password: ${FLYWAY_DB_PASSWORD}When you set spring.flyway.url, user and password, Flyway opens its own connection and the application keeps the lower-privilege account. Read passwords from environment variables, never from a committed file.
Splitting locations by environment
Because locations takes a list, you can add environment-specific scripts. For example, a dev profile can load seed data that production never sees.
# application-dev.yml
spring:
flyway:
locations: classpath:db/migration,classpath:db/dev-dataKeep the versions in both folders in one sequence. Flyway merges all locations into a single ordered list, so two files with the same version cause an error.
Common errors
- Files are never found. The path is wrong or the files sit outside
src/main/resources. Check thatlocationsmatches the folder exactly, and look attarget/classes/db/migrationafter a build. Found non-empty schema(s) without schema history table. You pointed Flyway at an existing database. Either use a baseline (covered in a later chapter) or a fresh schema.- Validation fails after you edit an applied file.
validate-on-migratecompares checksums. Add a new migration instead of editing the old one. - Migrations are silently skipped in tests. A test profile may set
spring.flyway.enabled: false. Check which profile is active.
Exercise
Your application connects as a database user without permission to create tables. How do you let Flyway run migrations without giving the application that permission?
Show answer
Set spring.flyway.url, spring.flyway.user and spring.flyway.password to a separate account that has DDL rights. Flyway then uses its own connection, and spring.datasource keeps the restricted runtime user.