fix database import in docker

This commit is contained in:
Zimeng Xiong
2025-11-23 09:40:00 -08:00
parent d581eb3e88
commit d93b6493c1
3 changed files with 28 additions and 5 deletions
Binary file not shown.
+27 -5
View File
@@ -70,6 +70,29 @@ console.log("Allowed origins:", allowedOrigins);
const uploadDir = path.resolve(__dirname, "../uploads"); const uploadDir = path.resolve(__dirname, "../uploads");
const moveFile = async (source: string, destination: string) => {
try {
await fsPromises.rename(source, destination);
} catch (error) {
const err = error as NodeJS.ErrnoException;
if (!err || err.code !== "EXDEV") {
throw error;
}
// Cross-device rename fallback: copy then delete source
await fsPromises
.unlink(destination)
.catch((unlinkError: NodeJS.ErrnoException) => {
if (unlinkError && unlinkError.code !== "ENOENT") {
throw unlinkError;
}
});
await fsPromises.copyFile(source, destination);
await fsPromises.unlink(source);
}
};
// Initialize upload directory asynchronously // Initialize upload directory asynchronously
const initializeUploadDir = async () => { const initializeUploadDir = async () => {
try { try {
@@ -918,7 +941,7 @@ app.post("/import/sqlite/verify", upload.single("db"), async (req, res) => {
await removeFileIfExists(stagedPath); await removeFileIfExists(stagedPath);
if (!isValid) { if (!isValid) {
return res.status(400).json({ error: "Invalid SQLite file" }); return res.status(400).json({ error: "Invalid database format" });
} }
res.json({ valid: true, message: "Database file is valid" }); res.json({ valid: true, message: "Database file is valid" });
@@ -945,8 +968,7 @@ app.post("/import/sqlite", upload.single("db"), async (req, res) => {
); );
try { try {
// Use async rename instead of blocking renameSync await moveFile(originalPath, stagedPath);
await fsPromises.rename(originalPath, stagedPath);
} catch (error) { } catch (error) {
console.error("Failed to stage uploaded database", error); console.error("Failed to stage uploaded database", error);
await removeFileIfExists(originalPath); await removeFileIfExists(originalPath);
@@ -975,8 +997,8 @@ app.post("/import/sqlite", upload.single("db"), async (req, res) => {
// Database doesn't exist, skip backup // Database doesn't exist, skip backup
} }
// Move staged file to final location // Move staged file to final location, supporting cross-device mounts
await fsPromises.rename(stagedPath, dbPath); await moveFile(stagedPath, dbPath);
} catch (error) { } catch (error) {
console.error("Failed to replace database", error); console.error("Failed to replace database", error);
await removeFileIfExists(stagedPath); await removeFileIfExists(stagedPath);
+1
View File
@@ -16,6 +16,7 @@
"noUncheckedSideEffectImports": true, "noUncheckedSideEffectImports": true,
"moduleDetection": "force", "moduleDetection": "force",
"skipLibCheck": true, "skipLibCheck": true,
"allowJs": true,
}, },
"include": ["src/**/*"], "include": ["src/**/*"],
"exclude": ["node_modules", "dist", "prisma.config.ts"] "exclude": ["node_modules", "dist", "prisma.config.ts"]