feat: enhance agent permissions migration with DocumentDB compatibility and add dry-run script

This commit is contained in:
Atef Bellaaj 2025-06-18 14:37:51 +02:00 committed by Danny Avila
parent c62a23fafc
commit f9994d1547
No known key found for this signature in database
GPG key ID: BF31EEB2C5CA0956
2 changed files with 23 additions and 16 deletions

View file

@ -32,32 +32,36 @@ async function migrateAgentPermissionsEnhanced({ dryRun = true, batchSize = 100
logger.info(`Found ${globalAgentIds.size} agents in global project`); logger.info(`Found ${globalAgentIds.size} agents in global project`);
// Find agents without ACL entries // Find agents without ACL entries using DocumentDB-compatible approach
const agentsToMigrate = await Agent.aggregate([ const agentsToMigrate = await Agent.aggregate([
{ {
$lookup: { $lookup: {
from: 'aclentries', from: 'aclentries',
let: { agentId: '$_id' }, localField: '_id',
pipeline: [ foreignField: 'resourceId',
{
$match: {
$expr: {
$and: [
{ $eq: ['$resourceType', 'agent'] },
{ $eq: ['$resourceId', '$$agentId'] },
{ $eq: ['$principalType', 'user'] },
],
},
},
},
],
as: 'aclEntries', as: 'aclEntries',
}, },
}, },
{
$addFields: {
userAclEntries: {
$filter: {
input: '$aclEntries',
as: 'aclEntry',
cond: {
$and: [
{ $eq: ['$$aclEntry.resourceType', 'agent'] },
{ $eq: ['$$aclEntry.principalType', 'user'] },
],
},
},
},
},
},
{ {
$match: { $match: {
author: { $exists: true, $ne: null }, author: { $exists: true, $ne: null },
$expr: { $eq: [{ $size: '$aclEntries' }, 0] }, userAclEntries: { $size: 0 },
}, },
}, },
{ {

View file

@ -71,7 +71,10 @@
"b:test:api": "cd api && bun run b:test", "b:test:api": "cd api && bun run b:test",
"b:balance": "bun config/add-balance.js", "b:balance": "bun config/add-balance.js",
"b:list-balances": "bun config/list-balances.js", "b:list-balances": "bun config/list-balances.js",
"reset-terms": "node config/reset-terms.js" "reset-terms": "node config/reset-terms.js",
"migrate:agent-permissions:dry-run": "node config/migrate-agent-permissions.js --dry-run",
"migrate:agent-permissions": "node config/migrate-agent-permissions.js",
"migrate:agent-permissions:batch": "node config/migrate-agent-permissions.js --batch-size=50"
}, },
"repository": { "repository": {
"type": "git", "type": "git",