Skip to content

Commit a95244f

Browse files
committed
feat: add migration script to update agent roles to default 'admin' value based on creation date
#77
1 parent bf4dfed commit a95244f

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { Logger } from "@nestjs/common"
2+
import { InjectConnection, InjectModel } from "@nestjs/mongoose"
3+
import { Connection, Model } from "mongoose"
4+
import { isNumber } from "radash"
5+
6+
export default class AgentsRoles1772806842 {
7+
private readonly logger = new Logger(AgentsRoles1772806842.name)
8+
9+
public constructor(@InjectConnection() private mongo: Connection) {
10+
}
11+
12+
public async up(): Promise<void> {
13+
this.logger.log('AgentsRoles1772806842 up started')
14+
15+
await this._migrateAgentsRolesToDefaultAdminValue()
16+
}
17+
18+
private async _migrateAgentsRolesToDefaultAdminValue(): Promise<void> {
19+
const agents = await this.mongo.collection('agents').find();
20+
21+
for await (const agent of agents) {
22+
let roles = agent.roles;
23+
24+
if (!roles || roles.length === 0 && agent.metadata.createdAt?.getTime() < 1772806842) {
25+
this.logger.log(`Migrating role for agent ${agent._id}`);
26+
27+
28+
this.mongo.collection('agents').updateOne(
29+
{ _id: agent._id },
30+
{
31+
$set: {
32+
roles: ['admin'],
33+
}
34+
},
35+
);
36+
}
37+
}
38+
39+
this.logger.log('Migration terminée avec succès');
40+
}
41+
}

0 commit comments

Comments
 (0)