Sub topic 8
Repeatable migrations for views and similar objects
This chapter assumes you can write versioned SQL migrations (V1__...sql) and know that Flyway records each one in flyway_schema_history. It also assumes basic SQL for views and functions on PostgreSQL. The examples use the app_user table from the earlier chapters.
Why a second kind of migration exists
A versioned migration runs exactly once and must never change afterwards. That fits tables, columns, and indexes, where each change is a step from one state to the next.
Some database objects are better treated as a whole definition. A view is a stored query, and a function is a stored body of code. When it changes, you replace the full definition rather than describe a step. With versioned migrations, every edit to a view would need a new file that repeats the whole CREATE OR REPLACE VIEW statement. The history of the view would be scattered across many files, and git diff would show a new file instead of the lines that changed.
A repeatable migration solves this. It has no version number. Flyway applies it once, then applies it again whenever the file's checksum changes. You edit the file in place, and Git shows the real change.
Naming and ordering
A repeatable migration file starts with R__ (capital R, two underscores). It has no version.
src/main/resources/db/migration/
V1__create_app_user.sql
V2__add_display_name.sql
R__active_users_view.sqlFlyway turns the part after R__ into a description by replacing underscores with spaces. The file above gets the description active users view.
Three rules decide when repeatable migrations run:
- All pending versioned migrations run first, in version order.
- After that, Flyway considers every repeatable migration. It runs the ones that are new or whose checksum differs from the last applied run.
- Repeatable migrations run in order of their description. If two of them both run, the one that sorts first goes first.
The first rule matters most. A repeatable migration can always rely on the schema produced by the versioned migrations in the same run. A view that selects display_name is safe, because V2 has already added that column.
A first repeatable migration
This view is the example from the Topic Spine:
-- 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;Start the application and look at the history table:
SELECT installed_rank, version, description, type, success
FROM flyway_schema_history
ORDER BY installed_rank;| installed_rank | version | description | type | success |
|---|---|---|---|---|
| 1 | 1 | create app user | SQL | true |
| 2 | 2 | add display name | SQL | true |
| 3 | NULL | active users view | SQL | true |
The version column is NULL for repeatable migrations. That is how Flyway tells them apart from versioned ones.
Now change the view by editing the same file:
-- src/main/resources/db/migration/R__active_users_view.sql
CREATE OR REPLACE VIEW active_users AS
SELECT id, username, display_name, created_at
FROM app_user;On the next start, Flyway sees a new checksum and runs the file again. The history now has a fourth row with the same description, active users view, and a later installed_rank. Flyway compares the file with the most recent row for that description. Unchanged files are skipped, so restarting the application without edits does nothing.
Editing an applied repeatable migration is the intended workflow. Editing an applied versioned migration is the mistake that causes checksum validation errors.
What belongs in a repeatable migration
The script runs many times against a database that already contains the object. It must be safe to run again. That is the main design rule.
| Object | Good fit? | Notes |
|---|---|---|
| Views | Yes | Use `CREATE OR REPLACE VIEW`, with the limits below. |
| Functions and procedures | Yes | `CREATE OR REPLACE FUNCTION` works well. |
| Triggers | Yes, with care | Use `DROP TRIGGER IF EXISTS` followed by `CREATE TRIGGER`. |
| Reference data refresh | Sometimes | Only if written idempotently, for example with `INSERT ... ON CONFLICT DO UPDATE`. |
| Tables, columns, indexes | No | These are steps in a history. Use versioned migrations. |
Ordering between repeatable migrations
Repeatable migrations run in description order, not in the order you created them. If R__report_view.sql selects from a view defined in R__active_users_view.sql, the order is active users view, then report view. This works by accident, because "a" sorts before "r".
Do not rely on accidents. Name files so the order is visible:
R__10_active_users_view.sql
R__20_user_report_view.sqlThe descriptions are 10 active users view and 20 user report view, and they sort as you intend. A numeric prefix makes the dependency order explicit and keeps it stable when you add more files. Use the same width for every prefix, because sorting is textual and 9 sorts after 10.
The trap: versioned changes that collide with a view
Versioned migrations run before repeatable ones. That ordering causes a common failure. Suppose V4__drop_display_name.sql runs this:
ALTER TABLE app_user DROP COLUMN display_name;The existing active_users view still selects display_name. PostgreSQL refuses to drop a column that a view uses, and the migration fails before the repeatable file can be updated.
The fix is to drop the view in the versioned migration first:
-- V4__drop_display_name.sql
DROP VIEW IF EXISTS active_users;
ALTER TABLE app_user DROP COLUMN display_name;Now the view is gone, but the repeatable migration will not run again on its own, because its file did not change and its checksum matches the last applied run. You also need to edit R__10_active_users_view.sql in the same commit. You will normally do that anyway, to remove display_name from the select list. The edit changes the checksum, and the view is recreated at the end of the same run.
If a view is unchanged but a versioned migration drops it, the view stays missing until you touch the file. Even a comment change is enough, but make the edit deliberate and mention it in the commit message.
Removing a repeatable object
Deleting an R__ file does not drop the object from the database. Flyway only runs scripts that it finds. To remove a view, add a versioned migration:
-- V5__drop_active_users_view.sql
DROP VIEW IF EXISTS active_users;Then delete the R__ file in the same change, so the next run does not recreate the view. Check the result with flyway info or by querying flyway_schema_history.
Behaviour on failure
Each migration runs in its own transaction by default. PostgreSQL supports transactional DDL, so a failed repeatable script rolls back its changes. The failure stops the application during startup, and no history row is written. Fix the file and restart, and Flyway tries again.
On databases where DDL is not transactional, such as MySQL, a script can fail halfway and leave partial changes. Idempotent scripts make recovery much easier there, since you can run the fixed script again over whatever was left.
Checksum sensitivity
The checksum is derived from the content of the file. Any edit that changes the content changes the checksum and triggers a rerun. This includes added comments and edits to unused SQL. The rerun is cheap for a view, but a repeatable script that refreshes a large table would run again after a trivial edit. Keep repeatable scripts light, or make sure they are safe and fast to run repeatedly.
Summary of the differences
| Row | Versioned (`V`) | Repeatable (`R`) |
|---|---|---|
| Filename | `V2__add_display_name.sql` | `R__active_users_view.sql` |
| Identified by | Version number | Description |
| Runs | Once | When new, and again when the checksum changes |
| Run order | By version | After versioned ones, by description |
| Edit after applying | Not allowed | Expected |
| Typical use | Tables, columns, indexes, data steps | Views, functions, idempotent refreshes |
Exercise
Your R__active_users_view.sql file selects display_name. A new migration V4__drop_display_name.sql runs ALTER TABLE app_user DROP COLUMN display_name; and fails on PostgreSQL. Explain why it fails, and describe the changes to V4 and to the repeatable file that make the next start succeed.
Show answer
Versioned migrations run before repeatable ones, so when V4 runs, the old active_users view still exists and depends on display_name. PostgreSQL refuses to drop a column that a view uses. Add DROP VIEW IF EXISTS active_users; before the ALTER TABLE in V4. Also edit the repeatable file to remove display_name from its select list. The edit changes the checksum, so Flyway reapplies the file after V4 and recreates the view. Without an edit, the checksum would match the last applied run and the view would stay dropped.