import { PrismaClient } from '@prisma/client';
const prisma = new PrismaClient();
async function main() {
console.log('Starting Execution agentId backfill...');
const executions = await prisma.execution.findMany({
where: {
agentName: { not: null },
agentId: null
}
});
console.log(`Found ${executions.length} execution records to update.`);
let updatedCount = 0;
for (const exec of executions) {
if (!exec.agentName || !exec.framework) {
continue;
}
const platform = exec.framework;
const name = exec.agentName;
const user = exec.user || null;
try {
let agent = await prisma.registeredAgent.findFirst({
where: {
platform,
name,
user
}
});
if (!agent) {
const agentTypeToUse = (exec as any).agentType || 'main';
agent = await prisma.registeredAgent.create({
data: {
platform,
name,
user,
agentOwnership: 'unregistered',
agentType: agentTypeToUse
}
});
console.log(`Created new unregistered agent: ${name} (${platform}) for user ${user} with type ${agentTypeToUse}`);
}
await prisma.execution.update({
where: { id: exec.id },
data: { agentId: agent.id }
});
updatedCount++;
} catch (e) {
console.error(`Failed to process execution ${exec.id}:`, e);
}
}
console.log(`Finished! Successfully updated ${updatedCount} execution records.`);
}
main()
.catch((e) => {
console.error(e);
process.exit(1);
})
.finally(async () => {
await prisma.$disconnect();
});