Sub topic 5
Reading the flyway_schema_history table
This chapter assumes you can already run a Spring Boot application with Flyway. You should have written at least one versioned migration, such as V1__create_app_user.sql, and be able to query PostgreSQL with psql or an IDE. Here you will learn what Flyway records in its own bookkeeping table, how to read each column, and which rows signal trouble.
What the table is for
Flyway does not decide what to run by scanning your database for differences. It compares two lists:
- The migration files it finds on the classpath, for example in
db/migration. - The rows in a table it creates and maintains, called
flyway_schema_history.
The table is the record of what has already been applied. If a migration file has a matching successful row, Flyway skips it. If it has no row, Flyway treats it as pending and runs it. Every later chapter on validation, baselining and team workflow depends on this table, so it pays to read it fluently.
Flyway creates the table the first time it runs against a schema. It lives in the connection's default schema unless you change that.
Querying the table
Run this against the database after your application has started once:
SELECT installed_rank, version, description, type, script,
checksum, installed_by, installed_on, execution_time, success
FROM flyway_schema_history
ORDER BY installed_rank;With the migrations from earlier chapters (V1, V2, a Java V3, and the repeatable view), the result looks like this. The checksums and times are illustrative, so yours will differ.
| installed_rank | version | description | type | script | checksum | success |
|---|---|---|---|---|---|---|
| 1 | 1 | create app user | SQL | V1__create_app_user.sql | 1803421877 | true |
| 2 | 2 | add display name | SQL | V2__add_display_name.sql | -914402318 | true |
| 3 | 3 | Backfill display name | JDBC | db.migration.V3__Backfill_display_name | NULL | true |
| 4 | NULL | active users view | SQL | R__active_users_view.sql | 552310046 | true |
The columns installed_by, installed_on and execution_time are left out of the table above to keep it narrow. They are covered below.
Column by column
installed_rank
This is the primary key. It increases by one for every row Flyway writes, so it records the order in which things were actually applied. It is not the same as version order. When out-of-order migrations are enabled (a later chapter covers this), a migration with version 2.5 can be applied after version 4 and will get a higher rank.
version
This is the version string taken from the filename: V2__add_display_name.sql gives 2. Versions are compared part by part as numbers, so 1.10 is newer than 1.9. Repeatable migrations have no version, so this column is NULL for them.
description
Flyway takes this from the filename and replaces the underscores with spaces. Renaming only the description of an already-applied file does not change what runs, but it will trigger a validation error because the description is compared too.
type
This says how Flyway ran the entry. The common values are:
| Type | Meaning |
|---|---|
| SQL | A versioned or repeatable `.sql` file |
| JDBC | A Java migration, such as one extending `BaseJavaMigration` |
| BASELINE | A marker row created by the baseline command or `baseline-on-migrate` |
| SCHEMA | A row noting that Flyway created the schema itself |
script
For SQL migrations this is the file name. For Java migrations it is the fully qualified class name, which is why the V3 row above shows db.migration.V3__Backfill_display_name.
checksum
This is an integer calculated from the content of a SQL file. Flyway recalculates it on every start and compares it with the stored value. If they differ, validation fails for versioned migrations. For repeatable migrations, a different checksum means "run this again". Java migrations store NULL unless the class overrides getChecksum(), so Flyway cannot detect edits to them by default. Baseline rows also have no checksum.
installed_by
This is the database user that ran the migration. In production it is usually the application's account or a dedicated migration account. Reading it tells you who applied a change, but not which person triggered the deploy.
installed_on
This is the timestamp at which the row was written. It is the time the migration finished, so it is useful for lining up a schema change with an incident timeline.
execution_time
This is the duration in milliseconds. A long time here on a copy of production data is an early warning that a migration will hold locks for a while.
success
This is a boolean. true means the migration completed. false means it started and failed. On PostgreSQL, DDL is transactional, so a failed migration normally rolls back together with its history row and leaves nothing behind. Databases such as MySQL cannot roll back DDL, so a half-applied migration can leave a row with success = false. Flyway refuses to continue until you fix the database state and run repair.
Reading the table from Java
You can see the same information without SQL by asking the Flyway bean. This is handy in a diagnostic endpoint or a startup log.
import org.flywaydb.core.Flyway;
import org.flywaydb.core.api.MigrationInfo;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
class MigrationReport {
@Bean
CommandLineRunner printMigrations(Flyway flyway) {
return args -> {
for (MigrationInfo info : flyway.info().all()) {
System.out.printf("%s | %s | %s | %s%n",
info.getVersion(),
info.getDescription(),
info.getState(),
info.getInstalledOn());
}
};
}
}info().all() merges the files on the classpath with the rows in the table. That is why it can report states that a plain table query cannot, such as PENDING for a file with no row yet.
| State | What it means |
|---|---|
| SUCCESS | Applied, and the file still exists |
| PENDING | File found, not yet applied |
| FAILED | Row exists with `success = false` |
| MISSING_SUCCESS | Row exists, but the file is no longer on the classpath |
| OUTDATED | A repeatable migration whose checksum changed and is waiting to be re-run |
| SUPERSEDED | An older run of a repeatable migration that has since been re-applied |
| IGNORED | A file with a lower version than the latest applied one, skipped because out-of-order is off |
How repeatable migrations appear
A repeatable migration gets a new row each time its checksum changes. If you edit R__active_users_view.sql and restart, the table gains a fifth row with a higher installed_rank, version still NULL, and a new checksum. The earlier row stays, and Flyway shows it as SUPERSEDED. Repeatable migrations always run after all pending versioned migrations in the same run.
Where the table lives and what it is called
Flyway puts the table in the first schema it manages, which by default is the connection's current schema. You can change this with spring.flyway properties:
spring:
flyway:
table: schema_history
default-schema: app
schemas: appWith these settings, Flyway keeps its history in app.schema_history. Changing table on a database that already has a history means Flyway will not find the old rows and will try to run every migration again. Choose the name before the first deployment and do not change it later.
Rules for handling the table
A few more points to keep in mind:
- The table is created by Flyway. Hibernate with
ddl-auto: validateignores it, because no entity maps to it. - Never reuse a version number. A second
V2__...file produces a duplicate-version error during validation, because the table'sversionmust stay unique among versioned rows. - A file that disappears from the repository after being applied shows up as
MISSING_SUCCESS. By default validation fails on it. This protects you from a branch that quietly dropped a migration. - Back up the table together with the rest of the database. Restoring data without the history table makes Flyway believe nothing has run.
Exercise
After deploying, you see a row in flywayschemahistory with version NULL, type SQL, script R_activeusersview.sql, and a higher installedrank than an earlier row for the same script. What does this tell you, and does it indicate a problem?
Show answer
It is a repeatable migration that was re-applied because its file content changed, which produced a new checksum. This is normal and not a problem. The older row remains as history and is reported as SUPERSEDED, and repeatable migrations run after all pending versioned migrations.
Exercise
Your application connects to a PostgreSQL database and a new migration V5 fails halfway through its statements. Will you normally find a row for V5 with success = false? What would be different on a database without transactional DDL?
Show answer
On PostgreSQL you normally will not, because DDL is transactional and the whole migration, including its history row, rolls back. The table still ends at V4 and V5 stays pending. On a database that cannot roll back DDL, part of V5 may remain applied and a row with success = false is written. Flyway then refuses to continue until you clean up the partial changes and run repair.