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

Abierto
#70 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
52/100
Tipo de issue
Error
Claridad
Bastante claro
Estado de actividad
Tranquilo
Stack tecnológico
csharp, postgresql
Área
databases

Línea de trabajo

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.

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

Descripción

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.

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.