Skip to content
This repository was archived by the owner on Nov 25, 2026. It is now read-only.

fix: preserve FK referential actions in schema IR - #608

Merged
SevInf merged 1 commit into
prisma:mainfrom
medz:codex/preserve-fk-referential-actions
May 28, 2026
Merged

SevInf merged 1 commit into
prisma:mainfrom
medz:codex/preserve-fk-referential-actions

Conversation

@medz

@medz medz commented May 28, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Preserves onDelete and onUpdate when converting contract foreign keys into SQL schema IR for migration planning.

This prevents unchanged foreign keys with explicit non-default referential actions from being compared as if the source side used the default noAction behavior.

Fixes #606.

Changes

  • Carry fk.onDelete and fk.onUpdate through convertForeignKey() in contract-to-schema-ir.
  • Extend the existing foreign-key conversion test to assert both referential actions survive the conversion.

Validation

Summary by CodeRabbit

  • New Features

    • Foreign key referential action behavior (onDelete/onUpdate actions such as cascade and restrict) is now properly preserved during schema conversion.
  • Tests

    • Added test coverage to verify foreign key referential action fields are correctly captured in schema IR output.

Review Change Stack

@coderabbitai

coderabbitai Bot commented May 28, 2026 •

Copy link
Copy Markdown
Contributor
📝 Walkthrough

Walkthrough

The PR fixes a bug where foreign key referential actions (onDelete, onUpdate) were dropped during contract-to-schema IR conversion. The convertForeignKey function now conditionally preserves these fields using ifDefined, with test fixture and assertion updates validating the behavior.

Changes

Foreign Key Referential Actions Preservation

Layer / File(s) Summary
FK referential action conversion and test validation
packages/2-sql/9-family/src/core/migrations/contract-to-schema-ir.ts, packages/2-sql/9-family/test/contract-to-schema-ir.test.ts
convertForeignKey now conditionally sets onDelete and onUpdate in SqlForeignKeyIR based on the contract ForeignKey source. Test fixture for Post_authorId_fkey includes onDelete: 'cascade' and onUpdate: 'restrict', and the expected IR assertion includes these same properties.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~3 minutes

Poem

🐰 A foreign key's heart now beats complete,
With cascade and restrict, oh so neat!
Actions preserved through IR's sweet flight,
Schema conversions now shine just right! ✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'fix: preserve FK referential actions in schema IR' accurately and concisely summarizes the main change: preserving foreign key referential actions (onDelete/onUpdate) in the schema IR conversion.
Linked Issues check ✅ Passed The pull request implementation directly addresses issue #606 by adding onDelete and onUpdate preservation in convertForeignKey(), matching the expected behavior described in the issue and the provided patch.
Out of Scope Changes check ✅ Passed All changes are directly scoped to fixing issue #606: the conversion logic adds onDelete/onUpdate fields, and tests are updated to validate these fields are preserved.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

ESLint skipped: no ESLint configuration detected in root package.json. To enable, add eslint to devDependencies.


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

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

@medz
medz force-pushed the codex/preserve-fk-referential-actions branch from 90ff74f to 951b608 Compare May 28, 2026 15:22
@pkg-pr-new

pkg-pr-new Bot commented May 28, 2026

Copy link
Copy Markdown

Open in StackBlitz

@prisma-next/extension-author-tools

npm i https://pkg.pr.new/@prisma-next/extension-author-tools@608

@prisma-next/mongo-runtime

npm i https://pkg.pr.new/@prisma-next/mongo-runtime@608

@prisma-next/family-mongo

npm i https://pkg.pr.new/@prisma-next/family-mongo@608

@prisma-next/sql-runtime

npm i https://pkg.pr.new/@prisma-next/sql-runtime@608

@prisma-next/family-sql

npm i https://pkg.pr.new/@prisma-next/family-sql@608

@prisma-next/extension-arktype-json

npm i https://pkg.pr.new/@prisma-next/extension-arktype-json@608

@prisma-next/extension-cipherstash

npm i https://pkg.pr.new/@prisma-next/extension-cipherstash@608

@prisma-next/middleware-cache

npm i https://pkg.pr.new/@prisma-next/middleware-cache@608

@prisma-next/mongo

npm i https://pkg.pr.new/@prisma-next/mongo@608

@prisma-next/extension-paradedb

npm i https://pkg.pr.new/@prisma-next/extension-paradedb@608

@prisma-next/extension-pgvector

npm i https://pkg.pr.new/@prisma-next/extension-pgvector@608

@prisma-next/extension-postgis

npm i https://pkg.pr.new/@prisma-next/extension-postgis@608

@prisma-next/postgres

npm i https://pkg.pr.new/@prisma-next/postgres@608

@prisma-next/sql-orm-client

npm i https://pkg.pr.new/@prisma-next/sql-orm-client@608

@prisma-next/sqlite

npm i https://pkg.pr.new/@prisma-next/sqlite@608

@prisma-next/target-mongo

npm i https://pkg.pr.new/@prisma-next/target-mongo@608

@prisma-next/adapter-mongo

npm i https://pkg.pr.new/@prisma-next/adapter-mongo@608

@prisma-next/driver-mongo

npm i https://pkg.pr.new/@prisma-next/driver-mongo@608

@prisma-next/contract

npm i https://pkg.pr.new/@prisma-next/contract@608

@prisma-next/utils

npm i https://pkg.pr.new/@prisma-next/utils@608

@prisma-next/config

npm i https://pkg.pr.new/@prisma-next/config@608

@prisma-next/errors

npm i https://pkg.pr.new/@prisma-next/errors@608

@prisma-next/framework-components

npm i https://pkg.pr.new/@prisma-next/framework-components@608

@prisma-next/operations

npm i https://pkg.pr.new/@prisma-next/operations@608

@prisma-next/ts-render

npm i https://pkg.pr.new/@prisma-next/ts-render@608

@prisma-next/contract-authoring

npm i https://pkg.pr.new/@prisma-next/contract-authoring@608

@prisma-next/ids

npm i https://pkg.pr.new/@prisma-next/ids@608

@prisma-next/psl-parser

npm i https://pkg.pr.new/@prisma-next/psl-parser@608

@prisma-next/psl-printer

npm i https://pkg.pr.new/@prisma-next/psl-printer@608

@prisma-next/cli

npm i https://pkg.pr.new/@prisma-next/cli@608

@prisma-next/cli-telemetry

npm i https://pkg.pr.new/@prisma-next/cli-telemetry@608

@prisma-next/emitter

npm i https://pkg.pr.new/@prisma-next/emitter@608

@prisma-next/migration-tools

npm i https://pkg.pr.new/@prisma-next/migration-tools@608

prisma-next

npm i https://pkg.pr.new/prisma-next@608

@prisma-next/vite-plugin-contract-emit

npm i https://pkg.pr.new/@prisma-next/vite-plugin-contract-emit@608

@prisma-next/mongo-codec

npm i https://pkg.pr.new/@prisma-next/mongo-codec@608

@prisma-next/mongo-contract

npm i https://pkg.pr.new/@prisma-next/mongo-contract@608

@prisma-next/mongo-value

npm i https://pkg.pr.new/@prisma-next/mongo-value@608

@prisma-next/mongo-contract-psl

npm i https://pkg.pr.new/@prisma-next/mongo-contract-psl@608

@prisma-next/mongo-contract-ts

npm i https://pkg.pr.new/@prisma-next/mongo-contract-ts@608

@prisma-next/mongo-emitter

npm i https://pkg.pr.new/@prisma-next/mongo-emitter@608

@prisma-next/mongo-schema-ir

npm i https://pkg.pr.new/@prisma-next/mongo-schema-ir@608

@prisma-next/mongo-query-ast

npm i https://pkg.pr.new/@prisma-next/mongo-query-ast@608

@prisma-next/mongo-orm

npm i https://pkg.pr.new/@prisma-next/mongo-orm@608

@prisma-next/mongo-query-builder

npm i https://pkg.pr.new/@prisma-next/mongo-query-builder@608

@prisma-next/mongo-lowering

npm i https://pkg.pr.new/@prisma-next/mongo-lowering@608

@prisma-next/mongo-wire

npm i https://pkg.pr.new/@prisma-next/mongo-wire@608

@prisma-next/sql-contract

npm i https://pkg.pr.new/@prisma-next/sql-contract@608

@prisma-next/sql-errors

npm i https://pkg.pr.new/@prisma-next/sql-errors@608

@prisma-next/sql-operations

npm i https://pkg.pr.new/@prisma-next/sql-operations@608

@prisma-next/sql-schema-ir

npm i https://pkg.pr.new/@prisma-next/sql-schema-ir@608

@prisma-next/sql-contract-psl

npm i https://pkg.pr.new/@prisma-next/sql-contract-psl@608

@prisma-next/sql-contract-ts

npm i https://pkg.pr.new/@prisma-next/sql-contract-ts@608

@prisma-next/sql-contract-emitter

npm i https://pkg.pr.new/@prisma-next/sql-contract-emitter@608

@prisma-next/sql-lane-query-builder

npm i https://pkg.pr.new/@prisma-next/sql-lane-query-builder@608

@prisma-next/sql-relational-core

npm i https://pkg.pr.new/@prisma-next/sql-relational-core@608

@prisma-next/sql-builder

npm i https://pkg.pr.new/@prisma-next/sql-builder@608

@prisma-next/target-postgres

npm i https://pkg.pr.new/@prisma-next/target-postgres@608

@prisma-next/target-sqlite

npm i https://pkg.pr.new/@prisma-next/target-sqlite@608

@prisma-next/adapter-postgres

npm i https://pkg.pr.new/@prisma-next/adapter-postgres@608

@prisma-next/adapter-sqlite

npm i https://pkg.pr.new/@prisma-next/adapter-sqlite@608

@prisma-next/driver-postgres

npm i https://pkg.pr.new/@prisma-next/driver-postgres@608

@prisma-next/driver-sqlite

npm i https://pkg.pr.new/@prisma-next/driver-sqlite@608

commit: 951b608

@medz
medz marked this pull request as ready for review May 28, 2026 15:25
@medz
medz requested a review from a team as a code owner May 28, 2026 15:26
@medz medz changed the title [codex] Preserve FK referential actions in schema IR fix: preserve FK referential actions in schema IR May 28, 2026

@SevInf SevInf left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thank you very much!
Waiting for CI and merging

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
packages/2-sql/9-family/test/contract-to-schema-ir.test.ts (1)

473-517: ⚡ Quick win

Consider adding test cases for additional edge cases.

The test validates that both onDelete and onUpdate are preserved when present. Consider adding test cases for:

  • Only onDelete specified (no onUpdate)
  • Only onUpdate specified (no onDelete)
  • Explicit 'noAction' values (to verify they're preserved as-is, not normalized during conversion)

The existing test at line 643 already covers the case where neither field is present, so basic coverage exists.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/2-sql/9-family/test/contract-to-schema-ir.test.ts` around lines 473
- 517, Add three new test cases alongside the existing "converts foreign keys
(reshapes references)" test that call contractToSchemaIR with a SqlStorage
containing a Post->User foreign key but vary the cascade/update settings: one
where only onDelete is set, one where only onUpdate is set, and one where
onDelete/onUpdate are explicitly 'noAction'; in each test assert that
result.tables['Post']!.foreignKeys contains the corresponding object with the
exact onDelete/onUpdate values preserved (use the same storage construction
pattern and testRenderer as in the current test to locate SqlStorage and
contractToSchemaIR usage).
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@packages/2-sql/9-family/test/contract-to-schema-ir.test.ts`:
- Around line 473-517: Add three new test cases alongside the existing "converts
foreign keys (reshapes references)" test that call contractToSchemaIR with a
SqlStorage containing a Post->User foreign key but vary the cascade/update
settings: one where only onDelete is set, one where only onUpdate is set, and
one where onDelete/onUpdate are explicitly 'noAction'; in each test assert that
result.tables['Post']!.foreignKeys contains the corresponding object with the
exact onDelete/onUpdate values preserved (use the same storage construction
pattern and testRenderer as in the current test to locate SqlStorage and
contractToSchemaIR usage).

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yml

Review profile: CHILL

Plan: Pro

Run ID: 0ecdd879-2fce-4116-aab1-de3db8f20422

📥 Commits

Reviewing files that changed from the base of the PR and between 96318b7 and 951b608.

📒 Files selected for processing (2)
  • packages/2-sql/9-family/src/core/migrations/contract-to-schema-ir.ts
  • packages/2-sql/9-family/test/contract-to-schema-ir.test.ts

@SevInf
SevInf merged commit 4233977 into prisma:main May 28, 2026
13 checks passed
@medz
medz deleted the codex/preserve-fk-referential-actions branch May 28, 2026 15:36
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug(migration): preserve FK referential actions in contract-to-schema IR

2 participants