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

オープン
#65 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
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分
マージ済み PR(30日)
2

環境構築

Codespaces で開く

このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

Nimblesite/DataProvider のほかの issue

Nimblesite/DataProvider の issue をすべて見る

似ている issue

C# の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。