Skip to content

API and CLI ​

CLI ​

sh
pg-dolly-schema --migrate <command> [--file schema.sql] [--cwd .] [--check] [--data <table>...] [--prefix pg_dolly]
OptionDefaultDescription
--migratePG_DOLLY_SCHEMA_MIGRATERequired. Shell command that migrates the database in DATABASE_URL from empty.
--fileschema.sqlFile to write the schema to, relative to --cwd.
--cwd.Directory the command runs in, and --file is relative to.
--check, --citrue in CIFail if the file is not up to date, rather than writing it. --no-check writes even in CI.
--data—Table to keep the rows of too, e.g. the migration tool's own. Repeat for several.
--prefixpg_dollyPrefix of the databases created, as in PgDolly.connect().

Every option can also be set as an environment variable: PG_DOLLY_SCHEMA_ and the option in capitals (PG_DOLLY_SCHEMA_MIGRATE). The server is always DATABASE_URL.

snapshotSchema(options) ​

What the CLI runs.

ts
import { 
snapshotSchema
} from 'pg-dolly-schema';
await
snapshotSchema
({
migrate
: 'drizzle-kit migrate',
cwd
: 'packages/app',
check
: true });
OptionTypeDefaultDescription
migratestring—Shell command that migrates the database in DATABASE_URL from empty.
filestring'schema.sql'Where the schema is written, relative to cwd.
cwdstringprocess.cwd()Directory the command runs in, and file is relative to.
checkbooleantrue in CIFail if the file isn't up to date, rather than writing it.
urlstringprocess.env.DATABASE_URLServer to migrate a database on, as a postgres:// URL.
datastring[][]Tables to keep the rows of too, as pg_dump --table patterns, e.g. drizzle.__drizzle_migrations.
prefixstring'pg_dolly'Prefix of the databases created.

Returns Promise<void>.

Throws

ErrorWhen
PgDollySchemaMigrateErrorThe migrate command failed. Has command, exitCode (null if killed) and database, which is kept.
PgDollySchemaEmptyErrorThe migrate command succeeded but left the database empty. Has command.
From populate-filesWith check, the file is not up to date.
From pg-dollyNo DATABASE_URL, or an invalid prefix.

PgDollySchemaMigrateError and PgDollySchemaEmptyError extend PgDollySchemaError.

ts
import { 
PgDollySchemaMigrateError
,
snapshotSchema
} from 'pg-dolly-schema';
try { await
snapshotSchema
({
migrate
: 'drizzle-kit migrate' });
} catch (
error
) {
if (
error
instanceof
PgDollySchemaMigrateError
) {
console
.
error
(`Migrations failed: inspect ${
error
.
database
}`);
} }

The SnapshotSchemaOptions type is exported too.