1

How to prevent bootstraping of data in grails app. when we configured our DataSource.groovy like this

 development {
    dataSource {
        dbCreate = "update"
        url = "jdbc:mysql://localhost:3306/test"
        pooled = true
        driverClassName = "com.mysql.jdbc.Driver"
        dialect = org.hibernate.dialect.MySQL5InnoDBDialect
        username = "root"
        password = "root"
    }

}

BootStrap.groovy

class BootStrap {
def bootstrapService
def grailsApplication
def init = { servletContext ->
    switch (Environment.getCurrent().name) {
        case "dev":
            bootstrapService.bootstrapDummyData()
            break;
        case "test":
            bootstrapService.bootstrapDummyData()
            break;
    }
}
def destroy = {
}

I want bootstrapService.bootstrapDummyData() not to be called when i configrued my datasource to update mode. i.e. dbCreate = "update"

nickdos
  • 8,348
  • 5
  • 30
  • 47
Tushar Saxena
  • 345
  • 4
  • 15
  • can you please explain your problem clearly. – Visme Mar 27 '14 at 07:54
  • I configured DataSource.groovy in update mode. when i run my grails- app it called the BootStrap.groovy which will result in unique constraints validation in database. because already i have data in my database. so i want when i run grails-app in update mode then it shoul not called BootStrap.groovy. – Tushar Saxena Mar 27 '14 at 08:27
  • I'm guessing you have some code in your bootstrap which creates some DB entities? Please include your `Boostrap.groovy` code in the question (see edit link). One way is to check if the entity exists (search for some unique contrstraint) and only create the entitiy if it doesn't exist. – nickdos Mar 27 '14 at 08:32
  • Is there any way in grails by which we restrict our application to call BootStrap.groovy. – Tushar Saxena Mar 27 '14 at 08:40
  • Not that I know of. You can detect the running dev/test/prod envornment and only run some code for (say) the `dev` environment. See http://grails.org/doc/latest/guide/conf.html#environments -> `Environment.current` – nickdos Mar 27 '14 at 10:57
  • You can use `findOrSaveWhere` when creating objects or just verify that nothing exists before adding data, like `if(!DomainClass.count()) proceed()` – Ivar Mar 28 '14 at 00:31

2 Answers2

2
 class BootStrap {
 def bootstrapService
 def grailsApplication
 def init = { servletContext -> 
 if(grailsApplication.config.dataSource.dbCreate == "create-drop"){
    switch (Environment.getCurrent().name) {
        case "dev":
            bootstrapService.bootstrapDummyData()
            break;
        case "test":
            bootstrapService.bootstrapDummyData()
            break;
     }
   }
}
Tushar Saxena
  • 345
  • 4
  • 15
  • we can access dataSource dbCreate property by grailsAppliation. so this code works if you dont want to call your bootstrap code when you set your dbCreate="update" – Tushar Saxena Apr 02 '14 at 06:13
0
def init(){

    if(User.list().size()==0)
       objectCreation()
}

def objectCreation(){

def userObject1 = new User()
def userObject2 = new User()


}

the above code for example. if user is created in bootstarp . first check user is already created or not. If not create object.

Visme
  • 983
  • 8
  • 29