fix: resolve system user ID in backup restore for projects

The restore service hardcoded ownerId as the literal string 'system'
instead of looking up the actual system user ID. This caused FK
constraint violations when restoring projects to a fresh database.

Now resolves the system user by email, falling back to the first
available user.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Michal
2026-04-08 02:04:32 +01:00
parent 5e45960a18
commit 3663963a32

View File

@@ -270,10 +270,20 @@ export class RestoreService {
continue; continue;
} }
// Resolve a valid owner — prefer system user, fall back to first user
let ownerId = '';
if (this.userRepo) {
const allUsers = await this.userRepo.findAll();
for (const u of allUsers) {
if (u.email === 'system@mcpctl.local') { ownerId = u.id; break; }
if (!ownerId) ownerId = u.id;
}
}
const projectCreateData: { name: string; description: string; ownerId: string; proxyModel?: string; llmProvider?: string; llmModel?: string } = { const projectCreateData: { name: string; description: string; ownerId: string; proxyModel?: string; llmProvider?: string; llmModel?: string } = {
name: project.name, name: project.name,
description: project.description, description: project.description,
ownerId: 'system', ownerId,
}; };
if (project.proxyModel) projectCreateData.proxyModel = project.proxyModel; if (project.proxyModel) projectCreateData.proxyModel = project.proxyModel;
if (project.llmProvider != null) projectCreateData.llmProvider = project.llmProvider; if (project.llmProvider != null) projectCreateData.llmProvider = project.llmProvider;