|
| 1 | +import fs from 'node:fs'; |
| 2 | +import path from 'node:path'; |
| 3 | +import Database from 'better-sqlite3'; |
| 4 | +import { findDbPath } from './db.js'; |
| 5 | +import { debug } from './logger.js'; |
| 6 | + |
| 7 | +const NAME_RE = /^[a-zA-Z0-9_-]+$/; |
| 8 | + |
| 9 | +/** |
| 10 | + * Validate a snapshot name (alphanumeric, hyphens, underscores only). |
| 11 | + * Throws on invalid input. |
| 12 | + */ |
| 13 | +export function validateSnapshotName(name) { |
| 14 | + if (!name || !NAME_RE.test(name)) { |
| 15 | + throw new Error( |
| 16 | + `Invalid snapshot name "${name}". Use only letters, digits, hyphens, and underscores.`, |
| 17 | + ); |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +/** |
| 22 | + * Return the snapshots directory for a given DB path. |
| 23 | + */ |
| 24 | +export function snapshotsDir(dbPath) { |
| 25 | + return path.join(path.dirname(dbPath), 'snapshots'); |
| 26 | +} |
| 27 | + |
| 28 | +/** |
| 29 | + * Save a snapshot of the current graph database. |
| 30 | + * Uses VACUUM INTO for an atomic, WAL-free copy. |
| 31 | + * |
| 32 | + * @param {string} name - Snapshot name |
| 33 | + * @param {object} [options] |
| 34 | + * @param {string} [options.dbPath] - Explicit path to graph.db |
| 35 | + * @param {boolean} [options.force] - Overwrite existing snapshot |
| 36 | + * @returns {{ name: string, path: string, size: number }} |
| 37 | + */ |
| 38 | +export function snapshotSave(name, options = {}) { |
| 39 | + validateSnapshotName(name); |
| 40 | + const dbPath = options.dbPath || findDbPath(); |
| 41 | + if (!fs.existsSync(dbPath)) { |
| 42 | + throw new Error(`Database not found: ${dbPath}`); |
| 43 | + } |
| 44 | + |
| 45 | + const dir = snapshotsDir(dbPath); |
| 46 | + const dest = path.join(dir, `${name}.db`); |
| 47 | + |
| 48 | + if (fs.existsSync(dest)) { |
| 49 | + if (!options.force) { |
| 50 | + throw new Error(`Snapshot "${name}" already exists. Use --force to overwrite.`); |
| 51 | + } |
| 52 | + fs.unlinkSync(dest); |
| 53 | + debug(`Deleted existing snapshot: ${dest}`); |
| 54 | + } |
| 55 | + |
| 56 | + fs.mkdirSync(dir, { recursive: true }); |
| 57 | + |
| 58 | + const db = new Database(dbPath, { readonly: true }); |
| 59 | + try { |
| 60 | + db.exec(`VACUUM INTO '${dest.replace(/'/g, "''")}'`); |
| 61 | + } finally { |
| 62 | + db.close(); |
| 63 | + } |
| 64 | + |
| 65 | + const stat = fs.statSync(dest); |
| 66 | + debug(`Snapshot saved: ${dest} (${stat.size} bytes)`); |
| 67 | + return { name, path: dest, size: stat.size }; |
| 68 | +} |
| 69 | + |
| 70 | +/** |
| 71 | + * Restore a snapshot over the current graph database. |
| 72 | + * Removes WAL/SHM sidecar files before overwriting. |
| 73 | + * |
| 74 | + * @param {string} name - Snapshot name |
| 75 | + * @param {object} [options] |
| 76 | + * @param {string} [options.dbPath] - Explicit path to graph.db |
| 77 | + */ |
| 78 | +export function snapshotRestore(name, options = {}) { |
| 79 | + validateSnapshotName(name); |
| 80 | + const dbPath = options.dbPath || findDbPath(); |
| 81 | + const dir = snapshotsDir(dbPath); |
| 82 | + const src = path.join(dir, `${name}.db`); |
| 83 | + |
| 84 | + if (!fs.existsSync(src)) { |
| 85 | + throw new Error(`Snapshot "${name}" not found at ${src}`); |
| 86 | + } |
| 87 | + |
| 88 | + // Remove WAL/SHM sidecar files for a clean restore |
| 89 | + for (const suffix of ['-wal', '-shm']) { |
| 90 | + const sidecar = dbPath + suffix; |
| 91 | + if (fs.existsSync(sidecar)) { |
| 92 | + fs.unlinkSync(sidecar); |
| 93 | + debug(`Removed sidecar: ${sidecar}`); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + fs.copyFileSync(src, dbPath); |
| 98 | + debug(`Restored snapshot "${name}" → ${dbPath}`); |
| 99 | +} |
| 100 | + |
| 101 | +/** |
| 102 | + * List all saved snapshots. |
| 103 | + * |
| 104 | + * @param {object} [options] |
| 105 | + * @param {string} [options.dbPath] - Explicit path to graph.db |
| 106 | + * @returns {Array<{ name: string, path: string, size: number, createdAt: Date }>} |
| 107 | + */ |
| 108 | +export function snapshotList(options = {}) { |
| 109 | + const dbPath = options.dbPath || findDbPath(); |
| 110 | + const dir = snapshotsDir(dbPath); |
| 111 | + |
| 112 | + if (!fs.existsSync(dir)) return []; |
| 113 | + |
| 114 | + return fs |
| 115 | + .readdirSync(dir) |
| 116 | + .filter((f) => f.endsWith('.db')) |
| 117 | + .map((f) => { |
| 118 | + const filePath = path.join(dir, f); |
| 119 | + const stat = fs.statSync(filePath); |
| 120 | + return { |
| 121 | + name: f.replace(/\.db$/, ''), |
| 122 | + path: filePath, |
| 123 | + size: stat.size, |
| 124 | + createdAt: stat.birthtime, |
| 125 | + }; |
| 126 | + }) |
| 127 | + .sort((a, b) => b.createdAt - a.createdAt); |
| 128 | +} |
| 129 | + |
| 130 | +/** |
| 131 | + * Delete a named snapshot. |
| 132 | + * |
| 133 | + * @param {string} name - Snapshot name |
| 134 | + * @param {object} [options] |
| 135 | + * @param {string} [options.dbPath] - Explicit path to graph.db |
| 136 | + */ |
| 137 | +export function snapshotDelete(name, options = {}) { |
| 138 | + validateSnapshotName(name); |
| 139 | + const dbPath = options.dbPath || findDbPath(); |
| 140 | + const dir = snapshotsDir(dbPath); |
| 141 | + const target = path.join(dir, `${name}.db`); |
| 142 | + |
| 143 | + if (!fs.existsSync(target)) { |
| 144 | + throw new Error(`Snapshot "${name}" not found at ${target}`); |
| 145 | + } |
| 146 | + |
| 147 | + fs.unlinkSync(target); |
| 148 | + debug(`Deleted snapshot: ${target}`); |
| 149 | +} |
0 commit comments