Skip to content

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

fix(inc-62): auto migration script

Tre Dubravac51a2d006036
diff --git a/backend/src/auto-start-migrations-fns.test.ts b/backend/src/auto-start-migrations-fns.test.ts
new file mode 100644
index 0000000000..f6cae0353a
--- /dev/null
+++ b/backend/src/auto-start-migrations-fns.test.ts
@@ -0,0 +1,60 @@
+import { describe, expect, test } from "vitest";
+
+import { getMigrationBootDirection } from "./auto-start-migrations-fns";
+
+describe("getMigrationBootDirection", () => {
+ test("returns 'current' when applied and bundled match exactly", () => {
+ const result = getMigrationBootDirection({
+ appliedMigrationNames: ["20260101000000_a.mjs", "20260102000000_b.mjs"],
+ bundledMigrationNames: ["20260101000000_a.mjs", "20260102000000_b.mjs"]
+ });
+ expect(result.direction).toBe("current");
+ });
+
+ test("returns 'behind' when the image bundles migrations not yet applied", () => {
+ const result = getMigrationBootDirection({
+ appliedMigrationNames: ["20260101000000_a.mjs"],
+ bundledMigrationNames: ["20260101000000_a.mjs", "20260102000000_b.mjs"]
+ });
+ expect(result.direction).toBe("behind");
+ expect(result.pendingMigrationNames).toEqual(["20260102000000_b.mjs"]);
+ });
+
+ test("returns 'ahead' on a clean rolling deploy (DB has strictly-newer migrations, nothing pending)", () => {
+ const result = getMigrationBootDirection({
+ appliedMigrationNames: ["20260101000000_a.mjs", "20260102000000_b.mjs", "20260103000000_c.mjs"],
+ bundledMigrationNames: ["20260101000000_a.mjs", "20260102000000_b.mjs"]
+ });
+ expect(result.direction).toBe("ahead");
+ expect(result.unknownAppliedMigrationNames).toEqual(["20260103000000_c.mjs"]);
+ });
+
+ // INC-62: a backdated migration merged after later-timestamped ones (common on long-lived branches).
+ // The DB has an unknown migration whose timestamp is OLDER than the image's newest bundled migration,
+ // but the image has nothing pending. Its bundled set is still a strict subset of what's applied, so it
+ // must boot ('ahead'), not fail ('invalid').
+ test("returns 'ahead' for an interleaved/backdated unknown migration when nothing is pending", () => {
+ const result = getMigrationBootDirection({
+ appliedMigrationNames: [
+ "20260101000000_a.mjs",
+ "20260102000000_b.mjs",
+ "20260102120000_backdated.mjs", // older than the image's newest bundled migration
+ "20260103000000_c.mjs"
+ ],
+ bundledMigrationNames: ["20260101000000_a.mjs", "20260102000000_b.mjs", "20260103000000_c.mjs"]
+ });
+ expect(result.direction).toBe("ahead");
+ expect(result.unknownAppliedMigrationNames).toEqual(["20260102120000_backdated.mjs"]);
+ expect(result.pendingMigrationNames).toEqual([]);
+ });
+
+ test("returns 'invalid' only on a true fork (DB has unknown migrations AND the image has pending ones)", () => {
+ const result = getMigrationBootDirection({
+ appliedMigrationNames: ["20260101000000_a.mjs", "20260102000000_db_only.mjs"],
+ bundledMigrationNames: ["20260101000000_a.mjs", "20260103000000_image_only.mjs"]
+ });
+ expect(result.direction).toBe("invalid");
+ expect(result.unknownAppliedMigrationNames).toEqual(["20260102000000_db_only.mjs"]);
+ expect(result.pendingMigrationNames).toEqual(["20260103000000_image_only.mjs"]);
+ });
+});
diff --git a/backend/src/auto-start-migrations-fns.ts b/backend/src/auto-start-migrations-fns.ts
index 9d0a537260..8e7b24b1d9 100644
--- a/backend/src/auto-start-migrations-fns.ts
+++ b/backend/src/auto-start-migrations-fns.ts
@@ -14,8 +14,6 @@ type TMigrationBootDirection = {
pendingMigrationNames: string[];
};
-const getMigrationTimestamp = (migrationName: string) => migrationName.match(/^(\d+)/)?.[1] ?? "";
-
export const getMigrationBootDirection = ({
appliedMigrationNames,
bundledMigrationNames
@@ -34,6 +32,9 @@ export const getMigrationBootDirection = ({
(migrationName) => !appliedMigrationNameSet.has(migrationName)
);
+ // A true fork: the database has migrations this image lacks AND this image has migrations to apply on
+ // top of that diverged history. Applying pending migrations onto a divergent DB is the only genuinely
+ // unsafe case, so this is the one that must fail loud.
if (unknownAppliedMigrationNames.length && pendingMigrationNames.length) {
return {
direction: "invalid",
@@ -42,12 +43,11 @@ export const getMigrationBootDirection = ({
};
}
- const latestBundledMigrationTimestamp = bundledMigrationNames.map(getMigrationTimestamp).sort().at(-1) ?? "";
- const hasOnlyNewerUnknownMigrations = unknownAppliedMigrationNames.every(
- (migrationName) => getMigrationTimestamp(migrationName) > latestBundledMigrationTimestamp
- );
-
- if (unknownAppliedMigrationNames.length && hasOnlyNewerUnknownMigrations) {
+ // The database has migrations this image lacks, but this image has nothing pending to apply — its bundled
+ // migrations are a strict subset of what's already applied. This is safe to skip and boot regardless of
+ // timestamp ordering: backdated / late-merged migrations (common on long-lived branches) leave the history
+ // interleaved-but-compatible, not forked.
+ if (unknownAppliedMigrationNames.length) {
return {
direction: "ahead",
unknownAppliedMigrationNames,
@@ -55,14 +55,6 @@ export const getMigrationBootDirection = ({
};
}
- if (unknownAppliedMigrationNames.length) {
- return {
- direction: "invalid",
- unknownAppliedMigrationNames,
- pendingMigrationNames
- };
- }
-
if (pendingMigrationNames.length) {
return {
direction: "behind",
diff --git a/backend/src/auto-start-migrations.ts b/backend/src/auto-start-migrations.ts
index f5b2d9810f..3f5af076b7 100644
--- a/backend/src/auto-start-migrations.ts
+++ b/backend/src/auto-start-migrations.ts
@@ -78,9 +78,9 @@ const logUnknownAppliedMigrations = ({
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.`
+ `Database has applied migrations that this image does not bundle [database=${databaseName}] [imageVersion=${imageVersion}].`,
+ `This image has nothing pending to apply, so startup migrations are skipped.`,
+ `This is expected during rolling deployments or rollbacks when a peer instance on a different image has already applied them.`
];
if (dateRange) {
sections.push(