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
improvement(migrations): make migration history mismatch error actionable
Tre Dubrava94db66620056
diff --git a/backend/src/auto-start-migrations.ts b/backend/src/auto-start-migrations.tsindex 6d0344293e..f5c4e10de6 100644--- a/backend/src/auto-start-migrations.ts+++ b/backend/src/auto-start-migrations.ts@@ -38,6 +38,33 @@ const migrationConfig = {tableName: "infisical_migrations"};+const getImageVersionTag = (): string => {+ const version = getConfig().INFISICAL_PLATFORM_VERSION;+ if (!version) {+ return "development";+ }+ return `v${version}`;+};++const getMigrationDateRange = (migrationNames: string[]): { earliest: string; latest: string } | null => {+ if (!migrationNames.length) {+ return null;+ }+ const timestamps = migrationNames+ .map((name) => name.match(/^(\d{8})/)?.[1])+ .filter((timestamp): timestamp is string => Boolean(timestamp))+ .sort();+ if (!timestamps.length) {+ return null;+ }+ const formatDate = (timestamp: string) =>+ `${timestamp.slice(0, 4)}-${timestamp.slice(4, 6)}-${timestamp.slice(6, 8)}`;+ return {+ earliest: formatDate(timestamps[0]),+ latest: formatDate(timestamps[timestamps.length - 1])+ };+};+const logUnknownAppliedMigrations = ({databaseName,logger,@@ -47,13 +74,31 @@ const logUnknownAppliedMigrations = ({logger: Logger;unknownAppliedMigrationNames: string[];}) => {+ const imageVersion = getImageVersionTag();+ const dateRange = getMigrationDateRange(unknownAppliedMigrationNames);++ const sections = [+ `Database has migrations newer than this image [database=${databaseName}] [imageVersion=${imageVersion}].`,+ `Skipping startup migrations.`,+ `This is expected during rolling deployments when a peer instance on a newer image has already applied them.`+ ];+ if (dateRange) {+ sections.push(+ `The database contains migrations dated up to ${dateRange.latest} that this image does not bundle. If this is not a rolling deployment, verify the intended image version is deployed — an Infisical image released on or after ${dateRange.latest} should include them.`+ );+ } else {+ sections.push(`If this is not a rolling deployment, verify that the intended image version is deployed.`);+ }+logger.warn({databaseName,+ imageVersion,unknownAppliedMigrationCount: unknownAppliedMigrationNames.length,+ unknownAppliedMigrationDateRange: dateRange,unknownAppliedMigrationNames: unknownAppliedMigrationNames.slice(-5)},- `Database has migrations newer than this image [database=${databaseName}]. Skipping startup migrations.`+ sections.join(" "));};@@ -66,13 +111,40 @@ const throwInvalidMigrationHistory = ({pendingMigrationNames: string[];unknownAppliedMigrationNames: string[];}): never => {- throw new Error(- [- `Invalid migration history detected [database=${databaseName}].`,- `Unknown applied migrations: ${unknownAppliedMigrationNames.join(", ") || "none"}.`,- `Pending bundled migrations: ${pendingMigrationNames.join(", ") || "none"}.`- ].join(" ")- );+ const imageVersion = getImageVersionTag();+ const unknownAppliedList = unknownAppliedMigrationNames.join(", ") || "none";+ const pendingList = pendingMigrationNames.join(", ") || "none";+ const unknownDateRange = getMigrationDateRange(unknownAppliedMigrationNames);+ const pendingDateRange = getMigrationDateRange(pendingMigrationNames);++ const sections = [+ `Database migration history does not match this image [database=${databaseName}] [imageVersion=${imageVersion}].`,+ `This usually means the database was migrated by a different (typically newer) Infisical image than the one currently running — for example after a rollback or a mixed-version rollout.`,+ `Unknown applied migrations (in DB but not in this image, count=${unknownAppliedMigrationNames.length}): ${unknownAppliedList}.`,+ `Pending bundled migrations (in this image but not yet applied, count=${pendingMigrationNames.length}): ${pendingList}.`+ ];++ if (unknownDateRange && pendingDateRange) {+ sections.push(+ `Timeline: this image's pending migrations are dated ${pendingDateRange.earliest} to ${pendingDateRange.latest}; the unknown applied migrations in the database are dated ${unknownDateRange.earliest} to ${unknownDateRange.latest}.`+ );+ } else if (unknownDateRange) {+ sections.push(+ `Timeline: the unknown applied migrations in the database are dated ${unknownDateRange.earliest} to ${unknownDateRange.latest}.`+ );+ }++ if (unknownDateRange) {+ sections.push(+ `To resolve: deploy an Infisical image released on or after ${unknownDateRange.latest} so its bundled migrations include the unknown ones above, or restore the database to a state matching this image's migrations.`+ );+ } else {+ sections.push(+ `To resolve: deploy a newer Infisical image whose bundled migrations include the unknown ones above, or restore the database to a state matching this image's migrations.`+ );+ }++ throw new Error(sections.join(" "));};const getLockTableName = (tableName: string): string => {