fix: use prisma db push after backup import to sync schema reliably

applyMissingMigrations ran the entire migration SQL as one transaction,
which rolled back all changes when any statement failed (e.g. adding a
NOT NULL column to a non-empty table). Replaced with prisma db push
which compares the schema and applies only what's needed. Also added
support for importing .sql files (not just .zip) in auto-import.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Summit Dental Care
2026-06-25 16:05:18 -04:00
parent f93fef56aa
commit 7deba90db3
3 changed files with 327 additions and 87 deletions

View File

@@ -21,51 +21,29 @@ import { importLatestBackup } from "../services/autoImportService";
const UPLOADS_DIR = path.join(process.cwd(), "uploads");
const MIGRATIONS_DIR = path.resolve(__dirname, "../../../../packages/db/prisma/migrations");
const SCHEMA_PATH = path.resolve(__dirname, "../../../../packages/db/prisma/schema.prisma");
// Applies migration SQL files that are missing from the database.
// Reads each folder in the migrations directory in sorted order, checks
// whether the migration is already recorded as successfully applied in
// _prisma_migrations, and runs the SQL if not. Safe to call after any
// restore because it uses IF NOT EXISTS semantics or checks first.
async function applyMissingMigrations() {
let folders: string[];
try {
folders = fs.readdirSync(MIGRATIONS_DIR)
.filter((name) => fs.statSync(path.join(MIGRATIONS_DIR, name)).isDirectory())
.sort();
} catch {
console.warn("Could not read migrations directory, skipping post-restore migration.");
return;
}
function runDbPush(): Promise<void> {
return new Promise((resolve, reject) => {
const proc = spawn("npx", ["prisma", "db", "push", "--config", path.resolve(__dirname, "../../../../packages/db/prisma/prisma.config.ts"), "--schema", SCHEMA_PATH, "--accept-data-loss"], {
cwd: process.cwd(),
env: process.env,
});
// Fetch the set of successfully-applied migration names from the DB.
let applied: Set<string>;
try {
const rows = await prisma.$queryRaw<{ migration_name: string }[]>`
SELECT migration_name FROM "_prisma_migrations" WHERE finished_at IS NOT NULL
`;
applied = new Set(rows.map((r) => r.migration_name));
} catch {
// _prisma_migrations may not exist in very old backups; proceed anyway.
applied = new Set();
}
for (const folder of folders) {
if (applied.has(folder)) continue;
const sqlFile = path.join(MIGRATIONS_DIR, folder, "migration.sql");
if (!fs.existsSync(sqlFile)) continue;
const sql = fs.readFileSync(sqlFile, "utf8");
try {
await prisma.$executeRawUnsafe(sql);
console.log(`Applied migration: ${folder}`);
} catch (err: any) {
// Log but continue — some statements may already exist (e.g. after a
// partial restore) and that is acceptable.
console.warn(`Migration ${folder} had errors (may already be applied):`, err.message);
}
}
let stderr = "";
proc.stdout.on("data", (d) => console.log(`[db-push] ${d.toString().trim()}`));
proc.stderr.on("data", (d) => {
const line = d.toString().trim();
stderr += line + "\n";
console.log(`[db-push] ${line}`);
});
proc.on("error", (err) => reject(new Error(`Failed to run prisma db push: ${err.message}`)));
proc.on("close", (code) => {
if (code !== 0) return reject(new Error(`prisma db push failed (exit ${code}): ${stderr}`));
console.log("[db-push] Schema synced successfully");
resolve();
});
});
}
const restoreUpload = multer({
@@ -459,9 +437,9 @@ router.post("/restore", restoreUpload.single("file"), async (req: Request, res:
// restored _prisma_migrations table may contain orphaned entries (migration
// names that have no matching file) which cause Prisma CLI to abort.
try {
await applyMissingMigrations();
await runDbPush();
} catch (err) {
console.error("applyMissingMigrations failed after restore:", err);
console.error("[restore] db push failed after restore:", err);
}
res.json({ success: true });

View File

@@ -5,48 +5,36 @@ import { prisma } from "@repo/db/client";
const LOCAL_BACKUP_DIR = path.resolve(process.cwd(), "backups");
const MIGRATIONS_DIR = path.resolve(__dirname, "../../../../packages/db/prisma/migrations");
const SCHEMA_PATH = path.resolve(__dirname, "../../../../packages/db/prisma/schema.prisma");
async function applyMissingMigrations() {
let folders: string[];
try {
folders = fs.readdirSync(MIGRATIONS_DIR)
.filter((name) => fs.statSync(path.join(MIGRATIONS_DIR, name)).isDirectory())
.sort();
} catch {
console.warn("Could not read migrations directory, skipping post-import migration.");
return;
}
function runDbPush(): Promise<void> {
return new Promise((resolve, reject) => {
const proc = spawn("npx", ["prisma", "db", "push", "--config", path.resolve(__dirname, "../../../../packages/db/prisma/prisma.config.ts"), "--schema", SCHEMA_PATH, "--accept-data-loss"], {
cwd: process.cwd(),
env: process.env,
});
let applied: Set<string>;
try {
const rows = await prisma.$queryRaw<{ migration_name: string }[]>`
SELECT migration_name FROM "_prisma_migrations" WHERE finished_at IS NOT NULL
`;
applied = new Set(rows.map((r: { migration_name: string }) => r.migration_name));
} catch {
applied = new Set();
}
for (const folder of folders) {
if (applied.has(folder)) continue;
const sqlFile = path.join(MIGRATIONS_DIR, folder, "migration.sql");
if (!fs.existsSync(sqlFile)) continue;
const sql = fs.readFileSync(sqlFile, "utf8");
try {
await prisma.$executeRawUnsafe(sql);
console.log(`Applied migration: ${folder}`);
} catch (err: any) {
console.warn(`Migration ${folder} had errors (may already be applied):`, err.message);
}
}
let stderr = "";
proc.stdout.on("data", (d) => console.log(`[db-push] ${d.toString().trim()}`));
proc.stderr.on("data", (d) => {
const line = d.toString().trim();
stderr += line + "\n";
console.log(`[db-push] ${line}`);
});
proc.on("error", (err) => reject(new Error(`Failed to run prisma db push: ${err.message}`)));
proc.on("close", (code) => {
if (code !== 0) return reject(new Error(`prisma db push failed (exit ${code}): ${stderr}`));
console.log("[db-push] Schema synced successfully");
resolve();
});
});
}
function getLatestBackupFile(): string | null {
if (!fs.existsSync(LOCAL_BACKUP_DIR)) return null;
const files = fs.readdirSync(LOCAL_BACKUP_DIR)
.filter((f) => f.endsWith(".zip"))
.filter((f) => f.endsWith(".zip") || f.endsWith(".sql"))
.map((f) => ({ name: f, mtime: fs.statSync(path.join(LOCAL_BACKUP_DIR, f)).mtimeMs }))
.sort((a, b) => b.mtime - a.mtime);
@@ -98,20 +86,28 @@ export async function importLatestBackup(): Promise<void> {
} catch (_) {}
try {
await applyMissingMigrations();
await runDbPush();
} catch (err) {
console.error("applyMissingMigrations failed after auto-import:", err);
console.error("[auto-import] db push failed after import:", err);
}
console.log(`[auto-import] Successfully imported ${path.basename(backupFile)}`);
resolve();
});
const unzip = spawn("unzip", ["-p", backupFile, "*.sql"]);
unzip.stderr.on("data", (d) => console.warn(`[auto-import] unzip: ${d.toString().trim()}`));
unzip.on("error", (err) => {
reject(new Error(`Failed to extract backup zip: ${err.message}`));
});
unzip.stdout.pipe(psql.stdin);
if (backupFile.endsWith(".zip")) {
const unzip = spawn("unzip", ["-p", backupFile, "*.sql"]);
unzip.stderr.on("data", (d) => console.warn(`[auto-import] unzip: ${d.toString().trim()}`));
unzip.on("error", (err) => {
reject(new Error(`Failed to extract backup zip: ${err.message}`));
});
unzip.stdout.pipe(psql.stdin);
} else {
const sqlStream = fs.createReadStream(backupFile);
sqlStream.on("error", (err) => {
reject(new Error(`Failed to read backup file: ${err.message}`));
});
sqlStream.pipe(psql.stdin);
}
});
}