如何使用 sqitch postgresql 验证更改列数据类型的更改?

How can I verify an alter column data type change with sqitch postgresql?

我正在进行以下 deploy 更改。将 timestamp 列的值更改为 timestamptz.

-- Alter the is_deleted flags to be timestamp with time zone
alter table source_meta.sources alter column is_deleted set data type timestamptz
      using
        is_deleted at time zone 'UTC';
alter table source_meta.series alter column is_deleted set data type timestamptz
      using
        is_deleted at time zone 'UTC';

如何编写一个 verify 脚本,如果未更改数据类型,该脚本会出错?

还需要一些帮助 revert 从 timestamptz 中删除时区。

SELECT 1/count(*)
FROM   information_schema.columns
WHERE  table_name = 'sources'
 and column_name = 'is_deleted'
 and data_type = 'timestamp with time zone';

这就是我能想到的,但我不是专家,我不确定这是这样做的方法还是愚蠢的。 :)

如果没有找到符合该条件的行,这将抛出一个被零除的错误。

@nackjicholson,我觉得你的解决方案很不错。如果您想要一个信息更丰富的错误消息,您可以将其包装在 DO 块中并引发错误:

DO $$
BEGIN
    PERFORM TRUE
       FROM   information_schema.columns
      WHERE  table_name.  = 'sources'
        AND column_name = 'is_deleted'
        AND data_type   = 'timestamp with time zone';
    IF NOT FOUND THEN
        RAISE EXCEPTION 'sources.is_deleted type is not timestamptz';
    END IF;
END;
$$;