feat(backup): support custom backup file name templates and path preview

This commit is contained in:
二丫讲梵 authored and GitHub committed 2026-09-10 00:29:15 +08:00
1 parent 38be5ba35c
commit ce3bd1c8ed
31 files changed
+744 -125

No files matched your search

@@ -1,5 +1,14 @@
import { describe, expect, it } from "vitest";
import { databaseBackupFilePath, databaseBackupRunsToPrune, databaseBackupTableMatchesPattern, normalizeDatabaseBackupRun, resolveScheduledDatabaseBackupTableScope, type DatabaseBackupRun } from "../../backup/scheduledDatabaseBackup";
import {
databaseBackupFileNamePatternIsValid,
databaseBackupFilePath,
sanitizeDatabaseBackupFileSegment,
databaseBackupRunsToPrune,
databaseBackupTableMatchesPattern,
normalizeDatabaseBackupRun,
resolveScheduledDatabaseBackupTableScope,
type DatabaseBackupRun,
} from "../../backup/scheduledDatabaseBackup";
const startedAt = "2026-08-12T00:00:00.000Z";
@@ -23,6 +32,24 @@ describe("database backup run persistence", () => {
expect(databaseBackupFilePath("/backups", "Nightly", "app", startedAt, "run-12345678", "gzip")).toMatch(/\.sql\.gz$/);
});
it("uses a custom file-name template without implicit run ID suffixes", () => {
const path = databaseBackupFilePath("/backups", "Nightly", "app", startedAt, "run-12345678", "none", "before-migration");
expect(path).toBe("/backups/before-migration__app.sql");
});
it("renders all file-name template variables without duplicating suffixes", () => {
const path = databaseBackupFilePath("/backups", "Nightly", "app", startedAt, "run-12345678", "none", "{schedule}__{date}__{database}__{runId}");
expect(path).toBe("/backups/Nightly__20260812__app__run-1234.sql");
});
it("rejects file-name templates that could form a path", () => {
expect(databaseBackupFileNamePatternIsValid("before-migration")).toBe(true);
expect(databaseBackupFileNamePatternIsValid("../before-migration")).toBe(false);
expect(databaseBackupFileNamePatternIsValid("before-migration ")).toBe(false);
});
it("accepts a one-shot run without a schedule id", () => {
const normalized = normalizeDatabaseBackupRun(run());
@@ -36,6 +63,12 @@ describe("database backup run persistence", () => {
);
});
it("keeps the backup destination root in persisted run records", () => {
const normalized = normalizeDatabaseBackupRun(run({ destinationDirectory: "/backups" }));
expect(normalized?.destinationDirectory).toBe("/backups");
});
it("keeps legacy runs compatible and treats them as scheduled history", () => {
const normalized = normalizeDatabaseBackupRun(run({ scheduleId: "schedule-1", source: undefined, scheduleName: "Nightly backup" }));
@@ -70,3 +103,16 @@ describe("database backup table pattern matching", () => {
expect(scope.excludedTables).toEqual(["vector_data"]);
});
});
describe("database backup file name segment sanitization", () => {
it("appends an underscore to Windows reserved device names", () => {
for (const reserved of ["con", "PRN", "Aux", "nul", "com3", "LPT9"]) {
expect(sanitizeDatabaseBackupFileSegment(reserved)).toBe(`${reserved}_`);
}
});
it("keeps ordinary segment names unchanged", () => {
expect(sanitizeDatabaseBackupFileSegment("nightly-full")).toBe("nightly-full");
expect(sanitizeDatabaseBackupFileSegment("con_backup")).toBe("con_backup");
});
});