Problem
We have grails project which is already running in PROD without using database migration/liquibase plugins.
Need to do changes in database structure without destroying of existing data.
Solution
Let’s call database states:
- state A is current state of database
- state B new state of database, which we want to get in result
First of all need to have locally old version of project which is working on databases A and also new version of project which is working on database B.
It can be same one just without changes in domain model.
First need to generate changelog for database A.
Going to old project structure and install plugin first of all
grails install-plugin database-migration
After need generate changelog (I prefer groovy file)
grails dbm-generate-changelog changelog.groovy
and change Config.groovy to load data with changelog always
grails.plugin.databasemigration.updateOnStart = true grails.plugin.databasemigration.updateOnStartFileNames = ['changelog.groovy']
But database is already exists and we do not want to re-create it from scratch neither on local install nor in PROD. So we use
grails dbm-changelog-sync-sql
After this we get SQL commands to execute on local and PROD db.
Migrate database from state A to state B
Now we can make all changes we need on our domain structure. After this run command
grails dbm-gorm-diff addition.groovy
Then put data from addition.groovy to changelog.groovy (I prefer just copy/paste to the end of file and remove addition.groovy after this)
And simple grails run-app your local app and create/deploy war for PROD (do not forget to execute SQL from previous step first!)
Hope this helps someone…
1