使用Liquibase添加计算列

时间:2017-08-21 18:55:09

标签: sql-server liquibase

如何在liquibase中添加计算列?当我在sql标记中运行以下SQL时,我得到了我期望的计算值:

ALTER TABLE TableName
ADD ComputedColumn AS (CASE WHEN DateColumn1 IS NULL OR DateColumn2 IS NULL
THEN 0
ELSE DATEDIFF(DAY, DateColumn1, DateColumn2) END)

如何在不使用sql标签的情况下执行此操作?我能够使用以下yaml创建一个列:

- addColumn: tableName: TableName columns: - column: name: ComputedColumn type: tinyint constraints: nullable: true defaultValueComputed: 0 valueComputed: DATEDIFF(DAY, DateColumn1, DateColumn2) END

但是,列类型不像我运行原始SQL时那样计算。

2 个答案:

答案 0 :(得分:0)

尝试像这样放置类型的计算列表达式-请注意,该表达式也是YAML折叠样式,我发现该样式可防止Liquibase破坏表达式:

- addColumn:
  tableName: TableName
  columns:
    - column:
        name: ComputedColumn
        type: > 
            tinyint AS (DATEDIFF(DAY, DateTimestamp, DateTimestamp))
        constraints:
          nullable: true

答案 1 :(得分:0)

使用建议的解决方案没有运气。 但是这是我的工作对象:

- addColumn:
  tableName: TableName
  columns:
    - column:
        name: ComputedColumn as (case when ((([fieldFlagOne]+(0))+([fieldFlagTwo]+(0)))+([fieldFlagThree]+(0)))=(3) then (1) else (0) end)
        computed: true
        type: ""
        constraints:
          nullable: true