Scrivio Interview library
ConceptFoundations6 min read · updated 12 November 2026

How does Flyway apply migrations in a Spring Boot application? Explain versioned and repeatable migrations and the schema history table.

When Flyway is on the classpath, Spring Boot runs it at startup, before JPA starts, applying SQL scripts from classpath:db/migration by default. Versioned migrations such as V1__init.sql run exactly once each, in version order; repeatable migrations such as R__views.sql run again whenever their checksum changes, after all pending versioned ones. Flyway records every applied script and its checksum in the flyway_schema_history table, which is how it knows what to run next; in Spring Boot 4 the auto-configuration moved into its own module and starter, so check your version's dependencies.

Why this question is asked

Every application with a relational database needs a reliable way to change the schema as the code evolves, and to make every environment, from a developer laptop to production, end up with the same structure. Flyway is the most common tool for that in Spring Boot projects because it is simple: we write plain SQL files with a naming convention, and it runs the ones that have not run yet. Interviewers ask this question to check that we know what actually happens at startup, because most migration incidents come from misunderstanding exactly that. In this answer, we will set Flyway up in Spring Boot, look at versioned migrations, repeatable migrations and the schema history table, and follow one startup step by step.

1A Quick Recap of Database Migrations

A migration is a small script that moves the database schema from one version to the next: create a table, add a column, create an index. Instead of changing databases by hand, we keep these scripts in the source code next to the application, in order, so the schema history is reviewed and versioned like the code. A migration tool then applies the scripts that a given database has not seen yet.

The tool needs to remember, for each database, which scripts have already been applied. Flyway does that with a table inside the database itself. That way the knowledge travels with the database: a fresh test database has an empty history and gets every script, while production has a long history and gets only the new ones.

2Setting Flyway Up in Spring Boot

In Spring Boot 3, adding flyway-core to the dependencies, plus the database module for our engine, such as flyway-database-postgresql, which Flyway 10 and later need for most databases, is enough. Spring Boot detects Flyway on the classpath, creates a Flyway instance using the application's DataSource and runs migrate() during startup. In Spring Boot 4, auto-configuration was split into smaller modules, and Flyway support lives in its own module with its own starter, spring-boot-starter-flyway, so adding only flyway-core may no longer be enough. Check the reference documentation for your version.

Text
# application.properties (defaults shown)
spring.flyway.enabled=true
spring.flyway.locations=classpath:db/migration
src/main/resources/db/migration/
V1__create_customer.sql
V2__add_customer_email.sql
V3__create_order.sql
R__customer_summary_view.sql

Spring Boot also makes sure Flyway runs before JPA. The EntityManagerFactory depends on the Flyway initializer, so by the time Hibernate validates or uses the schema, the migrations have been applied. If a migration fails, startup fails, which is what we want: an application should not run against a schema it does not expect.

3Versioned Migrations

A versioned migration has a name of the form V, a version number, two underscores, and a description: V2__add_customer_email.sql. The version can have parts, such as V2.1 or V20261105.1, and Flyway compares versions numerically, so V10 comes after V9. Each versioned migration runs exactly once per database, in ascending version order.

SQL
-- V1__create_customer.sql
CREATE TABLE customer (id bigint PRIMARY KEY, name text NOT NULL);
-- V2__add_customer_email.sql
ALTER TABLE customer ADD COLUMN email text;

Once a versioned migration has been applied anywhere that matters, it is frozen. Flyway stores a checksum of the file, and on the next startup it validates that the file still matches. If someone edits V2 after it ran, startup fails with a checksum mismatch. So a change to the schema is always a new file with a higher version, never an edit to an old one.

4Repeatable Migrations

A repeatable migration has an R prefix and no version: R__customer_summary_view.sql. It is meant for objects that we can always recreate from scratch, such as views, functions and stored procedures. Flyway runs it the first time, records its checksum, and runs it again whenever the file's checksum changes. If the file is unchanged, it is skipped.

SQL
-- R__customer_summary_view.sql
CREATE OR REPLACE VIEW customer_summary AS
SELECT c.id, c.name, count(o.id) AS orders
FROM customer c LEFT JOIN orders o ON o.customer_id = c.id
GROUP BY c.id, c.name;

Repeatable migrations always run after all pending versioned migrations, in order of their description. The reason is that views and functions usually depend on tables, so the tables must reach their latest shape first. Because they may run many times, they must be written to be rerun safely, which is why CREATE OR REPLACE is the natural style.

5The flyway_schema_history Table

The first time Flyway runs against a database, it creates flyway_schema_history. Every applied migration gets a row with its installed rank (the order it ran in), version, description, type, script name, checksum, the user who ran it, when it ran, how long it took and whether it succeeded.

SQL
SELECT installed_rank, version, script, checksum, success
FROM flyway_schema_history ORDER BY installed_rank;
-- 1 | 1 | V1__create_customer.sql | 1408123771 | t
-- 2 | 2 | V2__add_customer_email.sql | -845571230 | t
-- 3 | 3 | V3__create_order.sql | 2031553217 | t
-- 4 | | R__customer_summary_view.sql | 77210944 | t
Step throughWhat Flyway does at startup
  1. Read flyway_schema_historycreate it if this is a new database
  2. Scan classpath:db/migrationversioned and repeatable scripts
  3. Do applied checksums still match?
    YesContinueNoFail startup: checksum mismatch
  4. Run pending versioned migrationsin version order, each recorded
  5. Run changed repeatable migrationsafter the versioned ones
  6. Start JPA and the applicationschema is now at the latest version

6 steps in order. Play them, or step through them one at a time.

Notice that the repeatable migration has no version. When its checksum changes, Flyway adds a new row rather than updating this one, so the table is a complete log of what ran. This table is what makes migrate() decide correctly: compare the scripts on the classpath with the rows here, validate the checksums, and run only what is new.

6Follow-up questions

What happens if a migration fails halfway?

On PostgreSQL, Flyway runs each migration in a transaction, and DDL is transactional, so a failure rolls the whole script back and startup fails cleanly. On MySQL, DDL statements commit implicitly, so a failed script can leave some statements applied; Flyway records the failure and refuses to continue until we fix the database by hand and run repair. Keeping one change per migration limits the damage.

Can I use Java code instead of SQL for a migration?

Yes. A class extending BaseJavaMigration with a name like V4__backfill_email in the db/migration package is picked up the same way and recorded in the history. It is useful for complex data changes, but plain SQL is easier to review and should be the default.

What does spring.flyway.baseline-on-migrate do?

It lets Flyway start managing a database that already has tables but no history table. Flyway writes a baseline row at the configured version and only runs migrations above it. It is meant for adopting Flyway on an existing database, not for everyday use.

7Conclusion

In this answer, we saw how Spring Boot runs Flyway at startup, before JPA, using scripts from classpath:db/migration. Versioned migrations run once each in version order and are frozen by their checksums; repeatable migrations rerun whenever their content changes and always after the versioned ones. The flyway_schema_history table records every run and is what lets Flyway decide what is pending. We also noted that Spring Boot 4 moved Flyway support into its own module and starter.

What to say in the interviewAbout 80 seconds aloud

Open with

When Flyway is on the classpath, Spring Boot auto-configures it with the application's DataSource and runs migrate during startup, before the EntityManagerFactory is created, so Hibernate always sees the migrated schema.

Then, if they want more

By default it reads scripts from classpath:db/migration. Versioned migrations are named V, a version, two underscores and a description, like V2__add_customer_email.sql. Each runs exactly once per database, in numeric version order, and is frozen after that: Flyway stores its checksum and fails startup if the file is later edited, so every change is a new migration.

Repeatable migrations are named R__ with a description and have no version. Flyway runs them again whenever their checksum changes, always after the pending versioned migrations, which suits views and functions written with CREATE OR REPLACE.

The flyway_schema_history table records every applied script with its version, checksum, time and success flag, and Flyway compares it with the scripts on the classpath to decide what to run.

One version note: with Flyway 10 I also need the database module, such as flyway-database-postgresql, and in Spring Boot 4 the Flyway auto-configuration moved into its own module and starter, spring-boot-starter-flyway, so I would check the dependencies for the Boot version in use.

About this answer

Last updated
12 November 2026
Versions it names
Spring Boot 4, Spring Boot 3
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "How does Flyway apply migrations in a Spring Boot application? Explain versioned and...": https://scrivio-interview-library.netlify.app/databases/schema-migration/9442116c01/