Skip to content

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

Update 20260807120000_add-org-scope-to-audit-reports.ts

varonix5037aff9ab51
diff --git a/backend/src/db/migrations/20260807120000_add-org-scope-to-audit-reports.ts b/backend/src/db/migrations/20260807120000_add-org-scope-to-audit-reports.ts
index aaa1f76e80..f84fe686ee 100644
--- a/backend/src/db/migrations/20260807120000_add-org-scope-to-audit-reports.ts
+++ b/backend/src/db/migrations/20260807120000_add-org-scope-to-audit-reports.ts
@@ -2,36 +2,35 @@ import { Knex } from "knex";
import { TableName } from "@app/db/schemas";
-// Audit reports gain an org scope: a report row is either project-scoped (projectId set) or
-// org-scoped (orgId set), never both. Raw SQL is used for the NOT NULL toggle so the existing
-// projectId FK and index are left untouched.
export async function up(knex: Knex): Promise<void> {
const hasOrgId = await knex.schema.hasColumn(TableName.AuditReport, "orgId");
- if (!hasOrgId) {
+ const hasTable = await knex.schema.hasTable(TableName.AuditReport);
+ if (hasTable && !hasOrgId) {
await knex.raw(`ALTER TABLE "${TableName.AuditReport}" ALTER COLUMN "projectId" DROP NOT NULL`);
await knex.schema.alterTable(TableName.AuditReport, (t) => {
t.uuid("orgId").nullable();
t.foreign("orgId").references("id").inTable(TableName.Organization).onDelete("CASCADE");
t.index(["orgId"]);
- });
- await knex.raw(
- `ALTER TABLE "${TableName.AuditReport}" ADD CONSTRAINT "audit_reports_one_scope_check"
- CHECK ((("projectId" IS NOT NULL)::int + ("orgId" IS NOT NULL)::int) = 1)`
- );
+ t.check(
+ '("projectId" IS NOT NULL AND "orgId" IS NULL) OR ("projectId" IS NULL AND "orgId" IS NOT NULL)',
+ [],
+ "audit_reports_one_scope_check"
+ );
+ });
}
}
export async function down(knex: Knex): Promise<void> {
const hasOrgId = await knex.schema.hasColumn(TableName.AuditReport, "orgId");
- if (hasOrgId) {
- await knex.raw(`ALTER TABLE "${TableName.AuditReport}" DROP CONSTRAINT IF EXISTS "audit_reports_one_scope_check"`);
-
+ const hasTable = await knex.schema.hasTable(TableName.AuditReport);
+ if (hasTable && hasOrgId) {
// Org-scoped rows cannot survive the NOT NULL restore on projectId.
await knex(TableName.AuditReport).whereNull("projectId").delete();
await knex.schema.alterTable(TableName.AuditReport, (t) => {
+ t.dropChecks("audit_reports_one_scope_check");
t.dropColumn("orgId");
});