Skip to Content

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): string
scripts/write-migration.ts
import { 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

ComponentEquivalent shipped asset for public
managementgood-webhooks/migrations/management.sql
deliverygood-webhooks/migrations/delivery.sql
allgood-webhooks/migrations/001-initial.sql

Tables each component creates:

  • management: webhook_schema_version (if absent) and webhook_endpoints.
  • delivery: webhook_schema_version (if absent), webhook_endpoint_state, webhook_events, webhook_deliveries, and webhook_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): string
scripts/write-upgrade.ts
import { 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.

UpgradeComponentShipped asset for public
3 to 4allgood-webhooks/migrations/upgrades/004-all.sql
managementgood-webhooks/migrations/upgrades/004-management.sql
deliverygood-webhooks/migrations/upgrades/004-delivery.sql
4 to 5allgood-webhooks/migrations/upgrades/005-all.sql
managementgood-webhooks/migrations/upgrades/005-management.sql
deliverygood-webhooks/migrations/upgrades/005-delivery.sql
5 to 6allgood-webhooks/migrations/upgrades/006-all.sql
managementgood-webhooks/migrations/upgrades/006-management.sql
deliverygood-webhooks/migrations/upgrades/006-delivery.sql
6 to 7allgood-webhooks/migrations/upgrades/007-all.sql
managementgood-webhooks/migrations/upgrades/007-management.sql
deliverygood-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 records management.
    • delivery: delivery in its own namespace. Rewrites the version row.
  • 4 to 5
    • all: adds the columns for automatic pauses to both components.
    • management: adds webhook_endpoints.pause_reason.
    • delivery: adds failing_since and failing_revision to webhook_endpoint_state.
  • 5 to 6
    • all: adds the delivery retry window and the endpoint headers column.
    • management: adds webhook_endpoints.encrypted_headers.
    • delivery: adds webhook_deliveries.window_started_at and sets it to created_at for every row.
  • 6 to 7
    • all: indexes replays for prune() and records version 7 in both components.
    • management: records version 7 only.
    • delivery: adds the partial index webhook_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.

On this page