feat: Add migration to assign default incident roles to existing projects

This commit is contained in:
Nawaz Dhandala
2026-01-29 21:41:30 +00:00
parent 35deea863b
commit 33fdabaea3
3 changed files with 57 additions and 1 deletions

View File

@@ -935,7 +935,7 @@ export class ProjectService extends DatabaseService<Model> {
return createdItem;
}
private async addDefaultIncidentRoles(createdItem: Model): Promise<Model> {
public async addDefaultIncidentRoles(createdItem: Model): Promise<Model> {
let incidentCommander: IncidentRole = new IncidentRole();
incidentCommander.name = "Incident Commander";
incidentCommander.description =

View File

@@ -0,0 +1,54 @@
import DataMigrationBase from "./DataMigrationBase";
import LIMIT_MAX, { LIMIT_INFINITY } from "Common/Types/Database/LimitMax";
import ProjectService from "Common/Server/Services/ProjectService";
import IncidentRoleService from "Common/Server/Services/IncidentRoleService";
import Project from "Common/Models/DatabaseModels/Project";
import IncidentRole from "Common/Models/DatabaseModels/IncidentRole";
export default class AddDefaultIncidentRolesToExistingProjects extends DataMigrationBase {
public constructor() {
super("AddDefaultIncidentRolesToExistingProjects");
}
public override async migrate(): Promise<void> {
// Get all projects
const projects: Array<Project> = await ProjectService.findBy({
query: {},
select: {
_id: true,
},
skip: 0,
limit: LIMIT_INFINITY,
props: {
isRoot: true,
},
});
for (const project of projects) {
// Check if this project already has incident roles
const existingRoles: Array<IncidentRole> =
await IncidentRoleService.findBy({
query: {
projectId: project.id!,
},
select: {
_id: true,
},
skip: 0,
limit: 1,
props: {
isRoot: true,
},
});
// Only add default roles if none exist
if (existingRoles.length === 0) {
await ProjectService.addDefaultIncidentRoles(project);
}
}
}
public override async rollback(): Promise<void> {
return;
}
}

View File

@@ -53,6 +53,7 @@ import AddOnCallNotificationForUsers from "./AddOnCallNotificationForUsers";
import StartOnCallUserTimeLog from "./StartOnCallUserTimeLog";
import LowercaseDomains from "./LowercaseDomains";
import AddAttributeKeysColumnToTelemetryTables from "./AddAttributeKeysColumnToTelemetryTables";
import AddDefaultIncidentRolesToExistingProjects from "./AddDefaultIncidentRolesToExistingProjects";
// This is the order in which the migrations will be run. Add new migrations to the end of the array.
@@ -110,6 +111,7 @@ const DataMigrations: Array<DataMigrationBase> = [
new StartOnCallUserTimeLog(),
new LowercaseDomains(),
new AddAttributeKeysColumnToTelemetryTables(),
new AddDefaultIncidentRolesToExistingProjects(),
];
export default DataMigrations;