Skip to content

Commit 3db7e57

Browse files
committed
refactor: use explicit Project file ownership storage
1 parent 0aa7193 commit 3db7e57

17 files changed

Lines changed: 63 additions & 159 deletions

‎apps/sim/lib/mothership/agent-cli/project-file-read.integration.ts‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import { setUploadDirServer, uploadsSetupMock } from '@sim/testing/mocks/uploads
1919
import { getErrorMessage } from '@sim/utils/errors'
2020
import { generateId } from '@sim/utils/id'
2121
import { Document, Packer, Paragraph } from 'docx'
22-
import { and, eq } from 'drizzle-orm'
22+
import { eq } from 'drizzle-orm'
2323
import sharp from 'sharp'
2424
import { afterAll, beforeEach, describe, expect, it, vi } from 'vitest'
2525

@@ -501,11 +501,7 @@ describe('Project CLI representations against PostgreSQL and stored bytes', () =
501501

502502
afterAll(async () => {
503503
for (const f of fixtures) {
504-
await db
505-
.delete(workspaceFiles)
506-
.where(
507-
and(eq(workspaceFiles.entityType, 'project'), eq(workspaceFiles.entityId, f.projectId))
508-
)
504+
await db.delete(workspaceFiles).where(eq(workspaceFiles.projectId, f.projectId))
509505
await deleteWorkspaceFixture(db, eq(workspace.id, f.workspaceId))
510506
await db.delete(user).where(eq(user.id, f.userId))
511507
}

‎apps/sim/lib/mothership/agent-cli/project-file-write-transport.integration.ts‎

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { deleteWorkspaceFixture, insertWorkspaceFixture } from '@sim/db/testing/
1818
import { setUploadDirServer, uploadsSetupMock } from '@sim/testing/mocks/uploads-setup.mock'
1919
import { getErrorMessage } from '@sim/utils/errors'
2020
import { generateId } from '@sim/utils/id'
21-
import { and, eq, sql } from 'drizzle-orm'
21+
import { eq, sql } from 'drizzle-orm'
2222
import { afterAll, beforeEach, describe, expect, it, vi } from 'vitest'
2323

2424
vi.mock('@/lib/uploads/core/setup.server', () => uploadsSetupMock)
@@ -138,10 +138,7 @@ function create(
138138
}
139139

140140
async function rows(projectId: string) {
141-
return db
142-
.select()
143-
.from(workspaceFiles)
144-
.where(and(eq(workspaceFiles.entityType, 'project'), eq(workspaceFiles.entityId, projectId)))
141+
return db.select().from(workspaceFiles).where(eq(workspaceFiles.projectId, projectId))
145142
}
146143

147144
describe('private native Project writes against PostgreSQL and local storage', () => {
@@ -288,14 +285,8 @@ afterAll(async () => {
288285
await db
289286
.delete(outboxEvent)
290287
.where(sql`${outboxEvent.payload}::jsonb ->> 'key' LIKE ${`project/${f.projectId}/%`}`)
291-
await db
292-
.delete(workspaceFiles)
293-
.where(
294-
and(eq(workspaceFiles.entityType, 'project'), eq(workspaceFiles.entityId, f.projectId))
295-
)
296-
await db
297-
.delete(folder)
298-
.where(and(eq(folder.entityType, 'project'), eq(folder.entityId, f.projectId)))
288+
await db.delete(workspaceFiles).where(eq(workspaceFiles.projectId, f.projectId))
289+
await db.delete(folder).where(eq(folder.projectId, f.projectId))
299290
await deleteWorkspaceFixture(db, eq(workspace.id, f.workspaceId))
300291
await db.delete(user).where(eq(user.id, f.userId))
301292
}

‎apps/sim/lib/mothership/chat/project-file-context.integration.ts‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,7 @@ async function fixture() {
104104
await db.insert(copilotChats).values({ id: chatId, userId, workspaceId, type: 'mothership' })
105105
await db.insert(workspaceFiles).values({
106106
id: fileId,
107-
entityType: 'project',
108-
entityId: projectId,
107+
projectId: projectId,
109108
key: `project/${projectId}/context.md`,
110109
userId,
111110
originalCreatorUserId: userId,

‎apps/sim/lib/projects/files/__integration__/authorization.integration.ts‎

Lines changed: 20 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -238,14 +238,8 @@ afterAll(async () => {
238238
await mkdir(dirname(reportPath), { recursive: true })
239239
await writeFile(reportPath, JSON.stringify({ checks }, null, 2))
240240
for (const f of fixtures) {
241-
await db
242-
.delete(workspaceFiles)
243-
.where(
244-
and(eq(workspaceFiles.entityType, 'project'), eq(workspaceFiles.entityId, f.projectId))
245-
)
246-
await db
247-
.delete(folder)
248-
.where(and(eq(folder.entityType, 'project'), eq(folder.entityId, f.projectId)))
241+
await db.delete(workspaceFiles).where(eq(workspaceFiles.projectId, f.projectId))
242+
await db.delete(folder).where(eq(folder.projectId, f.projectId))
249243
await deleteWorkspaceFixture(db, inArray(workspace.id, f.workspaces))
250244
await db.delete(organization).where(eq(organization.id, f.organizationId))
251245
await db.delete(user).where(inArray(user.id, f.users))
@@ -403,8 +397,7 @@ describe('Project file authority at the database boundary', () => {
403397
const folderId = generateId()
404398
await db.insert(folder).values({
405399
id: folderId,
406-
entityType: 'project',
407-
entityId: f.projectId,
400+
projectId: f.projectId,
408401
resourceType: 'file',
409402
name: 'Docs',
410403
userId: f.ownerId,
@@ -413,8 +406,7 @@ describe('Project file authority at the database boundary', () => {
413406
{
414407
id: rootFileId,
415408
userId: f.ownerId,
416-
entityType: 'project',
417-
entityId: f.projectId,
409+
projectId: f.projectId,
418410
context: 'project',
419411
key: `project/${f.projectId}/${rootFileId}`,
420412
originalName: 'a.md',
@@ -424,8 +416,7 @@ describe('Project file authority at the database boundary', () => {
424416
{
425417
id: nestedFileId,
426418
userId: f.ownerId,
427-
entityType: 'project',
428-
entityId: f.projectId,
419+
projectId: f.projectId,
429420
context: 'project',
430421
folderId,
431422
key: `project/${f.projectId}/${nestedFileId}`,
@@ -446,8 +437,7 @@ describe('Project file authority at the database boundary', () => {
446437
{
447438
id: generateId(),
448439
userId: other.ownerId,
449-
entityType: 'project',
450-
entityId: other.projectId,
440+
projectId: other.projectId,
451441
context: 'project',
452442
key: `project/${other.projectId}/a.md`,
453443
originalName: 'a.md',
@@ -484,25 +474,22 @@ describe('Project file authority at the database boundary', () => {
484474
await db.insert(folder).values([
485475
{
486476
id: docsId,
487-
entityType: 'project',
488-
entityId: f.projectId,
477+
projectId: f.projectId,
489478
resourceType: 'file',
490479
name: 'Docs',
491480
userId: f.ownerId,
492481
},
493482
{
494483
id: nestedId,
495-
entityType: 'project',
496-
entityId: f.projectId,
484+
projectId: f.projectId,
497485
resourceType: 'file',
498486
name: 'Q3/Q4',
499487
parentId: docsId,
500488
userId: f.ownerId,
501489
},
502490
{
503491
id: siblingId,
504-
entityType: 'project',
505-
entityId: f.projectId,
492+
projectId: f.projectId,
506493
resourceType: 'file',
507494
name: 'Elsewhere',
508495
userId: f.ownerId,
@@ -513,8 +500,7 @@ describe('Project file authority at the database boundary', () => {
513500
fileIds.map((id, index) => ({
514501
id,
515502
userId: f.ownerId,
516-
entityType: 'project' as const,
517-
entityId: f.projectId,
503+
projectId: f.projectId,
518504
context: 'project',
519505
folderId: [null, docsId, nestedId, siblingId][index],
520506
key: `project/${f.projectId}/${id}`,
@@ -567,8 +553,7 @@ describe('Project file authority at the database boundary', () => {
567553
const folderId = generateId()
568554
await db.insert(folder).values({
569555
id: folderId,
570-
entityType: 'project',
571-
entityId: f.projectId,
556+
projectId: f.projectId,
572557
resourceType: 'file',
573558
name: 'Q3/Q4',
574559
userId: f.ownerId,
@@ -580,8 +565,7 @@ describe('Project file authority at the database boundary', () => {
580565
{
581566
id: fileId,
582567
userId: f.ownerId,
583-
entityType: 'project',
584-
entityId: f.projectId,
568+
projectId: f.projectId,
585569
context: 'project',
586570
folderId,
587571
key: `project/${f.projectId}/${fileId}`,
@@ -592,8 +576,7 @@ describe('Project file authority at the database boundary', () => {
592576
{
593577
id: foreignId,
594578
userId: other.ownerId,
595-
entityType: 'project',
596-
entityId: other.projectId,
579+
projectId: other.projectId,
597580
context: 'project',
598581
key: `project/${other.projectId}/${foreignId}`,
599582
originalName: 'Architecture overview.md',
@@ -603,8 +586,7 @@ describe('Project file authority at the database boundary', () => {
603586
{
604587
id: archivedId,
605588
userId: f.ownerId,
606-
entityType: 'project',
607-
entityId: f.projectId,
589+
projectId: f.projectId,
608590
context: 'project',
609591
key: `project/${f.projectId}/${archivedId}`,
610592
originalName: 'old.md',
@@ -655,8 +637,7 @@ describe('Project file authority at the database boundary', () => {
655637
{
656638
id: fileId,
657639
userId: f.ownerId,
658-
entityType: 'project',
659-
entityId: f.projectId,
640+
projectId: f.projectId,
660641
context: 'project',
661642
key: `project/${f.projectId}/${fileId}`,
662643
originalName: 'architecture.md',
@@ -666,8 +647,7 @@ describe('Project file authority at the database boundary', () => {
666647
{
667648
id: foreignFileId,
668649
userId: other.ownerId,
669-
entityType: 'project',
670-
entityId: other.projectId,
650+
projectId: other.projectId,
671651
context: 'project',
672652
key: `project/${other.projectId}/${foreignFileId}`,
673653
originalName: 'architecture.md',
@@ -936,15 +916,13 @@ describe('Project file authority at the database boundary', () => {
936916
await db.insert(folder).values({
937917
id: folderId,
938918
userId: f.ownerId,
939-
entityType: 'project',
940-
entityId: f.projectId,
919+
projectId: f.projectId,
941920
resourceType: 'file',
942921
name: 'Architecture',
943922
})
944923
await db.insert(workspaceFiles).values({
945924
id: fileId,
946-
entityType: 'project',
947-
entityId: f.projectId,
925+
projectId: f.projectId,
948926
userId: f.ownerId,
949927
context: 'project',
950928
folderId,
@@ -993,8 +971,7 @@ describe('Project file authority at the database boundary', () => {
993971
await db.insert(workspaceFiles).values({
994972
id: fileId,
995973
userId: f.ownerId,
996-
entityType: 'project',
997-
entityId: f.projectId,
974+
projectId: f.projectId,
998975
context: 'project',
999976
key: `project/${f.projectId}/${fileId}`,
1000977
originalName: 'architecture.md',
@@ -1048,8 +1025,7 @@ describe('Project file authority at the database boundary', () => {
10481025
await db.insert(workspaceFiles).values({
10491026
id: fileId,
10501027
userId: f.ownerId,
1051-
entityType: 'project',
1052-
entityId: f.projectId,
1028+
projectId: f.projectId,
10531029
context: 'project',
10541030
key: `project/${f.projectId}/${fileId}`,
10551031
originalName: 'transport.md',

‎apps/sim/lib/projects/files/__integration__/browser.integration.ts‎

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,7 @@ async function fixture() {
107107
owned.fileIds.push(id)
108108
await db.insert(workspaceFiles).values({
109109
id,
110-
entityType: 'project',
111-
entityId: projectId,
110+
projectId: projectId,
112111
context: 'project',
113112
workspaceId: null,
114113
userId: options.userId ?? ownerId,
@@ -127,8 +126,7 @@ async function fixture() {
127126
owned.folderIds.push(id)
128127
await db.insert(folder).values({
129128
id,
130-
entityType: 'project',
131-
entityId: projectId,
129+
projectId: projectId,
132130
workspaceId: null,
133131
resourceType: 'file',
134132
name,
@@ -152,8 +150,7 @@ afterAll(async () => {
152150
.delete(workspaceFiles)
153151
.where(
154152
and(
155-
eq(workspaceFiles.entityType, 'project'),
156-
eq(workspaceFiles.entityId, fixture.projectId),
153+
eq(workspaceFiles.projectId, fixture.projectId),
157154
inArray(workspaceFiles.id, fixture.fileIds)
158155
)
159156
)
@@ -162,8 +159,7 @@ afterAll(async () => {
162159
.delete(folder)
163160
.where(
164161
and(
165-
eq(folder.entityType, 'project'),
166-
eq(folder.entityId, fixture.projectId),
162+
eq(folder.projectId, fixture.projectId),
167163
inArray(folder.id, fixture.folderIds)
168164
)
169165
)

0 commit comments

Comments
 (0)