Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java equivalent for database schema changes like South for Django?

I've been working on a Django project using South to track and manage database schema changes. I'm starting a new Java project using Google Web Toolkit and wonder if there is an equivalent tool. For those who don't know, here's what South does:

  • Automatically recognize changes to my Python database models (add/delete columns, tables etc.)
  • Automatically create SQL statements to apply those changes to my database
  • Track the applied schema migrations and apply them in order
  • Allow data migrations using Python code. For example, splitting a name field into a first-name and last-name field using the Python split() function

I haven't decided on my Java ORM yet, but Hibernate looks like the most popular. For me, the ability to easily make database schema changes will be an important factor.

like image 722
gerdemb Avatar asked Mar 11 '10 18:03

gerdemb


People also ask

What is Makemigrations and migrate in Django?

makemigrations is responsible for packaging up your model changes into individual migration files - analogous to commits - and migrate is responsible for applying those to your database.

Where are Django migrations stored?

Migrations are generated per app, and are stored in some_app/migrations . Even if you do not define migrations for your apps, there will usually be migrations that take place, since you (likely) included some apps defined by Django (and other third parties) in your INSTALLED_APPS , these have migrations as well.


2 Answers

Wow, South sounds pretty awesome! I'm not sure of anything out-of-the-box that will help you nearly as much as that does, however if you choose Hibernate as your ORM solution you can build your own incremental data migration suite without a lot of trouble.

Here was the approach that I used in my own project, it worked fairly well for me for over a couple of years and several updates/schema changes:

  1. Maintain a schema_version table in the database that simply defines a number that represents the version of your database schema. This table can be handled outside of the scope of Hibernate if you wish.

  2. Maintain the "current" version number for your schema inside your code.

  3. When the version number in code is newer than what's the database, you can use Hibernate's SchemaUpdate utility which will detect any schema additions (NOTE, just additions) such as new tables, columns, and constraints.

  4. Finally I maintained a "script" if you will of migration steps that were more than just schema changes that were identified by which schema version number they were required for. For instance new columns needed default values applied or something of that nature.

This may sounds like a lot of work, especially when coming from an environment that took care of a lot of it for you, but you can get a setup like this rolling pretty quickly with Hibernate and it is pretty easy to add onto as you go on. I never ended up making any changes to my incremental update framework over that time except to add new migration tasks.

Hopefully someone will come along with a good answer for a more "hands-off" approach, but I thought I'd share an approach that worked pretty well for me.

Good luck to you!

like image 125
BryanD Avatar answered Sep 21 '22 20:09

BryanD


as I'm looking for the same heres what I've achieved so far.

We first used dbdeploy. It manages the most stuff for you but you will have to write all the transition scripts by yourself! That means every change you make has to be in its own script which you will have to write from scratch. Not very handy, but works very reliable.

The second thing I encountered is liquibase. It stores the configuration in one single xml file. Not very intuitive to read, but managable. Plus there is an Intellij Idea plugin for it. At the moment of writing it still has some minor issues, but as the author assured me, they will be fixed soon.

The perfect solution would be to get south working with your java environment. That really would be a tool to marry :D

like image 30
Jonas Avatar answered Sep 21 '22 20:09

Jonas