buildscript {
dependencies {
...
classpath 'org.graceframework.plugins:database-migration:6.3.0-SNAPSHOT'
}
}
dependencies {
...
implementation 'org.graceframework.plugins:database-migration:6.3.0-SNAPSHOT'
}
2 Getting Started
Version: 6.3.0-SNAPSHOT
2 Getting Started
The first step is to add a dependency for the plugin in build.gradle
:
It is also recommended to add a direct dependency to liquibase because Spring Boot overrides the one provided by this plugin
dependencies {
...
implementation 'org.liquibase:liquibase-core'
}
You should also tell Gradle about the migrations folder. If using Grace 4, make sure the configuration below is BEFORE the
dependencies
configuration, so that the sourceSets
declaration takes effect.
sourceSets {
main {
resources {
srcDir 'db/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:
grace dbm-generate-changelog changelog.groovy
or
grace 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 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:
grace 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:
grace dbm-generate-gorm-changelog changelog.groovy
or
grace dbm-generate-gorm-changelog changelog.xml
If you haven’t created the database yet, run the dbm-update script to create the corresponding tables:
grace dbm-update
or the dbm-changelog-sync script if the database is already in sync with your code:
grace 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.