5 Groovy Changes - Reference Documentation
Authors: Burt Beckwith
Version: 1.4.0
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 thegrailsChange 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).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'
} }
}Available variables
These variables are available throughout the change closure:changeSet- the current Liquibase
ChangeSetinstance resourceAccessor- the current Liquibase
ResourceAccessorinstance ctx- the Spring
ApplicationContext application- the
GrailsApplication
change and rollback closures also have the following available:
database- the current Liquibase
Databaseinstance databaseConnection- the current Liquibase
DatabaseConnectioninstance, which is a wrapper around the JDBCConnection(but doesn't implement theConnectioninterface) connection- the real JDBC
Connectioninstance (a shortcut fordatabase.connection.wrappedConnection) sql- a
groovy.sql.Sqlinstance which uses the currentconnectionand 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 callingwarn(String message) and stop processing by calling error(String message). It may make more sense to use one or more preConditions instead of directly validating here.change
All migration changes are done in thechange closure. You can make changes directly (using the sql instance or the connection) and/or return one or more SqlStatements. 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 therollback closure. You can make changes directly (using the sql instance or the connection) and/or return one or more SqlStatements. 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
Theconfirm(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 thecheckSum(String value) method.