DB Helper - REST

1 vote
Dependency :
compile ":db-helper-rest:0.8.1"

Documentation Source Issues

Summary

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

Installation

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
       }
   }
}

Description

The plugin provides methods that are exposed via REST API (use GET request). It makes possible to:
  • drop database schema
http://localhost:8080/your-app/dbHelper/drop
  • create database schema
http://localhost:8080/your-app/dbHelper/create
  • call init closure from the project BootStrap.groovy
http://localhost:8080/your-app/dbHelper/init
  • drop and create database schema
http://localhost:8080/your-app/dbHelper/dropCreate
  • drop and create database schema and call init closure from the project BootStrap.groovy
http://localhost:8080/your-app/dbHelper/dropCreateInit

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 (System.properties[dbInitScript]) {
    String config = System.properties[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()) {
            dbInitScript.execute()
         }
      }
   }
}

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 org.grails.plugin.db.helper.rest.DbHelperController
import grails.plugins.springsecurity.Secured

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