import type { LedgerReport } from '@pgpmjs/core'; import { buildLedgerReport, isDatabaseSpec, loadDiffSide, PgpmMigrate, resolveDatabaseSpec } from '@pgpmjs/core'; import { deltaChangesToRows, DiffSide, resolveDiffSideKind } from '@pgpmjs/diff'; import { Logger } from '@pgpmjs/logger'; import { appendModule, CHANGE_GRANULARITIES, ChangeGranularity, diffChangeSets, DiffPolicyError, EXPORT_GRANULARITIES, ExportGranularity, isChangeGranularity, isExportGranularity, loadModule, parseRenameDeclaration, RenameDeclaration, SemanticDiffResult, SemanticObjectDiff, withoutColumnOrder, writeModule } from '@pgpmjs/transform'; import * as fs from 'fs'; import { cliExitWithError, CLIOptions, extractFirst, Inquirerer, ParsedArgs } from 'inquirerer'; import * as os from 'os'; import * as path from 'path'; import { getPgPool } from 'pg-cache'; import type { PgConfig } from 'pg-env'; import { getPgEnvOptions } from 'pg-env'; import { emitModuleBundle, emitModuleSql, STDOUT_TARGET } from '../utils/module-projections'; import { catalogDifferences, withScratchDatabases } from '../utils/scratch-db'; const log = new Logger('diff'); const diffUsageText = ` Diff Command: pgpm diff [OPTIONS] Identity-keyed semantic diff between two schema sources. Each side is normalized to an object set keyed by identity (kind/schema/name), so the same schema authored at different granularity, naming, or partitioning diffs as empty. Tables are compared column-by-column and constraint-by-constraint, so table changes emit ALTER TABLE, not a rebuild. Sides: / may each be: - a pgpm module directory (flattened in plan order) - a pgpm workspace directory (every local module, in dependency order, with package-qualified change names) - a raw .sql file - a live database: a postgres:// connection string, or db: (uses PG* env for host/port/user; schema is read via pg_dump) The delta is one model; every --emit-* flag is a projection of it, so they compose (a single run can emit a module, a linear SQL file, and a bundle). Options: --help, -h Show this help message --emit-migration Write the delta as a pgpm module (deploy/revert/ verify per change, spec-derived paths, graph-derived requires) into / --emit-module Alias of --emit-migration --append-module Append the delta into an EXISTING pgpm module at : existing changes, scripts and .control are left untouched; a change reworking an existing name is appended as -N. Standalone: not combinable with --emit-*/--verify. --emit-sql Also project the delta to a single consolidated SQL file (deparsed in plan order); - writes to stdout --emit-bundle Also project the delta to a content-addressed .bundle.tar.gz archive --ledger Read a database's pgpm_migrate ledger (db: or a connection string) as the deployment cursor for side B, and report which of B's plan entries that database already has (identical / drifted / pending / orphaned / out-of-order). Combines with --emit-ledger. --emit-ledger Write an idempotent pgpm_migrate backfill script that records B's already-satisfied changes without executing them. Satisfaction is semantic (identity- keyed against side A), so regenerated or reordered plans still classify as satisfied. --pkg Emitted migration package name (default: diff-migration) --granularity Granularity for emitted changes: atomic | object | consolidated (default: object) --change-granularity Change-level distribution for emitted changes: alteration | object | single (default: object) --naming