-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Order RLS policy drops before column drops in migration plans #30302
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Sanjays2402
wants to merge
1
commit into
prisma:main
Choose a base branch
from
Sanjays2402:fix/policy-drop-before-column-drop
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
169 changes: 169 additions & 0 deletions
169
packages/3-targets/3-targets/postgres/test/migrations/rls-policy-drop-order.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,169 @@ | ||
| /** | ||
| * Reproduction for https://github.com/prisma/orm/issues/30226: when one | ||
| * contract change drops a column AND an RLS policy whose `using` expression | ||
| * references that column, the planner must emit the policy drop before the | ||
| * column drop — Postgres refuses `DROP COLUMN` while a policy depends on | ||
| * it (2BP01). | ||
| */ | ||
|
|
||
| import type { Contract } from '@internal/contract/types'; | ||
| import { coreHash, profileHash } from '@internal/contract/types'; | ||
| import type { ExecuteRequestLowerer } from '@internal/family-sql/control-adapter'; | ||
| import { APP_SPACE_ID } from '@internal/framework-components/control'; | ||
| import { SqlStorage, StorageTable } from '@internal/sql-contract/types'; | ||
| import { namingOfLiveName, parseNaming } from '@internal/sql-schema-ir/naming'; | ||
| import { applicationDomainOf } from '@repo/test-utils'; | ||
| import { describe, expect, it } from 'vitest'; | ||
| import { createPostgresMigrationPlanner } from '../../src/core/migrations/planner'; | ||
| import { PostgresRlsEnablement } from '../../src/core/postgres-rls-enablement'; | ||
| import { PostgresRlsPolicy } from '../../src/core/postgres-rls-policy'; | ||
| import { PostgresSchema } from '../../src/core/postgres-schema'; | ||
| import { PostgresDatabaseSchemaNode } from '../../src/core/schema-ir/postgres-database-schema-node'; | ||
| import { PostgresNamespaceSchemaNode } from '../../src/core/schema-ir/postgres-namespace-schema-node'; | ||
| import { PostgresPolicySchemaNode } from '../../src/core/schema-ir/postgres-policy-schema-node'; | ||
| import { PostgresTableSchemaNode } from '../../src/core/schema-ir/postgres-table-schema-node'; | ||
|
|
||
| const stubLowerer: ExecuteRequestLowerer = { | ||
| lower(_ast, _ctx) { | ||
| return { sql: 'DROP POLICY stub', params: [] }; | ||
| }, | ||
| async lowerToExecuteRequest(_ast, _ctx) { | ||
| return { sql: 'DROP POLICY stub', params: [] }; | ||
| }, | ||
| }; | ||
|
|
||
| const TABLE = 'note'; | ||
| const POLICY_NAME = 'note_public_read_443ba5fa'; | ||
| const USING = 'published = true'; | ||
|
|
||
| function buildContract( | ||
| columns: Record<string, { nativeType: string; codecId: string; nullable: boolean }>, | ||
| withPolicy: boolean, | ||
| ): Contract<SqlStorage> { | ||
| const policy = new PostgresRlsPolicy({ | ||
| naming: namingOfLiveName(POLICY_NAME), | ||
| tableName: TABLE, | ||
| namespaceId: 'public', | ||
| operation: 'select', | ||
| roles: ['anon', 'authenticated'], | ||
| using: USING, | ||
| permissive: true, | ||
| withCheck: undefined, | ||
| }); | ||
| const schema = new PostgresSchema({ | ||
| id: 'public', | ||
| entries: { | ||
| table: { | ||
| [TABLE]: new StorageTable({ | ||
| columns, | ||
| primaryKey: { columns: ['id'] }, | ||
| foreignKeys: [], | ||
| uniques: [], | ||
| indexes: [], | ||
| }), | ||
| }, | ||
| ...(withPolicy ? { policy: { [POLICY_NAME]: policy } } : {}), | ||
| rls: { | ||
| [TABLE]: new PostgresRlsEnablement({ tableName: TABLE, namespaceId: 'public' }), | ||
| }, | ||
| }, | ||
| }); | ||
| return { | ||
| target: 'postgres', | ||
| targetFamily: 'sql', | ||
| profileHash: profileHash('rls-policy-drop-order'), | ||
| storage: new SqlStorage({ | ||
| storageHash: coreHash('rls-policy-drop-order'), | ||
| namespaces: { public: schema }, | ||
| }), | ||
| roots: {}, | ||
| domain: applicationDomainOf({ models: {} }), | ||
| capabilities: {}, | ||
| extensions: {}, | ||
| meta: {}, | ||
| }; | ||
| } | ||
|
|
||
| /** Live schema: the old world, with the column and the policy still present. */ | ||
| function liveSchema(): PostgresDatabaseSchemaNode { | ||
| return new PostgresDatabaseSchemaNode({ | ||
| namespaces: { | ||
| public: new PostgresNamespaceSchemaNode({ | ||
| schemaName: 'public', | ||
| tables: { | ||
| [TABLE]: new PostgresTableSchemaNode({ | ||
| name: TABLE, | ||
| columns: { | ||
| id: { name: 'id', nativeType: 'uuid', nullable: false }, | ||
| title: { name: 'title', nativeType: 'text', nullable: false }, | ||
| published: { name: 'published', nativeType: 'bool', nullable: false }, | ||
| }, | ||
| foreignKeys: [], | ||
| uniques: [], | ||
| indexes: [], | ||
| policies: [ | ||
| new PostgresPolicySchemaNode({ | ||
| naming: parseNaming(POLICY_NAME, undefined), | ||
| tableName: TABLE, | ||
| namespaceId: 'public', | ||
| operation: 'select', | ||
| roles: ['anon', 'authenticated'], | ||
| using: USING, | ||
| withCheck: undefined, | ||
| permissive: true, | ||
| dependsOn: undefined, | ||
| }), | ||
| ], | ||
| rlsEnabled: true, | ||
| }), | ||
| }, | ||
| }), | ||
| }, | ||
| roles: [], | ||
| existingSchemas: ['public'], | ||
| pgVersion: 'unknown', | ||
| }); | ||
| } | ||
|
|
||
| const FULL_COLUMNS = { | ||
| id: { nativeType: 'uuid', codecId: 'pg/uuid@1', nullable: false }, | ||
| title: { nativeType: 'text', codecId: 'pg/text@1', nullable: false }, | ||
| published: { nativeType: 'bool', codecId: 'pg/bool@1', nullable: false }, | ||
| }; | ||
| const TRIMMED_COLUMNS = { | ||
| id: { nativeType: 'uuid', codecId: 'pg/uuid@1', nullable: false }, | ||
| title: { nativeType: 'text', codecId: 'pg/text@1', nullable: false }, | ||
| }; | ||
|
|
||
| const DB_UPDATE_POLICY = { | ||
| allowedOperationClasses: ['additive', 'widening', 'destructive'] as const, | ||
| }; | ||
|
|
||
| describe('RLS policy drop vs column drop ordering (prisma/orm#30226)', () => { | ||
| it('drops the policy before the column it references', async () => { | ||
| const planner = createPostgresMigrationPlanner(stubLowerer); | ||
| const result = planner.plan({ | ||
| contract: buildContract(TRIMMED_COLUMNS, false), | ||
| fromContract: buildContract(FULL_COLUMNS, true), | ||
| schema: liveSchema(), | ||
| policy: DB_UPDATE_POLICY, | ||
| frameworkComponents: [], | ||
| spaceId: APP_SPACE_ID, | ||
| snapshotsImportPath: '../../snapshots', | ||
| }); | ||
|
|
||
| expect(result.kind).toBe('success'); | ||
| if (result.kind !== 'success') return; | ||
|
|
||
| const ops = await Promise.all(result.plan.operations); | ||
| const opIds = ops.map((op) => op.id); | ||
| const dropPolicyIdx = opIds.findIndex( | ||
| (id) => id.startsWith('rlsPolicy.') && id.endsWith('.drop'), | ||
| ); | ||
| const dropColumnIdx = opIds.findIndex((id) => id.startsWith('dropColumn.')); | ||
|
|
||
| expect(dropPolicyIdx).toBeGreaterThanOrEqual(0); | ||
| expect(dropColumnIdx).toBeGreaterThanOrEqual(0); | ||
| expect(dropPolicyIdx).toBeLessThan(dropColumnIdx); | ||
| }); | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win
Use an unambiguous table key.
DropColumnCallandDropPostgresRlsPolicyCallpreserve separate schema and table names.quoteIdentifierkeeps periods inside quoted identifiers. Therefore,(schemaName: "a.b", tableName: "c")and(schemaName: "a", tableName: "b.c")are distinct pairs with the same string key.A policy drop for one pair can then move before the other pair's first column drop. This violates the helper's same-table ordering contract and changes unrelated migration ordering.
Encode the pair structurally:
Proposed fix
📝 Committable suggestion
🤖 Prompt for AI Agents