SchemaIntegrityVerifier reports false drift on partial + functional indexes (indpred unread, ParseIndexExpressions mis-parses)

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

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

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

调研方向

Start in PostgresSchemaInspector, reading the index read-back query and ParseIndexExpressions, then follow how IndexDefinition is checked by SchemaIntegrityVerifier.SameSql. Reproduce the schema with the partial functional index and verify that the inspector round-trips its filter and expressions without reporting drift.

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

描述

Summary

A unique index that is both functional and partial always reports drift in the post-apply SchemaIntegrityVerifier, even immediately after migrate creates it. Two independent bugs in Nimblesite.DataProvider.Migration.Postgres.PostgresSchemaInspector cause this.

Reproduced with DataProviderMigrate 0.9.12-beta against postgres:16.

Repro schema.yaml index
- name: uq_invitations_pending_email
  expressions:
    - tenant_id
    - lower(email)
  isUnique: true
  filter: "accepted_at IS NULL AND revoked_at IS NULL"
Observed

migrate creates the index, then the integrity check fails:

public.invitations: index uq_invitations_pending_email expressions drifted
public.invitations: index uq_invitations_pending_email filter drifted

Postgres stores the index as:

CREATE UNIQUE INDEX uq_invitations_pending_email ON public.invitations
  USING btree (tenant_id, lower(email))
  WHERE ((accepted_at IS NULL) AND (revoked_at IS NULL))

Bug 1 — Filter is never read back

PostgresSchemaInspector builds IndexDefinition from the read-back query but never reads pg_index.indpred (pg_get_expr(indpred, indrelid)), so actual.Filter is always null. SchemaIntegrityVerifier.SameSql(null, "accepted_at IS NULL AND revoked_at IS NULL") is therefore always false → "filter drifted" for ANY partial index.

Bug 2 — ParseIndexExpressions mis-parses functional / partial indexes

ParseIndexExpressions(indexDef) locates the column list with indexDef.LastIndexOf('(') and LastIndexOf(')'). For ... (tenant_id, lower(email)) WHERE (...) the last (/) belong to the WHERE clause (or to lower(), so the substring extracted is not the index column list. Result: parsed Expressions are garbage → "expressions drifted". This also misfires on a functional index with no WHERE ((tenant_id, lower(email)) → parses email only).

Suggested fix

  • Read pg_get_expr(i.indpred, i.indrelid, true) and populate IndexDefinition.Filter; normalize parenthesisation in SameSql (Postgres re-parenthesises predicates).
  • Replace the LastIndexOf bracket scan with a depth-aware scan anchored on USING <method> ( … matching ) so the WHERE clause and nested function parens are not mistaken for the column-list bounds.

Impact / workaround

NAP's [INVITE-EMAIL] wanted a partial+functional unique index ((tenant_id, lower(email)) WHERE accepted_at IS NULL AND revoked_at IS NULL). Because the verifier fails it, NAP currently enforces the invariant in the application layer instead and omits the DB index. We'd like to restore the DB constraint once the verifier round-trips filtered/expression indexes.

主要语言
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 摘要。