fix(file-service): sync tenantId in domain entity and add migration

FileORM had tenant_id column but FileEntity domain class was missing it,
causing "column FileORM.tenant_id does not exist" errors on production.

- Add tenantId to FileEntity (constructor, create, fromPersistence)
- Pass tenantId in repository toEntity() mapping
- Add idempotent migration script for files.tenant_id + indexes

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
hailin 2026-02-07 04:21:43 -08:00
parent 6592e72758
commit cf2fd07ead
3 changed files with 52 additions and 0 deletions

View File

@ -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,

View File

@ -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;

View File

@ -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 $$;