After reading about the issue of migrating databases on apps based on Play 2.2.X with JPA + Hibernate here: http://stackoverflow.com/questions/221379/hibernate-hbm2ddl-auto-update-in-production and here: http://stackoverflow.com/questions/6681445/play-hibernate-and-evolutions
But inspired on other technologies that I used before, and on a comment I saw somewhere on StackOverflow (sorry, no link, I cannot remember where I saw it), I decided to make my life easier by using Hibernate’s automatic schema migration features to bootstrap my migration script.
After a bit of googling I knew that it was possible to do it programmatically or with an SBT/Maven/whatever plugin because of this: http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/toolsetguide.html#toolsetguide-s1-6
After briefly considering, and giving up on the idea of making a build ‘target’ (call it target, goal or whatever depending on how you build your app). I gave up on the idea because I knew very little about SBT, and I didn’t want to migrate the Play app to Maven, just for the sake of doing this.
So, inspired by this page I wrote the following Plugin instead:
As you can see, it writes the migration script to the “conf/db/migration” + <persistenceUnitName> + “.sql” file, that can then be used as a base to write your own migration script.
After writing that plugin, I simply had to enable it by adding it to the play.plugins file, after the flyway one (to allow the migration to occur):
I also commented the
<property name="hibernate.hbm2ddl.auto" value="validate" />
line at the persistence.xml file, as the ‘validation’ is now done with the plugin.
If you have any suggestions/comments please feel free to share