Sub topic 6
Configuring Flyway with spring.flyway properties
This chapter assumes you can already add Flyway to a Spring Boot project, write a versioned migration, and read flyway_schema_history. It also assumes you know how application.yml and Spring profiles work. Here you will learn which spring.flyway properties matter, which defaults you should keep, and which misconfigurations cause the most common startup failures.
How the properties reach Flyway
Spring Boot's FlywayAutoConfiguration binds every spring.flyway.* property to a FlywayProperties object. It copies those values onto a FluentConfiguration, builds a Flyway instance, and runs it during startup. Any property you leave out keeps Flyway's own default.
Because these are ordinary Spring properties, all the usual sources work: application.yml, profile files such as application-prod.yml, command-line arguments, and environment variables such as SPRING_FLYWAY_LOCATIONS.
The core properties
A realistic baseline configuration looks like this:
spring:
flyway:
enabled: true
locations: classpath:db/migration
table: flyway_schema_history
default-schema: public
schemas: public
validate-on-migrate: true
clean-disabled: true
connect-retries: 5The table below lists the properties you will touch most often, with their defaults.
| Property | Default | Purpose |
|---|---|---|
| `enabled` | `true` | Turns Flyway auto-configuration on or off. |
| `locations` | `classpath:db/migration` | Where Flyway scans for migrations. |
| `table` | `flyway_schema_history` | Name of the history table. |
| `schemas` | the connection's default schema | Schemas Flyway manages. The first one holds the history table unless `default-schema` is set. |
| `default-schema` | the first entry in `schemas` | Schema Flyway uses for its connection and history table. |
| `validate-on-migrate` | `true` | Checks applied migrations against the files before migrating. |
| `baseline-on-migrate` | `false` | Baselines a non-empty schema that has no history table. |
| `out-of-order` | `false` | Allows applying a lower version after a higher one. |
| `clean-disabled` | `true` | Blocks the destructive `clean` command. |
| `connect-retries` | `0` | Retries the database connection at startup. |
| `fail-on-missing-locations` | `false` | Fails if a configured location does not exist. |
The sections below cover the properties that most often need a deliberate decision.
locations
locations takes a list. Each entry is classpath: or filesystem: followed by a path. In YAML you can write a list, and in a properties file you use commas:
spring:
flyway:
locations:
- classpath:db/migration
- classpath:db/seedFlyway scans each location recursively, so subfolders work. Java migrations in a db.migration package are found through the same classpath:db/migration location.
Spring Boot also expands a {vendor} placeholder to the database type, for example postgresql. That lets one codebase carry separate SQL per database:
spring:
flyway:
locations: classpath:db/migration/{vendor}All locations are merged into one sequence ordered by version. Two files with the same version in different locations are a conflict, and Flyway reports an error.
schemas, default-schema and table
By default Flyway manages whatever schema the connection uses and puts flyway_schema_history there. If you list schemas, Flyway creates any that are missing (controlled by create-schemas, default true). It also uses the first one for the history table unless default-schema says otherwise.
Change table only when two applications share one schema and each needs its own history. The names must differ, or the two applications will see each other's migrations as missing or unexpected.
validate-on-migrate
Leave this true. It makes Flyway compare the checksum of every applied migration with the file on the classpath before it applies anything new. The next chapter covers failures and repairs in detail. Turning it off hides edited migrations instead of fixing them.
clean-disabled
clean drops every object in the managed schemas. Recent Flyway versions disable it by default, and you should keep it that way. Enable it only in a profile used for throwaway local databases:
# application-local.yml
spring:
flyway:
clean-disabled: falseconnect-retries
Flyway tries the connection once by default. In containers the application often starts before the database accepts connections. Setting connect-retries: 10 makes Flyway retry, waiting about a second between attempts with exponential backoff, so a slow database does not crash the application.
Using a separate database user
Many teams give the running application a restricted user that can read and write rows but not change tables. Flyway needs DDL rights, so give it its own connection:
spring:
datasource:
url: jdbc:postgresql://db:5432/shop
username: shop_app
password: ${APP_DB_PASSWORD}
flyway:
url: jdbc:postgresql://db:5432/shop
user: shop_owner
password: ${FLYWAY_DB_PASSWORD}When spring.flyway.url or user is set, Spring Boot builds a dedicated DataSource for Flyway and closes it after migration. Otherwise Flyway reuses the primary DataSource. If you define the Flyway data source as a bean instead, annotate it with @FlywayDataSource so Boot selects it over the primary one.
Placeholders
Flyway replaces ${name} tokens inside SQL migrations with values you define. This is useful for values that differ per environment, such as a schema or role name:
spring:
flyway:
placeholders:
app_role: shop_app-- src/main/resources/db/migration/V4__grant_access.sql
GRANT SELECT, INSERT, UPDATE, DELETE ON app_user TO ${app_role};Flyway also uses the ${...} syntax by default, which collides with any SQL or procedural code that contains a literal ${. A missing placeholder value fails the migration with an error naming it. Set placeholder-replacement: false to turn replacement off, or change the delimiters with placeholder-prefix and placeholder-suffix.
Placeholder values are part of what Flyway executes, but the checksum covers the file text and not the substituted values. Changing a placeholder value later will therefore not trigger a validation error, and it will not re-run the migration either.
Naming conventions
The defaults (V prefix, R prefix, __ separator, .sql suffix) match the file names used in this tutorial. You can override them with sql-migration-prefix, repeatable-sql-migration-prefix, sql-migration-separator and sql-migration-suffixes. Changing them is rarely worth it, because other developers and tools expect the standard form. One useful property is validate-migration-naming. When it is true, a file such as V2_add_column.sql (one underscore) fails validation instead of being silently ignored.
Profiles: one set of files, different settings
Keep shared settings in application.yml and put environment differences in profile files. Do not keep different migrations per environment. Environments that run different SQL stop being comparable, and a migration tested in one will not behave the same in production.
# application.yml
spring:
flyway:
locations: classpath:db/migration
fail-on-missing-locations: true
---
spring:
config:
activate:
on-profile: local
flyway:
clean-disabled: false
connect-retries: 0
---
spring:
config:
activate:
on-profile: prod
flyway:
connect-retries: 10
validate-on-migrate: trueA test-data location is the one reasonable exception. Add classpath:db/testdata to locations in a test or local profile only. Give its files versions that cannot collide with real ones, or use repeatable R__ files, which sort after all versioned migrations.
Common errors
- "Found non-empty schema(s) without schema history table": the schema already has tables but no
flyway_schema_history. Baseline it deliberately; do not setbaseline-on-migrate: truejust to silence the message. - "No migrations found" and an empty database:
locationspoints at the wrong folder. Check the path andfail-on-missing-locations. - "Unable to obtain connection" in containers: the database is still starting. Raise
connect-retries. - Permission denied during migration: the Flyway user lacks DDL rights, or the dedicated
spring.flyway.userwas never set and the restricted app user is being used. - A file that never runs: the name does not follow
V<version>__<description>.sql. Enablevalidate-migration-naming. - "Unknown property" warnings in the IDE: the property name is misspelled. Spring binding is relaxed about
validate-on-migrateversusvalidateOnMigrate, but not about spelling.
Checking the effective configuration
To see what Flyway actually received, raise the logging level for its core package:
logging:
level:
org.flywaydb: DEBUGThe startup log then lists the database, the locations scanned, and the number of migrations found. If Spring Boot Actuator is on the classpath, the /actuator/flyway endpoint (exposed with management.endpoints.web.exposure.include: flyway) returns each migration's state and checksum.
Exercise
Your application runs as a database user that cannot create tables, and Flyway fails with "permission denied" at startup. Which properties do you add, and what must be true of the user they refer to?
Show answer
Set spring.flyway.url, spring.flyway.user and spring.flyway.password (the password read from an environment variable). Spring Boot then gives Flyway a dedicated DataSource. The user in spring.flyway.user must have DDL rights on the managed schemas, and the application's own spring.datasource user can keep its restricted rights.
Exercise
A teammate mistypes locations as classpath:db/migrations. The application starts and no tables exist. Why did startup succeed, and which property makes it fail instead?
Show answer
By default Flyway treats a missing location as an empty one, so it finds zero migrations and does nothing. Setting spring.flyway.fail-on-missing-locations to true makes Flyway fail at startup when a configured location does not exist.