17 lines
668 B
JavaScript
17 lines
668 B
JavaScript
import fs from "fs";
|
|
/**
|
|
* # Replace the database file with a copy of `src`
|
|
* @description All connections to `db_file_path` must be closed first.
|
|
* Leftover `-wal`/`-shm`/`-journal` files are removed so SQLite doesn't
|
|
* replay stale pages onto the new file, and the copy is swapped in with an
|
|
* atomic rename.
|
|
*/
|
|
export default function replaceDbFile({ src, db_file_path }) {
|
|
const tmp_file_path = `${db_file_path}.replace-${Date.now()}`;
|
|
fs.copyFileSync(src, tmp_file_path);
|
|
for (const suffix of ["-wal", "-shm", "-journal"]) {
|
|
fs.rmSync(`${db_file_path}${suffix}`, { force: true });
|
|
}
|
|
fs.renameSync(tmp_file_path, db_file_path);
|
|
}
|