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

时间:2018-08-17 21:31:44

标签: postgresql database-migration postgresql-10 sqitch

我正在进行以下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方面的帮助,以将时区从时间戳删除。

2 个答案:

答案 0 :(得分:2)

您的解决方案对我来说非常好,@ 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;
$$;

答案 1 :(得分:1)

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

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

如果未找到符合该条件的行,则会抛出除以零的错误。