grailsgroovyggts

Initial data for grails application


I am developing a small grails application from a book and one of the steps it needs is to feed the database with initial data. When I do so using the GroovyConsole, the script executes but doesnt persist the data.

DataSource.groovy environment settings are as follows

development {
        dataSource {
            dbCreate = "update" // one of 'create', 'create-drop', 'update', 'validate', ''
            url = "jdbc:h2:devDb;MVCC=TRUE;LOCK_TIMEOUT=10000;DB_CLOSE_ON_EXIT=FALSE"
        }
    }

When I run the script in console, result returns NULL. I have been using GGTS for the application but I have no idea how to persist initial data from GGTS so I ran the grails console command from the command prompt for this. Is this the right way? Is there a way to do it directly from GGTS?

PS: I have seen this question Seed data for grails application but it seems to be too advanced for where I am now and what I need.

Thanks


Solution

  • prepare a SQL file with the insert-statements you need and run it in the BootStrap.groovy:

    class BootStrap {
    
      def dataSource 
    
      def init = { servletContext ->
        Sql sql = new Sql( dataSource )
        new File( pathToSql ).eachLine{ sql.executeInsert it }
        sql.commit()
        sql.close()
      }
    }
    

    is simple enough, isn't it?