Scrivio Interview library
ScenarioFoundations6 min read · updated 12 November 2026

How do you test database migrations in a CI pipeline for a Spring Boot service?

We start a real database of the same engine and version as production in CI, usually with Testcontainers, let Flyway or Liquibase run every migration from an empty schema, and then run the integration tests against it. Hibernate's ddl-auto is set to validate, so any mismatch between the entities and the migrated schema fails the build. We do not rely on an in-memory database such as H2, because it accepts and rejects different SQL than PostgreSQL or MySQL and would hide real migration errors.

Why this question is asked

A migration that has never run against a real database is a guess. It may have a syntax error that only PostgreSQL rejects, depend on an earlier migration in the wrong order, or create a column with a different type from the one the entity expects, and all of that should be found by the build, not by the production deployment. Interviewers ask this question because testing migrations well is cheap with modern tools, and the common shortcut, an in-memory database, quietly tests the wrong thing. In this answer, we will see what a migration test has to prove, set up a real database with Testcontainers, run all migrations from empty, use ddl-auto=validate as an extra check, and look at why H2 is not a substitute.

1What a Migration Test Must Prove

A migration test answers three questions. Do all migrations run, in order, on the database engine we use in production? Does the resulting schema match what the code expects, in table names, column names and types? And does the application actually work on top of it, so queries and constraints behave as intended?

The first question needs a real database of the right engine and version, because SQL dialects differ in many small ways. The second needs a check that compares the entities with the schema. The third is answered by the normal integration tests, as long as they run against the migrated schema rather than one generated some other way.

2Starting a Real Database With Testcontainers

Testcontainers is a Java library that starts throwaway Docker containers from our tests. For a migration test, it starts the same PostgreSQL image as production, and Spring Boot's @ServiceConnection (available since Boot 3.1) points the DataSource at it automatically. When Spring starts, Flyway runs every migration against the empty database, exactly as it would on a brand new environment.

Java
@SpringBootTest
@Testcontainers
class MigrationIT {
@Container
@ServiceConnection
static PostgreSQLContainer<?> db = new PostgreSQLContainer<>("postgres:17.6"); // same major version as production
@Autowired Flyway flyway;
@Autowired CustomerRepository customers;
@Test
void allMigrationsApplied() {
assertThat(flyway.info().pending()).isEmpty(); // every migration ran: 0 pending
}
@Test
void customerRoundTrip() {
Customer saved = customers.save(new Customer("Anna", "anna@example.com"));
assertThat(customers.findById(saved.getId())).isPresent(); // the schema really works
}
}

Notice that the image tag pins the same major version as production. A migration using a feature added in PostgreSQL 17 would pass against 17 and fail against a production server on 16, so the test database must match. In Testcontainers 2 some container classes moved packages and lost their generic parameter, so check the import for your version. The container starts once per test class, which takes a few seconds, and all integration tests in that class share it.

3Running Every Migration From Empty

Starting from an empty schema matters because it is the path every new environment takes, and it is the path most likely to break silently. A developer's local database has years of history and maybe some manual changes; it can pass a test that a clean database would fail, for example when a migration depends on an index that someone created by hand. So CI always builds the schema from nothing, using only the migration files in the commit being tested.

The integration tests then run on that schema. This is also why the tests should not create tables themselves or rely on Hibernate generating the schema: if they did, they would test the generated schema, not the migrated one.

4Using ddl-auto=validate as a Safety Check

Integration tests only exercise the queries they happen to run. Hibernate's validation checks everything mapped. With spring.jpa.hibernate.ddl-auto=validate, Hibernate compares every entity with the migrated schema at startup and fails if a table or column is missing or has an incompatible type.

Java
# src/test/resources/application-test.properties
spring.jpa.hibernate.ddl-auto=validate
# A developer added a field to the entity but forgot the migration:
# @Column(name = "loyalty_tier") private String loyaltyTier;
# Test startup fails:
# Schema-validation: missing column [loyalty_tier] in table [customer]
Step throughA migration test in CI
  1. Start PostgreSQL 17 in a containerTestcontainers, same version as production
  2. Spring context startsFlyway runs all migrations from empty
  3. Does Hibernate validation pass?
    YesRun integration testsNoFail: entity and schema disagree
  4. Do the tests pass?
    YesMigrations and code agree: build greenNoFail: query or constraint broken

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

The build fails at context startup, with a message that names the exact column, which is far better than a production error on the first request that touches it. Validation does not check everything, for example it does not verify indexes or most constraints, so it complements the integration tests rather than replacing them.

5Why H2 Is Not Enough

An in-memory database such as H2 is tempting because it starts instantly and needs no Docker. But it is a different database. It supports a different set of SQL, its compatibility modes only imitate parts of PostgreSQL or MySQL, and it behaves differently in exactly the areas migrations touch: types, defaults, constraint syntax, index options and DDL inside transactions.

SQL
-- V12__index_orders.sql, valid on PostgreSQL
CREATE INDEX CONCURRENTLY idx_orders_customer ON orders (customer_id);
-- H2: syntax error, so the team skips this migration in tests or rewrites it for H2
-- V13__status_type.sql, valid on PostgreSQL
CREATE TYPE order_status AS ENUM ('NEW', 'PAID');
-- H2 has its own ENUM syntax; the H2 version of the migration is not the one production runs

Notice the trap: to make migrations run on H2, teams start writing H2 versions of them or disabling them in tests, and then the tests no longer test the migrations at all. The opposite also happens: SQL that H2 accepts but PostgreSQL rejects. A real database in a container costs a few seconds per test run and removes this whole class of problems.

6Follow-up questions

Does this make the build much slower?

A PostgreSQL container usually starts in a few seconds, and running a few hundred migrations takes seconds more. Sharing one container across test classes, through a common base class or Testcontainers' reuse features, keeps the cost to a single start per build. That is a small price for finding migration errors before deployment.

Should we also test migrations against existing data?

For larger changes, yes. The from-empty test proves the migrations are valid, but not that they work on real data, for example that a new unique constraint does not fail on existing duplicates. A separate job that applies new migrations to a masked copy of production, or seeded realistic data, covers that.

How do we test Liquibase migrations the same way?

Exactly the same: Testcontainers for the database, Spring Boot runs Liquibase at startup from spring.liquibase.change-log, and Hibernate validates the result. The test can also check that no changeSets are left unrun.

7Conclusion

In this answer, we tested database migrations in CI by starting a real database of the production engine and version with Testcontainers, letting Flyway or Liquibase apply every migration to an empty schema, and running the integration tests on top. Setting ddl-auto to validate makes any mismatch between entities and the migrated schema fail the build, and we avoided H2 because it accepts different SQL and hides exactly the errors we want to catch.

What to say in the interviewAbout 90 seconds aloud

Open with

I test migrations against a real database of the same engine and version as production, because SQL dialects differ in details that matter for migrations. In a Spring Boot service I use Testcontainers: a PostgreSQL container with the same major version as production, wired in with @ServiceConnection, so when the test context starts, Flyway or Liquibase runs every migration from an empty schema, which is exactly the path a new environment takes.

Then, if they want more

I can assert that Flyway reports no pending migrations, and then the normal integration tests run on top of that migrated schema rather than one Hibernate generated. I also set spring.jpa.hibernate.ddl-auto to validate in tests, so Hibernate compares every entity with the real schema and fails at startup if, say, someone added a field without a migration.

That catches mismatches even in code the tests do not exercise, though it does not check indexes, so it complements the tests. I avoid H2 for this. It accepts different SQL, so things like CREATE INDEX CONCURRENTLY or PostgreSQL enum types either fail on H2 or get rewritten for it, and then the tests stop testing the real migrations.

The container costs a few seconds per build, which is well worth it, and for big changes I would add a job against production-like data.

About this answer

Last updated
12 November 2026
Versions it names
PostgreSQL 17
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 do you test database migrations in a CI pipeline for a Spring Boot service?": https://scrivio-interview-library.netlify.app/databases/schema-migration/6d0586f9e7/