Skip to content

Commit 620d9f6

Browse files
committed
fix(knowledge): keep import records through account deletion, and recheck uploads on re-dispatch
- knowledge_base_import.user_id is attribution only, so the janitor's cleanup record outlives its importer's account - re-dispatching an unclaimed import repeats the knowledge.upload assertion
1 parent 5bb486a commit 620d9f6

5 files changed

Lines changed: 9 additions & 15 deletions

File tree

‎apps/sim/lib/knowledge/application/imports.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,8 @@ async function redispatchUnclaimedImport(
277277
record: KnowledgeImportRecord
278278
): Promise<void> {
279279
if (record.status !== 'running' || record.startedAt) return
280+
// A group that withdrew uploads since the record was written stops it here too.
281+
await assertImportedDocumentsAllowed(principal, context.workspaceId)
280282
try {
281283
await startKnowledgeImport(await importPayload(principal, context))
282284
} catch (error) {

‎packages/db/migrations/0436_knowledge_base_import.sql‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ CREATE TABLE "knowledge_base_import" (
1919
);
2020
--> statement-breakpoint
2121
ALTER TABLE "knowledge_base_import" ADD CONSTRAINT "knowledge_base_import_workspace_id_workspace_id_fk" FOREIGN KEY ("workspace_id") REFERENCES "public"."workspace"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
22-
ALTER TABLE "knowledge_base_import" ADD CONSTRAINT "knowledge_base_import_user_id_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
2322
ALTER TABLE "knowledge_base_import" ADD CONSTRAINT "knowledge_base_import_knowledge_base_id_knowledge_base_id_fk" FOREIGN KEY ("knowledge_base_id") REFERENCES "public"."knowledge_base"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
2423
CREATE INDEX "knowledge_base_import_watchdog_idx" ON "knowledge_base_import" USING btree ("status","updated_at");--> statement-breakpoint
2524
CREATE INDEX "knowledge_base_import_knowledge_base_id_idx" ON "knowledge_base_import" USING btree ("knowledge_base_id");

‎packages/db/migrations/meta/0436_snapshot.json‎

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"id": "a7a19a4e-8207-4fbf-918c-f5ae171bd9cc",
2+
"id": "0ac8e6c1-9440-4310-9af4-7ab28dc2de42",
33
"prevId": "2a4095de-e341-4264-becd-1714d26404f5",
44
"version": "7",
55
"dialect": "postgresql",
@@ -10223,15 +10223,6 @@
1022310223
"onDelete": "cascade",
1022410224
"onUpdate": "no action"
1022510225
},
10226-
"knowledge_base_import_user_id_user_id_fk": {
10227-
"name": "knowledge_base_import_user_id_user_id_fk",
10228-
"tableFrom": "knowledge_base_import",
10229-
"tableTo": "user",
10230-
"columnsFrom": ["user_id"],
10231-
"columnsTo": ["id"],
10232-
"onDelete": "cascade",
10233-
"onUpdate": "no action"
10234-
},
1023510226
"knowledge_base_import_knowledge_base_id_knowledge_base_id_fk": {
1023610227
"name": "knowledge_base_import_knowledge_base_id_knowledge_base_id_fk",
1023710228
"tableFrom": "knowledge_base_import",

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3050,7 +3050,7 @@
30503050
{
30513051
"idx": 436,
30523052
"version": "7",
3053-
"when": 1792720602032,
3053+
"when": 1792721320645,
30543054
"tag": "0436_knowledge_base_import",
30553055
"breakpoints": true
30563056
}

‎packages/db/schema.ts‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3865,9 +3865,11 @@ export const knowledgeBaseImport = pgTable(
38653865
workspaceId: text('workspace_id')
38663866
.notNull()
38673867
.references(() => workspace.id, { onDelete: 'cascade' }),
3868-
userId: text('user_id')
3869-
.notNull()
3870-
.references(() => user.id, { onDelete: 'cascade' }),
3868+
/**
3869+
* Attribution only, with no foreign key: the row is the janitor's handle on
3870+
* the bundle and the base, so it must outlive its importer's account.
3871+
*/
3872+
userId: text('user_id').notNull(),
38713873
/** The uploaded archive, deleted once the import settles. */
38723874
sourceKey: text('source_key').notNull(),
38733875
knowledgeBaseId: text('knowledge_base_id').references(() => knowledgeBase.id, {

0 commit comments

Comments
 (0)