Skip to content

Commit 54acc25

Browse files
jdollen1ru4l
andauthored
Adjust contracts' foreign key to delete when the target is deleted (#6792)
Co-authored-by: Laurin Quast <laurinquast@googlemail.com>
1 parent 0109982 commit 54acc25

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

‎.changeset/olive-melons-hide.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'hive': patch
3+
---
4+
5+
Adjust contract to target foreign key reference to cascade delete
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { type MigrationExecutor } from '../pg-migrator';
2+
3+
/**
4+
* Fix issue where a target cannot be deleted because it has contracts:
5+
*
6+
* """
7+
* Query violates a foreign key integrity constraint. update or delete on table "targets"
8+
* violates foreign key constraint "contracts_target_id_fkey" on table "contracts"
9+
* """"
10+
*/
11+
export default {
12+
name: '2025.05.15T00-00-00.contracts-foreign-key-constraint-fix.ts',
13+
noTransaction: true,
14+
run: ({ sql }) => [
15+
{
16+
name: 'drop constraint contracts_target_id_fkey',
17+
query: sql`
18+
ALTER TABLE "contracts"
19+
DROP CONSTRAINT IF EXISTS "contracts_target_id_fkey"
20+
, ADD CONSTRAINT "contracts_target_id_fkey"
21+
FOREIGN KEY ("target_id")
22+
REFERENCES "targets"("id")
23+
ON DELETE CASCADE
24+
`,
25+
},
26+
],
27+
} satisfies MigrationExecutor;

‎packages/migrations/src/run-pg-migrations.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,5 +164,6 @@ export const runPGMigrations = async (args: { slonik: DatabasePool; runTo?: stri
164164
await import('./actions/2025.03.20T00-00-00.dangerous_breaking'),
165165
await import('./actions/2025.05.14T00-00-00.cascade-deletion-indices-2'),
166166
await import('./actions/2025.05.15T00-00-00.redundant-indices'),
167+
await import('./actions/2025.05.15T00-00-00.contracts-foreign-key-constraint-fix'),
167168
],
168169
});

0 commit comments

Comments
 (0)
close