From e65acd1e9c6fa3e30e609a3360ff3d020178c18d Mon Sep 17 00:00:00 2001 From: noelorin Date: Fri, 25 Sep 2026 12:15:50 +0800 Subject: [PATCH] fix(models,dao): media_items unique key includes mountId --- packages/dao/src/media-items.ts | 17 ++++++++++++++--- packages/models/src/media-items.ts | 2 +- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/packages/dao/src/media-items.ts b/packages/dao/src/media-items.ts index 034ec39..cafe606 100644 --- a/packages/dao/src/media-items.ts +++ b/packages/dao/src/media-items.ts @@ -43,11 +43,21 @@ export const mediaItemDao = { .limit(1); return row ?? null; }, - async getByUserPath(userId: string, path: string): Promise { + async getByUserPath( + userId: string, + mountId: string, + path: string, + ): Promise { const [row] = await db .select() .from(mediaItems) - .where(and(eq(mediaItems.userId, userId), eq(mediaItems.path, path))) + .where( + and( + eq(mediaItems.userId, userId), + eq(mediaItems.mountId, mountId), + eq(mediaItems.path, path), + ), + ) .limit(1); return row ?? null; }, @@ -60,11 +70,12 @@ export const mediaItemDao = { size: number; mime?: string | undefined; }): Promise { - const existing = await this.getByUserPath(data.userId, data.path); + const existing = await this.getByUserPath(data.userId, data.mountId, data.path); if (existing) { const [row] = await db .update(mediaItems) .set({ + mountId: data.mountId, size: data.size, mime: data.mime, updatedAt: new Date(), diff --git a/packages/models/src/media-items.ts b/packages/models/src/media-items.ts index bba4d60..1cde2e6 100644 --- a/packages/models/src/media-items.ts +++ b/packages/models/src/media-items.ts @@ -31,7 +31,7 @@ export const mediaItems = sqliteTable( .notNull() .default(sql`(unixepoch())`), }, - (t) => [uniqueIndex("media_user_path_uq").on(t.userId, t.path)], + (t) => [uniqueIndex("media_user_mount_path_uq").on(t.userId, t.mountId, t.path)], ); export type MediaItem = typeof mediaItems.$inferSelect; export type NewMediaItem = typeof mediaItems.$inferInsert;