Plugins You can find out about all the publicly available Grails plugins.

DB Helper - REST

1 vote
compile "org.grails.plugins:db-helper-rest:0.8.1"

 Documentation  Source  Issues


Provides methods for dropping, creating and initialization of database schema from the project bootstrap.


Add dependency to plugins in BuildConfig

Add a dependency for the plugin in BuildConfig.groovy. The plugin must be excluded in the production mode since the plugin is meant only to be used in development or test environments.

import grails.util.Environment
plugins {
   runtime (':db-helper-rest:0.8.1') {
       if (Environment.current == Environment.PRODUCTION) {
           export = false


The plugin provides methods that are exposed via REST API (use GET request). It makes possible to:
  • drop database schema
  • create database schema
  • call init closure from the project BootStrap.groovy
  • drop and create database schema
  • drop and create database schema and call init closure from the project BootStrap.groovy

Running external Groovy together with SQL scripts

You can pass the path to a Groovy script that executes SQL scripts when starting up the application:
grails run-app -Dgrails.env=test -Dgrails.db.init.script.location=/path/to/DbInit.groovy

Here is an example Groovy script which is executing SQL scripts, DbInit.groovy

import org.grails.plugin.db.init.SqlHelper

SqlHelper sql = new SqlHelper()

sql.execute(sql.root + '/path/to/users.sql') sql.execute(sql.root + '/path/to/accounts.sql')

sql.root returns root folder of the Groovy script, /path/to/ for DbInit.groovy

Add this code into Config.groovy in your application:

String dbInitScript = 'grails.db.init.script.location'
if (System.env[dbInitScript]) {
    String config = System.env[dbInitScript]
    grails.db.init.script.location = config
if ([dbInitScript]) {
    String config =[dbInitScript]
    grails.db.init.script.location = config

Then in Bootstrap you need to make a hook, so the Groovy script is executed and SQL scripts are loaded. First we ask whether the Groovy script from grails.db.init.script.location is available. If true, then we execute the Groovy script.

import org.grails.plugin.db.init.DbInitScript
def grailsApplication
def init = { servletContext ->
   environments {
      test {
         DbInitScript dbInitScript = new DbInitScript(grailsApplication)
         if (dbInitScript.isAvailable()) {

Integration with Spring Security plugin

If you are using Spring Security plugin, you need to craete a controller which extends DbHelperController and annotate it with @Secure annotation. For example:

import grails.plugins.springsecurity.Secured

@Secured("IS_AUTHENTICATED_ANONYMOUSLY") class SecuredDbHelperController extends DbHelperController { }