diff --git a/packages/services/file-service/src/adapters/outbound/persistence/file-postgres.repository.ts b/packages/services/file-service/src/adapters/outbound/persistence/file-postgres.repository.ts index 5e30ea1..f0158a8 100644 --- a/packages/services/file-service/src/adapters/outbound/persistence/file-postgres.repository.ts +++ b/packages/services/file-service/src/adapters/outbound/persistence/file-postgres.repository.ts @@ -93,6 +93,7 @@ export class FilePostgresRepository private toEntity(orm: FileORM): FileEntity { return FileEntity.fromPersistence({ id: orm.id, + tenantId: orm.tenantId, userId: orm.userId, conversationId: orm.conversationId, originalName: orm.originalName, diff --git a/packages/services/file-service/src/domain/entities/file.entity.ts b/packages/services/file-service/src/domain/entities/file.entity.ts index 83ec120..4de4b70 100644 --- a/packages/services/file-service/src/domain/entities/file.entity.ts +++ b/packages/services/file-service/src/domain/entities/file.entity.ts @@ -19,6 +19,7 @@ export enum FileStatus { */ export class FileEntity { readonly id: string; + readonly tenantId: string; readonly userId: string; conversationId: string | null; originalName: string; @@ -37,6 +38,7 @@ export class FileEntity { private constructor(props: { id: string; + tenantId: string; userId: string; conversationId: string | null; originalName: string; @@ -58,6 +60,7 @@ export class FileEntity { static create(props: { id: string; + tenantId?: string; userId: string; conversationId?: string; originalName: string; @@ -70,6 +73,7 @@ export class FileEntity { const now = new Date(); return new FileEntity({ id: props.id, + tenantId: props.tenantId || '', userId: props.userId, conversationId: props.conversationId || null, originalName: props.originalName, @@ -90,6 +94,7 @@ export class FileEntity { static fromPersistence(props: { id: string; + tenantId: string; userId: string; conversationId: string | null; originalName: string; diff --git a/scripts/migrations/20260207_add_tenant_id_to_files.sql b/scripts/migrations/20260207_add_tenant_id_to_files.sql new file mode 100644 index 0000000..b77c523 --- /dev/null +++ b/scripts/migrations/20260207_add_tenant_id_to_files.sql @@ -0,0 +1,46 @@ +-- =========================================== +-- 迁移: file-service files 表添加 tenant_id +-- 日期: 2026-02-07 +-- =========================================== + +-- 默认租户 ID +DO $$ +DECLARE + default_tenant UUID := '00000000-0000-0000-0000-000000000001'; +BEGIN + -- 1. 添加 tenant_id 列(如果不存在) + IF NOT EXISTS ( + SELECT 1 FROM information_schema.columns + WHERE table_name = 'files' AND column_name = 'tenant_id' + ) THEN + ALTER TABLE files ADD COLUMN tenant_id UUID NOT NULL DEFAULT default_tenant; + RAISE NOTICE 'Added tenant_id column to files table'; + ELSE + RAISE NOTICE 'tenant_id column already exists in files table'; + END IF; + + -- 2. 回填现有记录 + UPDATE files SET tenant_id = default_tenant WHERE tenant_id IS NULL; + + -- 3. 创建索引(如果不存在) + IF NOT EXISTS ( + SELECT 1 FROM pg_indexes WHERE indexname = 'idx_files_tenant_id' + ) THEN + CREATE INDEX idx_files_tenant_id ON files(tenant_id); + RAISE NOTICE 'Created index idx_files_tenant_id'; + END IF; + + IF NOT EXISTS ( + SELECT 1 FROM pg_indexes WHERE indexname = 'idx_files_tenant' + ) THEN + CREATE INDEX idx_files_tenant ON files(tenant_id); + RAISE NOTICE 'Created index idx_files_tenant'; + END IF; + + IF NOT EXISTS ( + SELECT 1 FROM pg_indexes WHERE indexname = 'idx_files_tenant_user' + ) THEN + CREATE INDEX idx_files_tenant_user ON files(tenant_id, user_id); + RAISE NOTICE 'Created index idx_files_tenant_user'; + END IF; +END $$;