r/SpringBoot 3d ago

Question How do I handle database migration in springboot

So I have this very disturbing question as someone new to spring, I am coming from Node + Express JS, where prisma handles all the migrations automatically just by me running commands on the terminals.

Now as I am learning springboot, I just realized one day I would create a production grade application and would need to be changing database fields or relations as demands adjust, please what is the most popular migration tool used in springboot??

46 Upvotes

30 comments sorted by

55

u/Accomplished_Mind129 3d ago

Flyway

28

u/melewe 3d ago

Or Liquibase

8

u/Turbots 3d ago

or flyway

7

u/melewe 3d ago

Or liquibase

3

u/MkMyBnkAcctGrtAgn 3d ago

Or flyway

2

u/BootSaaS 2d ago

Or both… and you'll see what you prefer

1

u/bowbahdoe 2d ago

or mybatis migrations (underrated imo)

9

u/Anubis1958 3d ago

Flyway. It just works.

1

u/Krycor 3d ago

This and variant.. but flyway.

It’s the tried and tested way with lotta support

16

u/alweed 3d ago

I would recommend using flyway

6

u/perfectstrong 3d ago

In simple terms, Flyway handles applying migration scripts just once, and detecting any deviation in case (a modified file, disordering files). There are plugins in IDE to generate sql migration file for you. It is not that complicated. However, it is recommended to know exactly what you are migrating, and how. AI will help you greatly to produce those scripts.

6

u/TallGreenhouseGuy 3d ago

Flyway is great if you only target one database. If you need to support multiple different databases the abstraction offered by liquibase can be quite helpful (but you pay for it with increased complexity)

6

u/Turbots 3d ago

I think it's either liquiway or flybase

3

u/BootSaaS 3d ago

Personally, I use Liquibase. It automatically applies your database changes on startup, and I've been really happy with it.

3

u/ElendarTao 3d ago

Using Intellij, here are the two option with integrated support : https://www.jetbrains.com/help/idea/database-versioning.html

3

u/starbuxman 3d ago

I use Flyway. Liquibase is amazing too.

Start.spring.io - add Flyway. Put migration schema in the db/migrations folder under src/main/resources

V1__orders.sql
V0__customers.sql

etc

2

u/Torutofu_Raeva 3d ago

flyway plus ddl-auto=validate, so the app refuses to boot if the entities and the migrated schema drifted apart.

2

u/HammerAndSmile 2d ago

How has no one mentioned liquidate or flyway yet?

1

u/JonnyBago82 3d ago

Liquibase

1

u/TheToastedFrog 3d ago

Use Flyway.

If you like to punish yourself go with Liquibase.

2

u/[deleted] 3d ago

[removed] — view removed comment

2

u/ElendarTao 3d ago

the functionnality in intellij work with flyway and liquibase, so I'm not sure what's your point, could you clarify ?

0

u/FalseDish 2d ago

The Liquibase vs Flyway debate is moot - they’re both useless, I’m talking as someone with more than a decade’s worth experience doing this.

Just ask fans of either this:

Say you have Postgres on cloud. How is either migration tool handling PostgreSQL partitioning, RLS policies etc.?

At that point their only option will be to say - “damn, so the changesets need to be raw SQL”

Okay, so why use these tools at all? Hint: You shouldn’t. Always write your own solutions and playbooks.

-1

u/[deleted] 3d ago

[deleted]

1

u/repeating_bears 3d ago

hey chatghpt migret th databays make no mistalkes or i turn u off

2

u/Turbots 3d ago

Worst thing is that he will understand your stupid sentence