Custom Groovy changes using Grails db-migration plugin (Part I)

Shashank Agrawal
WizPanda
Published in
3 min readMay 9, 2019

Grails has a wonderful plugin to manage database changes & state in production server, known by Grails Database Migration Plugin. This plugin helps by managing database changes while developing a grails application. The plugin uses the Liquibase library.

This plugin manages changes of the database structure in a very consistent manner, avoiding inconsistencies, communication issues, and other problems by representing database migration scripts in text format, either using groovy DSL or liquibase XML. This also helps to maintain the change log migration files in source control like Git.

You can read more here in the documentation of the DB migration plugin. If you are new to database migration plugin & don’t know how to use the migration plugin and write basic migration script please read Getting Started. (See this blog post for a quick start.)

This plugin can generate most of the database change logs automatically by using liquibase build-in changes. But there are situations & changes where this plugin can’t help you out to write the changes.

For example: To rename a column ( a field in grails domain class), this plugin generates a script which removes the column with old name & creates a new column, which is not acceptable when there are records in the production database.

Another example can be where you have to migrate some data from one table to another table without repeating the code & in the proper way. Similarly, there are many more situations where you could not depend on this plugin’s auto-generated code.

These situations can be handled by writing database changes using Groovy code (as long as you are using Groovy DSL file format). These changes use the grailsChange closure name and are contained in a changeSet closure like standard built-in tags. See Groovy Changes to read more about this.

Here I’m providing some scenarios where you can easily migrate your data & database structure without data redundancy & loss of data by writing custom grails DB migration scripts which the plugin will not generate for you.

Case 1: Updating Some Records

Suppose you have added a not null field in a domain class & want to add default value or add/update data to the other fields based on some conditions. Here you can write:

1.2) Using SQL Query:

Case 2: Inserting records to a table

Suppose you need to migrate some data from one table to another table or you simply need to add data to the table.

Case 3: Using Grails domain directly to update

There are situations where when we add some new data fields in a domain class table which already has records.

For example: If you added a date field. In this case, grails add the column with type datetime but its default value is initialized to 0000–00–00 00:00:00. Due to which grails throws an exception on server startup. So these fields need to be initialized before server startup. Here is an example:

Case 4: Increasing field size for a text field

Case 5: Dropping a column

For more examples & cases, read Part II.

Want to see your amazing idea in action? Or want to join us? Connect us at https://www.wizpanda.com/

--

--

Shashank Agrawal
WizPanda

Serial Entrepreneur | Founder / CTO @ Cooee® — AI-driven Personalised Notifications platform for Better Customer Engagement. bit.ly/shashanka