fix: run custom migrations on backup restore

This commit is contained in:
Thomas Camlong committed 2026-08-16 22:25:15 +02:00
1 parent 7e569bff4c
commit 199f6ffc3b
2 files changed
+11 -6

No files matched your search

+10 -6
View File
@@ -4,7 +4,7 @@ import path from "path";
import { NextResponse } from "next/server"; import { NextResponse } from "next/server";
import AdmZip from "adm-zip"; import AdmZip from "adm-zip";
import Database from "better-sqlite3"; import BetterSqlite3 from "better-sqlite3";
import { drizzle } from "drizzle-orm/better-sqlite3"; import { drizzle } from "drizzle-orm/better-sqlite3";
import { migrate } from "drizzle-orm/better-sqlite3/migrator"; import { migrate } from "drizzle-orm/better-sqlite3/migrator";
@@ -18,6 +18,9 @@ import { env } from "@homarr/common/env";
import { DB_CASING } from "@homarr/core/infrastructure/db/constants"; import { DB_CASING } from "@homarr/core/infrastructure/db/constants";
import { dbEnv } from "@homarr/core/infrastructure/db/env"; import { dbEnv } from "@homarr/core/infrastructure/db/env";
import { db } from "@homarr/db"; import { db } from "@homarr/db";
import type { Database } from "@homarr/db";
import { applyCustomMigrationsAsync } from "@homarr/db/migrations/custom";
import { schema } from "@homarr/db/schema";
import { findMigrationsFolder } from "../shared"; import { findMigrationsFolder } from "../shared";
@@ -120,7 +123,7 @@ const assertArchiveSizeLimits = (zip: AdmZip) => {
} }
}; };
const reEncryptSecrets = (tempDb: InstanceType<typeof Database>, importedKeyHex: string) => { const reEncryptSecrets = (tempDb: InstanceType<typeof BetterSqlite3>, importedKeyHex: string) => {
const currentKeyHex = env.SECRET_ENCRYPTION_KEY; const currentKeyHex = env.SECRET_ENCRYPTION_KEY;
if (importedKeyHex === currentKeyHex) return; if (importedKeyHex === currentKeyHex) return;
@@ -172,7 +175,7 @@ const reEncryptSecrets = (tempDb: InstanceType<typeof Database>, importedKeyHex:
transaction(); transaction();
}; };
const getHomeBoardName = (tempDb: InstanceType<typeof Database>): string | null => { const getHomeBoardName = (tempDb: InstanceType<typeof BetterSqlite3>): string | null => {
const row = tempDb const row = tempDb
.prepare( .prepare(
`SELECT "name" FROM ( `SELECT "name" FROM (
@@ -234,7 +237,7 @@ export async function POST(req: Request) {
restoreInProgress = true; restoreInProgress = true;
let tempDirectory: string | null = null; let tempDirectory: string | null = null;
let tempDb: InstanceType<typeof Database> | null = null; let tempDb: InstanceType<typeof BetterSqlite3> | null = null;
try { try {
let formData: FormData; let formData: FormData;
@@ -285,9 +288,10 @@ export async function POST(req: Request) {
} }
fs.writeFileSync(tempPath, dbEntry.getData()); fs.writeFileSync(tempPath, dbEntry.getData());
tempDb = new Database(tempPath); tempDb = new BetterSqlite3(tempPath);
const drizzleDb = drizzle(tempDb, { casing: DB_CASING }); const drizzleDb = drizzle(tempDb, { casing: DB_CASING, schema });
migrate(drizzleDb, { migrationsFolder }); migrate(drizzleDb, { migrationsFolder });
await applyCustomMigrationsAsync(drizzleDb as unknown as Database);
const metadataEntry = zip.getEntry("metadata.json"); const metadataEntry = zip.getEntry("metadata.json");
if (!metadataEntry) { if (!metadataEntry) {
+1
View File
@@ -9,6 +9,7 @@
"exports": { "exports": {
".": "./index.ts", ".": "./index.ts",
"./collection": "./collection.ts", "./collection": "./collection.ts",
"./migrations/custom": "./migrations/custom/index.ts",
"./migrations/seed": "./migrations/seed.ts", "./migrations/seed": "./migrations/seed.ts",
"./proxy-reader": "./proxy-reader.ts", "./proxy-reader": "./proxy-reader.ts",
"./schema": "./schema/index.ts", "./schema": "./schema/index.ts",