Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to upgrade database schema built with an ORM tool?

I'm looking for a general solution for upgrading database schema with ORM tools, like JPOX or Hibernate. How do you do it in your projects?

The first solution that comes to my mind is to create my own mechanism for upgrading databases, with SQL scripts doing all the work. But in this case I'll have to remember about creating new scripts every time the object mappings are updated. And I'll still have to deal with low-level SQL queries, instead of just defining mappings and allowing the ORM tools to do all the job...

So the question is how to do it properly. Maybe some tools allow for simplifying this task (for example, I heard that Rails have such mechanism built-in), if so please help me decide which ORM tool to choose for my next Java project.

like image 653
rmaruszewski Avatar asked Sep 15 '08 15:09

rmaruszewski


People also ask

Can a database schema be changed?

A schema change is an alteration made to a collection of logical structures (or schema objects) in a database. Schema changes are generally made using structured query language (SQL) and are typically implemented during maintenance windows.

How do I update a mysql schema?

To change the name of the default schema, double-click the schema tab. This opens a schema editor window docked at the bottom of the application. To undock or redock this window, double-click anywhere in the editor title bar. To rename the schema, use the field labeled Name.

What tool is specifically recommended to help manage database schema changes when updating your application code?

Use an online schema migration framework such as gh-ost or pt-online-schema-change. These tools create a "ghost" copy of each table you want to change, migrate the empty copy, and then incrementally copy data from the original table including any updates that happen during the migration.


3 Answers

LiquiBase is an interesting open source library for handling database refactorings (upgrades). I have not used it, but will definitely give it a try on my next project where I need to upgrade a db schema.

like image 80
killdash10 Avatar answered Sep 28 '22 09:09

killdash10


I don't see why ORM generated schemas are any different to other DB schemas - the problem is the same. Assuming your ORM will spit out a generation script, you can use an external tool to do the diff

I've not tried it but google came back with SQLCompare as one option - I'm sure there are others.

like image 45
MarcE Avatar answered Sep 28 '22 08:09

MarcE


We hand code SQL update scripts and we tear down the schema and rebuild it applying the update scripts as part of our continuous build process. If any hibernate mappings do not match the schema, the build will fail.

like image 36
Paul Croarkin Avatar answered Sep 28 '22 10:09

Paul Croarkin