Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle database migrations in Spring Boot with Hibernate?

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).

like image 860
Ville Miekk-oja Avatar asked Jun 04 '17 10:06

Ville Miekk-oja


People also ask

What is DB migration in spring boot?

Spring Boot makes this integration very simple. You only need to add a dependency for Liquibase or Flyway and put a description of your database update operations in the default folder. Spring Boot then provides a default configuration and triggers the migration.

What is Flyway migration in spring boot?

Flyway is the Apache v2 licensed open-source tool that makes database migrations easy. You can think of Flyway as version control for your database. It lets you evolve your database schema easily and reliably across all your instances. Flyway updates a database from one version to the next using migrations.

Can we use hibernate with spring boot?

To test hibernate configuration with Spring boot, we need to autowire the EmployeeRepository dependency in a class and use it's method to save or fetch employee entities. Let's do this testing in @SpringBootApplication annotated class and using CommandLineRunner interface.


1 Answers

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.

like image 91
Darren Forsythe Avatar answered Oct 14 '22 03:10

Darren Forsythe