-
Notifications
You must be signed in to change notification settings - Fork 232
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add incidentNumber field to Incident model and implement data m…
…igration for existing incidents
- Loading branch information
Showing
6 changed files
with
106 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 17 additions & 10 deletions
27
Common/Server/Infrastructure/Postgres/SchemaMigrations/1736675947746-MigrationName.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,23 @@ | ||
import { MigrationInterface, QueryRunner } from "typeorm"; | ||
|
||
export class MigrationName1736675947746 implements MigrationInterface { | ||
public name = 'MigrationName1736675947746' | ||
public name = "MigrationName1736675947746"; | ||
|
||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query(`ALTER TABLE "Incident" ADD "incidentNumber" integer`); | ||
await queryRunner.query(`CREATE INDEX "IDX_0eca9ce7d12a4c472386dfc781" ON "Incident" ("incidentNumber") `); | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query(`DROP INDEX "public"."IDX_0eca9ce7d12a4c472386dfc781"`); | ||
await queryRunner.query(`ALTER TABLE "Incident" DROP COLUMN "incidentNumber"`); | ||
} | ||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query( | ||
`ALTER TABLE "Incident" ADD "incidentNumber" integer`, | ||
); | ||
await queryRunner.query( | ||
`CREATE INDEX "IDX_0eca9ce7d12a4c472386dfc781" ON "Incident" ("incidentNumber") `, | ||
); | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query( | ||
`DROP INDEX "public"."IDX_0eca9ce7d12a4c472386dfc781"`, | ||
); | ||
await queryRunner.query( | ||
`ALTER TABLE "Incident" DROP COLUMN "incidentNumber"`, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import DataMigrationBase from "./DataMigrationBase"; | ||
import LIMIT_MAX from "Common/Types/Database/LimitMax"; | ||
import ProjectService from "Common/Server/Services/ProjectService"; | ||
import Project from "Common/Models/DatabaseModels/Project"; | ||
import Incident from "Common/Models/DatabaseModels/Incident"; | ||
import IncidentService from "Common/Server/Services/IncidentService"; | ||
import SortOrder from "Common/Types/BaseDatabase/SortOrder"; | ||
|
||
export default class AddIncidentNumber extends DataMigrationBase { | ||
public constructor() { | ||
super("AddIncidentNumber"); | ||
} | ||
|
||
public override async migrate(): Promise<void> { | ||
// get all the users with email isVerified true. | ||
|
||
const projects: Array<Project> = await ProjectService.findBy({ | ||
query: {}, | ||
select: { | ||
_id: true, | ||
}, | ||
skip: 0, | ||
limit: LIMIT_MAX, | ||
props: { | ||
isRoot: true, | ||
}, | ||
}); | ||
|
||
for (const project of projects) { | ||
// add ended scheduled maintenance state for each of these projects. | ||
// first fetch resolved state. Ended state order is -1 of resolved state. | ||
|
||
// get all incicents for this project | ||
const incidents: Array<Incident> = await IncidentService.findBy({ | ||
query: { | ||
projectId: project.id!, | ||
}, | ||
select: { | ||
_id: true, | ||
incidentNumber: true, | ||
}, | ||
skip: 0, | ||
limit: LIMIT_MAX, | ||
sort: { | ||
createdAt: SortOrder.Descending | ||
}, | ||
props: { | ||
isRoot: true, | ||
}, | ||
}); | ||
|
||
const totalIncidentForProject = incidents.length; | ||
let incidentCounter = totalIncidentForProject; // start from the last incident number | ||
|
||
for(const incident of incidents) { | ||
await IncidentService.updateOneById({ | ||
id: incident.id!, | ||
data: { | ||
incidentNumber: incidentCounter, | ||
}, | ||
props: { | ||
isRoot: true, | ||
}, | ||
}); | ||
|
||
incidentCounter = incidentCounter - 1; | ||
} | ||
|
||
} | ||
} | ||
|
||
public override async rollback(): Promise<void> { | ||
return; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters