我的DB中目前有一个枚举类型,它是通过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)]
我不明白为什么,因为目的地类型包括原始类型的所有值。
有什么办法可以做到这一点吗?
提前感谢
我不是Postgres的专家,但我认为以下可以做到:
- 将列
letter
重命名为letter_copy
- 创建类型为
letters_2
的新列letter
- 将所有值从
letter_copy
复制到letter
。也许您必须将letter_copy
中的值复制为类似update table_name set letter = letter_copy::text::letters;
的文本 - 删除列
letter_copy
现在table_name.letter
列应该具有letters_2
枚举类型,所有值都从枚举letters
转换为枚举letters_2
。
不需要创建另一个枚举并跳过重重关卡来理顺一切。只需更改现有枚举:
ALTER letter ADD VALUE 'D' after 'C';
这对我来说是失败的
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