Sub topic 2
Adding Flyway to a Spring Boot project
This chapter adds Flyway to a Spring Boot application and runs a first migration at startup. It assumes you know Spring Boot starters, application.yml, Maven or Gradle dependencies, basic SQL DDL, and a relational database such as PostgreSQL. A datasource must already be configured.
What Flyway does here
Flyway is a library that applies numbered SQL scripts to your database in order. It records each applied script in a table, so a script runs only once per database. Spring Boot detects Flyway on the classpath and runs it for you during startup.
Add the dependencies
Add flyway-core. Recent Flyway versions also need a database-specific module. For PostgreSQL that is flyway-database-postgresql. Spring Boot's dependency management supplies the versions, so you don't write them.
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
</dependency>
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-database-postgresql</artifactId>
</dependency>If you use Gradle, add the same two artifacts as implementation dependencies.
Configure Flyway and Hibernate
With the dependencies present, Flyway is enabled by default and looks for scripts in classpath:db/migration. The configuration below states those defaults explicitly and tells Hibernate to leave schema creation alone.
spring:
flyway:
enabled: true
locations: classpath:db/migration
validate-on-migrate: true
jpa:
hibernate:
ddl-auto: validateSetting ddl-auto: validate means Flyway owns the schema. Hibernate only checks at startup that your entities match the tables Flyway created. If they don't match, the application fails to start, which is what you want.
Create the first migration
Put a file in src/main/resources/db/migration. A versioned migration is named V<version>__<description>.sql, with two underscores between the version and the description.
-- src/main/resources/db/migration/V1__create_app_user.sql
CREATE TABLE app_user (
id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
username VARCHAR(100) NOT NULL UNIQUE,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);Start the application. Flyway connects with the datasource settings, creates its history table if it is missing, runs V1, and records it. On the next start, Flyway sees V1 in the history and skips it.
Startup order
The sequence matters because Hibernate validation depends on the tables existing.
Spring Boot makes the JPA entity manager factory depend on Flyway, so migrations always finish before validation.
Common errors
| Symptom | Likely cause |
|---|---|
| Migration is ignored | The file name has one underscore, or it is outside the configured `locations` |
| Unsupported database error | The database-specific module is missing |
| `Schema-validation: missing table` | Entities refer to a table no migration creates |
| Nothing runs | `spring.flyway.enabled` is `false`, or no datasource is configured |
Exercise
You add flyway-core and a V1__create_app_user.sql file, but the file is named V1_create_app_user.sql. What happens at startup, and how do you fix it?
Show answer
Flyway does not recognise the file as a versioned migration, because the version and description must be separated by two underscores. The table is not created, and with ddl-auto: validate Hibernate then fails because the app_user table is missing. Rename the file to V1__create_app_user.sql.