Wednesday, July 29, 2009

grails conditional bootstrapping

I've been fortunate enough to fit grails into a new development project, and it's definitely made things more pleasant. I'm still missing eclipse and autocompletion a bit, but overall I am more productive. Anyway, I've created a bunch of objects in my grails-app/conf/BootStrap.groovy for testing, that also happen to be good initial values for production. However, they're getting created every time I restart the app, causing duplication! I've seen many references to checking GrailsUtil.environment, i.e.
def init = {  
if (GrailsUtil.environment=="development") initData();
}
This is close, but this doesn't do what I want. When I'm doing some of my testing, I don't need my objects created, as I want to persist my data between restarts. The decision to re-create my objects isn't the environment, it's whether I've told my DataSource to delete all data. That happens when the dbCreate property is "create" or "create-drop".
def init = {
// only bootstrap if data was deleted
def dbCreate = org.codehaus.groovy.grails.commons.ConfigurationHolder.config.dataSource?.dbCreate
if(dbCreate.startsWith("create")) {
initData();
}
}
Please forgive the non-grooviness of my code, I still like my groovy code to read like java.