Liquibase无法将枚举类型转换为枚举类型。

3

我目前在我的数据库中有一个枚举类型,是通过Liquibase创建的,并使用以下脚本:

- changeSet:
      id: id_1
      author: my_team
      changes:
        - sql: CREATE TYPE my_team.letters AS ENUM ('A', 'B', 'C')

因为我需要向枚举中添加字母D,所以我创建了一个新的枚举

- changeSet:
      id: id_2
      author: my_team
      changes:
        - sql: CREATE TYPE my_team.letters_2 AS ENUM ('A', 'B', 'C', 'D')

我更新了类型

  - changeSet:
      id: id_3
      author: my_team
      changes:
        - modifyDataType:
            columnName: letter
            newDataType: my_team.letters_2
            schemaName: my_team
            tableName: table_name

执行 Liquibase 脚本时,我遇到了以下错误:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'liquibase' defined in class path resource [org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfiguration$LiquibaseConfiguration.class]: Invocation of init method failed; nested exception is liquibase.exception.MigrationFailedException: Migration failed for change set db/changelog/ddl-my_team-v.0.0.15.my_team::id_3::my_team team:
     Reason: liquibase.exception.DatabaseException: ERROR: cannot cast type my_team.letters to my_team.letters_2
  Position: 89 [Failed SQL: (0) ALTER TABLE my_team.table_name ALTER COLUMN case_status TYPE my_team.letters_2 USING (letter::my_team.letters_2)]

我无法理解为什么这样做,因为目标类型包含了原始类型的所有值。

是否有其他方法可以实现此功能?

谢谢提前!

3个回答

2

无需创建另一个枚举并绕过所有障碍来使一切顺利。只需修改现有的枚举即可:

 ALTER letter ADD VALUE 'D' after 'C';

谢谢您的回复,但是Liquibase不允许您在changeset内使用该命令。 - ldepablo
你不能在事务内使用它,但是你可以在变更集中使用它。只需使用 runInTransaction="false" 即可! - Blockost

2
我不是Postgres专家,但我认为以下方法可能会奏效:
  1. 将你的列名letter重命名为letter_copy
  2. 创建一个新的类型为letters_2letter列。
  3. letter_copy中的所有值复制到letter中。也许你需要将letter_copy中的值作为文本复制,例如:update table_name set letter = letter_copy::text::letters;
  4. 删除letter_copy列。
现在table_name.letter列应该具有letters_2枚举类型,并将所有值从枚举letters转换为枚举letters_2

0

这对我来说失败了

ALTER TYPE status_type ADD VALUE IF NOT EXISTS 'NEW_ENUM_VALUE';

以下代码有效

--liquibase formatted sql

ALTER TYPE status_type ADD VALUE IF NOT EXISTS 'NEW_ENUM_VALUE';

我认为上述查询能够工作的原因是liquibase不支持枚举类型的直接更新。为了本地运行查询,必须使用--liquibase格式化SQL。

https://docs.liquibase.com/concepts/changelogs/sql-format.html


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接