如何使用Grails DBMigration插件更新已经运行的数据库迁移文件?

如何使用Grails DBMigration插件更新已经运行的数据库迁移文件?,grails,database-migration,liquibase,Grails,Database Migration,Liquibase,我有一个名为test-plugin-migration-1.2.groovy的迁移文件,其中只需重命名为其中列出的迁移的NEDD已经运行,还有一些其他自定义迁移 test-plugin-migration-1.2.groovy->test-plugin-migration-1-2.groovy 我尝试在名为rename migration.groovy的新文件中为DATABASECHANGELOG表添加自定义迁移 重命名迁移.groovy changeSet(author: "Laxmi Sal

我有一个名为
test-plugin-migration-1.2.groovy
的迁移文件,其中只需重命名为其中列出的迁移的NEDD已经运行,还有一些其他自定义迁移

test-plugin-migration-1.2.groovy
->
test-plugin-migration-1-2.groovy

我尝试在名为
rename migration.groovy的新文件中为
DATABASECHANGELOG
表添加自定义迁移

重命名迁移.groovy

changeSet(author: "Laxmi Salunkhe", id: "12345-1") {
    grailsChange {
        change {
            sql.execute("""update DATABASECHANGELOG set 
                filename='test-plugin-migration-1-2.groovy' where
                filename='test-plugin-migration-1.2.groovy'""")
        }
    }
}
databaseChangeLog = {

    // Some Old Migrations

    include file: 'rename-migration.groovy'

    // Previously it was test-plugin-migration-1.2.groovy
    include file: 'test-plugin-migration-1-2.groovy' 

    include file: 'new-plugin-migration.groovy'
}
import groovy.sql.Sql

@GrabConfig(systemClassLoader = true)
@Grab(group = "mysql", module = "mysql-connector-java", version = "5.1.29")

// Get instance of MYSQL database of old system
Sql sql = Sql.newInstance("jdbc:mysql://localhost:3306/causecode", "root", "sql", "com.mysql.jdbc.Driver")

String oldFileName = "test-plugin-migration-1.2"
String newFileName = oldFileName.replace('.', '_') + '.groovy'
oldFileName = oldFileName + '.groovy'
String query = """
    Update DATABASECHANGELOG
    set filename = "$newFileName", MD5SUM = null
    where filename = "$oldFileName"
"""
sql.executeUpdate query


// Close the connections
sql.close()
changelog.groovy

changeSet(author: "Laxmi Salunkhe", id: "12345-1") {
    grailsChange {
        change {
            sql.execute("""update DATABASECHANGELOG set 
                filename='test-plugin-migration-1-2.groovy' where
                filename='test-plugin-migration-1.2.groovy'""")
        }
    }
}
databaseChangeLog = {

    // Some Old Migrations

    include file: 'rename-migration.groovy'

    // Previously it was test-plugin-migration-1.2.groovy
    include file: 'test-plugin-migration-1-2.groovy' 

    include file: 'new-plugin-migration.groovy'
}
import groovy.sql.Sql

@GrabConfig(systemClassLoader = true)
@Grab(group = "mysql", module = "mysql-connector-java", version = "5.1.29")

// Get instance of MYSQL database of old system
Sql sql = Sql.newInstance("jdbc:mysql://localhost:3306/causecode", "root", "sql", "com.mysql.jdbc.Driver")

String oldFileName = "test-plugin-migration-1.2"
String newFileName = oldFileName.replace('.', '_') + '.groovy'
oldFileName = oldFileName + '.groovy'
String query = """
    Update DATABASECHANGELOG
    set filename = "$newFileName", MD5SUM = null
    where filename = "$oldFileName"
"""
sql.executeUpdate query


// Close the connections
sql.close()
它仍然会再次运行重命名的文件迁移

在阅读了的
liquibase
文档之后,它解释了

Liquibase执行databaseChangeLog,它按顺序读取变更集,并检查每个变更集的“databaseChangeLog”表,查看是否已运行id/author/filepath组合。


如何避免重命名文件更改集应用于数据库?

如果您没有对现有更改集进行任何更改,可以使用命令
dbm changelog sync
将所有更改集标记为已执行,以便再次运行应用程序时,您的变更日志将不会再次执行,并将被视为已应用


请参见

我找到了解决此问题的方法。我添加了一个groovy脚本
rename file.groovy
,它重命名了
DATABASECHANGELOG
表中的无效文件名,然后迁移运行成功

重命名文件.groovy

changeSet(author: "Laxmi Salunkhe", id: "12345-1") {
    grailsChange {
        change {
            sql.execute("""update DATABASECHANGELOG set 
                filename='test-plugin-migration-1-2.groovy' where
                filename='test-plugin-migration-1.2.groovy'""")
        }
    }
}
databaseChangeLog = {

    // Some Old Migrations

    include file: 'rename-migration.groovy'

    // Previously it was test-plugin-migration-1.2.groovy
    include file: 'test-plugin-migration-1-2.groovy' 

    include file: 'new-plugin-migration.groovy'
}
import groovy.sql.Sql

@GrabConfig(systemClassLoader = true)
@Grab(group = "mysql", module = "mysql-connector-java", version = "5.1.29")

// Get instance of MYSQL database of old system
Sql sql = Sql.newInstance("jdbc:mysql://localhost:3306/causecode", "root", "sql", "com.mysql.jdbc.Driver")

String oldFileName = "test-plugin-migration-1.2"
String newFileName = oldFileName.replace('.', '_') + '.groovy'
oldFileName = oldFileName + '.groovy'
String query = """
    Update DATABASECHANGELOG
    set filename = "$newFileName", MD5SUM = null
    where filename = "$oldFileName"
"""
sql.executeUpdate query


// Close the connections
sql.close()

然后对数据库运行迁移。

此命令无法使用,因为我想重命名迁移文件名,并添加一些新的迁移文件。请检查我是否已更新我的问题。您可以先重命名迁移文件,然后进行dbm changelog sync,然后添加新的迁移文件,当您的应用程序下次运行时,将应用新的迁移。迁移文件不打算重命名。一旦它们被创造出来,它们就应该保持这种状态。您试图通过重命名它们来解决什么问题?此外,DATABASECHANGELOG表不打算直接修改——它是用来跟踪changelog文件中的哪些更改已应用于任何给定的数据库实例的。我刚刚将我的应用程序Grails版本从2.4.x升级到2.5.0。我遇到运行时异常的无效迁移文件名。为了解决这个问题,我必须重命名那些无效的文件。我想出了解决办法,补充如下。