Skip to content

fix: verify PostgreSQL foreign key trigger enforcement - #528

Merged
medz merged 2 commits into
mainfrom
fix/postgres-foreign-key-enforcement
Oct 9, 2026
Merged

medz merged 2 commits into
mainfrom
fix/postgres-foreign-key-enforcement

Conversation

@medz

@medz medz commented Oct 9, 2026 •

Copy link
Copy Markdown
Owner

Refs #516

PostgreSQL can retain an enforced, validated foreign-key definition after its internal triggers have been disabled. The migration runner previously accepted that catalog and committed history markers even though later writes could violate the relationship.

Check all four ordinary foreign-key triggers, including the referenced parent's actions, inside the existing batched catalog query. Reject disabled and replica-only triggers and reject an active replica session unless the checks are ALWAYS enabled. Preserve the caller's session configuration. Frozen schemas do not describe partitions, so reject partitioned roots and individual partitions instead of incompletely validating their descendant constraints.

Validation: eight live PostgreSQL regression cases failed before the fix. They cover both trigger sides, disabled/replica modes, saved-history drift before pending DDL, active replication role and partition roots/leaves. The corrected runner rolls back new DDL/markers, preserves existing data/history, and resumes after explicit repair. Ordinary and ALWAYS triggers pass; an unchanged two-table history still takes ten statements, with the same three catalog reads per batch. The complete PostgreSQL migration test file and analysis pass.

This verifies current enforcement configuration, not historical PostgreSQL row integrity after privileged bypass. No business-table scan, per-table query or new abstraction is added. Reviewed definitions and fingerprints remain unchanged.

Summary by CodeRabbit

  • Bug Fixes
    • PostgreSQL migrations now reject foreign keys whose internal checks or parent actions are disabled or bypassed for ordinary application sessions or the migration session.
    • Partitioned tables and partitions cannot satisfy PostgreSQL migration requirements.
    • Changes to foreign-key enforcement settings are detected before pending database changes are applied.
  • Documentation
    • Clarified PostgreSQL table and foreign-key requirements, including that checks do not scan for data violations from periods when enforcement was disabled.

@medz medz added bug Something isn't working type:engine platform:dart labels Oct 9, 2026
@netlify

netlify Bot commented Oct 9, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for prisma-dart canceled.

Name Link
🔨 Latest commit ef21c1b
🔍 Latest deploy log https://app.netlify.com/projects/prisma-dart/deploys/6ac898ddbd09660008920e90

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 9, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-09T07:38:46.590668Z ef21c1b Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@coderabbitai

coderabbitai Bot commented Oct 9, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: c32db8bc-8ed7-4671-bf13-d58e11c799cc
📥 Commits

Reviewing files that changed from the base of the PR and between 730d0b9 and ef21c1b.

📒 Files selected for processing (2)
  • CONTRIBUTING.md
  • test/migration_postgres_test.dart

Included review availability: This review used your included allowance. Your plan provides up to 2 included reviews per hour; 0 remain after this review.


📝 Walkthrough

Walkthrough

PostgreSQL migration catalog checks now reject partitioned tables and foreign keys whose internal constraint triggers are disabled or replica-only. Documentation describes these requirements and clarifies that catalog checks do not scan rows for historical violations. PostgreSQL tests cover these checks and related migration behavior.

Changes

PostgreSQL Migration Catalog Validation

Layer / File(s) Summary
Catalog requirements and checks
doc/migrations.md, lib/src/migration/catalog.dart, lib/src/migration/runner.dart
The catalog query now selects ordinary, non-partitioned permanent tables. Foreign-key checks require four internal constraint triggers that are always enabled or normally enabled when the replication role is not replica. The documentation describes these requirements and clarifies that checks do not scan for historical violations.
PostgreSQL migration validation tests
test/migration_postgres_test.dart, CONTRIBUTING.md
Tests cover disabled and replica-only triggers, trigger drift, replication-role changes, and partitioned tables. They also check migration state and data after failures. Contributor guidance describes the superuser requirements for six privileged tests.

Priority: ⬇️ Low

Estimated code review effort: 3 (Moderate) | ~25 minutes

Change: Bug fix

Merge Risk: ⚪ Minimal · up to ef21c

The migration checks and regression tests cover the described trigger and partition safeguards, with no identified merge-blocking risk.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly describes the main change: verifying PostgreSQL foreign-key trigger enforcement during migrations.
Docstring Coverage Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff. Analyzed 0 functions across 0…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

A rabbit checks each trigger’s place,
And bounds the tables with care.
No partition slips through the gate,
No stale check hides there.
The migration runs, then rests,
While carrots mark success.

Comment @coderabbitai help to get the list of available commands.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 730d0b9ce2

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread test/migration_postgres_test.dart

medz commented Oct 9, 2026

Copy link
Copy Markdown
Owner Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep them coming!

Reviewed commit: ef21c1bd2e

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@medz
medz merged commit 59c1759 into main Oct 9, 2026
6 checks passed
@medz
medz deleted the fix/postgres-foreign-key-enforcement branch October 9, 2026 08:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working platform:dart type:engine

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant