|
| 1 | +import { Logger } from '@pgpmjs/logger'; |
| 2 | +import { CLIOptions, Inquirerer } from 'inquirerer'; |
| 3 | +import { ParsedArgs } from 'minimist'; |
| 4 | +import { getPgEnvOptions, getSpawnEnvWithPg } from 'pg-env'; |
| 5 | +import { getPgPool } from 'pg-cache'; |
| 6 | +import { spawn } from 'child_process'; |
| 7 | +import fs from 'fs'; |
| 8 | +import path from 'path'; |
| 9 | +import { QuoteUtils } from 'pgsql-deparser/utils/quote-utils'; |
| 10 | + |
| 11 | +import { getTargetDatabase } from '../utils'; |
| 12 | + |
| 13 | +const log = new Logger('dump'); |
| 14 | + |
| 15 | +const dumpUsageText = ` |
| 16 | +Dump Command: |
| 17 | +
|
| 18 | + pgpm dump [options] |
| 19 | +
|
| 20 | + Dump a postgres database to a sql file. |
| 21 | +
|
| 22 | +Options: |
| 23 | + --help, -h Show this help message |
| 24 | + --db, --database <name> Target postgres database name |
| 25 | + --out <path> Output file path |
| 26 | + --database-id <id> When set, the dump will include a prune step that keeps only this database_id after restore |
| 27 | + --cwd <directory> Working directory (default: current directory) |
| 28 | +
|
| 29 | +Examples: |
| 30 | + pgpm dump |
| 31 | + pgpm dump --database mydb |
| 32 | + pgpm dump --database mydb --out ./mydb.sql |
| 33 | + pgpm dump --database mydb --database-id 00000000-0000-0000-0000-000000000000 |
| 34 | +`; |
| 35 | + |
| 36 | +function nowStamp(): string { |
| 37 | + const d = new Date(); |
| 38 | + const pad = (n: number) => String(n).padStart(2, '0'); |
| 39 | + return `${d.getFullYear()}${pad(d.getMonth() + 1)}${pad(d.getDate())}-${pad(d.getHours())}${pad(d.getMinutes())}${pad(d.getSeconds())}`; |
| 40 | +} |
| 41 | + |
| 42 | +async function runPgDump(args: string[], env: NodeJS.ProcessEnv): Promise<void> { |
| 43 | + await new Promise<void>((resolve, reject) => { |
| 44 | + const child = spawn('pg_dump', args, { |
| 45 | + env, |
| 46 | + stdio: 'inherit', |
| 47 | + shell: false |
| 48 | + }); |
| 49 | + |
| 50 | + child.on('error', (err: any) => { |
| 51 | + if (err.code === 'ENOENT') { |
| 52 | + log.error('pg_dump not found; ensure PostgreSQL client tools are installed and in PATH'); |
| 53 | + } |
| 54 | + reject(err); |
| 55 | + }); |
| 56 | + |
| 57 | + child.on('close', (code) => { |
| 58 | + if (code === 0) { |
| 59 | + resolve(); |
| 60 | + return; |
| 61 | + } |
| 62 | + reject(new Error(`pg_dump exited with code ${code ?? 1}`)); |
| 63 | + }); |
| 64 | + }); |
| 65 | +} |
| 66 | + |
| 67 | +async function resolveDatabaseId(dbname: string, databaseIdRaw: string): Promise<{ id: string; name: string } | null> { |
| 68 | + const pool = getPgPool(getPgEnvOptions({ database: dbname })); |
| 69 | + const res = await pool.query(`select id, name from metaschema_public.database order by name`); |
| 70 | + |
| 71 | + const byId = res.rows.find((r: any) => String(r.id) === databaseIdRaw); |
| 72 | + if (byId) return { id: String(byId.id), name: String(byId.name) }; |
| 73 | + |
| 74 | + const byName = res.rows.find((r: any) => String(r.name) === databaseIdRaw); |
| 75 | + if (byName) return { id: String(byName.id), name: String(byName.name) }; |
| 76 | + |
| 77 | + return null; |
| 78 | +} |
| 79 | + |
| 80 | +async function buildPruneSql(dbname: string, databaseId: string): Promise<string> { |
| 81 | + const pool = getPgPool(getPgEnvOptions({ database: dbname })); |
| 82 | + |
| 83 | + const tables = await pool.query(` |
| 84 | + select c.table_schema, c.table_name |
| 85 | + from information_schema.columns c |
| 86 | + join information_schema.tables t |
| 87 | + on t.table_schema = c.table_schema |
| 88 | + and t.table_name = c.table_name |
| 89 | + where c.column_name = 'database_id' |
| 90 | + and t.table_type = 'BASE TABLE' |
| 91 | + and c.table_schema not in ('pg_catalog', 'information_schema') |
| 92 | + order by c.table_schema, c.table_name |
| 93 | + `); |
| 94 | + |
| 95 | + const lines: string[] = []; |
| 96 | + lines.push(''); |
| 97 | + lines.push('-- pgpm dump prune'); |
| 98 | + lines.push('-- this section keeps only one database_id after restore'); |
| 99 | + lines.push('do $$ begin'); |
| 100 | + lines.push(` raise notice 'pruning data to database_id ${databaseId}';`); |
| 101 | + lines.push('end $$;'); |
| 102 | + lines.push('set session_replication_role = replica;'); |
| 103 | + |
| 104 | + for (const row of tables.rows) { |
| 105 | + const schema = String(row.table_schema); |
| 106 | + const table = String(row.table_name); |
| 107 | + // Use QuoteUtils for robust identifier quoting |
| 108 | + const qualified = QuoteUtils.quoteQualifiedIdentifier(schema, table); |
| 109 | + // Use formatEString to safely escape the UUID/string literal |
| 110 | + const dbIdLiteral = QuoteUtils.formatEString(databaseId); |
| 111 | + lines.push(`delete from ${qualified} where database_id <> ${dbIdLiteral};`); |
| 112 | + } |
| 113 | + |
| 114 | + // Handle metaschema_public.database deletion |
| 115 | + const metaschemaDb = QuoteUtils.quoteQualifiedIdentifier('metaschema_public', 'database'); |
| 116 | + const dbIdLiteral = QuoteUtils.formatEString(databaseId); |
| 117 | + lines.push(`delete from ${metaschemaDb} where id <> ${dbIdLiteral};`); |
| 118 | + |
| 119 | + lines.push('set session_replication_role = origin;'); |
| 120 | + lines.push('do $$ begin'); |
| 121 | + lines.push(` raise notice 'prune done';`); |
| 122 | + lines.push('end $$;'); |
| 123 | + lines.push(''); |
| 124 | + |
| 125 | + return lines.join('\n'); |
| 126 | +} |
| 127 | + |
| 128 | +// Helper to retrieve argument from parsed argv or positional _ array |
| 129 | +function getArg(argv: Partial<ParsedArgs>, key: string): string | undefined { |
| 130 | + if (argv[key]) return argv[key] as string; |
| 131 | + const args = (argv._ as string[]) || []; |
| 132 | + const idx = args.indexOf(`--${key}`); |
| 133 | + if (idx > -1 && args.length > idx + 1) { |
| 134 | + return args[idx + 1]; |
| 135 | + } |
| 136 | + return undefined; |
| 137 | +} |
| 138 | + |
| 139 | +export default async ( |
| 140 | + argv: Partial<ParsedArgs>, |
| 141 | + prompter: Inquirerer, |
| 142 | + _options: CLIOptions |
| 143 | +) => { |
| 144 | + if (argv.help || argv.h) { |
| 145 | + console.log(dumpUsageText); |
| 146 | + process.exit(0); |
| 147 | + } |
| 148 | + |
| 149 | + const cwd = (argv.cwd as string) || process.cwd(); |
| 150 | + const dbname = await getTargetDatabase(argv, prompter, { message: 'Select database' }); |
| 151 | + |
| 152 | + const outPath = path.resolve(cwd, (argv.out as string) || `pgpm-dump-${dbname}-${nowStamp()}.sql`); |
| 153 | + fs.mkdirSync(path.dirname(outPath), { recursive: true }); |
| 154 | + |
| 155 | + let databaseIdInfo: { id: string; name: string } | null = null; |
| 156 | + const databaseIdRaw = getArg(argv, 'database-id'); |
| 157 | + if (databaseIdRaw) { |
| 158 | + databaseIdInfo = await resolveDatabaseId(dbname, databaseIdRaw); |
| 159 | + if (!databaseIdInfo) { |
| 160 | + throw new Error(`unknown database-id ${databaseIdRaw}`); |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + log.info(`dumping database ${dbname}`); |
| 165 | + log.info(`writing to ${outPath}`); |
| 166 | + if (databaseIdInfo) { |
| 167 | + log.info(`database id ${databaseIdInfo.id}`); |
| 168 | + } |
| 169 | + |
| 170 | + const pgEnv = getPgEnvOptions({ database: dbname }); |
| 171 | + const spawnEnv = getSpawnEnvWithPg(pgEnv); |
| 172 | + |
| 173 | + const args = [ |
| 174 | + '--format=plain', |
| 175 | + '--no-owner', |
| 176 | + '--no-privileges', |
| 177 | + '--file', |
| 178 | + outPath, |
| 179 | + dbname |
| 180 | + ]; |
| 181 | + |
| 182 | + await runPgDump(args, spawnEnv); |
| 183 | + |
| 184 | + if (databaseIdInfo) { |
| 185 | + const pruneSql = await buildPruneSql(dbname, databaseIdInfo.id); |
| 186 | + // Use writeFileSync with 'a' flag for explicit append as requested |
| 187 | + fs.writeFileSync(outPath, pruneSql, { encoding: 'utf8', flag: 'a' }); |
| 188 | + log.info('added prune section to dump file'); |
| 189 | + } |
| 190 | + |
| 191 | + log.success('dump complete'); |
| 192 | + return argv; |
| 193 | +}; |
| 194 | + |
| 195 | + |
0 commit comments