Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

feat: added transaction

=555f90017285
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 12aa38edc2..71e8924d49 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<void> {
- 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");
-
- const identityToOrgMapping: Record<string, string> = {};
- identityMemberships.forEach((el) => {
- if (el.actorIdentityId) {
- identityToOrgMapping[el.actorIdentityId] = el.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<string, string> = {};
+ 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 tx(TableName.Identity).whereIn("id", identityIds).select("*");
+ await tx(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 knex(TableName.Identity).whereIn("id", identityIds).select("*");
- await knex(TableName.Identity)
- .insert(
- identities.map((el) => ({
- ...el,
- orgId: identityToOrgMapping[el.id]
- }))
- )
- .onConflict("id")
- .merge();
- }
- }
- await knex(TableName.Identity).whereNull("orgId").delete();
- await knex.schema.alterTable(TableName.Identity, (t) => {
- t.uuid("orgId").notNullable().alter();
- });
- }
+ await tx(TableName.Identity).whereNull("orgId").delete();
+ await tx.schema.alterTable(TableName.Identity, (t) => {
+ t.uuid("orgId").notNullable().alter();
+ });
+ }
+ });
}
export async function down(): Promise<void> {}