Monday, September 16, 2013

GRAILS Liquibase migration - Add / Create new table

Below is the DSL which can be used to create a table using GRAILS and liquibase. The preconditions can be used to check if the table already exists, if not then a new table is created.
databaseChangeLog = {

    changeSet(author: "myname", id: "123456-3") {
        preConditions(onFail: "MARK_RAN", onFailMessage: "table already exists") {
            not {
                tableExists(tableName: "TABLE_NAME")
            }
        }
        createTable(tableName: "TABLE_NAME") {
            column(name: "id", type: "number(19,0)") {
                constraints(nullable: "false", primaryKey: "true", primaryKeyName: "ABC_PK")
            }
            column(name: "COLUMN1", type: "varchar2(255)") {
                constraints(nullable: "true")
            }
            column(name: "COLUMN2", type: "number(19,0)") {
                constraints(nullable: "true")
            }
            column(name: "COLUMN3", type: "timestamp") {
                constraints(nullable: "false")
            }
        }
    }

}