Groovy DSL dbmigrations using Liquidbase documentation

If you need a finer level of control with dbmigrations, you can create your own groovy dsl change sets using liquibase documentation. Below are some examples: 

Example 1: Adding Unique constraints:

Example code from Liquibase Documentation

<changeset author="liquibase-docs" id="addUniqueConstraint-example">
    <adduniqueconstraint catalogname="cat"
            columnnames="id, name"
                            constraintname="const_name"
                            deferrable="true"
                            disabled="true"
                            initiallydeferred="true"
                            schemaname="public"
                            tablename="person"
                            tablespace="A String">
    </adduniqueconstraint>
</changeset>

Example code from Second Blog

changeSet(author: "John", id: "10182013-1") {
    addUniqueConstraint(columnNames: "user_id", tableName: "user_profile")
}

If we match the example code from Liquibase documentation with the code of groovy dsl, we can see that:

1. changeSet xml entity from liquibase example is written as changeSet clousure in groovy dsl with author & id as method parameters,

2. addUniqueConstraint xml entity from liquibase doc is written as inner method which accepts some required & optional value as required, which can be seen from liquibase documentation.

Example 2: Renaming a column

Example code from Liquibase Documentation’s Renaming a column:

<changeSet author="liquibase-docs" id="renameColumn-example">
    <renameColumn catalogName="cat"
            columnDataType="int"
            newColumnName="id"
            oldColumnName="id"
            schemaName="public"
            tableName="person"/>
</changeSet>

To write this is groovy dsl from grails db migration, we can write:

changeSet(author: "John", id: "10182013-1") {
    renameColumn(columnDataType: "long(11)", tableName: "user_profile", newColumnName: "employee_id", oldColumnName: "user_id")
}
About CauseCode: We are a technology company specializing in Healthtech related Web and Mobile application development. We collaborate with passionate companies looking to change health and wellness tech for good. If you are a startup, enterprise or generally interested in digital health, we would love to hear from you! Let's connect at bootstrap@causecode.com
Have you subscribed to our blogs and newsletter? If not, what are you waiting for?  Click Here

Leave a Reply

Your email address will not be published. Required fields are marked *

SUBSCRIBE!

Do you want to get articles like these in your inbox?

Email *

Interested groups *
Healthtech
Business
Technical articles

Archives