Bug: CHECK constraint with a disjunction predicate ("x IS NULL OR x IN (...)") is dropped + re-added on every migrate (non-idempotent)

未关闭
#70 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
52/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
冷清
技术栈
csharp, postgresql
领域
databases

调研方向

Start by tracing SchemaDiff's PostgreSQL CHECK-constraint comparison and reproduce the provided YAML case with migrate against PostgreSQL 16. Compare the YAML predicate with the pg_catalog form and verify that a second migrate produces zero operations for both the disjunction and existing simple checks.

由索引模型根据 Issue 内容生成。

描述

Summary

A converged database should produce zero operations on a re-run (per the migrations docs: "Re-running against a converged database → zero operations (idempotent)"). But a CHECK constraint whose YAML predicate contains a boolean disjunction (e.g. x IS NULL OR x IN (...)) is dropped and re-added on every migrate run. Plain IN (...) / single-comparison checks are correctly idempotent.

Environment

  • DataProviderMigrate built from source at v0.9.12-beta (commit 8e3af36)
  • --provider postgres, PostgreSQL 16

Repro

A table with two checked columns:

columns:
  - { name: status, type: Text, isNullable: false, checkConstraint: "status IN ('active','done')" }            # idempotent ✓
  - { name: recurrence_kind, type: Text, checkConstraint: "recurrence_kind IS NULL OR recurrence_kind IN ('daily','weekly')" }  # churns ✗

migrate once (converges), then migrate again against the unchanged DB:

Phase: all — applying 4 of 4 operation(s):
  DropCheckConstraintOperation
  AddCheckConstraintOperation     # <- the recurrence_kind check, every single run
  ...

Run a third time → identical 4 ops. The churn is perpetual.

Root cause (hypothesis)

SchemaDiff compares the YAML predicate string against the pg_catalog-normalized form. Postgres stores:

CHECK (((recurrence_kind IS NULL) OR (recurrence_kind = ANY (ARRAY['daily'::text, 'weekly'::text]))))

The differ appears to normalize the simple IN (...) → = ANY (ARRAY[...]) case (those match → no churn), but does not canonicalize a disjunction (... IS NULL OR ...), so the comparison never matches and it drops + re-adds forever.

Impact

  • Breaks the documented idempotency guarantee.
  • Every CI/build migrate performs needless DDL — each drop+add takes an ACCESS EXCLUSIVE lock on the table.
  • Real drift on that constraint can be masked by the constant churn.

Suggested fix

Canonicalize CHECK expressions on both sides before diffing (parse to an AST, or compare the pg_get_constraintdef-normalized text rather than the raw YAML string). Failing that, document that check predicates must be authored in pg-normalized form.

Workaround

For Option-style nullable enums the IS NULL OR prefix is redundant anyway — a SQL CHECK passes when its predicate is TRUE or NULL, so CHECK (col IN (...)) already permits NULL. Dropping the prefix restored idempotency. But disjunctions are legitimate in general and should diff cleanly.

主要语言
C#
星标
68
派生
4
平均合并
50 分钟
30 天内合并 PR
2

环境准备

在 Codespaces 中打开

在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

Nimblesite/DataProvider 的其他 Issue

查看 Nimblesite/DataProvider 的全部 Issue

相似的 Issue

更多 C# Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。