Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.
TypeScript acmeagentsai-agentscertificate-managementdevsecopsenvironment-variableskubernetesopen-sourcepampkipostgresprivate-caprivileged-access-managementsecret-managementsecret-managersecretssecrets-managementsecuritysecurity-toolsvault
feat: corrected the migration
=2bac6ea6ad51
diff --git a/backend/src/auto-start-migrations.ts b/backend/src/auto-start-migrations.tsindex 497692e1e5..f531dbdc4a 100644--- a/backend/src/auto-start-migrations.ts+++ b/backend/src/auto-start-migrations.ts@@ -8,12 +8,7 @@ import { Logger } from "pino";import { applyJitter, delay } from "@app/lib/delay";-import {- acquireSanitizedSchemaLock,- createSanitizedSchema,- dropSanitizedSchema,- releaseSanitizedSchemaLock-} from "./db/sanitized-schema";+import { acquireSanitizedSchemaLock, createSanitizedSchema, dropSanitizedSchema } from "./db/sanitized-schema";import { PgSqlLock } from "./keystore/keystore";const SANITIZED_SCHEMA_ERROR = "SANITIZED_SCHEMA_ERROR";@@ -293,23 +288,8 @@ const withStartupLock = async (db: Knex, logger: Logger, doMigrations: () => Proexport const runMigrations = async ({ applicationDb, auditLogDb, logger }: TArgs) => {const generateSanitizedSchema = process.env.GENERATE_SANITIZED_SCHEMA === "true";- let sanitizedLockAcquired = false;try {- // we drop the sanitized schema before migrations to avoid conflicts with the migrations- if (generateSanitizedSchema) {- try {- await acquireSanitizedSchemaLock({ db: applicationDb, logger });- sanitizedLockAcquired = true;- await dropSanitizedSchema({ db: applicationDb, logger });- } catch (err) {- logger.error(- { err, errorId: SANITIZED_SCHEMA_ERROR, phase: "drop" },- `${SANITIZED_SCHEMA_ERROR}: Failed to drop sanitized schema before migrations`- );- }- }-// akhilmhdh(Feb 10 2025): 2 years from now remove thisif (isProduction) {const migrationTable = migrationConfig.tableName;@@ -343,12 +323,18 @@ export const runMigrations = async ({ applicationDb, auditLogDb, logger }: TArgsif (generateSanitizedSchema) {try {- await createSanitizedSchema({ db: applicationDb, logger });- logger.info("Finished sanitized schema generation.");+ await applicationDb.transaction(async (tx) => {+ const isLocked = await acquireSanitizedSchemaLock({ db: tx, logger });+ if (isLocked) {+ await dropSanitizedSchema({ db: tx, logger });+ await createSanitizedSchema({ db: tx, logger });+ logger.info("Finished sanitized schema generation.");+ }+ });} catch (err) {logger.error(- { err, errorId: SANITIZED_SCHEMA_ERROR, phase: "create" },- `${SANITIZED_SCHEMA_ERROR}: Failed to create sanitized schema`+ { err, errorId: SANITIZED_SCHEMA_ERROR, phase: "drop" },+ `${SANITIZED_SCHEMA_ERROR}: Failed to drop and recreate sanitized schema`);}}@@ -385,6 +371,7 @@ export const runMigrations = async ({ applicationDb, auditLogDb, logger }: TArgs// Use startup lock to ensure only one instance runs migrations at a timeawait withStartupLock(applicationDb, logger, async () => {+ if (generateSanitizedSchema) await dropSanitizedSchema({ db: applicationDb, logger });await applicationDb.migrate.latest(migrationConfig);});logger.info("Finished application migrations.");@@ -404,16 +391,5 @@ export const runMigrations = async ({ applicationDb, auditLogDb, logger }: TArgs} catch (err) {logger.error(err, "Boot up migration failed");process.exit(1);- } finally {- if (sanitizedLockAcquired) {- try {- await releaseSanitizedSchemaLock({ db: applicationDb, logger });- } catch (err) {- logger.error(- { err, errorId: SANITIZED_SCHEMA_ERROR, phase: "unlock" },- `${SANITIZED_SCHEMA_ERROR}: Failed to release sanitized schema lock`- );- }- }}};diff --git a/backend/src/db/sanitized-schema.ts b/backend/src/db/sanitized-schema.tsindex 84cdc9c61d..b911f7d1a7 100644--- a/backend/src/db/sanitized-schema.ts+++ b/backend/src/db/sanitized-schema.ts@@ -1,6 +1,5 @@-import path from "path";-import { Knex } from "knex";+import path from "path";import { Logger } from "pino";import { PgSqlLock } from "@app/keystore/keystore";@@ -12,14 +11,15 @@ type TArgs = {logger: Logger;};-export const acquireSanitizedSchemaLock = async ({ db, logger }: TArgs): Promise<void> => {- await db.raw("SELECT pg_advisory_lock(?)", [PgSqlLock.SanitizedSchemaGeneration]);- logger.info("Acquired sanitized schema generation lock");-};--export const releaseSanitizedSchemaLock = async ({ db, logger }: TArgs): Promise<void> => {- await db.raw("SELECT pg_advisory_unlock(?)", [PgSqlLock.SanitizedSchemaGeneration]);- logger.info("Released sanitized schema generation lock");+export const acquireSanitizedSchemaLock = async ({ db, logger }: TArgs): Promise<boolean> => {+ const res = await db.raw<{ rows: { lock_acquired: boolean }[] }>(+ "SELECT pg_try_advisory_xact_lock(?) as lock_acquired",+ [PgSqlLock.SanitizedSchemaGeneration]+ );+ if (res?.rows[0]?.lock_acquired) {+ logger.info("Acquired sanitized schema generation lock");+ }+ return res?.rows[0]?.lock_acquired;};export const dropSanitizedSchema = async ({ db, logger }: TArgs): Promise<void> => {