hibernated icon indicating copy to clipboard operation
hibernated copied to clipboard

update table columns with updateDBSchema if required

Open hyOzd opened this issue 2 years ago • 1 comments

It would be nice if updateDBSchema also added/removed table columns when fields are added and removed from the respective class. I suspect this is one of those features that sounds easy but has a lot of edge cases to be handled?

hyOzd avatar Mar 09 '22 18:03 hyOzd

This is one of those intractable problems that even the original Hibernate is unable to solve. The root of the issue is that some changes simply aren't able to be migrated automatically. Consider the following examples:

  • A new non-null column is added, but existing records are there and have no value for this column.
  • A column is changed from a double to an integer, but data already exists with fractional values.
  • A column is renamed. For a human this is trivial, but for a computer, it's unclear whether the old column was simply deleted and a new column was inserted, or if it is supposed to migrate data.

There are just a few simple examples, you can imagine a lot more I'm sure. For this reason, the typical workflow is to use automatic schema generation during local development, or in early stages of the project's development.

As systems enter production or have data that must be delicately preserved, automatic schema generation is disabled, and migration systems are used to make incremental changes to the DB. Systems like Flyway or Liquibase are common, but have their own trade-offs.

Alternatively, one can use a home-grown shellscript like the following: https://gist.github.com/vnayar/692d9c0d18dda4057f9a58add3449ebd

vnayar avatar Jan 08 '24 08:01 vnayar