Created
August 9, 2014 15:43
-
-
Save joantune/ca15826dc6d6cab1eb9a to your computer and use it in GitHub Desktop.
Play 2.2.X + JPA + Hibernate DB migration bootstrapper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package util.plugins; | |
import java.io.File; | |
import java.util.HashMap; | |
import java.util.Map; | |
import org.hibernate.HibernateException; | |
import org.hibernate.ejb.Ejb3Configuration; | |
import org.hibernate.tool.hbm2ddl.SchemaUpdate; | |
import org.hibernate.tool.hbm2ddl.SchemaValidator; | |
import org.hibernate.tool.hbm2ddl.Target; | |
import play.Application; | |
import play.Configuration; | |
import play.Logger; | |
import play.api.Plugin; | |
public class DBMigrationMR implements Plugin { | |
private final Application application; | |
public DBMigrationMR(Application application) { | |
this.application = application; | |
} | |
private final Map<String, org.hibernate.cfg.Configuration> hibernateConfs = | |
new HashMap<String, org.hibernate.cfg.Configuration>(); | |
@Override | |
public boolean enabled() { | |
return true; | |
} | |
private void initHibernateConfs() { | |
Ejb3Configuration cfg = null; | |
Configuration jpaConf = Configuration.root().getConfig("jpa"); | |
for (String key : jpaConf.keys()) { | |
String persistenceUnit = jpaConf.getString(key); | |
cfg = new Ejb3Configuration(); | |
Ejb3Configuration configure = cfg.configure(persistenceUnit, null); | |
hibernateConfs.put(persistenceUnit, configure.getHibernateConfiguration()); | |
} | |
} | |
@Override | |
public void onStart() { | |
boolean interrupt = false; | |
Logger.info("Initing DBMigrationMR"); | |
initHibernateConfs(); | |
//let's validate the schema now for each one | |
for (String pUnit : hibernateConfs.keySet()) { | |
org.hibernate.cfg.Configuration cfg = hibernateConfs.get(pUnit); | |
SchemaValidator schemaValidator = new SchemaValidator(cfg); | |
try { | |
schemaValidator.validate(); | |
} catch (HibernateException ex) { | |
interrupt = true; | |
SchemaUpdate schemaUpdate = new SchemaUpdate(cfg); | |
File outputFile = application.getFile("conf/db/migration" + pUnit + ".sql"); | |
Logger.warn(pUnit + " persistence unit isn't valid. Dumping migration SQL to " + outputFile.getAbsolutePath(), ex); | |
schemaUpdate.setOutputFile(outputFile.getAbsolutePath()); | |
schemaUpdate.setDelimiter(";"); | |
schemaUpdate.execute(Target.SCRIPT); | |
} | |
} | |
if (interrupt) { | |
//Logger.error("Killing off application"); | |
//Play.stop(); | |
} | |
} | |
@Override | |
public void onStop() { | |
// TODO Auto-generated method stub | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Please let me know what you think about it, also, be sure to comment the link for further improvements you might have done, or give hints/suggestions.
My blog post explaining this can be found here: http://web.ist.utl.pt/~joao.a.p.antunes/2014/08/09/play-2-2-x-jpa-hibernate-database-migration