Skip to content

Instantly share code, notes, and snippets.

@rzymek
Created September 5, 2013 09:38
Show Gist options
  • Save rzymek/6448035 to your computer and use it in GitHub Desktop.
Save rzymek/6448035 to your computer and use it in GitHub Desktop.
package config
import com.googlecode.flyway.core.Flyway
import java.util.logging.Logger
import javax.annotation.PostConstruct
import javax.annotation.Resource
import javax.ejb.EJBException
import javax.ejb.Singleton
import javax.ejb.Startup
import javax.ejb.TransactionManagement
import javax.ejb.TransactionManagementType
import javax.sql.DataSource
/**
* http://www.hascode.com/2013/04/easy-database-migrations-using-flyway-java-ee-6-and-glassfish/#Adding_Flyway_Database_Migrations
*/
@Singleton @Startup
@TransactionManagement(value=TransactionManagementType.BEAN)
class JeeDbMigrator {
static val log = Logger.getLogger(typeof(JeeDbMigrator).name)
@Resource(name='java:jboss/datasources/ds')
var DataSource dataSource
@PostConstruct
def void onStartup() {
if (dataSource === null) {
throw new EJBException('no datasource found to execute the db migrations!');
}
val flyway = new Flyway();
flyway.cleanOnValidationError = true
flyway.initOnMigrate =true
flyway.dataSource = dataSource
flyway.info.all.forEach[
log.info('''migrate task: «it.version»: "«it.description»" («it.script»)''')
]
flyway.migrate
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment