1. Introduction to the Database Migration Plugin

The Database Migration plugin helps you manage database changes while developing Grails applications. The plugin uses the Liquibase library.

Using this plugin (and Liquibase in general) adds some structure and process to managing database changes. It will help avoid inconsistencies, communication issues, and other problems with ad-hoc approaches.

Database migrations are represented in text form, either using a Groovy DSL or native Liquibase XML, in one or more changelog files. This approach makes it natural to maintain the changelog files in source control and also works well with branches. Changelog files can include other changelog files, so often developers create hierarchical files organized with various schemes. One popular approach is to have a root changelog named changlog.groovy (or changelog.xml) and to include a changelog per feature/branch that includes multiple smaller changelogs. Once the feature is finished and merged into the main development tree/trunk the changelog files can either stay as they are or be merged into one large file. Use whatever approach makes sense for your applications, but keep in mind that there are many options available for changelog management.

Individual changes have an ID that should be globally unique, although they also include the username of the user making the change, making the combination of ID and username unique (although technically the ID, username, and changelog location are the "unique key").

As you make changes in your code (typically domain classes) that require changes in the database, you add a new change set to the changelog. Commit the code changes along with the changelog additions, and the other developers on your team will get both when they update from source control. Once they apply the new changes their code and development database will be in sync with your changes. Likewise when you deploy to a QA, a staging server, or production, you’ll run the un-run changes that correspond to the code updates to being that environment’s database in sync. Liquibase keeps track of previously executed changes so there’s no need to think about what has and hasn’t been run yet.

Scripts

Your primary interaction with the plugin will be using the provided scripts. For the most part these correspond to the many Liquibase commands that are typically executed directly from the commandline or with its Ant targets, but there are also a few Grails-specific scripts that take advantage of the information available from the GORM mappings.

All of the scripts start with dbm- to ensure that they’re unique and don’t clash with scripts from Grails or other plugins.

2. Getting Started

The first step is to add a dependency for the plugin in build.gradle:

buildscript {
   dependencies {
      ...
      classpath 'org.grails.plugins:database-migration:2.0.1'
   }
}

dependencies {
   ...
     compile 'org.grails.plugins:database-migration:2.0.1'
}

It is also recommended to add a direct dependency to liquibase because Spring Boot overrides the one provided by this plugin

dependencies {
   ...
     compile 'org.liquibase:liquibase-core:3.5.3'
}

You should also tell Gradle about the migrations folder

sourceSets {
    main {
        resources {
            srcDir 'grails-app/migrations'
        }
    }
}

Typical initial workflow

Next you’ll need to create an initial changelog. You can use Liquibase XML or the plugin’s Groovy DSL for individual files. You can even mix and match; Groovy files can include other Groovy files and Liquibase XML files (but XML files can’t include Groovy files).

Depending on the state of your database and code, you have two options; either create a changelog from the database or create it from your domain classes. The decision tends to be based on whether you prefer to design the database and adjust the domain classes to work with it, or to design your domain classes and use Hibernate to create the corresponding database structure.

To create a changelog from the database, use the dbm-generate-changelog script:

grails dbm-generate-changelog changelog.groovy

or

grails dbm-generate-changelog changelog.xml

depending on whether you prefer the Groovy DSL or XML. The filename is relative to the changelog base folder, which defaults to grails-app/migrations.

If you use the XML format (or use a non-default Groovy filename), be sure to change the name of the file in application.groovy so dbm-update and other scripts find the file:
grails.plugin.databasemigration.changelogFileName = 'changelog.xml'

Since the database is already correct, run the dbm-changelog-sync script to record that the changes have already been applied:

grails dbm-changelog-sync

Running this script is primarily a no-op except that it records the execution(s) in the Liquibase DATABASECHANGELOG table.

To create a changelog from your domain classes, use the dbm-generate-gorm-changelog script:

grails dbm-generate-gorm-changelog changelog.groovy

or

grails dbm-generate-gorm-changelog changelog.xml

If you haven’t created the database yet, run the dbm-update script to create the corresponding tables:

grails dbm-update

or the dbm-changelog-sync script if the database is already in sync with your code:

grails dbm-changelog-sync

Source control

Now you can commit the changelog and the corresponding application code to source control. Other developers can then update and synchronize their databases, and start doing migrations themselves.

3. Configuration

There are a few configuration options for the plugin. All configurations are prefixed with grails.plugin.databasemigration:

Property Default Meaning

changelogLocation

grails-app/migrations

the folder containing the main changelog file (which can include one or more other files)

changelogFileName

changelog.groovy

the name of the main changelog file

changelogProperties

none

a map of properties to use for property substitution in Groovy DSL changelogs

contexts

none

A comma-delimited list of context names. If specified, only changesets tagged with one of the context names will be run

dbDocLocation

target/dbdoc

the directory where the output from the dbm-db-doc script is written

dbDocController.enabled

true in dev mode

whether the /dbdoc/ url is accessible at runtime

dropOnStart

false

if true then drops all tables before auto-running migrations (if updateOnStart is true)

updateOnStart

false

if true then changesets from the specified list of names will be run at startup

updateOnStartFileName

none

the file name (relative to changelogLocation) to run at startup if updateOnStart is true

updateOnStartDefaultSchema

none

the default schema to use when running auto-migrate on start

updateOnStartContexts

none

A comma-delimited list of context names. If specified, only changesets tagged with one of the context names will be run

autoMigrateScripts

[RunApp]

the scripts when running auto-migrate. Useful to run auto-migrate during test phase with: [RunApp, TestApp]

excludeObjects

none

A comma-delimited list of database object names to ignore while performing a dbm-gorm-diff or dbm-generate-gorm-changelog

includeObjects

none

A comma-delimited list of database object names to look for while performing a dbm-gorm-diff or dbm-generate-gorm-changelog

databaseChangeLogTableName

databasechangelog

the Liquibase changelog record table name

databaseChangeLogLockTableName

databasechangeloglock

the Liquibase lock table name

loadPluginMigrations

false

if true, load migrations from the classpath (i.e. plugin migrations) using GrailsClassLoaderResourceAccessor instead of FileSystemResourceAccessor (War deployed mode always uses GrailsClassLoaderResourceAccessor)

All of the above configs can be used for a multiple datasources

Multiple DataSource Example:

If a reports dataSource is configured in application.yml

dataSources:
    dataSource:
        ...
    reports:
        url: ...
        driverClassName: ...

The configuration for this data source would be:

grails.plugin.databasemigration.reports.updateOnStart = true
grails.plugin.databasemigration.reports.changelogFileName = changelog-reports.groovy

4. General Usage

After creating the initial changelog, the typical workflow will be along the lines of: * make domain class changes that affect the schema * add changes to the changelog for them * backup your database in case something goes wrong * run grails dbm-update to update your development environment (or wherever you’re applying the changes) * check the updated domain class(es) and changelog(s) into source control

When running migration scripts on non-development databases, it’s important that you backup the database before running the migration in case anything goes wrong. You could also make a copy of the database and run the script against that, and if there’s a problem the real database will be unaffected.

To create the changelog additions, you can either manually create the changes or with the dbm-gorm-diff script (you can also use the dbm-diff script but it’s far less convenient and requires a 2nd temporary database).

You have a few options with dbm-gorm-diff: * dbm-gorm-diff will dump to the console if no filename is specified, so you can copy/paste from there * if you include the --add parameter when running the script with a filename it will register an include for the the filename in the main changelog for you

Regardless of which approach you use, be sure to inspect generated changes and adjust as necessary.

4.1. Autorun on start

Since Liquibase maintains a record of changes that have been applied, you can avoid manually updating the database by taking advantage of the plugin’s auto-run feature. By default this is disabled, but you can enable it by adding

grails.plugin.databasemigration.updateOnStart = true

to application.groovy. In addition you must specify the file containing changes; specify the name using the updateOnStartFileName property, e.g.:

grails.plugin.databasemigration.updateOnStartFileName = 'changelog.groovy'

Since changelogs can contain changelogs you’ll most often just specify the root changelog, changelog.groovy by convention. Any changes that haven’t been executed (in the specified file(s) or files included by them) will be run in the order specified.

You may optionally limit the plugin’s auto-run feature to run only specific contexts. If this configuration parameter is empty or omitted, all contexts will be run.

grails.plugin.databasemigration.updateOnStartContexts = ['context1,context2']

You can be notified when migration are run (for example to do some work before and/or after the migrations execute) by registering a "callback" class as a Spring bean. The class can have any name and package and doesn’t have to implement any interface since its methods will be called using Groovy duck-typing.

The bean name is "migrationCallbacks" and there are currently three callback methods supported (all are optional):

  • beforeStartMigration will be called (if it exists) for each datasource before any migrations have run; the method will be passed a single argument, the Liquibase Database for that datasource

  • onStartMigration will be called (if it exists) for each migration script; the method will be passed three arguments, the Liquibase Database, the Liquibase instance, and the changelog file name

  • afterMigrations will be called (if it exists) for each datasource after all migrations have run; the method will be passed a single argument, the Liquibase Database for that datasource

An example class will look like this:

package com.mycompany.myapp

import liquibase.Liquibase
import liquibase.database.Database

class MigrationCallbacks {

   void beforeStartMigration(Database Database) {
      ...
   }

   void onStartMigration(Database database, Liquibase liquibase, String changelogName) {
      ...
   }

   void afterMigrations(Database Database) {
      ...
   }
}

Register it in resources.groovy:

import com.mycompany.myapp.MigrationCallbacks

beans = {
   migrationCallbacks(MigrationCallbacks)
}

5. Groovy Changes

In addition to the built-in Liquibase changes (see the documentation for what’s available) you can also make database changes using Groovy code (as long as you’re using the Groovy DSL file format). These changes use the grailsChange tag name and are contained in a changeSet tag like standard built-in tags.

There are four supported inner tags and two callable methods (to override the default confirmation message and checksum value).

5.1. General format

This is the general format of a Groovy-based change; all inner tags and methods are optional:

databaseChangeLog = {

   changeSet(author: '...', id: '...') {

      grailsChange {
         init {
             // arbitrary initialization code; note that no
             // database or connection is available
         }

         validate {
            // can call warn(String message) to log a warning
            // or error(String message) to stop processing
         }

         change {
            // arbitrary code; make changes directly and/or return a
            // SqlStatement using the sqlStatement(SqlStatement sqlStatement)
            // method or multiple with sqlStatements(List sqlStatements)

            confirm 'change confirmation message'
         }

         rollback {
            // arbitrary code; make rollback changes directly and/or
            // return a SqlStatement using the sqlStatement(SqlStatement sqlStatement)
            // method or multiple with sqlStatements(List sqlStatements)

            confirm 'rollback confirmation message'
         }

         confirm 'confirmation message'

         checkSum 'override value for checksum'
      }

   }
}

5.2. Available variables

These variables are available throughout the change closure: * changeSet the current Liquibase ChangeSet instance * resourceAccessor the current Liquibase ResourceAccessor instance * ctx the Spring ApplicationContext * application the GrailsApplication

The change and rollback closures also have the following available: * database the current Liquibase Database instance * databaseConnection the current Liquibase DatabaseConnection instance, which is a wrapper around the JDBC Connection (but doesn’t implement the Connection interface) * connection the real JDBC Connection instance (a shortcut for database.connection.wrappedConnection) * sql a groovy.sql.Sql instance which uses the current connection and can be used for arbitrary queries and updates

init

This is where any optional initialization should happen. You can’t access the database from this closure.

validate

If there are any necessary validation checks before executing changes or rollbacks they should be done here. You can log warnings by calling warn(String message) and stop processing by calling error(String message). It may make more sense to use one or more `preCondition`s instead of directly validating here.

change

All migration changes are done in the change closure. You can make changes directly (using the sql instance or the connection) and/or return one or more SqlStatement`s. You can call `sqlStatement(SqlStatement statement) multiple times to register instances to be run. You can also call the sqlStatements(statements) method with an array or list of instances to be run.

rollback

All rollback changes are done in the rollback closure. You can make changes directly (using the sql instance or the connection) and/or return one or more SqlStatement`s. You can call `sqlStatement(SqlStatement statement) multiple times to register instances to be run. You can also call the sqlStatements(statements) method with an array or list of instances to be run.

confirm

The confirm(String message) method is used to specify the confirmation message to be shown. The default is "Executed GrailsChange" and it can be overridden in the change or rollback closures to allow phase-specific messages or outside of both closures to use the same message for the update and rollback phase.

checkSum

The checksum for the change will be generated automatically, but if you want to override the value that gets hashed you can specify it with the checkSum(String value) method.

6. Groovy Preconditions

In addition to the built-in Liquibase preconditions (see the documentation for what’s available) you can also specify preconditions using Groovy code (as long as you’re using the Groovy DSL file format). These changes use the grailsPrecondition tag name and are contained in the databaseChangeLog tag or in a changeSet tag like standard built-in tags.

6.1. General format

This is general format of a Groovy-based precondition:

databaseChangeLog = {

   changeSet(author: '...', id: '...') {

      preConditions {

         grailsPrecondition {

            check {

               // use an assertion
               assert x == x

               // use an assertion with an error message
               assert y == y : 'value cannot be 237'

               // call the fail method
               if (x != x) {
                  fail 'x != x'
               }

               // throw an exception (the fail method is preferred)
               if (y != y) {
                  throw new RuntimeException('y != y')
               }
            }

         }

      }
   }
}

As you can see there are a few ways to indicate that a precondition wasn’t met: * use a simple assertion * use an assertion with a message * call the fail(String message) method (throws a PreconditionFailedException) * throw an exception (shouldn’t be necessary - use assert or fail() instead)

6.2. Available variables

  • database

    • the current Liquibase Database instance

  • databaseConnection

    • the current Liquibase DatabaseConnection instance, which is a wrapper around the JDBC Connection (but doesn’t implement the Connection interface)

  • connection

    • the real JDBC Connection instance (a shortcut for database.connection.wrappedConnection)

  • sql

    • a groovy.sql.Sql instance which uses the current connection and can be used for arbitrary queries and updates

  • resourceAccessor

    • the current Liquibase ResourceAccessor instance

  • ctx

    • the Spring ApplicationContext

  • application

    • the GrailsApplication

  • changeSet

    • the current Liquibase ChangeSet instance

  • changeLog

    • the current Liquibase DatabaseChangeLog instance

6.3. Utility methods

  • createDatabaseSnapshotGenerator()

    • retrieves the DatabaseSnapshotGenerator for the current Database

  • createDatabaseSnapshot(String schemaName = null)

    • creates a DatabaseSnapshot for the current Database (and schema if specified)

7. GORM Support

The plugin’s support for GORM is one feature that differentiates it from using Liquibase directly. Typically when using Liquibase you make changes to a database yourself, and then create changesets manually, or use a diff script to compare your updated database to one that hasn’t been updated yet. This is a decent amount of work and is rather error-prone. It’s easy to forget some changes that aren’t required but help performance, for example creating an index on a foreign key when using MySQL.

create-drop, create, and update

On the other end of the spectrum, Hibernate’s create-drop mode (or create) will create a database that matches your domain model, but it’s destructive since all previous data is lost when it runs. This works well in the very early stages of development but gets frustrating quickly. Unfortunately Hibernate’s update mode seems like a good compromise since it only makes changes to your existing schema, but it’s very limited in what it will do. It’s very pessimistic and won’t make any changes that could lose data. So it will add new tables and columns, but won’t drop anything. If you remove a not-null domain class property you’ll find you can’t insert anymore since the column is still there. And it will create not-null columns as nullable since otherwise existing data would be invalid. It won’t even widen a column e.g. from VARCHAR(100) to VARCHAR(200).

dbm-gorm-diff

The plugin provides a script that will compare your GORM current domain model with a database that you specify, and the result is a Liquibase changeset - dbm-gorm-diff. This is the same changeset you would get if you exported your domain model to a scratch database and diffed it with the other database, but it’s more convenient.

So a good workflow would be:

  • make whatever domain class changes you need (add new ones, delete unneeded ones, add/change/remove properties, etc.)

  • once your tests pass and you’re ready to commit your changes to source control, run the script to generate the changeset that will bring your database back in line with your code

  • add the changeset to an existing changelog file, or use the include tag to include the whole file

  • run the changeset on your functional test database

  • assuming your functional tests pass, check everything in as one commit

  • the other members of your team will get both the code and database changes when they next update, and will know to run the update script to sync their database with the latest code

  • once you’re ready to deploy to QA for testing (or staging or production), you can run all of the un-run changes since the last deployment

dbm-generate-gorm-changelog

The dbm-generate-gorm-changelog script is useful for when you want to switch from create-drop mode to doing proper migrations. It’s not very useful if you already have a database that’s in sync with your code, since you can just use the dbm-generate-changelog script that creates a changelog from your database.

8. DbDoc Controller

You can use the dbm-db-doc script to generate static HTML files to view changelog information, but another option is to use the DbDocController at runtime. By default this controller is mapped to /appname/dbdoc/ but this can be customized with UrlMappings like any controller.

You probably don’t want to expose this information to all of your application’s users so by default the controller is only enabled in the development environment. But you can enable or disable it for any environment in application.groovy with the dbDocController.enabled config option. For example to enable for all environments (be sure to guard the URL with a security plugin in prod):

grails.plugin.databasemigration.dbDocController.enabled = true

or to enable in the production environment:

environments {
   production {
      grails.plugin.databasemigration.dbDocController.enabled = true
   }
   ...
}

9. Reference

9.1. Diff Scripts

9.1.1. dbm-diff

Purpose

Compares two databases and creates a changelog that will make the changes required to bring them into sync.

Description

Executes against the database configured in application.[yml|groovy] for the current environment (defaults to dev) and another configured datasource in application.[yml|groovy].

If a filename parameter is specified then the output will be written to the named file, otherwise it will write to the console. If the filename ends with .groovy a Groovy DSL file will be created, otherwise a standard XML file will be created.

File are written to the migrations folder, so specify the filename relative to the migrations folder (grails-app/migrations by default).

Usage:

grails <<environment>> dbm-diff <<otherEnv>> <<filename>> --defaultSchema=<<defaultSchema>> --dataSource=<<dataSource>> --add

Required arguments:

  • otherEnv - The name of the environment to compare to

Optional arguments:

  • filename - The path to the output file to write to. If not specified output is written to the console

  • defaultSchema - The default schema name to use

  • add - if specified add an include in the root changelog file referencing the new file

  • dataSource - if provided will run the script for the specified dataSource. Not needed for the default dataSource.

Note that the defaultSchema and dataSource parameter name and value must be quoted if executed in Windows, e.g.
grails dbm-diff "--defaultSchema=<<defaultSchema>>" "--dataSource=<<dataSource>>"
For the dataSource parameter; If the data source is configured as reports underneath the dataSources key in application.[yml|groovy], the value should be reports.
--dataSource=reports

9.1.2. dbm-gorm-diff

Purpose

Diffs GORM classes against a database and generates a changelog XML or Groovy DSL file.

Description

Creates a Groovy DSL file if the filename is specified and it ends with .groovy. If another extension is specified it creates a standard Liquibase XML file, and if no filename is specified it writes to the console.

File are written to the migrations folder, so specify the filename relative to the migrations folder (grails-app/migrations by default).

Similar to dbm-diff but diffs the current configuration based on the application’s domain classes with the database configured in application.[yml|groovy] for the current environment (defaults to dev).

Doesn’t modify any existing files - you need to manually merge the output into the changeset along with any necessary modifications.

You can configure database objects to be ignored by this script - either in the GORM classes or in the target database. For example you may want domain objects that are transient, or you may have externally-managed tables, keys, etc. that you want left alone by the diff script. The configuration name for these ignored objects is grails.plugin.databasemigration.ignoredObjects, whose value is a list of strings.

Usage:

grails <<environment>> dbm-gorm-diff <<filename>> --defaultSchema=<<defaultSchema>> --dataSource=<<dataSource>> --add

Required arguments: none .

Optional arguments:

  • filename - The path to the output file to write to. If not specified output is written to the console

  • defaultSchema - The default schema name to use

  • add - if specified add an include in the root changelog file referencing the new file

  • dataSource - if provided will run the script for the specified dataSource. Not needed for the default dataSource.

Note that the defaultSchema and dataSource parameter name and value must be quoted if executed in Windows, e.g.
grails dbm-gorm-diff "--defaultSchema=<<defaultSchema>>" "--dataSource=<<dataSource>>"
For the dataSource parameter; If the data source is configured as reports underneath the dataSources key in application.[yml|groovy], the value should be reports.
--dataSource=reports

9.2. Documentation Scripts

9.2.1. dbm-db-doc

Purpose

Generates Javadoc-like documentation based on current database and change log.

Description

Writes to the folder specified by the destination parameter, or to the grails.plugin.databasemigration.dbDocLocation configuration option (defaults to target/dbdoc).

Usage:

grails <<environment>> dbm-db-doc <<destination>> --contexts=<<contexts>> --dataSource=<<dataSource>>

Required arguments: none .

Optional arguments:

  • destination - The path to write to

  • contexts - A comma-delimited list of context names. If specified, only changesets tagged with one of the context names will be included

  • dataSource - if provided will run the script for the specified dataSource. Not needed for the default dataSource.

Note that the contexts and dataSource parameter name and value must be quoted if executed in Windows, e.g.
grails dbm-db-doc "--contexts=<<contexts>>" "--dataSource=<<dataSource>>"
For the dataSource parameter; If the data source is configured as reports underneath the dataSources key in application.[yml|groovy], the value should be reports.
--dataSource=reports

9.3. Maintenance Scripts

9.3.1. dbm-add-migration

Purpose

Adds a template migration file to your project and to the changelog file.

Description

This script provides a template in which to place your migration behaviour code, whether grails code or raw SQL.

Usage:

grails <<environment>> dbm-add-migration <<migrationName>>

Required arguments:

  • migrationName - The name of the migration - will be used as a filename and the default migration id.

This script only supports .groovy-style migrations at the moment.

9.3.2. dbm-changelog-sync-sql

Purpose

Writes the SQL that will mark all changes as executed in the database to STDOUT or a file.

Description

Generates the SQL statements for the Liquibase DATABASECHANGELOG control table.

Usage:

grails <<environment>> dbm-changelog-sync-sql <<filename>> --contexts=<<contexts>> --defaultSchema=<<defaultSchema>> --dataSource=<<dataSource>>

Required arguments: none .

Optional arguments:

  • filename - The path to the output file to write to. If not specified output is written to the console

  • contexts - A comma-delimited list of context names. If specified, only changesets tagged with one of the context names will be included

  • defaultSchema - The default schema name to use

  • dataSource - if provided will run the script for the specified dataSource. Not needed for the default dataSource.

Note that the contexts, defaultSchema, and dataSource parameter name and value must be quoted if executed in Windows, e.g.
grails dbm-changelog-sync "--contexts=<<contexts>>" "--defaultSchema=<<defaultSchema>>" "--dataSource=<<dataSource>>"
For the dataSource parameter if the data source is configured as reports underneath the dataSources key` in application.[yml|groovy] the suffix of reports will be used as the parameter value.
--dataSource=reports

9.3.3. dbm-changelog-sync

Purpose

Mark all changes as executed in the database.

Description

Registers all changesets as having been run in the Liquibase control table. This is useful when then changes have already been applied, for example if you’ve just created a changelog from your database using the dbm-generate-changelog script.

Usage:

grails <<environment>> dbm-changelog-sync --contexts=<<contexts>> --defaultSchema=<<defaultSchema>> --dataSource=<<dataSource>>

Required arguments: none .

Optional arguments:

  • contexts - A comma-delimited list of context names. If specified, only changesets tagged with one of the context names will be included

  • defaultSchema - The default schema name to use

  • dataSource - if provided will run the script for the specified dataSource. Not needed for the default dataSource.

Note that the contexts, defaultSchema, and dataSource parameter name and value must be quoted if executed in Windows, e.g.
grails dbm-changelog-sync "--contexts=<<contexts>>" "--defaultSchema=<<defaultSchema>>" "--dataSource=<<dataSource>>"
For the dataSource parameter; If the data source is configured as reports underneath the dataSources key in application.[yml|groovy], the value should be reports.
--dataSource=reports

9.3.4. dbm-changelog-to-groovy

Purpose

Converts a Liquibase XML changelog file to a Groovy DSL file.

Description

If the Groovy file name isn’t specified the name and location will be the same as the original XML file with a .groovy extension.

Usage:

grails <<environment>> dbm-changelog-to-groovy [xml_file_name] [groovy_file_name]

Required arguments:

  • xml_file_name - The name and path of the XML file to convert

Optional arguments:

  • groovy_file_name - The name and path of the Groovy file

9.3.5. dbm-clear-checksums

Purpose

Removes current checksums from database. On next run checksums will be recomputed.

Description

Usage:

grails <<environment>> dbm-clear-checksums --dataSource=<<dataSource>>

Required arguments: none .

Optional arguments:

  • dataSource - if provided will run the script for the specified dataSource. Not needed for the default dataSource.

Note that the dataSource parameter name and value must be quoted if executed in Windows, e.g.
grails dbm-clear-checksums  "--dataSource=<<dataSource>>"
For the dataSource parameter; If the data source is configured as reports underneath the dataSources key in application.[yml|groovy], the value should be reports.
--dataSource=reports

9.3.6. dbm-create-changelog

Purpose

Creates an empty changelog file.

Description

Creates a new empty file instead of generating the file from the database (using dbm-generate-changelog) or your GORM classes (using dbm-generate-gorm-changelog).

Usage:

grails <<environment>> dbm-create-changelog <<filename>> --dataSource=<<dataSource>>

Required arguments:

  • filename - The path to the output file to write to

Optional arguments:

  • dataSource - if provided will run the script for the specified dataSource creating a file named changelog-<<dataSource>>.groovy if a filename is not given. Not needed for the default dataSource.

Note that the dataSource parameter name and value must be quoted if executed in Windows, e.g.
grails dbm-create-changelog "--dataSource=<<dataSource>>"
For the dataSource parameter; If the data source is configured as reports underneath the dataSources key in application.[yml|groovy], the value should be reports.
--dataSource=reports

9.3.7. dbm-drop-all

Purpose

Drops all database objects owned by the user.

Description

Usage:

grails <<environment>> dbm-drop-all <<schemaNames>> --defaultSchema=<<defaultSchema>> --dataSource=<<dataSource>>

Required arguments: none .

Optional arguments:

  • schemaNames - A comma-delimited list of schema names to use

  • defaultSchema - The default schema name to use if the schemaNames parameter isn’t present

  • dataSource - if provided will run the script for the specified dataSource. Not needed for the default dataSource.

Note that the defaultSchema and dataSource parameter name and value must be quoted if executed in Windows, e.g.
grails dbm-drop-all "--defaultSchema=<<defaultSchema>>" "--dataSource=<<dataSource>>"
For the dataSource parameter; If the data source is configured as reports underneath the dataSources key in application.[yml|groovy], the value should be reports.
--dataSource=reports

9.3.8. dbm-list-locks

Purpose

Lists who currently has locks on the database changelog to STDOUT or a file.

Description

Usage:

grails <<environment>> dbm-list-locks <<filename>> --defaultSchema=<<defaultSchema>> --dataSource=<<dataSource>>

Required arguments: none .

Optional arguments:

  • filename - The path to the output file to write to. If not specified output is written to the console

  • defaultSchema - The default schema name to use

  • dataSource - if provided will run the script for the specified dataSource. Not needed for the default dataSource.

Note that the defaultSchema and dataSource parameter name and value must be quoted if executed in Windows, e.g.
grails dbm-list-locks "--defaultSchema=<<defaultSchema>>" "--dataSource=<<dataSource>>"
For the dataSource parameter; If the data source is configured as reports underneath the dataSources key in application.[yml|groovy], the value should be reports.
--dataSource=reports

9.3.9. dbm-list-tags

Purpose

Lists the tags in the current database.

Description

Usage:

grails <<environment>> dbm-list-tags --defaultSchema=<<defaultSchema>>

Required arguments:

Required arguments: none .

Optional arguments:

  • defaultSchema - The default schema name to use

Note that the defaultSchema parameter name and value must be quoted if executed in Windows, e.g.
grails dbm-tag "--defaultSchema=<<defaultSchema>>"

9.3.10. dbm-mark-next-changeset-ran

Purpose

Mark the next change set as executed in the database.

Description

If a filename is specified, writes the SQL that will perform the update that file but doesn’t update.

Usage:

grails <<environment>> dbm-mark-next-changeset-ran <<filename>> --contexts=<<contexts>> --defaultSchema=<<defaultSchema>> --dataSource=<<dataSource>>

Required arguments: none .

Optional arguments:

  • filename - The path to the output file to write to

  • contexts - A comma-delimited list of context names. If specified, only changesets tagged with one of the context names will be run

  • defaultSchema - The default schema name to use

  • dataSource - if provided will run the script for the specified dataSource. Not needed for the default dataSource.

Note that the contexts, defaultSchema, and dataSource parameter name and value must be quoted if executed in Windows, e.g.
grails dbm-mark-next-changeset-ran "--contexts=<<contexts>>" "--defaultSchema=<<defaultSchema>>" "--dataSource=<<dataSource>>"
For the dataSource parameter; If the data source is configured as reports underneath the dataSources key in application.[yml|groovy], the value should be reports.
--dataSource=reports

9.3.11. dbm-register-changelog

Purpose

Adds an include for the specified changelog to the main changelog.

Description

Usage:

grails <<environment>> dbm-register-changelog <<filename>> --dataSource=<<dataSource>>

Required arguments:

  • filename - The path to the changelog file to include.

Optional arguments:

  • dataSource - if provided will run the script for the specified dataSource using changelog-<<dataSource>>.groovy if a filename is not given. Not needed for the default dataSource.

Note that the dataSource parameter name and value must be quoted if executed in Windows, e.g.
grails dbm-diff  "--dataSource=<<dataSource>>"
For the dataSource parameter, if the data source is configured as reports underneath the dataSources key in application.[yml|groovy] the suffix of reports will be used as the parameter value.
--dataSource=reports

9.3.12. dbm-release-locks

Purpose

Releases all locks on the database changelog.

Description

Usage:

grails <<environment>> dbm-release-locks --defaultSchema=<<defaultSchema>> --dataSource=<<dataSource>>

Required arguments: none .

Optional arguments:

  • defaultSchema - The default schema name to use

  • dataSource - if provided will run the script for the specified dataSource. Not needed for the default dataSource.

Note that the defaultSchema and dataSource parameter name and value must be quoted if executed in Windows, e.g.
grails dbm-release-locks "--defaultSchema=<<defaultSchema>>" "--dataSource=<<dataSource>>"
For the dataSource parameter; If the data source is configured as reports underneath the dataSources key in application.[yml|groovy], the value should be reports.
--dataSource=reports

9.3.13. dbm-status

Purpose

Outputs count or list of unrun change sets to STDOUT or a file.

Description

Usage:

grails <<environment>> dbm-status <<filename>> --verbose=<<verbose>> --contexts=<<contexts>> --defaultSchema=<<defaultSchema>> --dataSource=<<dataSource>>

Required arguments: none .

Optional arguments:

  • filename - The path to the output file to write to. If not specified output is written to the console

  • verbose - If true (the default) the changesets are listed; if false only the count is displayed

  • contexts - A comma-delimited list of context names. If specified, only changesets tagged with one of the context names will be included

  • defaultSchema - The default schema name to use

  • dataSource - if provided will run the script for the specified dataSource. Not needed for the default dataSource.

Note that the verbose, contexts, defaultSchema and dataSource parameter name and value must be quoted if executed in Windows, e.g.
grails dbm-status "--verbose=<<verbose>>" "--contexts=<<contexts>>" "--defaultSchema=<<defaultSchema>>" "--dataSource=<<dataSource>>"
For the dataSource parameter; If the data source is configured as reports underneath the dataSources key in application.[yml|groovy], the value should be reports.
--dataSource=reports

9.3.14. dbm-tag

Purpose

Adds a tag to mark the current database state.

Description

Useful for future rollbacks to a specific tag (e.g. using the dbm-rollback script).

Usage:

grails <<environment>> dbm-tag <<tagName>> --defaultSchema=<<defaultSchema>> --dataSource=<<dataSource>>

Required arguments:

  • tagName - The name of the tag to use

Optional arguments:

  • defaultSchema - The default schema name to use

  • dataSource - if provided will run the script for the specified dataSource. Not needed for the default dataSource.

Note that the defaultSchema and dataSource parameter name and value must be quoted if executed in Windows, e.g.
grails dbm-tag "--defaultSchema=<<defaultSchema>>" "--dataSource=<<dataSource>>"
For the dataSource parameter; If the data source is configured as reports underneath the dataSources key in application.[yml|groovy], the value should be reports.
--dataSource=reports

9.3.15. dbm-validate

Purpose

Checks the changelog for errors.

Description

Prints any validation messages to the console.

Usage:

grails <<environment>> dbm-validate --dataSource=<<dataSource>>

Required arguments: none .

Optional arguments:

  • dataSource - if provided will run the script for the specified dataSource. Not needed for the default dataSource.

Note that the dataSource parameter name and value must be quoted if executed in Windows, e.g.
grails dbm-validate "--dataSource=<<dataSource>>"
For the dataSource parameter; If the data source is configured as reports underneath the dataSources key in application.[yml|groovy], the value should be reports.
--dataSource=reports

9.4. Rollback Scripts

9.4.1. dbm-future-rollback-sql

Purpose

Writes SQL to roll back the database to the current state after the changes in the changeslog have been applied to STDOUT or a file.

Description

Usage:

grails <<environment>> dbm-future-rollback-sql <<filename>> --contexts=<<contexts>> --defaultSchema=<<defaultSchema>> --dataSource=<<dataSource>>

Required arguments: none .

Optional arguments:

  • filename - The path to the output file to write to. If not specified output is written to the console

  • contexts - A comma-delimited list of context names. If specified, only changesets tagged with one of the context names will be included

  • defaultSchema - The default schema name to use

  • dataSource - if provided will run the script for the specified dataSource. Not needed for the default dataSource.

Note that the contexts, defaultSchema, and dataSource parameter name and value must be quoted if executed in Windows, e.g.
grails dbm-future-rollback-sql "--contexts=<<contexts>>" "--defaultSchema=<<defaultSchema>>" "--dataSource=<<dataSource>>"
For the dataSource parameter; If the data source is configured as reports underneath the dataSources key in application.[yml|groovy], the value should be reports.
--dataSource=reports

9.4.2. dbm-generate-changelog

Purpose

Generates an initial changelog XML or Groovy DSL file from the database.

Description

Creates a Groovy DSL file if the filename is specified and it ends with .groovy. If another extension is specified it creates a standard Liquibase XML file, and if no filename is specified it writes to the console.

File are written to the migrations folder, so specify the filename relative to the migrations folder (grails-app/migrations by default).

Executes against the database configured in application.[yml|groovy] for the current environment (defaults to dev).

Usage:

grails <<environment>> dbm-generate-changelog <<filename>> --diffTypes=<<diffTypes>> --defaultSchema=<<defaultSchema>> --dataSource=<<dataSource>> --add

Required arguments: none .

Optional arguments:

  • filename - The path to the output file to write to. If not specified output is written to the console

  • diffTypes - A comma-delimited list of change types to include - see the documentation for what types are available

  • defaultSchema - The default schema name to use

  • dataSource - if provided will run the script for the specified dataSource. Not needed for the default dataSource.

  • add - if specified add an include in the root changelog file referencing the new file

Note that the diffTypes, defaultSchema, and dataSource parameter name and value must be quoted if executed in Windows, e.g.
grails dbm-generate-changelog "--diffTypes=<<diffTypes>>" "--defaultSchema=<<defaultSchema>>" "--dataSource=<<dataSource>>"
For the dataSource parameter; If the data source is configured as reports underneath the dataSources key in application.[yml|groovy], the value should be reports.
--dataSource=reports

9.4.3. dbm-generate-gorm-changelog

Purpose

Generates an initial changelog XML or Groovy DSL file from current GORM classes.

Description

Creates a Groovy DSL file if the filename is specified and it ends with .groovy. If another extension is specified it creates a standard Liquibase XML file, and if no filename is specified it writes to the console.

File are written to the migrations folder, so specify the filename relative to the migrations folder (grails-app/migrations by default).

Executes against the database configured in DataSource.groovy for the current environment (defaults to dev).

Usage:

grails <<environment>> dbm-generate-gorm-changelog <<filename>> --dataSource=<<dataSource>> --add

Required arguments: none .

Optional arguments:

  • filename - The path to the output file to write to. If not specified output is written to the console

  • dataSource - if provided will run the script for the specified dataSource. Not needed for the default dataSource.

  • add - if specified add an include in the root changelog file referencing the new file

Note that the dataSource parameter name and value must be quoted if executed in Windows, e.g.
grails dbm-generate-gorm-changelog  "--dataSource=<<dataSource>>"
For the dataSource parameter; If the data source is configured as reports underneath the dataSources key in application.[yml|groovy], the value should be reports.
--dataSource=reports

9.4.4. dbm-rollback-count-sql

Purpose

Writes the SQL to roll back the specified number of change sets to STDOUT or a file.

Description

Usage:

grails <<environment>> dbm-rollback-count-sql <<number>> <<filename>> --contexts=<<contexts>> --defaultSchema=<<defaultSchema>> --dataSource=<<dataSource>>

Required arguments:

  • number - The number of changesets to roll back

Optional arguments:

  • filename - The path to the output file to write to. If not specified output is written to the console

  • contexts - A comma-delimited list of context names. If specified, only changesets tagged with one of the context names will be included

  • defaultSchema - The default schema name to use

  • dataSource - if provided will run the script for the specified dataSource. Not needed for the default dataSource.

Note that the contexts, defaultSchema, and dataSource parameter name and value must be quoted if executed in Windows, e.g.
grails dbm-rollback-count-sql "--contexts=<<contexts>>" "--defaultSchema=<<defaultSchema>>" "--dataSource=<<dataSource>>"
For the dataSource parameter; If the data source is configured as reports underneath the dataSources key in application.[yml|groovy], the value should be reports.
--dataSource=reports

9.4.5. dbm-rollback-count

Purpose

Rolls back the specified number of change sets

Description

Usage:

grails <<environment>> dbm-rollback-count <<number>> --contexts=<<contexts>> --defaultSchema=<<defaultSchema>> --dataSource=<<dataSource>>

Required arguments:

  • number - The number of changesets to roll back

Optional arguments:

  • contexts - A comma-delimited list of context names. If specified, only changesets tagged with one of the context names will be run

  • defaultSchema - The default schema name to use

  • dataSource - if provided will run the script for the specified dataSource. Not needed for the default dataSource.

Note that the contexts, defaultSchema, and dataSource parameter name and value must be quoted if executed in Windows, e.g.
grails dbm-rollback-count "--contexts=<<contexts>>" "--defaultSchema=<<defaultSchema>>" "--dataSource=<<dataSource>>"
For the dataSource parameter; If the data source is configured as reports underneath the dataSources key in application.[yml|groovy], the value should be reports.
--dataSource=reports

9.4.6. dbm-rollback-sql

Purpose

Writes SQL to roll back the database to the state it was in when the tag was applied to STDOUT or a file.

Description

Requires that the named tag exists. You can create tags with the dbm-tag script.

Usage:

grails <<environment>> dbm-rollback-sql <<tagName>> <<filename>> --contexts=<<contexts>> --defaultSchema=<<defaultSchema>> --dataSource=<<dataSource>>

Required arguments:

  • tagName - The name of the tag to use

Optional arguments:

  • filename - The path to the output file to write to. If not specified output is written to the console

  • contexts - A comma-delimited list of context names. If specified, only changesets tagged with one of the context names will be included

  • defaultSchema - The default schema name to use

  • dataSource - if provided will run the script for the specified dataSource. Not needed for the default dataSource.

Note that the contexts, defaultSchema, and dataSource parameter name and value must be quoted if executed in Windows, e.g.
grails dbm-rollback-sql "--contexts=<<contexts>>" "--defaultSchema=<<defaultSchema>>" --dataSource=<<dataSource>>
For the dataSource parameter; If the data source is configured as reports underneath the dataSources key in application.[yml|groovy], the value should be reports.
--dataSource=reports

9.4.7. dbm-rollback-to-date-sql

Purpose

Writes SQL to roll back the database to the state it was in at the given date/time to STDOUT or a file.

Description

You can specify just the date, or the date and time. The date format must be yyyy-MM-dd and the time format must be HH:mm:ss.

Usage:

grails <<environment>> dbm-rollback-to-date-sql <<date>> <<time>> <<filename>> --contexts=<<contexts>> --defaultSchema=<<defaultSchema>> --dataSource=<<dataSource>>

Required arguments:

  • date - The rollback date

Optional arguments:

  • time - The rollback time

  • filename - The path to the output file to write to. If not specified output is written to the console

  • contexts - A comma-delimited list of context names. If specified, only changesets tagged with one of the context names will be included

  • defaultSchema - The default schema name to use

  • dataSource - if provided will run the script for the specified dataSource. Not needed for the default dataSource.

Note that the contexts, defaultSchema, dataSource parameter name and value must be quoted if executed in Windows, e.g.
grails dbm-rollback-to-date-sql "--contexts=<<contexts>>" "--defaultSchema=<<defaultSchema>>" "--dataSource=<<dataSource>>"
For the dataSource parameter; If the data source is configured as reports underneath the dataSources key in application.[yml|groovy], the value should be reports.
--dataSource=reports

9.4.8. dbm-rollback-to-date

Purpose

Rolls back the database to the state it was in at the given date/time.

Description

You can specify just the date, or the date and time. The date format must be yyyy-MM-dd and the time format must be HH:mm:ss.

Usage:

grails <<environment>> dbm-rollback-to-date <<date>> <<time>> --contexts=<<contexts>> --defaultSchema=<<defaultSchema>> --dataSource=<<dataSource>>

Required arguments:

  • date - The rollback date

Optional arguments:

  • time - The rollback time

  • contexts - A comma-delimited list of context names. If specified, only changesets tagged with one of the context names will be included

  • defaultSchema - The default schema name to use

  • dataSource - if provided will run the script for the specified dataSource. Not needed for the default dataSource.

Note that the contexts, defaultSchema, and dataSource parameter name and value must be quoted if executed in Windows, e.g.
grails dbm-rollback-to-date "--contexts=<<contexts>>" "--defaultSchema=<<defaultSchema>>" "--dataSource=<<dataSource>>"
For the dataSource parameter; If the data source is configured as reports underneath the dataSources key in application.[yml|groovy], the value should be reports.
--dataSource=reports

9.4.9. dbm-rollback

Purpose

Rolls back the database to the state it was in when the tag was applied.

Description

Requires that the named tag exists. You can create tags with the dbm-tag script.

Usage:

grails <<environment>> dbm-rollback <<tagName>> --contexts=<<contexts>> --defaultSchema=<<defaultSchema>> --dataSource=<<dataSource>>

Required arguments:

  • tagName - The name of the tag to use

Optional arguments:

  • contexts - A comma-delimited list of context names. If specified, only changesets tagged with one of the context names will be run

  • defaultSchema - The default schema name to use

  • dataSource - if provided will run the script for the specified dataSource. Not needed for the default dataSource.

Note that the contexts, defaultSchema, and dataSource parameter name and value must be quoted if executed in Windows, e.g.
grails dbm-rollback "--contexts=<<contexts>>" "--defaultSchema=<<defaultSchema>>" "--dataSource=<<dataSource>>"
For the dataSource parameter; If the data source is configured as reports underneath the dataSources key in application.[yml|groovy], the value should be reports.
--dataSource=reports

9.5. Update Scripts

9.5.1. dbm-previous-changeset-sql

Purpose

Writes the SQL to STDOUT or a file for the specified number of previous changesets whether they have run or not.

Description

Generates SQL for the specifed number of changeSets from the changelog. Executes against the database configured in application.[yml|groovy] for the current environment (defaults to dev).

Usage:

grails <<environment>> dbm-previous-changeset-sql <<number>> <<filename>> --skip=<<skip>> --contexts=<<contexts>> --defaultSchema=<<defaultSchema>>

Required arguments:

  • number - The number of un-run changesets to run

Optional arguments:

  • filename - The path to the output file to write to. If not specified output is written to the console

  • skip - The number of changesets to skip if you want to exclude recent ones

  • contexts - A comma-delimited list of context names. If specified, only changesets tagged with one of the context names will be run

  • defaultSchema - The default schema name to use

Note that the contexts and defaultSchema parameter name and value must be quoted if executed in Windows, e.g.
grails dbm-update-count-sql "--contexts=<<contexts>>" "--defaultSchema=<<defaultSchema>>"

9.5.2. dbm-update-count-sql

Purpose

Writes the SQL that will partially update a database to STDOUT or a file.

Description

Generates SQL for the specifed number of changeSets from the changelog. Executes against the database configured in application.[yml|groovy] for the current environment (defaults to dev).

This is useful for inspecting the generated SQL before running an update, or to generate SQL which can be tuned before running manually.

Usage:

grails <<environment>> dbm-update-count-sql <<number>> <<filename>> --contexts=<<contexts>> --defaultSchema=<<defaultSchema>> --dataSource=<<dataSource>>

Required arguments:

  • number - The number of un-run changesets to run

Optional arguments:

  • filename - The path to the output file to write to. If not specified output is written to the console

  • contexts - A comma-delimited list of context names. If specified, only changesets tagged with one of the context names will be run

  • defaultSchema - The default schema name to use

  • dataSource - if provided will run the script for the specified dataSource. Not needed for the default dataSource.

Note that the contexts, defaultSchema, and dataSource parameter name and value must be quoted if executed in Windows, e.g.
grails dbm-update-count-sql "--contexts=<<contexts>>" "--defaultSchema=<<defaultSchema>>" "--dataSource=<<dataSource>>"
For the dataSource parameter; If the data source is configured as reports underneath the dataSources key in application.[yml|groovy], the value should be reports.
--dataSource=reports

9.5.3. dbm-update-count

Purpose

Partially updates a database.

Description

Runs the specifed number of un-run changesets from the Changelog. Executes against the database configured in application.[yml|groovy] for the current environment (defaults to dev).

Usage:

grails <<environment>> dbm-update-count <<number>> --contexts=<<contexts>> --defaultSchema=<<defaultSchema>> --dataSource=<<dataSource>>

Required arguments:

  • number - The number of un-run changesets to run

Optional arguments:

  • contexts - A comma-delimited list of context names. If specified, only changesets tagged with one of the context names will be run

  • defaultSchema - The default schema name to use

  • dataSource - if provided will run the script for the specified dataSource. Not needed for the default dataSource.

Note that the contexts, defaultSchema, dataSource parameter name and value must be quoted if executed in Windows, e.g.
grails dbm-update-count "--contexts=<<contexts>>" "--defaultSchema=<<defaultSchema>>" "--dataSource=<<dataSource>>"
For the dataSource parameter; If the data source is configured as reports underneath the dataSources key in application.[yml|groovy], the value should be reports.
--dataSource=reports

9.5.4. dbm-update-sql

Purpose

Writes the SQL that will update the database to the current version to STDOUT or a file.

Description

Generates SQL for all un-run changeSets from the changelog. Executes against the database configured in application.[yml|groovy] for the current environment (defaults to dev).

This is useful for inspecting the generated SQL before running an update, or to generate SQL which can be tuned before running manually.

Usage:

grails <<environment>> dbm-update-sql <<filename>> --contexts=<<contexts>> --defaultSchema=<<defaultSchema>> --dataSource=<<dataSource>>

Required arguments: none .

Optional arguments:

  • filename - The path to the output file to write to. If not specified output is written to the console

  • contexts - A comma-delimited list of context names. If specified, only changesets tagged with one of the context names will be run

  • defaultSchema - The default schema name to use

  • dataSource - if provided will run the script for the specified dataSource. Not needed for the default dataSource.

Note that the contexts, defaultSchema, and dataSource parameter name and value must be quoted if executed in Windows, e.g.
grails dbm-update-sql "--contexts=<<contexts>>" "--defaultSchema=<<defaultSchema>>" --dataSource=<<dataSource>>
For the dataSource parameter; If the data source is configured as reports underneath the dataSources key in application.[yml|groovy], the value should be reports.
--dataSource=reports

9.5.5. dbm-update

Purpose

Updates a database to the current version.

Description

Runs all un-run changeSets from the changelog. Executes against the database configured in application.[yml|groovy] for the current environment (defaults to dev).

Usage:

grails <<environment>> dbm-update --contexts=<<contexts>> --defaultSchema=<<defaultSchema>> --dataSource=<<dataSource>>

Required arguments: none .

Optional arguments:

  • contexts - A comma-delimited list of context names. If specified, only changesets tagged with one of the context names will be run

  • defaultSchema - The default schema name to use

  • dataSource - if provided will run the script for the specified dataSource. Not needed for the default dataSource.

Note that the contexts and defaultSchema parameter name and value must be quoted if executed in Windows, e.g.
grails dbm-update "--contexts=<<contexts>>" "--defaultSchema=<<defaultSchema>>" "--dataSource=<<dataSource>>"
For the dataSource parameter; If the data source is configured as reports underneath the dataSources key in application.[yml|groovy], the value should be reports.
--dataSource=reports