5

I'd like to make my Bootstrap dependant on the createdb property in the grails DataSource.groovy file. When the setting is 'create', new Master Data should be generated, if the setting is 'update', none.

I've found GrailsDataSource in the Grails API which also has a method getCreateDb, but I don't know how to access it from the Bootstrap for the respective Bootstrap environment.

Siguza
  • 21,155
  • 6
  • 52
  • 89
Jörg Brenninkmeyer
  • 3,304
  • 2
  • 35
  • 50

3 Answers3

9

The up-to-date way to obtain dbCreate property is injecting 'grailsApplication' to BootStrap.groovy:

def grailsApplication

if(grailsApplication.config.dataSource.dbCreate == 'create'){
    ...
}

ApplicationHolder is now deprecated and I couldn't get it to work.

Hope it helps someone.

Tomas Romero
  • 8,418
  • 11
  • 50
  • 72
3

The easiest way is probably to just check the value of dbCreate from the config, like so:

import org.codehaus.groovy.grails.commons.ApplicationHolder

if (ApplicationHolder.application.config.dataSource.dbCreate == "create") {
    ...do create stuff...
} else if (ApplicationHolder.application.config.dataSource.dbCreate == "update") {
    ...do update stuff...
}
ataylor
  • 64,891
  • 24
  • 161
  • 189
0

Answer from ataylor is deprecated. Please refer to How to access Grails configuration in Grails 2.0?.

Also it should be noted in canotto90 answer that injection def grailsApplication should appear outside of init closure of BootStrap. In other way u get NPE.

Community
  • 1
  • 1
Gorky
  • 161
  • 2
  • 14