0

我正在使用 groovy 脚本进行 Liquibase 迁移。

我有一个类似的代码

def migrate() {

    def changeLog = "com/cadence/mdv/ngv/db.changelog.xml"
    Sql db = Sql.newInstance(profile.dbUrl, profile.dbUser, profile.dbPassword, profile.dbDriver)


    Database database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(new JdbcConnection(db.connection))


    def liquibase = new Liquibase(changeLog
            , new ClassLoaderResourceAccessor()
            , database
    )
    liquibase.update(null)

}

但我收到异常,告诉某些表不存在

liquibase.exception.MigrationFailedException: Migration failed for change set com/cadence/mdv/ngv/initial-changelog.xml::1345376497537-31::pavelber (generated):
 Reason: liquibase.exception.DatabaseException: Error executing SQL ALTER TABLE APP.ACTION_ATTRIBUTES ADD CONSTRAINT FKD1111B00C05F2ED6 FOREIGN KEY (RUN_ID) REFERENCES APP.RUNS (ID) ON UPDATE NO ACTION ON DELETE NO ACTION: 'ALTER TABLE' cannot be performed on 'APP.ACTION_ATTRIBUTES' because it does not exist.:
      Caused By: Error executing SQL ALTER TABLE APP.ACTION_ATTRIBUTES ADD CONSTRAINT FKD1111B00C05F2ED6 FOREIGN KEY (RUN_ID) REFERENCES APP.RUNS (ID) ON UPDATE NO ACTION ON DELETE NO ACTION: 'ALTER TABLE' cannot be performed on 'APP.ACTION_ATTRIBUTES' because it does not exist.:

我在使用 ALTER TABLE 使第一个变更集失败的变更集列表中看到,看起来所有以前的变更集都不起作用,但我在控制台中看到:

INFO 8/20/12 4:08 PM:liquibase: Successfully acquired change log lock
INFO 8/20/12 4:08 PM:liquibase: Creating database history table with name: DATABASECHANGELOG
INFO 8/20/12 4:08 PM:liquibase: Reading from DATABASECHANGELOG
INFO 8/20/12 4:08 PM:liquibase: Reading from DATABASECHANGELOG
INFO 8/20/12 4:08 PM:liquibase: ChangeSet com/cadence/mdv/ngv/initial-    changelog.xml::1345376497537-1::pavelber (generated) ran successfully in 56ms
INFO 8/20/12 4:08 PM:liquibase: ChangeSet com/cadence/mdv/ngv/initial-changelog.xml::1345376497537-2::pavelber (generated) ran successfully in 29ms
INFO 8/20/12 4:08 PM:liquibase: ChangeSet com/cadence/mdv/ngv/initial-changelog.xml::1345376497537-3::pavelber (generated) ran successfully in 42ms
INFO 8/20/12 4:08 PM:liquibase: ChangeSet com/cadence/mdv/ngv/initial-changelog.xml::1345376497537-4::pavelber (generated) ran successfully in 29ms
INFO 8/20/12 4:08 PM:liquibase: ChangeSet com/cadence/mdv/ngv/initial-changelog.xml::1345376497537-5::pavelber (generated) ran successfully in 190ms
INFO 8/20/12 4:08 PM:liquibase: ChangeSet com/cadence/mdv/ngv/initial-changelog.xml::1345376497537-6::pavelber (generated) ran successfully in 32ms
INFO 8/20/12 4:08 PM:liquibase: ChangeSet com/cadence/mdv/ngv/initial-changelog.xml::1345376497537-7::pavelber (generated) ran successfully in 26ms
INFO 8/20/12 4:08 PM:liquibase: ChangeSet com/cadence/mdv/ngv/initial-changelog.xml::1345376497537-8::pavelber (generated) ran successfully in 23ms
INFO 8/20/12 4:08 PM:liquibase: ChangeSet com/cadence/mdv/ngv/initial-changelog.xml::1345376497537-9::pavelber (generated) ran successfully in 15ms
INFO 8/20/12 4:08 PM:liquibase: ChangeSet com/cadence/mdv/ngv/initial-changelog.xml::1345376497537-10::pavelber (generated) ran successfully in 31ms
INFO 8/20/12 4:08 PM:liquibase: ChangeSet com/cadence/mdv/ngv/initial-changelog.xml::1345376497537-11::pavelber (generated) ran successfully in 26ms
INFO 8/20/12 4:08 PM:liquibase: ChangeSet com/cadence/mdv/ngv/initial-changelog.xml::1345376497537-12::pavelber (generated) ran successfully in 54ms
.....

从命令行运行时,相同的更改日志运行良好,所以我认为问题出在事务定义或类似的东西上。

如何解决这个问题呢?

谢谢

4

1 回答 1

2

了解 profile.dbUrl 是什么以及可能还有 profile.dbUser 会很有帮助。

Derby 数据库中不存在表的最常见原因是因为 Derby 作为嵌入式数据库引擎,可以在各处拥有多个数据库副本,并且很容易混淆并使用与您想象的不同的数据库是:

  1. 您的数据库是通过相对文件路径访问的,并且您的当前目录已更改,导致 Derby 访问不同的数据库
  2. 您的数据库是通过其他用户访问的,并且用户名隐式定义了架构,因此找不到表,因为您的用户名已更改。
  3. 您的数据库是一个内存数据库,并且这些数据库不会在应用程序的生命周期之外持续存在,因此每次运行它时,您都会从一个没有表的新数据库开始。
  4. 您的应用程序在一个巨大的事务中创建所有表并完成所有工作,并且不提交该事务,导致您的所有工作,包括所有表创建,在您的应用程序退出时回滚。

还有其他可能的原因,但这些是一些常见的原因。

于 2012-08-21T03:32:58.087 回答