Skip to content

Commit 3bed99b

Browse files
committed
refactor(db): fold creator lifetime guard into file ownership migration
1 parent e42a4c6 commit 3bed99b

21 files changed

Lines changed: 457 additions & 31391 deletions

‎apps/sim/lib/workspace-files/search/README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ The design uses PostgreSQL's documented [TOAST behavior](https://www.postgresql.
6969

7070
### File owner expansion
7171

72-
Migration `0427_file_search_owner_scope.sql` adds canonical workspace/Project ownership to the existing build, revision, and chunk pipeline. Legacy workspace inserts are translated by triggers, and the existing workspace trigram index remains usable. Project rows use a separate owner-scoped trigram index on the same chunk table. Organizations and personal users are not enabled search owners.
72+
Migration `0426_file_search_owner_scope.sql` adds canonical workspace/Project ownership to the existing build, revision, and chunk pipeline. Legacy workspace inserts are translated by triggers, and the existing workspace trigram index remains usable. Project rows use a separate owner-scoped trigram index on the same chunk table. Organizations and personal users are not enabled search owners.
7373

7474
The new `file_search_dispatch_queue` replaces the workspace-keyed scheduler because older workers cannot decode a Project without a workspace. During expansion, workspace mutations enqueue both queues, while the new dispatcher reads the owner queue. Both deployments share the dispatcher lock, revision claim token, build lease, and publication fence. Drain old app dispatchers and Trigger workers/retries before enabling Project dispatch. Retire the legacy queue and its dual enqueue in a later contract deploy; they are not a second indexing pipeline. The `file-search-owners-v3` cursor performs bounded owner/file keyset backfill and hourly reconciliation without rewriting extracted text in the migration.
7575

‎apps/sim/lib/workspace-files/search/chunks.integration.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ describe('chunked workspace file search on PostgreSQL', () => {
173173
'0359_workspace_file_search_chunks.sql',
174174
ginWriteMigration,
175175
'0382_workspace_file_search_dispatch_handoff.sql',
176-
'0427_file_search_owner_scope.sql',
176+
'0426_file_search_owner_scope.sql',
177177
]) {
178178
await applyMigration(migration)
179179
}

‎apps/sim/lib/workspace-files/search/dispatcher.integration.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ describe('workspace file search dispatch PostgreSQL deadlines', () => {
7070
})
7171
)
7272
const migration = readFileSync(
73-
resolve(process.cwd(), '../../packages/db/migrations/0427_file_search_owner_scope.sql'),
73+
resolve(process.cwd(), '../../packages/db/migrations/0426_file_search_owner_scope.sql'),
7474
'utf8'
7575
).replaceAll('"public".', `"${schemaName}".`)
7676
const ownerIndexes = [

‎packages/db/file-creator-lifetime.integration.ts‎

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,9 @@ import postgres, { type Sql } from 'postgres'
88
import { afterAll, beforeAll, beforeEach, describe, expect, it } from 'vitest'
99

1010
const databaseUrl = readTestDatabaseUrl()
11-
const migrations = [
12-
'0422_file_entity_ownership.sql',
13-
'0423_file_folder_version_ownership.sql',
14-
'0424_file_creator_lifetime.sql',
15-
].map((name) => readFileSync(new URL(`./migrations/${name}`, import.meta.url), 'utf8'))
11+
const migrations = ['0422_file_entity_ownership.sql', '0423_file_folder_version_ownership.sql'].map(
12+
(name) => readFileSync(new URL(`./migrations/${name}`, import.meta.url), 'utf8')
13+
)
1614
const checks: { name: string; status: 'passed' | 'failed'; durationMs: number; error?: string }[] =
1715
[]
1816
function check(name: string, run: () => Promise<void>) {
@@ -157,8 +155,8 @@ describe('Shared file creator lifetime in PostgreSQL', () => {
157155
check(
158156
'creator guard can be replayed without weakening existing resource protection',
159157
async () => {
160-
await applyMigration(migrations[2])
161-
await applyMigration(migrations[2])
158+
await applyMigration(migrations[1])
159+
await applyMigration(migrations[1])
162160
await sql`INSERT INTO folder (id,name,user_id,resource_type,project_id,deleted_at) VALUES ('folder','Archived','user-a','file','project-a',now())`
163161
await expect(sql`DELETE FROM "user" WHERE id='user-a'`).rejects.toMatchObject({
164162
code: '23514',

‎packages/db/file-entity-ownership.integration.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const migration = readFileSync(
1515
)
1616

1717
const workspaceBindingMigration = readFileSync(
18-
new URL('./migrations/0428_file_workspace_binding.sql', import.meta.url),
18+
new URL('./migrations/0427_file_workspace_binding.sql', import.meta.url),
1919
'utf8'
2020
)
2121

‎packages/db/file-folder-version-ownership.integration.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,8 @@ describe('file folder and version ownership in PostgreSQL', () => {
276276
code: '23503',
277277
})
278278
await expect(sql`DELETE FROM "user" WHERE id = 'user-a'`).rejects.toMatchObject({
279-
code: '23503',
279+
code: '23514',
280+
constraint_name: 'project_resource_creator_handoff',
280281
})
281282
await sql`UPDATE folder SET user_id = 'user-b' WHERE id = 'project-folder'`
282283
await sql`DELETE FROM "user" WHERE id = 'user-a'`

‎packages/db/migrations/0423_file_folder_version_ownership.sql‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,14 +165,17 @@ FOR EACH ROW EXECUTE FUNCTION workspace_file_version_owner_match();--> statement
165165
CREATE OR REPLACE FUNCTION project_file_uploader_delete_guard()
166166
RETURNS trigger LANGUAGE plpgsql AS $$
167167
BEGIN
168-
IF EXISTS (SELECT 1 FROM workspace_files WHERE project_id IS NOT NULL AND user_id = OLD.id)
169-
OR EXISTS (SELECT 1 FROM folder WHERE project_id IS NOT NULL AND user_id = OLD.id) THEN
170-
RAISE EXCEPTION 'Project file and folder attribution must be reassigned or retired before deleting its creator'
171-
USING ERRCODE = '23503';
168+
IF EXISTS (SELECT 1 FROM workspace_files WHERE user_id = OLD.id AND project_id IS NOT NULL)
169+
OR EXISTS (SELECT 1 FROM folder WHERE user_id = OLD.id AND project_id IS NOT NULL) THEN
170+
RAISE EXCEPTION 'Project resource creators must be handed to a successor before account deletion' USING ERRCODE = '23514', CONSTRAINT = 'project_resource_creator_handoff';
172171
END IF;
173172
RETURN OLD;
174173
END;
175174
$$;--> statement-breakpoint
175+
DROP TRIGGER IF EXISTS project_file_uploader_delete_guard ON "user";--> statement-breakpoint
176+
CREATE TRIGGER project_file_uploader_delete_guard
177+
BEFORE DELETE ON "user"
178+
FOR EACH ROW EXECUTE FUNCTION project_file_uploader_delete_guard();--> statement-breakpoint
176179
COMMIT;--> statement-breakpoint
177180
SET lock_timeout = 0;--> statement-breakpoint
178181
DROP INDEX CONCURRENTLY IF EXISTS "folder_project_id_idx";--> statement-breakpoint

‎packages/db/migrations/0424_file_creator_lifetime.sql‎

Lines changed: 0 additions & 14 deletions
This file was deleted.

packages/db/migrations/0425_project_file_upload_sessions.sql renamed to packages/db/migrations/0424_project_file_upload_sessions.sql

File renamed without changes.

packages/db/migrations/0426_public_share_entity_ownership.sql renamed to packages/db/migrations/0425_public_share_entity_ownership.sql

File renamed without changes.

0 commit comments

Comments
 (0)