如果 table 存在,则跳过 liquibase 更改

skipping liquibase change if table exists

liquibase 的新手。我有一个由休眠管理的现有模式。我现在需要删除一些 tables 并且我正在使用 liquibase。我写了一个成功删除 table.

的配置

但我希望仅当 table 存在时才检查 运行,因为新安装的系统不会有 table,因为休眠映射对象不再存在.

我试图添加一个先决条件。但是,我的日志仍然表明 liquibase 失败,因为它试图删除 table 但它不存在。我做错了什么?我正在使用 Spring 启动 / Java

databaseChangeLog:
  - preConditions:
      on-fail: mark_ran
      on-error: mark_ran
      tableExists:
        tableName: some_old_table
        schemaName: public
  - changeSet:
      id: 01_del_old_table
      author: some-dev-01
      comment: Remove old table
      changes:
        - dropTable:
            tableName: some_old_table

日志错误:Table public.some_old_table 不存在 ...

PreconditionFailedException

这就像 ti 正在检查先决条件...但仍然没有通过。

也试过

databaseChangeLog:
  - changeSet:
      id: zzchange-1.0-remove-xczczxc
      author: zzzz
      comment: Remove some_old_table table - no longer needed
      preConditions:
        on-fail: mark_ran
        tableExists:
          tableName: some_old_table
      changes:
        - dropTable:
            tableName: some_old_table

在你第二次尝试时,请在你的先决条件之后发表你的评论

databaseChangeLog:
  - changeSet:
      id: zzchange-1.0-remove-xczczxc
      author: zzzz
      preConditions:
        on-fail: mark_ran
        tableExists:
          tableName: some_old_table
      comment: Remove some_old_table table - no longer needed
      changes:
        - dropTable:
            tableName: some_old_table

这是他们在 liquibase 文档中推荐的内容:https://www.liquibase.org/documentation/preconditions.html

我不太确定为什么您的第一次尝试不起作用,但我认为拥有全局先决条件不是一个好主意。这是因为他们在同一文档中所说的:

Preconditions at the changelog level apply to all changesets, not just those listed in the current changelog or its child changelogs.

问题在于 on-fail 属性拼写不正确。 on-fail 应该是 onFail.

正如@Julian 所提到的,最好将先决条件置于特定变更集的范围内,并且注释应该放在先决条件之后,尽管这不是这里的问题。

databaseChangeLog:
  - changeSet:
      id: zzchange-1.0-remove-xczczxc
      author: zzzz
      preConditions:
        onFail: mark_ran
        tableExists:
          tableName: some_old_table
      comment: Remove some_old_table table - no longer needed
      changes:
        - dropTable:
            tableName: some_old_table

请使用以下语法跳过 table present

中的更改
CREATE table IF NOT EXISTS