Database Migrations with Flyway
Manage your schema changes safely and repeatably across environments using versioned Flyway migrations in Spring Boot.
Database Migrations with Flyway is a free Spring Boot 4 Complete Guide lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Spring Boot 4 Complete Guide learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
The Problem with Manual Schema Changes
Editing the database by hand is error-prone and hard to reproduce across dev, test, and production. Migrations turn schema changes into versioned, repeatable scripts.
What Is Flyway?
Flyway is a migration tool that applies SQL scripts in order, tracking which have run in a history table so each runs exactly once.
Adding Flyway
Spring Boot auto-detects Flyway on the classpath and runs migrations at startup. Just add the dependency.
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
</dependency>Migration File Naming
Versioned scripts follow the pattern V1__description.sql. The version number after V determines execution order.
- V1__create_users.sql
- V2__add_email_column.sql
Where Migrations Live
By default Flyway looks in src/main/resources/db/migration. Place your SQL files there and they are picked up automatically.
A First Migration
Your first script typically creates a table. Once applied, Flyway records it and never runs it again.
CREATE TABLE users (
id BIGINT PRIMARY KEY,
name VARCHAR(100) NOT NULL
);The Schema History Table
Flyway maintains a flyway_schema_history table recording each applied version, its checksum, and timestamp. This is how it knows what is pending.
Never Edit Applied Migrations
Once a migration has run anywhere, treat it as immutable. Editing it changes the checksum and Flyway will fail validation. Always add a new migration instead.
ALTER TABLE users ADD COLUMN email VARCHAR(200);Repeatable Migrations
Scripts prefixed with R__ instead of a version run whenever their content changes. They are ideal for views and stored procedures.
Flyway and JPA Together
Disable Hibernate's auto schema generation and let Flyway own the schema. This gives you a single, reliable source of truth for structure.
spring.jpa.hibernate.ddl-auto=validateBenefits in CI/CD
Because migrations run automatically and deterministically, every environment converges to the same schema, making deployments predictable and rollbacks easier to reason about.
Quick Check
Test your understanding of Flyway migrations.
Recap
You learned to version schema changes with Flyway: naming conventions, the history table, immutability of applied scripts, repeatable migrations, and pairing Flyway with JPA validation for reliable deployments.
Frequently asked questions
Is the “Database Migrations with Flyway” lesson free?
Yes — the full text of “Database Migrations with Flyway” is free to read here on the web, and the Spring Boot 4 Complete Guide course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Spring Boot 4 Complete Guide course, upgrade to CoddyKit PRO.
What will I learn in “Database Migrations with Flyway”?
Manage your schema changes safely and repeatably across environments using versioned Flyway migrations in Spring Boot. You practise Spring Boot 4 Complete Guide with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Spring Boot 4 Complete Guide?
No prior experience is required. Spring Boot 4 Complete Guide on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Database Migrations with Flyway” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Spring Boot 4 Complete Guide lesson?
Yes. Every Spring Boot 4 Complete Guide lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Custom Spring Data Repositories
- Integrating NoSQL Databases
- Caching with Spring Cache
- Database Migrations with Flyway