Sub topic 5
Reading the flyway_schema_history table
This chapter assumes you have Flyway running in a Spring Boot project with at least one versioned migration, such as V1__create_app_user.sql. You should also be able to run simple SQL queries against PostgreSQL.
What the table is for
Flyway has to know which migrations a database already has. It stores that information in a table named flyway_schema_history, in the default schema of the connection. Flyway creates the table the first time it runs against a database.
At startup Flyway compares two lists: the migration files on the classpath and the rows in this table. A file with no row is pending, so Flyway applies it. A row whose file has changed or disappeared is a problem, which the checksum chapter covers.
Querying the table
After the V1 and V2 migrations from the previous chapter have run, query the table like any other:
SELECT installed_rank, version, description, type, script, checksum, installed_by, installed_on, execution_time, success
FROM flyway_schema_history
ORDER BY installed_rank;The result looks roughly like this:
| installed_rank | version | description | type | script | success |
|---|---|---|---|---|---|
| 1 | 1 | create app user | SQL | V1__create_app_user.sql | true |
| 2 | 2 | add display name | SQL | V2__add_display_name.sql | true |
The columns
Each applied migration produces one row.
| Column | Meaning |
|---|---|
| `installed_rank` | The order in which Flyway applied the migrations. It always increases by one. |
| `version` | The version from the file name. It is empty for repeatable migrations. |
| `description` | The text after the double underscore, with underscores shown as spaces. |
| `type` | `SQL`, `JDBC` (a Java migration), `BASELINE` and so on. |
| `script` | The file name, or the class name for a Java migration. |
| `checksum` | A number computed from the file contents. Flyway uses it to detect edits. |
| `installed_by` | The database user that ran the migration. |
| `installed_on` | The time the migration was applied. |
| `execution_time` | How long it took, in milliseconds. |
| `success` | Whether the migration finished without error. |
Two columns deserve extra attention. installed_rank records the real order of execution, which can differ from version order when out-of-order migrations are enabled. checksum is what makes an applied migration effectively read-only.
How a run is recorded
The diagram shows what happens for each pending migration.
On PostgreSQL, DDL statements are transactional. If a migration fails, the whole migration rolls back and the history table has no row for it. Databases that cannot roll back DDL, such as MySQL, may leave a half-applied migration. Flyway then records a row with success = false, and you must repair it by hand before the next run.
Checking state without the database
You can also ask Flyway itself. The Flyway bean that Spring Boot creates exposes the same data as Java objects:
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.println(info.getVersion() + " "
+ info.getDescription() + " "
+ info.getState());
}
};
}
}getState() returns values such as SUCCESS, PENDING and FAILED. Pending files appear in this list even though they have no row yet.
Common mistakes
- Editing or deleting rows by hand. This breaks the link between files and history, so Flyway's validation will fail or it will re-run a migration. Use
flyway repairinstead when you need to fix the table. - Dropping the table to start over. Flyway then treats the database as unmigrated and tries to re-run
V1against existing tables. - Reading
installed_onas the order of changes. Useinstalled_rank.
Exercise
A migration V4__add_index.sql appears in flyway.info().all() with state PENDING, but the history table has no row for it. Is this a problem?
Show answer
No. PENDING means Flyway found the file but has not applied it yet. The row is inserted only after the migration runs, so the file and the missing row are consistent.
Exercise
Which column would you check to find out in what order Flyway actually applied the migrations, and why not use version?
Show answer
installed_rank. The version column shows the number in the file name, but with out-of-order migrations enabled a lower version can be applied after a higher one.