Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369import { loadDotEnvIfPresent } from "../lib/cli-env.ts";import { Buffer } from "node:buffer";import { createNeonExecuteClient, type DbExecuteClient, type DbValue, neonDatabaseUrl,} from "../lib/neon.ts";import { createPostgresExecuteClient } from "../lib/postgres.ts";import { NEON_APP_TABLES, NEON_TABLES_WITH_FOREIGN_KEYS, parseArgs, quoteIdent, quoteQualifiedTable,} from "../lib/neon-migration.ts";
const CHUNK_SIZE = 500;
interface ColumnInfo { name: string; ordinal: number; isGenerated: boolean;}
interface TableCopyResult { table: string; sourceRows: number; copiedRows: number; skipped: boolean; reason?: string;}
function usage(exitCode = 2): never { const write = exitCode === 0 ? console.log : console.error; write( [ "Usage: deno task db:copy:postgres [--write] [--reset] [--limit=N]", " [--tables=a,b] [--skip-tables=a,b]", "", "Default is dry-run. Use --write to copy data.", "--reset truncates selected Postgres tables first and requires --write.", "", "Environment:", " Source: NEON_DIRECT_DATABASE_URL or NEON_DATABASE_URL", " Destination: POSTGRES_DATABASE_URL, DATABASE_URL, or POSTGRES_URL", ].join("\n"), ); Deno.exit(exitCode);}
function valueForDb(value: unknown): DbValue | Buffer { if (value == null) return null; if ( typeof value === "string" || typeof value === "number" || typeof value === "bigint" || typeof value === "boolean" ) { return value; } if (value instanceof Uint8Array) return Buffer.from(value); if (value instanceof ArrayBuffer) return Buffer.from(value); return String(value);}
async function sourceColumns( db: DbExecuteClient, table: string,): Promise<ColumnInfo[]> { const result = await db.execute({ sql: ` SELECT column_name, ordinal_position, is_generated FROM information_schema.columns WHERE table_schema = 'public' AND table_name = ? ORDER BY ordinal_position `, args: [table], }); return result.rows.map((row) => ({ name: String(row.column_name), ordinal: Number(row.ordinal_position), isGenerated: String(row.is_generated ?? "NEVER") !== "NEVER", }));}
async function destinationColumns( db: DbExecuteClient, table: string,): Promise<ColumnInfo[]> { const result = await db.execute({ sql: ` SELECT column_name, ordinal_position, is_generated FROM information_schema.columns WHERE table_schema = 'public' AND table_name = ? ORDER BY ordinal_position `, args: [table], }); return result.rows.map((row) => ({ name: String(row.column_name), ordinal: Number(row.ordinal_position), isGenerated: String(row.is_generated ?? "NEVER") !== "NEVER", }));}
async function destinationPrimaryKeys( db: DbExecuteClient, table: string,): Promise<string[]> { const result = await db.execute({ sql: ` SELECT kcu.column_name FROM information_schema.table_constraints tc JOIN information_schema.key_column_usage kcu ON tc.constraint_name = kcu.constraint_name AND tc.table_schema = kcu.table_schema WHERE tc.table_schema = 'public' AND tc.table_name = ? AND tc.constraint_type = 'PRIMARY KEY' ORDER BY kcu.ordinal_position `, args: [table], }); return result.rows.map((row) => String(row.column_name));}
async function sourceRowCount( db: DbExecuteClient, table: string,): Promise<number> { const result = await db.execute( `SELECT COUNT(*) AS count FROM ${quoteQualifiedTable(table)}`, ); return Number(result.rows[0]?.count ?? 0);}
async function readSourceRows( db: DbExecuteClient, table: string, columns: string[], limit: number, offset: number,): Promise<Record<string, unknown>[]> { const columnList = columns.map(quoteIdent).join(", "); const result = await db.execute({ sql: `SELECT ${columnList} FROM ${ quoteQualifiedTable(table) } LIMIT ? OFFSET ?`, args: [limit, offset], }); return result.rows as Record<string, unknown>[];}
function buildUpsertSql( table: string, columns: string[], primaryKeys: string[],): string { const columnList = columns.map(quoteIdent).join(", "); const values = columns.map((_, index) => `$${index + 1}`).join(", "); const conflictTarget = primaryKeys.map(quoteIdent).join(", "); const updateColumns = columns.filter((column) => !primaryKeys.includes(column) ); const updates = updateColumns.map((column) => `${quoteIdent(column)} = EXCLUDED.${quoteIdent(column)}` ).join(", ");
return [ `INSERT INTO ${quoteQualifiedTable(table)} (${columnList})`, `VALUES (${values})`, updateColumns.length ? `ON CONFLICT (${conflictTarget}) DO UPDATE SET ${updates}` : `ON CONFLICT (${conflictTarget}) DO NOTHING`, ].join(" ");}
async function resetTables(db: DbExecuteClient, tables: string[]) { if (!tables.length) return; const tableList = tables.map(quoteQualifiedTable).join(", "); await db.execute(`TRUNCATE TABLE ${tableList} RESTART IDENTITY CASCADE`);}
async function syncSequences(db: DbExecuteClient) { const serials = await db.execute(` SELECT table_name, column_name FROM information_schema.columns WHERE table_schema = 'public' AND column_default LIKE 'nextval(%' ORDER BY table_name, ordinal_position `);
for (const row of serials.rows) { const table = String(row.table_name); const column = String(row.column_name); await db.execute({ sql: ` SELECT setval( pg_get_serial_sequence(?, ?), GREATEST(COALESCE((SELECT MAX(${quoteIdent(column)}) FROM ${ quoteQualifiedTable(table) }), 0), 1), (SELECT COUNT(*) > 0 FROM ${quoteQualifiedTable(table)}) ) `, args: [`public.${table}`, column], }); }}
async function copyTable( sourceDb: DbExecuteClient, destinationDb: DbExecuteClient, table: string, dryRun: boolean, limit: number | null,): Promise<TableCopyResult> { const source = await sourceColumns(sourceDb, table); if (!source.length) { return { table, sourceRows: 0, copiedRows: 0, skipped: true, reason: "missing source table", }; }
const destination = await destinationColumns(destinationDb, table); if (!destination.length) { return { table, sourceRows: 0, copiedRows: 0, skipped: true, reason: "missing destination table", }; }
const sourceSet = new Set( source.filter((column) => !column.isGenerated).map( (column) => column.name, ), ); const copyColumns = destination .filter((column) => !column.isGenerated) .map((column) => column.name) .filter((column) => sourceSet.has(column)); if (!copyColumns.length) { return { table, sourceRows: 0, copiedRows: 0, skipped: true, reason: "no shared columns", }; }
const primaryKeys = await destinationPrimaryKeys(destinationDb, table); if (!primaryKeys.length) { return { table, sourceRows: 0, copiedRows: 0, skipped: true, reason: "no destination primary key", }; }
const total = await sourceRowCount(sourceDb, table); const sourceRows = limit == null ? total : Math.min(total, limit); if (dryRun || sourceRows === 0) { return { table, sourceRows, copiedRows: 0, skipped: false }; }
const upsertSql = buildUpsertSql(table, copyColumns, primaryKeys); let copiedRows = 0; for (let offset = 0; offset < sourceRows; offset += CHUNK_SIZE) { const size = Math.min(CHUNK_SIZE, sourceRows - offset); const rows = await readSourceRows( sourceDb, table, copyColumns, size, offset, ); for (const row of rows) { await destinationDb.execute({ sql: upsertSql, args: copyColumns.map((column) => valueForDb(row[column])), }); copiedRows++; } }
return { table, sourceRows, copiedRows, skipped: false };}
const args = Deno.args.filter((arg) => arg !== "--");if (args.includes("--help") || args.includes("-h")) usage(0);await loadDotEnvIfPresent();neonDatabaseUrl();
const options = parseArgs(args);if (options.reset && options.dryRun) { throw new Error("--reset requires --write");}
const sourceDb = createNeonExecuteClient();const destinationDb = createPostgresExecuteClient();const selectedTables = (options.tables.length ? options.tables : [ ...NEON_APP_TABLES,]).filter((table) => !options.skipTables.has(table));
console.log( `[db:copy:postgres] ${ options.dryRun ? "dry-run" : "write" } ${selectedTables.length} tables`,);
try { if (!options.dryRun && options.reset) { const resetOrder = [ ...NEON_TABLES_WITH_FOREIGN_KEYS, ...selectedTables.filter((table) => !NEON_TABLES_WITH_FOREIGN_KEYS.includes( table as typeof NEON_TABLES_WITH_FOREIGN_KEYS[number], ) ), ].filter((table, index, list) => selectedTables.includes(table) && list.indexOf(table) === index ); console.log("[db:copy:postgres] truncating selected Postgres tables"); await resetTables(destinationDb, resetOrder); }
const started = performance.now(); const results: TableCopyResult[] = []; for (const table of selectedTables) { const result = await copyTable( sourceDb, destinationDb, table, options.dryRun, options.limit, ); results.push(result); const suffix = result.skipped ? `skipped (${result.reason})` : options.dryRun ? `${result.sourceRows} rows ready` : `${result.copiedRows}/${result.sourceRows} rows copied`; console.log(`[db:copy:postgres] ${table}: ${suffix}`); }
if (!options.dryRun) await syncSequences(destinationDb);
const copied = results.reduce((sum, result) => sum + result.copiedRows, 0); const ready = results.reduce((sum, result) => sum + result.sourceRows, 0); const elapsed = Math.round(performance.now() - started); console.log( `[db:copy:postgres] done (${ options.dryRun ? ready : copied } rows, ${elapsed}ms)`, );} finally { await (destinationDb as typeof destinationDb & { end?: () => Promise<void> }) .end?.();}