How do you configure database migration in Grails ...
# questions
j
How do you configure database migration in Grails 7
u
To configure database migration in Grails 7, add the
database-migration
plugin and required Liquibase dependencies to your
build.gradle
, and ensure your migration files are in
grails-app/migrations
. Set migration options in
application.groovy
or
application.yml
under the
grails.plugin.databasemigration
prefix, such as
changelogFileName
and
updateOnStart
for auto-running migrations at startup. Example:
Copy code
dependencies {
    implementation 'org.apache.grails:grails-data-hibernate5-dbmigration:7.0.0-RC2'
    implementation "org.liquibase:liquibase-core:4.27.0"
    implementation "org.liquibase.ext:liquibase-hibernate5:4.27.0"
}
grails.plugin.databasemigration.changelogFileName = 'changelog.groovy'
grails.plugin.databasemigration.updateOnStart = true
For more details or step-by-step setup, would you like a full example or explanation of migration scripts and changelog management? Sources: - Grails Database Migration Plugin Manual πŸ‘ 0 button πŸ‘Ž 0 button πŸ’‘ Hint: Mention @runllm in the thread for followups.
u
A message from kapa.ai
u
To configure database migration in Grails 7, you use the Database Migration plugin, which is based on Liquibase. Here’s a step-by-step guide based on the available knowledge sources: ### 1. Add Dependencies In your
build.gradle
, add the plugin and Liquibase dependencies:
Copy code
groovy buildscript {    dependencies {       // Add the database migration plugin       classpath 'org.apache.grails:grails-data-hibernate5-dbmigration:7.0.0-RC2'    } }  dependencies {    implementation 'org.apache.grails:grails-data-hibernate5-dbmigration:7.0.0-RC2'    // Recommended: add Liquibase dependencies directly    implementation "org.liquibase:liquibase-core:4.27.0"    implementation "org.liquibase.ext:liquibase-hibernate5:4.27.0" }
Make sure to check for the latest versions as needed. ### 2. Configure the Migrations Folder Tell Gradle where your migration files will be. Place this before the
dependencies
block:
Copy code
groovy sourceSets {     main {         resources {             srcDir 'grails-app/migrations'         }     } }
This ensures your migration scripts are included in the build process. ### 3. Configure the Plugin In
application.groovy
or
application.yml
, set the plugin configuration. For example, to run migrations automatically on startup using a root changelog file:
Copy code
yaml grails:     plugin:         databasemigration:             updateOnStart: true             updateOnStartFileName: changelog.groovy
Or in Groovy config:
Copy code
groovy grails.plugin.databasemigration.updateOnStart = true grails.plugin.databasemigration.updateOnStartFileName = 'changelog.groovy'
You can further customize the configuration, such as specifying contexts, changelog locations, or handling multiple data sources. See the [configuration options](https://docs.grails.org/latest/grails-data/hibernate5/manual/index.html#configuration) for more details. ### 4. Generate and Apply Changelogs - To generate an initial changelog from your domain classes:
Copy code
bash   grails dbm-generate-gorm-changelog changelog.groovy
- To apply migrations:
Copy code
bash   grails dbm-update
### 5. Source Control Commit your changelog files and code to source control so other developers can synchronize their databases. --- Note: The process for Grails 7 is consistent with Grails 6 and recent versions, as the plugin and its configuration have not changed significantly in the available documentation. If you need more advanced configuration (multiple datasources, contexts, etc.), refer to the [plugin configuration section](https://docs.grails.org/latest/grails-data/hibernate5/manual/index.html#configuration). References: [Database Migration Plugin - Getting Started](https://docs.grails.org/latest/grails-data/hibernate5/manual/index.html#gettingStarted) [Database Migration Plugin - Configuration](https://docs.grails.org/latest/grails-data/hibernate5/manual/index.html#configuration)
u
An alternative approach to configuring database migration in Grails 7 is to use the
dbm-generate-changelog
or
dbm-generate-gorm-changelog
scripts to create your initial changelog files. This method allows you to generate changelogs directly from your database or domain classes, depending on your preference. For example: 1. Generate Changelog from Database:
Copy code
bash    grails dbm-generate-changelog changelog.groovy
This creates a changelog based on the current state of your database. 2. Generate Changelog from Domain Classes:
Copy code
bash    grails dbm-generate-gorm-changelog changelog.groovy
This creates a changelog based on your domain class definitions. Once the changelog is created, you can manually inspect and adjust the generated changesets to ensure they meet your requirements. Then, use the
dbm-update
script to apply the changes to your database:
Copy code
bash grails dbm-update
Additionally, you can configure multiple datasources for migrations by specifying settings like
updateAllOnStart
or
updateOnStartContexts
in
application.yml
. This is particularly useful for applications with multitenancy or multiple database schemas. This approach provides more control over the initial setup and is ideal for projects where the database schema is already established or requires careful migration planning. Would you like to explore how to manage multiple datasources or use contexts for selective migrations? Sources: - [Database Migration Plugin General Usage](https://docs.grails.org/latest/grails-data/hibernate5/manual/index.html#generalUsage) - [Database Migration Plugin Configuration](https://docs.grails.org/latest/grails-data/hibernate5/manual/index.html#databaseMigration)
s
@User how would you run these commands if you use Gradle?
u
To run database migration commands with Gradle in Grails 7, use the corresponding Gradle tasks (in camelCase) instead of the Grails CLI. For example, run
./gradlew dbmUpdate
to apply migrations, or
./gradlew dbmGenerateChangelog -Pargs="changelog.groovy"
to generate a changelog file. Each Grails CLI command like
grails dbm-update
maps to a Gradle task such as
dbmUpdate
. You can list all available tasks with
./gradlew tasks
. Would you like a full mapping of Grails CLI commands to Gradle tasks or more usage examples? Sources: - Restoration of Grails Shell CLI - Database Migration Plugin Manual πŸ‘ 0 button πŸ‘Ž 0 button πŸ’‘ Hint: Mention @runllm in the thread for followups.
u
An alternative approach to running database migration commands with Gradle is to leverage the
runCommand
Gradle task for executing Grails-specific commands. This method is particularly useful for commands that are not directly mapped to Gradle tasks or when you need to pass complex arguments. For example: 1. Run
dbm-update
using
runCommand
:
Copy code
./gradlew runCommand -Pargs="dbm-update"
2. Generate a changelog using
runCommand
:
Copy code
./gradlew runCommand -Pargs="dbm-generate-changelog changelog.groovy"
This approach ensures compatibility with Grails commands that rely on the Grails runtime environment, as
runCommand
starts the Grails application context. It is particularly useful for commands that require access to GORM mappings or other application-specific configurations. Additionally, if your Grails application is part of a multi-project Gradle setup (e.g., using the Angular profile), you can specify the subproject when running the command:
Copy code
./gradlew server:runCommand -Pargs="dbm-update"
This method provides flexibility and ensures that commands are executed within the correct application context. Would you like to explore how to create custom commands for specific migration tasks? Sources: - Creating Custom Commands - Restoration of Grails Shell CLI πŸ‘ 1 button πŸ‘Ž 0 button πŸ’‘ Hint: Mention @User in the thread for followups.