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

Abierto
#65 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
4/5
Tiempo estimado
3-5 días
Aptitud para principiantes
72/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Tranquilo
Stack tecnológico
csharp, postgresql
Área
databases

Línea de trabajo

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.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

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.

Lenguaje dominante
C#
Estrellas
68
Forks
4
Merge medio
50 min
PR fusionados (30 d)
2

Preparar el entorno

Abrir en Codespaces

Inicia el contenedor de desarrollo del proyecto en tu navegador, con tu propia cuenta de GitHub.

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de Nimblesite/DataProvider

Todos los issues de Nimblesite/DataProvider

Issues similares

Más issues de C#

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.