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')
}
}
}
}
}
}
7 Groovy Preconditions
Version: 6.3.0
7 Groovy Preconditions
In addition to the built-in Liquibase preconditions 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.
General format
This is the general format of a Groovy-based precondition:
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 aPreconditionFailedException) -
throw an exception (shouldn’t be necessary - use
assertorfail()instead)
Available variables
-
database- the current LiquibaseDatabaseinstance -
databaseConnection- the current LiquibaseDatabaseConnectioninstance, which is a wrapper around the JDBCConnection(but doesn’t implement theConnectioninterface) -
connection- the real JDBCConnectioninstance (a shortcut fordatabase.connection.wrappedConnection) -
sql- agroovy.sql.Sqlinstance which uses the currentconnectionand can be used for arbitrary queries and updates -
resourceAccessor- the current LiquibaseResourceAccessorinstance -
ctx- the SpringApplicationContext -
application- theGrailsApplication -
changeSet- the current LiquibaseChangeSetinstance -
changeLog- the current LiquibaseDatabaseChangeLoginstance
Utility methods
-
createDatabaseSnapshotGenerator()- retrieves theDatabaseSnapshotGeneratorfor the currentDatabase -
createDatabaseSnapshot(String schemaName = null)- creates aDatabaseSnapshotfor the currentDatabase(and schema if specified)