r/SpringBoot • u/Aggravating_Kale7895 • Jan 24 '26
How-To/Tutorial What is Flyway in Spring Boot? And why teams stop using ddl-auto after learning it
Database changes in Spring Boot often go wrong because of:
- Manual SQL scripts (no tracking, env mismatch)
spring.jpa.hibernate.ddl-auto=update(no history, unsafe for prod)
Flyway solves this by versioning database changes.
Each schema change is written as a versioned SQL file:
V1__Create_users_table.sql
V2__Create_payments_table.sql
V3__Add_user_status_column.sqlOn app startup, Flyway:
- Runs only pending migrations
- Tracks everything in
flyway_schema_history - Keeps dev, staging, and prod in sync
- Prevents modifying old migrations
No manual SQL. No guessing what ran where.
I built a Spring Boot 3 + PostgreSQL demo showing real migrations (V1–V7), incremental changes, and safe production-style setup:
👉 https://github.com/Ashfaqbs/spring-flyway
Good example if Flyway feels abstract or confusing.
33
Upvotes
10
u/skidmark_zuckerberg Jan 24 '26
Biggest problem I’ve seen on teams is keeping a persistent local database in sync while switching branches. It’s common for one branch to apply a migration that another branch doesn’t contain, and I might switch between multiple branches in a single day given my workload.
The most simple workaround I’ve used (short of some automation or a separate DB per branch) is to remove the missing migration directly from
flyway_schema_historylocally so Flyway can start again on the current branch. In practice, this issue shows up primarily in local and test environments because they’re constantly in flux. Since the migrations are applied on startup, they just get reapplied when I run the branches again.By the time we cut a release, the release branch is brought up to date by merging from develop, so production has the full, consistent set of migrations.
6
u/Mikey-3198 Jan 24 '26 edited Jan 24 '26
I'd recommend using timestamps to version the migrations.
I.e
V2025010900__migration.sqlinstead of sequential increments likeV1__,V2__.Reduces naming conflicts and especially useful if you need to add a migration on a release branch. When doing this you'll have to set out of order migrations to true.
2
u/MaDpYrO Jan 24 '26
My problem with this workflow is using handwritten SQL as your source for the changelogs.
I want to work with my domain entities, modelling them, and having my database reflect that. This means that my intention with the data modelling is labeled on my entities - e.g. indexes are visible in the code on the entity, etc. not separate in the changelog.
For my setup I found liquibase to be better for this, but not flawless:
1: Make changes to my entities
2: Spin up a local db in a container, run my application with ddl=auto-update
3: Run a liquibase diff - comparing my dev db to my local, and have it generate a changelog
There are approaches to using hibernate entities directly as your comparison db against live db, but I have found them to be unreliable.
But I have also found manual SQL migrations to be extremely unreliable.
As for this post - I understand it as a learning exercise for yourself. I'm just slightly confused as to what you're achieving that Flyways own docs wouldnt do the same? I never found them to be lacking.
1
u/mrVragec Jan 24 '26
The two problems that I faced with engineers in my team were versioning and roll-back scripts. For the versions, be aware that V1, V2,… might not be suitable for environment with multiple engineers as each of them might create same version locally and merging it might confuse flyway which to run (at least this was the problem at the time) For rollback scripts, don’t forget that from time to time application might be reverted so databases changes should also be reverted otherwise previous version might not be compatible with the database schema.
1
u/maxip89 Jan 24 '26
simple auto-ddl doesnt does all the magic.
sometimes there are some errors that can only be handled by manually sql scripts.
In the end you have 1000 scripts hanging around. Which are not versioned, tested and so one.
When you setup a new environment you will have much drama with auto-ddl. With flyway it's just a start and see what is happening or incompatible.
1
u/m41k1204 Jan 25 '26
What is the difference with Flyway and Liquibase? I use liquibase on my application and it’s just fine. The only problem I’ve had is the one that was already mentioned. When there are multiple branches being worked on locally and when changing branches breaks the db schema. I tend to go nuclear and make copies of my db. What do you recommend?
1
u/iamwisespirit Jan 25 '26
It uses in db migration or schema migration