From Hibernate schema generation to Flyway in existing applications

submited by
Style Pass
2021-07-25 19:00:04

Recently, we got to work in a couple of Java projects that depend on Hibernate to evolve the production SQL schema, this post explains the approach we followed in order to move to Flyway.

While letting Hibernate handle the SQL schema evolution (hbm2ddl) is usually discouraged, sometimes you do not have control on the choices made from previous developers, which was our case.

Incremental SQL migration scripts allow us to keep a sane control on how your schema evolves. Even Hibernate docs mentions it:

Although the automatic schema generation is very useful for testing and prototyping purposes, in a production environment, it’s much more flexible to manage the schema using incremental migration scripts.

Besides those, one of our inherited projects got a cyclic foreign key dependency, table A depended on table B which depended on table A.

In MySQL, mysqldump db_name > schema.sql would write the db_name database schema to the schema.sql file, you will need to grab most of the creation statements to produce your first Flyway migration script.

Leave a Comment