From 0091960a61dd12cf88f5dad5ea5ef822554a39c0 Mon Sep 17 00:00:00 2001 From: = Date: Sat, 14 Dec 2024 00:58:58 +0530 Subject: [PATCH] feat: updated rollback --- ...241213122350_project-split-to-products.ts} | 53 ++++++++++++++++++- 1 file changed, 51 insertions(+), 2 deletions(-) rename backend/src/db/migrations/{20241205160300_project-split-to-products.ts => 20241213122350_project-split-to-products.ts} (83%) diff --git a/backend/src/db/migrations/20241205160300_project-split-to-products.ts b/backend/src/db/migrations/20241213122350_project-split-to-products.ts similarity index 83% rename from backend/src/db/migrations/20241205160300_project-split-to-products.ts rename to backend/src/db/migrations/20241213122350_project-split-to-products.ts index 13d08abf22..448e1a2d41 100644 --- a/backend/src/db/migrations/20241205160300_project-split-to-products.ts +++ b/backend/src/db/migrations/20241213122350_project-split-to-products.ts @@ -226,13 +226,62 @@ export async function up(knex: Knex): Promise { export async function down(knex: Knex): Promise { 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); }