"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 };