Skip to content

Commit

Permalink
feat: updated rollback
Browse files Browse the repository at this point in the history
  • Loading branch information
akhilmhdh committed Dec 13, 2024
1 parent df8c1e5 commit 0091960
Showing 1 changed file with 51 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -226,13 +226,62 @@ export async function up(knex: Knex): Promise<void> {

export async function down(knex: Knex): Promise<void> {
const hasTypeColumn = await knex.schema.hasColumn(TableName.Project, "type");
if (hasTypeColumn) {
const hasSplitMappingTable = await knex.schema.hasTable(TableName.ProjectSplitBackfillIds);

if (hasTypeColumn && hasSplitMappingTable) {
const splitProjecetMappings = await knex(TableName.ProjectSplitBackfillIds).where({});
const certMapping = splitProjecetMappings.filter(
(el) => el.destinationProjectType === ProjectType.CertificateManager
);
/* eslint-disable no-await-in-loop */
for (const project of certMapping) {
await knex(TableName.CertificateAuthority)
.where("projectId", project.destinationProjectId)
.update({ projectId: project.sourceProjectId });
await knex(TableName.PkiAlert)
.where("projectId", project.destinationProjectId)
.update({ projectId: project.sourceProjectId });
await knex(TableName.PkiCollection)
.where("projectId", project.destinationProjectId)
.update({ projectId: project.sourceProjectId });
}

/* eslint-enable */
const kmsMapping = splitProjecetMappings.filter((el) => el.destinationProjectType === ProjectType.KMS);
/* eslint-disable no-await-in-loop */
for (const project of kmsMapping) {
await knex(TableName.KmsKey)
.where({
isReserved: false,
projectId: project.destinationProjectId
})
.update({ projectId: project.sourceProjectId });
}
/* eslint-enable */
await knex(TableName.ProjectMembership)
.whereIn(
"projectId",
splitProjecetMappings.map((el) => el.destinationProjectId)
)
.delete();
await knex(TableName.ProjectRoles)
.whereIn(
"projectId",
splitProjecetMappings.map((el) => el.destinationProjectId)
)
.delete();
await knex(TableName.Project)
.whereIn(
"id",
splitProjecetMappings.map((el) => el.destinationProjectId)
)
.delete();

await knex.schema.alterTable(TableName.Project, (t) => {
t.dropColumn("type");
});
}

const hasSplitMappingTable = await knex.schema.hasTable(TableName.ProjectSplitBackfillIds);
if (hasSplitMappingTable) {
await knex.schema.dropTableIfExists(TableName.ProjectSplitBackfillIds);
}
Expand Down

0 comments on commit 0091960

Please sign in to comment.