How do I modify a column using Grails' databas

2020-07-14 14:13发布

问题:

Can you give me an example of a groovy changeset using the modifyDataType method?

I tried this:

databaseChangeLog = {
  changeSet(author: "user", id: "5-1") {
        modifyDataType(tableName: "test", columnName: "description4", newDataType: "int(11)")
    }
}

But modifyDataType is not recognized. I also tried modifyColumn, but I get the same result.

The underlying question is: What kind of tags does the dsl support, and how are they used?

回答1:

All Liquibase refactorings should work - the Groovy DSL mirrors the Liquibase XML. I didn't have a test for modifyDataType but added it to my test script and it worked fine - see https://github.com/grails-plugins/grails-database-migration/blob/master/testapp/price.changelog.groovy

It'd be useful to figure out what's wrong if you could show some information about how it fails.



回答2:

It will work like this:

databaseChangeLog = {

  changeSet(author: "test (generated)", id: "1422541392309-2") {
    comment { 'Rename tabTitle to tabName' }
    renameColumn(tableName: "user", oldColumnName: "tab_title", newColumnName: "tab_name", columnDataType: "varchar(255)")
  }
}