Diff two migration paths before they touch a real database
pglite-diff runs the same query probes against two SQL setups — for example, two versions of a migration — on isolated in-memory Postgres instances and reports exactly what differs.
What it does
PGlite is real PostgreSQL compiled to WASM, so a test can spin up a database in-process without Docker. Checking that one migration runs is common. The risky question, when a migration history is squashed, rewritten, or reordered, is whether the new path produces the same query-visible results as the path it replaced.
Each scenario’s setup SQL runs on its own PGlite instance. Every probe query then runs on both, and the results are compared row by row and column by column. The report names the probe, the row, the columns that changed, and the before and after values. A probe that fails on one side, such as a renamed column, is reported on its own rather than hidden inside a data mismatch. Setup failures are also separate, and they name which side failed to build.
Row comparison is positional. Probes need an explicit ORDER BY so the diff is deterministic. pglite-diff is not a migration runner and it does not diff schemas; two schemas that return the same probe results are treated as equivalent.
Install
npm install --save-dev pglite-diff @electric-sql/pglite
Requires Node.js 20 or newer. @electric-sql/pglite is a peer dependency.
Usage
Library
import { diffScenarios } from "pglite-diff";
const report = await diffScenarios(
{ setup: originalMigrationSql },
{ setup: refactoredMigrationSql },
[
{
name: "subscription pricing",
sql: "select plan, monthly_price, annual_price from subscriptions order by id",
},
]
);
if (!report.ok) {
console.error(JSON.stringify(report, null, 2));
process.exit(1);
}
Overall: REGRESSION DETECTED
Probe "subscription pricing": MISMATCH
row 0: changed columns [annual_price]
before: {"plan":"starter","monthly_price":"10","annual_price":"96.0"}
after: {"plan":"starter","monthly_price":"10","annual_price":"108.0"}
GitHub Action
- uses: jayblast-spec/pglite-diff@main
with:
config: ./pglite-diff.config.mjs
CLI
npx pglite-diff ./pglite-diff.config.mjs
The config file default-exports { before, after, probes }:
// pglite-diff.config.mjs
export default {
before: { setup: `/* schema, migrations, and seed data, as they are today */` },
after: { setup: `/* the same, after the change */` },
probes: [
{ name: "pricing", sql: "select plan, annual_price from subscriptions order by id" },
],
};
A local script that shows a fat-fingered discount surviving a migration squash is in examples/demo.ts. Run it with npx tsx examples/demo.ts.