mirror of
https://github.com/awatertrevi/infisical.git
synced 2026-10-10 21:29:20 +00:00
feat: seperated into two migration
This commit is contained in:
@@ -1,9 +1,8 @@
|
|||||||
import { Knex } from "knex";
|
import { Knex } from "knex";
|
||||||
|
|
||||||
import { dropConstraintIfExists } from "@app/db/migrations/utils/dropConstraintIfExists";
|
import { dropConstraintIfExists } from "@app/db/migrations/utils/dropConstraintIfExists";
|
||||||
import { chunkArray } from "@app/lib/fn";
|
|
||||||
|
|
||||||
import { AccessScope, TableName } from "../schemas";
|
import { TableName } from "../schemas";
|
||||||
|
|
||||||
export async function up(knex: Knex): Promise<void> {
|
export async function up(knex: Knex): Promise<void> {
|
||||||
const hasParentOrgId = await knex.schema.hasColumn(TableName.Organization, "parentOrgId");
|
const hasParentOrgId = await knex.schema.hasColumn(TableName.Organization, "parentOrgId");
|
||||||
@@ -19,8 +18,6 @@ export async function up(knex: Knex): Promise<void> {
|
|||||||
await dropConstraintIfExists(TableName.Organization, "organizations_slug_unique", knex);
|
await dropConstraintIfExists(TableName.Organization, "organizations_slug_unique", knex);
|
||||||
t.unique(["rootOrgId", "parentOrgId", "slug"]);
|
t.unique(["rootOrgId", "parentOrgId", "slug"]);
|
||||||
});
|
});
|
||||||
|
|
||||||
// had to switch to raw for null not distinct
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const hasIdentityOrgCol = await knex.schema.hasColumn(TableName.Identity, "orgId");
|
const hasIdentityOrgCol = await knex.schema.hasColumn(TableName.Identity, "orgId");
|
||||||
@@ -29,43 +26,6 @@ export async function up(knex: Knex): Promise<void> {
|
|||||||
t.uuid("orgId");
|
t.uuid("orgId");
|
||||||
t.foreign("orgId").references("id").inTable(TableName.Organization).onDelete("CASCADE");
|
t.foreign("orgId").references("id").inTable(TableName.Organization).onDelete("CASCADE");
|
||||||
});
|
});
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
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();
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,51 @@
|
|||||||
|
import { Knex } from "knex";
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
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();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function down(): Promise<void> {}
|
||||||
|
|
||||||
|
const config = { transaction: false };
|
||||||
|
export { config };
|
||||||
Reference in New Issue
Block a user