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:
parent
6592e72758
commit
cf2fd07ead
|
|
@ -93,6 +93,7 @@ export class FilePostgresRepository
|
||||||
private toEntity(orm: FileORM): FileEntity {
|
private toEntity(orm: FileORM): FileEntity {
|
||||||
return FileEntity.fromPersistence({
|
return FileEntity.fromPersistence({
|
||||||
id: orm.id,
|
id: orm.id,
|
||||||
|
tenantId: orm.tenantId,
|
||||||
userId: orm.userId,
|
userId: orm.userId,
|
||||||
conversationId: orm.conversationId,
|
conversationId: orm.conversationId,
|
||||||
originalName: orm.originalName,
|
originalName: orm.originalName,
|
||||||
|
|
|
||||||
|
|
@ -19,6 +19,7 @@ export enum FileStatus {
|
||||||
*/
|
*/
|
||||||
export class FileEntity {
|
export class FileEntity {
|
||||||
readonly id: string;
|
readonly id: string;
|
||||||
|
readonly tenantId: string;
|
||||||
readonly userId: string;
|
readonly userId: string;
|
||||||
conversationId: string | null;
|
conversationId: string | null;
|
||||||
originalName: string;
|
originalName: string;
|
||||||
|
|
@ -37,6 +38,7 @@ export class FileEntity {
|
||||||
|
|
||||||
private constructor(props: {
|
private constructor(props: {
|
||||||
id: string;
|
id: string;
|
||||||
|
tenantId: string;
|
||||||
userId: string;
|
userId: string;
|
||||||
conversationId: string | null;
|
conversationId: string | null;
|
||||||
originalName: string;
|
originalName: string;
|
||||||
|
|
@ -58,6 +60,7 @@ export class FileEntity {
|
||||||
|
|
||||||
static create(props: {
|
static create(props: {
|
||||||
id: string;
|
id: string;
|
||||||
|
tenantId?: string;
|
||||||
userId: string;
|
userId: string;
|
||||||
conversationId?: string;
|
conversationId?: string;
|
||||||
originalName: string;
|
originalName: string;
|
||||||
|
|
@ -70,6 +73,7 @@ export class FileEntity {
|
||||||
const now = new Date();
|
const now = new Date();
|
||||||
return new FileEntity({
|
return new FileEntity({
|
||||||
id: props.id,
|
id: props.id,
|
||||||
|
tenantId: props.tenantId || '',
|
||||||
userId: props.userId,
|
userId: props.userId,
|
||||||
conversationId: props.conversationId || null,
|
conversationId: props.conversationId || null,
|
||||||
originalName: props.originalName,
|
originalName: props.originalName,
|
||||||
|
|
@ -90,6 +94,7 @@ export class FileEntity {
|
||||||
|
|
||||||
static fromPersistence(props: {
|
static fromPersistence(props: {
|
||||||
id: string;
|
id: string;
|
||||||
|
tenantId: string;
|
||||||
userId: string;
|
userId: string;
|
||||||
conversationId: string | null;
|
conversationId: string | null;
|
||||||
originalName: string;
|
originalName: string;
|
||||||
|
|
|
||||||
|
|
@ -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 $$;
|
||||||
Loading…
Reference in New Issue