Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use of Hibernate and flyway in a production system

I've been looking into the using hibernate for a project at work. It looks like it does what we want for generating a database for a given set of classes. However, it appears to be strongly discouraged against using hbm2ddl.auto=update in a production environment.

Looking around, I can't see what people do for this situation. Once the database is populated and in use, we will want to add/update extra persistent classes into the databases, without using the unreliable hibernate update.

Flyway looks like it would be useful to handle the database schema update, but this still requires us to manually create upgrade script every time we make any code changes.

Is there an easy way to solve this problem automatically? I can't really see the point of hibernate if it's not good enough for updating in a live environment.

Am I missing something?

like image 520
wybourn Avatar asked Sep 26 '14 12:09

wybourn


1 Answers

ORM frameworks are not good for upgrades because they don't have contextual information about the differences in the schema. So in case you rename column bar to baz, all Hibernate will see is:

ALTER TABLE foo DROP COLUMN bar;
ALTER TABLE foo ADD COLUMN baz VARCHAR(255) NOT NULL;

But in reality you might want to do this:

ALTER TABLE foo ADD COLUMN baz VARCHAR(255);
UPDATE foo SET baz = bar;
ALTER TABLE foo ALTER COLUMN baz SET NOT NULL;
ALTER TABLE foo DROP COLUMN bar;

Unless you start explicitly describing your schema changes, no framework will generate correct DDL updates. And I am not aware of any Hibernate feature for this, hence you need Flyway or Liquibase.

like image 111
Pavel Horal Avatar answered Oct 12 '22 14:10

Pavel Horal