feat: added transaction

This commit is contained in:
=
2025-10-24 21:55:25 +05:30
parent 1d2708afe9
commit 555f900172
@@ -5,9 +5,10 @@ import { chunkArray } from "@app/lib/fn";
import { AccessScope, TableName } from "../schemas"; import { AccessScope, TableName } from "../schemas";
export async function up(knex: Knex): Promise<void> { export async function up(knex: Knex): Promise<void> {
const hasIdentityOrgCol = await knex.schema.hasColumn(TableName.Identity, "orgId"); await knex.transaction(async (tx) => {
const hasIdentityOrgCol = await tx.schema.hasColumn(TableName.Identity, "orgId");
if (hasIdentityOrgCol) { if (hasIdentityOrgCol) {
const identityMemberships = await knex(TableName.Membership) const identityMemberships = await tx(TableName.Membership)
.where({ .where({
scope: AccessScope.Organization scope: AccessScope.Organization
}) })
@@ -25,8 +26,8 @@ export async function up(knex: Knex): Promise<void> {
for await (const membership of batchMemberships) { for await (const membership of batchMemberships) {
const identityIds = membership.map((el) => el.actorIdentityId).filter(Boolean) as string[]; const identityIds = membership.map((el) => el.actorIdentityId).filter(Boolean) as string[];
if (identityIds.length) { if (identityIds.length) {
const identities = await knex(TableName.Identity).whereIn("id", identityIds).select("*"); const identities = await tx(TableName.Identity).whereIn("id", identityIds).select("*");
await knex(TableName.Identity) await tx(TableName.Identity)
.insert( .insert(
identities.map((el) => ({ identities.map((el) => ({
...el, ...el,
@@ -38,11 +39,12 @@ export async function up(knex: Knex): Promise<void> {
} }
} }
await knex(TableName.Identity).whereNull("orgId").delete(); await tx(TableName.Identity).whereNull("orgId").delete();
await knex.schema.alterTable(TableName.Identity, (t) => { await tx.schema.alterTable(TableName.Identity, (t) => {
t.uuid("orgId").notNullable().alter(); t.uuid("orgId").notNullable().alter();
}); });
} }
});
} }
export async function down(): Promise<void> {} export async function down(): Promise<void> {}