mirror of
https://github.com/awatertrevi/infisical.git
synced 2026-10-08 11:27:47 +00:00
feat: added transaction
This commit is contained in:
@@ -5,44 +5,46 @@ 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) => {
|
||||||
if (hasIdentityOrgCol) {
|
const hasIdentityOrgCol = await tx.schema.hasColumn(TableName.Identity, "orgId");
|
||||||
const identityMemberships = await knex(TableName.Membership)
|
if (hasIdentityOrgCol) {
|
||||||
.where({
|
const identityMemberships = await tx(TableName.Membership)
|
||||||
scope: AccessScope.Organization
|
.where({
|
||||||
})
|
scope: AccessScope.Organization
|
||||||
.whereNotNull("actorIdentityId")
|
})
|
||||||
.select("actorIdentityId", "scopeOrgId");
|
.whereNotNull("actorIdentityId")
|
||||||
|
.select("actorIdentityId", "scopeOrgId");
|
||||||
|
|
||||||
const identityToOrgMapping: Record<string, string> = {};
|
const identityToOrgMapping: Record<string, string> = {};
|
||||||
identityMemberships.forEach((el) => {
|
identityMemberships.forEach((el) => {
|
||||||
if (el.actorIdentityId) {
|
if (el.actorIdentityId) {
|
||||||
identityToOrgMapping[el.actorIdentityId] = el.scopeOrgId;
|
identityToOrgMapping[el.actorIdentityId] = el.scopeOrgId;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
const batchMemberships = chunkArray(identityMemberships, 500);
|
const batchMemberships = chunkArray(identityMemberships, 500);
|
||||||
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,
|
||||||
orgId: identityToOrgMapping[el.id]
|
orgId: identityToOrgMapping[el.id]
|
||||||
}))
|
}))
|
||||||
)
|
)
|
||||||
.onConflict("id")
|
.onConflict("id")
|
||||||
.merge();
|
.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<void> {}
|
export async function down(): Promise<void> {}
|
||||||
|
|||||||
Reference in New Issue
Block a user