Skip to content

Commit 4e0bd3d

Browse files
fix(db): preserve composite keys through backup restore
Co-Authored-By: Paperclip <noreply@paperclip.ing>
1 parent 3b77f6d commit 4e0bd3d

5 files changed

Lines changed: 29 additions & 52 deletions

File tree

packages/db/src/migrations/0212_orange_warpath.sql renamed to packages/db/src/migrations/0212_lame_vertigo.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ CREATE TABLE "interrupted_run_handoffs" (
1919
ALTER TABLE "interrupted_run_handoffs" ADD CONSTRAINT "interrupted_run_handoffs_company_id_companies_id_fk" FOREIGN KEY ("company_id") REFERENCES "public"."companies"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
2020
ALTER TABLE "interrupted_run_handoffs" ADD CONSTRAINT "interrupted_run_handoffs_owner_agent_id_agents_id_fk" FOREIGN KEY ("owner_agent_id") REFERENCES "public"."agents"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
2121
ALTER TABLE "interrupted_run_handoffs" ADD CONSTRAINT "interrupted_run_handoffs_recovery_action_id_issue_recovery_actions_id_fk" FOREIGN KEY ("recovery_action_id") REFERENCES "public"."issue_recovery_actions"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
22-
CREATE UNIQUE INDEX "heartbeat_runs_company_id_id_uq" ON "heartbeat_runs" USING btree ("company_id","id");--> statement-breakpoint
23-
CREATE UNIQUE INDEX "issues_company_id_id_uq" ON "issues" USING btree ("company_id","id");--> statement-breakpoint
22+
ALTER TABLE "heartbeat_runs" ADD CONSTRAINT "heartbeat_runs_company_id_id_uq" UNIQUE("company_id","id");--> statement-breakpoint
23+
ALTER TABLE "issues" ADD CONSTRAINT "issues_company_id_id_uq" UNIQUE("company_id","id");--> statement-breakpoint
2424
ALTER TABLE "interrupted_run_handoffs" ADD CONSTRAINT "interrupted_run_handoffs_issue_company_fk" FOREIGN KEY ("company_id","issue_id") REFERENCES "public"."issues"("company_id","id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
2525
ALTER TABLE "interrupted_run_handoffs" ADD CONSTRAINT "interrupted_run_handoffs_source_company_fk" FOREIGN KEY ("company_id","interrupted_run_id") REFERENCES "public"."heartbeat_runs"("company_id","id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
2626
ALTER TABLE "interrupted_run_handoffs" ADD CONSTRAINT "interrupted_run_handoffs_successor_company_fk" FOREIGN KEY ("company_id","successor_run_id") REFERENCES "public"."heartbeat_runs"("company_id","id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint

packages/db/src/migrations/meta/0212_snapshot.json

Lines changed: 21 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"id": "1f59f67e-5260-4064-a35a-2389ba811521",
2+
"id": "c0f0aeff-9ec8-4ec8-b70a-3dbc4daa32f1",
33
"prevId": "c56d3441-2ed4-4249-a115-99d55d395899",
44
"version": "7",
55
"dialect": "postgresql",
@@ -16618,27 +16618,6 @@
1661816618
}
1661916619
},
1662016620
"indexes": {
16621-
"heartbeat_runs_company_id_id_uq": {
16622-
"name": "heartbeat_runs_company_id_id_uq",
16623-
"columns": [
16624-
{
16625-
"expression": "company_id",
16626-
"isExpression": false,
16627-
"asc": true,
16628-
"nulls": "last"
16629-
},
16630-
{
16631-
"expression": "id",
16632-
"isExpression": false,
16633-
"asc": true,
16634-
"nulls": "last"
16635-
}
16636-
],
16637-
"isUnique": true,
16638-
"concurrently": false,
16639-
"method": "btree",
16640-
"with": {}
16641-
},
1664216621
"heartbeat_runs_company_agent_started_idx": {
1664316622
"name": "heartbeat_runs_company_agent_started_idx",
1664416623
"columns": [
@@ -16932,7 +16911,16 @@
1693216911
}
1693316912
},
1693416913
"compositePrimaryKeys": {},
16935-
"uniqueConstraints": {},
16914+
"uniqueConstraints": {
16915+
"heartbeat_runs_company_id_id_uq": {
16916+
"name": "heartbeat_runs_company_id_id_uq",
16917+
"nullsNotDistinct": false,
16918+
"columns": [
16919+
"company_id",
16920+
"id"
16921+
]
16922+
}
16923+
},
1693616924
"policies": {},
1693716925
"checkConstraints": {},
1693816926
"isRLSEnabled": false
@@ -23025,27 +23013,6 @@
2302523013
}
2302623014
},
2302723015
"indexes": {
23028-
"issues_company_id_id_uq": {
23029-
"name": "issues_company_id_id_uq",
23030-
"columns": [
23031-
{
23032-
"expression": "company_id",
23033-
"isExpression": false,
23034-
"asc": true,
23035-
"nulls": "last"
23036-
},
23037-
{
23038-
"expression": "id",
23039-
"isExpression": false,
23040-
"asc": true,
23041-
"nulls": "last"
23042-
}
23043-
],
23044-
"isUnique": true,
23045-
"concurrently": false,
23046-
"method": "btree",
23047-
"with": {}
23048-
},
2304923016
"issues_company_status_idx": {
2305023017
"name": "issues_company_status_idx",
2305123018
"columns": [
@@ -23791,7 +23758,16 @@
2379123758
}
2379223759
},
2379323760
"compositePrimaryKeys": {},
23794-
"uniqueConstraints": {},
23761+
"uniqueConstraints": {
23762+
"issues_company_id_id_uq": {
23763+
"name": "issues_company_id_id_uq",
23764+
"nullsNotDistinct": false,
23765+
"columns": [
23766+
"company_id",
23767+
"id"
23768+
]
23769+
}
23770+
},
2379523771
"policies": {},
2379623772
"checkConstraints": {},
2379723773
"isRLSEnabled": false

packages/db/src/migrations/meta/_journal.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1475,8 +1475,8 @@
14751475
{
14761476
"idx": 212,
14771477
"version": "7",
1478-
"when": 1786173683973,
1479-
"tag": "0212_orange_warpath",
1478+
"when": 1786175635996,
1479+
"tag": "0212_lame_vertigo",
14801480
"breakpoints": true
14811481
}
14821482
]

packages/db/src/schema/heartbeat_runs.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { sql } from "drizzle-orm";
2-
import { type AnyPgColumn, pgTable, uuid, text, timestamp, jsonb, index, integer, bigint, boolean, uniqueIndex } from "drizzle-orm/pg-core";
2+
import { type AnyPgColumn, pgTable, uuid, text, timestamp, jsonb, index, integer, bigint, boolean, unique } from "drizzle-orm/pg-core";
33
import { companies } from "./companies.js";
44
import { agents } from "./agents.js";
55
import { agentWakeupRequests } from "./agent_wakeup_requests.js";
@@ -60,7 +60,7 @@ export const heartbeatRuns = pgTable(
6060
updatedAt: timestamp("updated_at", { withTimezone: true }).notNull().defaultNow(),
6161
},
6262
(table) => ({
63-
companyIdCandidateKey: uniqueIndex("heartbeat_runs_company_id_id_uq").on(table.companyId, table.id),
63+
companyIdCandidateKey: unique("heartbeat_runs_company_id_id_uq").on(table.companyId, table.id),
6464
companyAgentStartedIdx: index("heartbeat_runs_company_agent_started_idx").on(
6565
table.companyId,
6666
table.agentId,

packages/db/src/schema/issues.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
integer,
99
jsonb,
1010
index,
11+
unique,
1112
uniqueIndex,
1213
} from "drizzle-orm/pg-core";
1314
import { agents } from "./agents.js";
@@ -77,7 +78,7 @@ export const issues = pgTable(
7778
updatedAt: timestamp("updated_at", { withTimezone: true }).notNull().defaultNow(),
7879
},
7980
(table) => ({
80-
companyIdCandidateKey: uniqueIndex("issues_company_id_id_uq").on(table.companyId, table.id),
81+
companyIdCandidateKey: unique("issues_company_id_id_uq").on(table.companyId, table.id),
8182
companyStatusIdx: index("issues_company_status_idx").on(table.companyId, table.status),
8283
companyHarnessKindIdx: index("issues_company_harness_kind_idx").on(table.companyId, table.harnessKind),
8384
assigneeStatusIdx: index("issues_company_assignee_status_idx").on(

0 commit comments

Comments
 (0)