ken_nogi/PleasanterSystem/dbbackup/lib/pgRestore.js
Kenichiro NOGI ed33892f08 chore: 作業中の変更を整理しコミット(複数プロジェクト分)
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-08-02 11:09:50 +09:00

40 lines
1.3 KiB
JavaScript

"use strict";
/*
* pg_restore をspawnし、カスタム形式(-Fc)のダンプファイルからDBへ復元する。
* ダンプファイルは分割済みパートを事前に1ファイルへ再結合したもの(restore.js側で実施)を渡す想定。
*/
const { spawn } = require("child_process");
function runPgRestore({ pgRestoreBin, host, port, user, password, database, dumpFilePath }) {
return new Promise((resolve, reject) => {
const args = ["-h", host, "-p", String(port), "-U", user, "-d", database, "--no-owner", dumpFilePath];
const env = { ...process.env };
if (password) {
env.PGPASSWORD = password;
}
const child = spawn(pgRestoreBin, args, { env });
let stderrOutput = "";
child.stderr.on("data", (chunk) => {
stderrOutput += chunk.toString("utf8");
});
child.on("error", (err) => {
reject(new Error(`pg_restore起動失敗: ${err.message}`));
});
child.on("close", (code) => {
if (code !== 0) {
reject(new Error(`pg_restore異常終了 (code=${code}): ${stderrOutput.trim()}`));
return;
}
resolve({ stderrOutput: stderrOutput.trim() });
});
});
}
module.exports = { runPgRestore };