PostgreSQL migrations
Fresh-install and upgrade SQL, component selection, custom schemas, and exported SQL assets.
good-webhooks/migrations generates the fresh-install and upgrade SQL for management and delivery tables; your migration tool applies it.
import {
getPostgresMigration,
getPostgresUpgrade,
POSTGRES_SCHEMA_VERSION,
} from 'good-webhooks/migrations'
import type { PostgresMigrationOptions, PostgresUpgradeOptions } from 'good-webhooks/migrations'The migrations guide covers applying, upgrading, and splitting storage.
getPostgresMigration
getPostgresMigration(options?: PostgresMigrationOptions): stringimport { writeFile } from 'node:fs/promises'
import { getPostgresMigration } from 'good-webhooks/migrations'
await writeFile(
'migrations/001-webhooks.sql',
getPostgresMigration({ schema: 'webhooks', component: 'all' }),
)Returns fresh-install SQL. It never connects to a database or applies DDL. The host applies the SQL once in a transaction and records it in its migration ledger.
Prop
Type
Every generated script starts with CREATE SCHEMA IF NOT EXISTS for its namespace. Table creation is not idempotent, and reapplying initial SQL is not an upgrade. Invalid names or components produce INVALID_CONFIG.
Components and SQL assets
| Component | Equivalent shipped asset for public |
|---|---|
management | good-webhooks/migrations/management.sql |
delivery | good-webhooks/migrations/delivery.sql |
all | good-webhooks/migrations/001-initial.sql |
Tables each component creates:
management:webhook_schema_version(if absent) andwebhook_endpoints.delivery:webhook_schema_version(if absent),webhook_endpoint_state,webhook_events,webhook_deliveries, andwebhook_attempts.all: both groups.
Each component records its version in webhook_schema_version; components that share a namespace share the table. The .sql files are public package exports. They are text assets, not executable JavaScript imports. Resolve them with import.meta.resolve or require.resolve and read them with your migration tool:
import { readFile } from 'node:fs/promises'
const sql = await readFile(
new URL(import.meta.resolve('good-webhooks/migrations/delivery.sql')),
'utf8',
)The current schema version is 7, exported as POSTGRES_SCHEMA_VERSION. Every check() expects exactly that version for its component. Fresh-install assets never upgrade existing data; use getPostgresUpgrade for that.
getPostgresUpgrade
getPostgresUpgrade(options: PostgresUpgradeOptions): stringimport { writeFile } from 'node:fs/promises'
import { getPostgresUpgrade } from 'good-webhooks/migrations'
await writeFile(
'migrations/005-webhooks-schema-7.sql',
getPostgresUpgrade({ schema: 'webhooks', component: 'all', from: 6 }),
)Returns SQL that moves storage recorded at version from to the current version. It never connects to a database. The SQL first checks the recorded version and raises inside your transaction if it differs, so a wrong or repeated application rolls back cleanly.
Prop
Type
from is the version recorded in the database: 3, 4, 5, or 6. From 3, 4, or 5, the SQL applies each published step in order up to version 7. from: 7 throws INVALID_CONFIG because no upgrade is needed. Versions before 3 were never published and need fresh storage.
| Upgrade | Component | Shipped asset for public |
|---|---|---|
| 3 to 4 | all | good-webhooks/migrations/upgrades/004-all.sql |
management | good-webhooks/migrations/upgrades/004-management.sql | |
delivery | good-webhooks/migrations/upgrades/004-delivery.sql | |
| 4 to 5 | all | good-webhooks/migrations/upgrades/005-all.sql |
management | good-webhooks/migrations/upgrades/005-management.sql | |
delivery | good-webhooks/migrations/upgrades/005-delivery.sql | |
| 5 to 6 | all | good-webhooks/migrations/upgrades/006-all.sql |
management | good-webhooks/migrations/upgrades/006-management.sql | |
delivery | good-webhooks/migrations/upgrades/006-delivery.sql | |
| 6 to 7 | all | good-webhooks/migrations/upgrades/007-all.sql |
management | good-webhooks/migrations/upgrades/007-management.sql | |
delivery | good-webhooks/migrations/upgrades/007-delivery.sql |
What each asset changes:
- 3 to 4
all: records a version per component. Delivery rewrites its version row; management gains one.management: management in its own namespace. Creates the version table and recordsmanagement.delivery: delivery in its own namespace. Rewrites the version row.
- 4 to 5
all: adds the columns for automatic pauses to both components.management: addswebhook_endpoints.pause_reason.delivery: addsfailing_sinceandfailing_revisiontowebhook_endpoint_state.
- 5 to 6
all: adds the delivery retry window and the endpoint headers column.management: addswebhook_endpoints.encrypted_headers.delivery: addswebhook_deliveries.window_started_atand sets it tocreated_atfor every row.
- 6 to 7
all: indexes replays forprune()and records version7in both components.management: records version7only.delivery: adds the partial indexwebhook_deliveries_replays.
Shipped upgrade files never change, and each covers one step. For 3 to 4 in a shared namespace, apply all, or delivery before management; the management step needs the rewritten table and fails otherwise. The 4-to-5, 5-to-6, and 6-to-7 components can run in either order. Each upgrade adds only a version record, nullable columns, or an index, so processes on the previous package keep working until they restart. The 5-to-6 delivery step also fills window_started_at from created_at in one UPDATE under the ALTER TABLE lock, so it blocks reads and writes of webhook_deliveries for a time that grows with the table's row count. Check the count and any pruning backlog first. The 6-to-7 delivery step builds an index, which blocks writes to webhook_deliveries while it reads the table. The migrations guide shows how to build it concurrently first and gives the rollout order.
Schema names
A schema must be a nonblank, well-formed string of at most 63 UTF-8 bytes without null bytes. The generator and runtime quote names as PostgreSQL identifiers. Invalid names produce INVALID_CONFIG.
The schema passed to a runtime must match the corresponding migration. Management and delivery can use separate namespaces by generating each component separately. An injected management provider keeps its own configured namespace regardless of delivery options.
Better Auth management follows its adapter's schema workflow. These migrations cover the standalone PostgreSQL provider and delivery only. goodWebhooks({ schema: { modelName, fields } }) changes model and field names, not PostgreSQL placement.