How to handle database migrations in Spring Boot with Hibernate?

Ville Miekk-oja :

My database background comes from the Django framework (python). In Django, getting started with database migrations was easy: Django migrations. The Django framework provided tool for creating the migrations based on your models, and also a tool to apply the migrations on your database. I think this way of doing worked in both development and in production. You did not have to write migrations by yourself, the framework created them for you.

Now I have started a project with Spring Boot and Hibernate. I configure my application to use hibernate with JPA. With these settings, I now would need to know how does my framework handle database migrations? I mean if I change a column, either it's type, or even might remove it, then how do I migrate the database to the change? I know that spring boot will automatically detect column changes on startup, and create columns that do not exist based on the models (Entity's). I guess it has something to do with variable

spring.jpa.hibernate.ddl-auto

But how does it handle the existing database objects? Does it add the column to them too, and with what value? The default value I set? What if I change the column type? Can it then handle the change? These settings and spring-boot automated database management probably are not enough in the long run?

What I want to know is, that what are the best practices on how to handle database migrations with Spring Boot and hibernate combination? I believe there is a standard how most of the people with this combination handle the migrations? I hope it is as easy as with Django... I know about flyway, but don't know if I really need it, or if it is used much with this combination of mine (including spring boot and hibernate).

Darren Forsythe :

Liquibase or Flyway are the two main options for versioning/handling database migrations. ddl-auto is quick and dirty, but it does not, nor can it, take into account everything that needs to be handled. THere's also the chance of race-conditions (two instances trying to update the DDL at the same time).

This answer goes into more detail about ddl-auto in a production environment, and why you shouldn't.

Hibernate: hbm2ddl.auto=update in production?

https://www.credera.com/blog/technology-insights/java/liquibase-fed-inconsistent-schemas/ has bit more info on the why/concepts.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=429813&siteId=1