6 Groovy Preconditions - Reference Documentation
Authors: Burt Beckwith
Version: 1.4.0
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 thegrailsPrecondition tag name and are contained in the databaseChangeLog tag or in a changeSet tag like standard built-in tags.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')
}
} } }
}
}- use a simple assertion
- use an assertion with a message
- call the
fail(String message)method (throws aPreconditionFailedException) - throw an exception (shouldn't be necessary - use
assertorfail()instead)
Available variables
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 resourceAccessor- the current Liquibase
ResourceAccessorinstance ctx- the Spring
ApplicationContext application- the
GrailsApplication changeSet- the current Liquibase
ChangeSetinstance changeLog- the current Liquibase
DatabaseChangeLoginstance
Utility methods
createDatabaseSnapshotGenerator()- retrieves the
DatabaseSnapshotGeneratorfor the currentDatabase createDatabaseSnapshot(String schemaName = null)- creates a
DatabaseSnapshotfor the currentDatabase(and schema if specified)