Sub topic 2
Reading the `flyway_schema_history` Table
Assume you know basic Spring Boot configuration, SQL, and relational databases. This chapter focuses on how Flyway records migrations and how Spring Boot coordinates Flyway with Hibernate.
Reading the schema history
Flyway creates a flyway_schema_history table in the configured schema. Each row records one migration attempt. Versioned migrations have a version such as 1 or 2. The table also records the migration description, script name, checksum, execution time, and whether it succeeded.
SELECT installed_rank, version, description, type,
script, checksum, installed_on, execution_time, success
FROM flyway_schema_history
ORDER BY installed_rank;A successful versioned migration normally appears once. A failed migration remains visible with success = false. Fix the cause, then run Flyway again. Do not edit an already-applied migration to change its meaning. Add a new migration instead.
The checksum helps Flyway detect changes to applied files.
Configuring Flyway and Hibernate
Common settings belong under spring.flyway:
spring:
flyway:
enabled: true
locations: classpath:db/migration
validate-on-migrate: true
jpa:
hibernate:
ddl-auto: validateSpring Boot runs Flyway during application startup after creating the data source. Startup fails when validation detects a changed migration or a failed migration that Flyway cannot repair automatically.
Hibernate should validate the schema, not create or update it. With ddl-auto: validate, Flyway owns database changes and Hibernate checks that entity mappings match the resulting tables. Using update or create alongside Flyway can produce changes outside migration history.
> callout warning > Treat flyway_schema_history as migration metadata. Do not manually delete rows or change checksums unless you understand the recovery procedure and have a backup.
Repeatable migrations
Repeatable migrations use names such as R__active_users_view.sql. Flyway runs them after pending versioned migrations and runs them again when their checksum changes. They suit views, stored procedures, and functions that can be recreated as a whole.
CREATE OR REPLACE VIEW active_users AS
SELECT id, username, display_name
FROM app_user;Use versioned migrations for ordered table changes. Use repeatable migrations for replaceable database objects.
Exercise
What should you do after changing an applied versioned migration?
Show answer
Restore the original file and create a new, higher-numbered migration for the change.