4 General Usage - Reference Documentation
Authors: Burt Beckwith
Version: 1.4.0
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
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 addinggrails.plugin.databasemigration.updateOnStart = true
updateOnStartFileNames
property, e.g.:grails.plugin.databasemigration.updateOnStartFileNames = ['changelog.groovy']
grails.plugin.databasemigration.updateOnStartContexts = ['context1,context2']
beforeStartMigration
will be called (if it exists) for each datasource before any migrations have run; the method will be passed a single argument, the LiquibaseDatabase
for that datasourceonStartMigration
will be called (if it exists) for each migration script; the method will be passed three arguments, the LiquibaseDatabase
, theLiquibase
instance, and the changelog file nameafterMigrations
will be called (if it exists) for each datasource after all migrations have run; the method will be passed a single argument, the LiquibaseDatabase
for that datasource
package com.mycompany.myappimport liquibase.Liquibase import liquibase.database.Databaseclass MigrationCallbacks { void beforeStartMigration(Database Database) { … } void onStartMigration(Database database, Liquibase liquibase, String changelogName) { … } void afterMigrations(Database Database) { … } }
import com.mycompany.myapp.MigrationCallbacksbeans = {
migrationCallbacks(MigrationCallbacks)
}