Feb 13, 2009
Autobase plugins
Sometimes, we need to refactor our domain / database table. For example, our first domain looks like this :
1 2 3 4 | class Person { String personName ; BigDecimal monthlyIncome ; } |
Now we want to eliminate/change the monthlyIncome and replace it with BigDecimal weeklyIncome
we type
1 2 3 4 | class Person { String personName ; BigDecimal weeklyIncome; } |
But, when we look at the table, it still have monthlyIncome field. That’s why we need to eliminate it. If we works as a team, this could be a messy and troublesome, because monthlyIncome is not nullable. So it produce error (JDBC errors) when someone tried to update or save into that table.
That’s why we need Autobase for this dirty job. Autobase is Grails plugin that created base on Liquibase. Liquibase, however using xml format to do database migration. I hate to write on xml actually.
Luckily, Robert Fischer, wrote this useful Grails’s plugin, so that we can write it using Groovy DSL.
Here is the steps we need to make:
- command line: grails install-plugin autobase. This command willl install autobase plugin into your project. You can download manually from http://plugins.grails.org
- command line: grails create-migration Person, this will produce migration script under migrations/computerName/PersonMigration.groovy
- write this inside PersonMigration.groovy
1 2 3 4 5 6 7
changeSet(id:'PersonMigration-1', author:'R61') { preConditions{ columnExists(tableName:"person", columnName:"monthly_income"); } comment("remove monthly_income from person table"); sql("alter table person drop monthly_income"); }
- Re run your grails and this script will be executed.
For more details about the syntax that you can have inside migration file, try to read the documentation at liquibase manual