mirror of
https://github.com/awatertrevi/infisical.git
synced 2026-09-22 13:39:35 +00:00
Merge pull request #4719 from Infisical/fix/sub-scope-migration
feat: resolved locking of membership table
This commit is contained in:
@@ -2,7 +2,7 @@ import { Knex } from "knex";
|
||||
|
||||
import { dropConstraintIfExists } from "@app/db/migrations/utils/dropConstraintIfExists";
|
||||
|
||||
import { AccessScope, TableName } from "../schemas";
|
||||
import { TableName } from "../schemas";
|
||||
|
||||
export async function up(knex: Knex): Promise<void> {
|
||||
const hasParentOrgId = await knex.schema.hasColumn(TableName.Organization, "parentOrgId");
|
||||
@@ -18,8 +18,6 @@ export async function up(knex: Knex): Promise<void> {
|
||||
await dropConstraintIfExists(TableName.Organization, "organizations_slug_unique", knex);
|
||||
t.unique(["rootOrgId", "parentOrgId", "slug"]);
|
||||
});
|
||||
|
||||
// had to switch to raw for null not distinct
|
||||
}
|
||||
|
||||
const hasIdentityOrgCol = await knex.schema.hasColumn(TableName.Identity, "orgId");
|
||||
@@ -28,24 +26,6 @@ export async function up(knex: Knex): Promise<void> {
|
||||
t.uuid("orgId");
|
||||
t.foreign("orgId").references("id").inTable(TableName.Organization).onDelete("CASCADE");
|
||||
});
|
||||
|
||||
await knex.raw(
|
||||
`
|
||||
UPDATE ?? AS identity
|
||||
SET "orgId" = membership."scopeOrgId"
|
||||
FROM ?? AS membership
|
||||
WHERE
|
||||
membership."actorIdentityId" = identity."id"
|
||||
AND membership."scope" = ?
|
||||
`,
|
||||
[TableName.Identity, TableName.Membership, AccessScope.Organization]
|
||||
);
|
||||
|
||||
await knex.raw(`DELETE FROM ?? WHERE "orgId" IS NULL`, [TableName.Identity]);
|
||||
|
||||
await knex.schema.alterTable(TableName.Identity, (t) => {
|
||||
t.uuid("orgId").notNullable().alter();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
import { Knex } from "knex";
|
||||
|
||||
import { chunkArray } from "@app/lib/fn";
|
||||
|
||||
import { AccessScope, TableName } from "../schemas";
|
||||
|
||||
export async function up(knex: Knex): Promise<void> {
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
export async function down(): Promise<void> {}
|
||||
|
||||
const config = { transaction: false };
|
||||
export { config };
|
||||
@@ -0,0 +1,22 @@
|
||||
import { Knex } from "knex";
|
||||
|
||||
import { TableName } from "../schemas";
|
||||
|
||||
export async function up(knex: Knex): Promise<void> {
|
||||
await knex.transaction(async (tx) => {
|
||||
await tx.schema.alterTable(TableName.IdentityAccessToken, (table) => {
|
||||
table.dropForeign("identityId");
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
export async function down(knex: Knex): Promise<void> {
|
||||
await knex.transaction(async (tx) => {
|
||||
await tx.schema.alterTable(TableName.IdentityAccessToken, (table) => {
|
||||
table.foreign("identityId").references("id").inTable(TableName.Identity);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
const config = { transaction: false };
|
||||
export { config };
|
||||
@@ -0,0 +1,30 @@
|
||||
import { Knex } from "knex";
|
||||
|
||||
import { TableName } from "../schemas";
|
||||
|
||||
const MIGRATION_TIMEOUT = 30 * 60 * 1000; // 30 minutes
|
||||
|
||||
export async function up(knex: Knex): Promise<void> {
|
||||
const result = await knex.raw("SHOW statement_timeout");
|
||||
const originalTimeout = result.rows[0].statement_timeout;
|
||||
|
||||
await knex.transaction(async (tx) => {
|
||||
try {
|
||||
await tx.raw(`SET statement_timeout = ${MIGRATION_TIMEOUT}`);
|
||||
const hasIdentityOrgCol = await tx.schema.hasColumn(TableName.Identity, "orgId");
|
||||
if (hasIdentityOrgCol) {
|
||||
await tx(TableName.Identity).whereNull("orgId").delete();
|
||||
await tx.schema.alterTable(TableName.Identity, (t) => {
|
||||
t.uuid("orgId").notNullable().alter();
|
||||
});
|
||||
}
|
||||
} finally {
|
||||
await tx.raw(`SET statement_timeout = '${originalTimeout}'`);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
export async function down(): Promise<void> {}
|
||||
|
||||
const config = { transaction: false };
|
||||
export { config };
|
||||
Reference in New Issue
Block a user