Sub topic 15
Production practices and customizing migration at startup
This chapter assumes you can already write versioned migrations, read flyway_schema_history, and configure spring.flyway properties. It covers how to customize the startup step and which habits keep migrations safe in production.
Customizing what runs at startup
By default, Spring Boot calls flyway.migrate() before the EntityManagerFactory is created. To change that step, define a FlywayMigrationStrategy bean. Boot then calls your strategy instead of migrate(), so you must call migrate() yourself.
This strategy logs the pending migrations before it applies them:
import org.flywaydb.core.Flyway;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.flyway.FlywayMigrationStrategy;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
class FlywayConfig {
private static final Logger log = LoggerFactory.getLogger(FlywayConfig.class);
@Bean
FlywayMigrationStrategy migrationStrategy() {
return flyway -> {
int pending = flyway.info().pending().length;
log.info("Applying {} pending migration(s)", pending);
flyway.migrate();
};
}
}Use this hook for logging, metrics, or a deliberate flyway.repair() call. Don't call repair() on every start, because it hides checksum problems that you should investigate.
Running migrations safely in production
Decide where migrations run
Running migrations inside the application is simple. It has two costs:
- A slow migration delays startup, and an orchestrator may kill the pod before it finishes.
- The application's database user needs DDL privileges for its whole lifetime.
Many teams run migrations as a separate step instead. A CI/CD job or an init container runs them with a dedicated user, and the application starts with spring.flyway.enabled=false and a least-privilege user. If you keep migrations in the application, set spring.flyway.user and spring.flyway.password so Flyway connects with its own credentials.
Multiple instances
Several instances may start at once. Flyway takes a lock on the database, such as an advisory lock on PostgreSQL. One instance migrates while the others wait, and then they find nothing pending. This works, but the waiting instances start late.
Habits that prevent incidents
- Keep
spring.flyway.clean-disabled=true.cleandrops every object in the schema. Recent Flyway versions disable it by default, but set it explicitly in production. - Back up before any migration that deletes or rewrites data.
- Rehearse each release's migrations against a copy of production data. A change that takes milliseconds on a test database can lock a large table for minutes.
- Keep
validate-on-migrateenabled so a modified migration stops the deployment. - Use expand and contract so the previous application version still works while the new schema is live. This allows a rollback of the application without a database rollback.
- Treat applied migrations as immutable. Fix a mistake with a new, higher-numbered migration.
Failed migrations
On databases without transactional DDL, such as MySQL, a failed migration can leave partial changes and a failed row in the history table. On PostgreSQL, DDL runs in a transaction, so a failure rolls the migration back completely. In both cases, fix the cause first. Then run repair to remove the failed row and re-run. Check the database state before you do.
| Practice | Reason |
|---|---|
| Separate migration user | Limits what the running application can change |
| `clean-disabled=true` | Prevents accidental data loss |
| Rehearse on a production copy | Exposes long locks before release |
| Expand and contract | Allows an application rollback |
Exercise
Your team wants to stop slow migrations from delaying application startup and to give the application a database user that cannot alter tables. What would you change?
Show answer
Run Flyway in a separate deployment step, such as a CI job or init container, using a dedicated user with DDL privileges. Set spring.flyway.enabled=false for the application, and have it connect with a least-privilege user.
Exercise
You define a FlywayMigrationStrategy bean that only logs a message. After deploying, no migrations are applied. Why, and how do you fix it?
Show answer
A custom strategy replaces the default call to migrate(). Call flyway.migrate() inside the strategy.