Sub topic 8
Repeatable migrations for views and similar objects
This chapter assumes you have a Spring Boot project with Flyway running at startup, you can write versioned migrations, and you know what the flyway_schema_history table records.
What a repeatable migration is
A versioned migration runs once and is never changed. Some database objects are better managed as one definition that you edit over time. Views, functions and stored procedures are examples. A repeatable migration fits these objects.
A repeatable migration has no version. Its file name starts with R__, followed by a description:
-- src/main/resources/db/migration/R__active_users_view.sql
CREATE OR REPLACE VIEW active_users AS
SELECT id, username, display_name
FROM app_user;Flyway computes a checksum of the file. If the checksum differs from the one stored in the schema history, Flyway applies the file again. An unchanged file is skipped. To change the view, you edit this one file. You don't add a new numbered file.
When Flyway runs them
Flyway applies repeatable migrations after all pending versioned migrations. The view above uses display_name, which V2__add_display_name.sql adds. The repeatable file therefore sees the final table shape in the same run.
If several repeatable migrations are pending, Flyway runs them in alphabetical order of description. Name them so dependencies come first when one object builds on another, for example R__01_base_view.sql and R__02_report_view.sql.
How they appear in the history
Repeatable rows in flyway_schema_history have a NULL version. Each re-application adds a new row with the new checksum. Flyway compares the newest row for that description with the file on disk.
SELECT version, description, type, checksum, success
FROM flyway_schema_history
ORDER BY installed_rank;Writing safe repeatable files
The file must be safe to run on a database where the object already exists, and on one where it doesn't. Use CREATE OR REPLACE where your database supports it.
PostgreSQL's CREATE OR REPLACE VIEW has a limit. The new query must keep the existing columns, with the same names, types and order. It may only add columns at the end. To remove or rename a column, drop the view first:
DROP VIEW IF EXISTS active_users;
CREATE VIEW active_users AS
SELECT id, username
FROM app_user;Dropping a view also drops nothing else by default. It fails if another object depends on the view. Check dependent views before you change a shared one.
Common errors
- Using repeatable files for tables. Tables hold data, so a re-run
CREATE TABLEcan't safely redefine one. Keep tables in versioned migrations. - Breaking the view from a versioned migration. If
V5drops a column the view uses, the migration fails because the view depends on that column. Drop or redefine the view in that versioned migration, and let the repeatable file recreate it afterward. The repeatable file only re-runs if its checksum changed, so make a small edit to it, such as a comment, to force a new run. - Non-idempotent contents. A repeatable file containing
INSERTstatements adds the rows again on every change. Keep its contents to definitions. - Editing whitespace. Any change to the file content changes the checksum and triggers a re-run. This is harmless for
CREATE OR REPLACE, but it is still a run.
Exercise
You change R__active_users_view.sql and start the application. Another repeatable file, R__audit_function.sql, is unchanged. A new V6__add_status.sql is also pending. Which migrations run, and in what order?
Show answer
Flyway runs V6__add_status.sql first, because versioned migrations come before repeatable ones. Then it runs R__active_users_view.sql, because its checksum changed. It skips R__audit_function.sql, because its checksum matches the stored one.