From 555f900172858d5e593d11e15a6710967b71453d Mon Sep 17 00:00:00 2001 From: = Date: Fri, 24 Oct 2025 21:55:25 +0530 Subject: [PATCH] feat: added transaction --- ...0251019061215_sub-org-identity-backfill.ts | 70 ++++++++++--------- 1 file changed, 36 insertions(+), 34 deletions(-) diff --git a/backend/src/db/migrations/20251019061215_sub-org-identity-backfill.ts b/backend/src/db/migrations/20251019061215_sub-org-identity-backfill.ts index 12aa38edc..71e8924d4 100644 --- a/backend/src/db/migrations/20251019061215_sub-org-identity-backfill.ts +++ b/backend/src/db/migrations/20251019061215_sub-org-identity-backfill.ts @@ -5,44 +5,46 @@ import { chunkArray } from "@app/lib/fn"; import { AccessScope, TableName } from "../schemas"; export async function up(knex: Knex): Promise { - const hasIdentityOrgCol = await knex.schema.hasColumn(TableName.Identity, "orgId"); - if (hasIdentityOrgCol) { - const identityMemberships = await knex(TableName.Membership) - .where({ - scope: AccessScope.Organization - }) - .whereNotNull("actorIdentityId") - .select("actorIdentityId", "scopeOrgId"); + await knex.transaction(async (tx) => { + const hasIdentityOrgCol = await tx.schema.hasColumn(TableName.Identity, "orgId"); + if (hasIdentityOrgCol) { + const identityMemberships = await tx(TableName.Membership) + .where({ + scope: AccessScope.Organization + }) + .whereNotNull("actorIdentityId") + .select("actorIdentityId", "scopeOrgId"); - const identityToOrgMapping: Record = {}; - identityMemberships.forEach((el) => { - if (el.actorIdentityId) { - identityToOrgMapping[el.actorIdentityId] = el.scopeOrgId; - } - }); + const identityToOrgMapping: Record = {}; + identityMemberships.forEach((el) => { + if (el.actorIdentityId) { + identityToOrgMapping[el.actorIdentityId] = el.scopeOrgId; + } + }); - const batchMemberships = chunkArray(identityMemberships, 500); - for await (const membership of batchMemberships) { - const identityIds = membership.map((el) => el.actorIdentityId).filter(Boolean) as string[]; - if (identityIds.length) { - const identities = await knex(TableName.Identity).whereIn("id", identityIds).select("*"); - await knex(TableName.Identity) - .insert( - identities.map((el) => ({ - ...el, - orgId: identityToOrgMapping[el.id] - })) - ) - .onConflict("id") - .merge(); + const batchMemberships = chunkArray(identityMemberships, 500); + for await (const membership of batchMemberships) { + const identityIds = membership.map((el) => el.actorIdentityId).filter(Boolean) as string[]; + if (identityIds.length) { + const identities = await tx(TableName.Identity).whereIn("id", identityIds).select("*"); + await tx(TableName.Identity) + .insert( + identities.map((el) => ({ + ...el, + orgId: identityToOrgMapping[el.id] + })) + ) + .onConflict("id") + .merge(); + } } + + await tx(TableName.Identity).whereNull("orgId").delete(); + await tx.schema.alterTable(TableName.Identity, (t) => { + t.uuid("orgId").notNullable().alter(); + }); } - - await knex(TableName.Identity).whereNull("orgId").delete(); - await knex.schema.alterTable(TableName.Identity, (t) => { - t.uuid("orgId").notNullable().alter(); - }); - } + }); } export async function down(): Promise {}