Skip to Content

Migrations

Pick the SQL for the components you run, apply it with your own migration runner, and keep runtime schema names in sync.

Good Webhooks never applies DDL. getPostgresMigration() returns SQL text, the shipped .sql files hold the same SQL for the public schema, and your migration runner applies it once.

Which SQL do I need?

SetupManagementDelivery
Standalone, one schema001-initial.sqlsame file
Standalone management onlymanagement.sqlnone
Better Auth onlyBetter Auth or ORMnone
Better Auth with deliveryBetter Auth or ORMdelivery.sql
Separate schemasgeneratedgenerated
  • Standalone, one schema: standalone management and delivery in the same schema. 001-initial.sql holds both components.
  • Standalone management only: apply management.sql. There is no delivery SQL.
  • Better Auth only: Better Auth management only. Use the Better Auth or ORM schema workflow. There is no delivery SQL.
  • Better Auth with delivery: Better Auth management with Good Webhooks delivery. Management comes from the Better Auth or ORM schema workflow; apply delivery.sql for delivery.
  • Separate schemas: management and delivery in separate schemas. Generate management SQL with component: 'management' and delivery SQL with component: 'delivery'.

Never apply the combined file and a component file to the same installation.

Generate SQL

import { getPostgresMigration } from 'good-webhooks/migrations'

const sql = getPostgresMigration() // both components, schema 'public'
const deliveryOnly = getPostgresMigration({ component: 'delivery', schema: 'notifications' })

Or from the shell:

node -e 'import("good-webhooks/migrations").then(m => process.stdout.write(m.getPostgresMigration()))' > 001-webhooks.sql
psql "$DATABASE_URL" --single-transaction -v ON_ERROR_STOP=1 -f 001-webhooks.sql

The SQL has no BEGIN or COMMIT, so wrap it in one transaction with your migration ledger entry. Table creation is not idempotent. Migration tools that read files can resolve the shipped files, for example import.meta.resolve('good-webhooks/migrations/delivery.sql').

Match the runtime schema

The schema you generate for must be the schema you configure at runtime:

import { createWebhooks } from 'good-webhooks'
import { getPostgresMigration } from 'good-webhooks/migrations'

const schema = 'notifications'
const sql = getPostgresMigration({ schema }) // apply with your migration tool

const webhooks = createWebhooks({ database: pool, schema, encryptionKey, events })

Management and delivery can use different schemas or different databases:

const management = createPostgresManagement({
  database: managementDatabase,
  schema: 'management',
  eventTypes: Object.keys(events),
  encryptionKey,
})
const delivery = createDelivery({
  database: deliveryDatabase,
  schema: 'delivery',
  source: management.source,
  events,
})

An injected management provider keeps its own schema; delivery's schema option never moves it. For Better Auth, generate only the delivery SQL.

Call await webhooks.check() at startup in the app and in every worker. It verifies the tables and that each component records schema version 7, and throws SCHEMA_MISMATCH naming the SQL to apply. It never changes anything.

Upgrades

Installing a new package never runs SQL. When a release needs a newer schema, its notes say so, and check() on the new package refuses to start until the upgrade is applied.

Generate the upgrade from the version your database records:

import { getPostgresUpgrade } from 'good-webhooks/migrations'

const sql = getPostgresUpgrade({ from: 6 }) // both components in 'public'

The shipped equivalent is good-webhooks/migrations/upgrades/007-all.sql. From an older version, from: 3, 4, or 5 applies each published step from that version in order, up to 7. Separate namespaces take component with their own schema. The SQL checks the recorded version first, so applying it twice or against the wrong version rolls back.

The 5-to-6 step fills webhook_deliveries.window_started_at from created_at for every existing delivery, under the lock its ALTER TABLE takes. Producers and workers that touch the deliveries table wait until it commits, for a time that grows with the table's row count. prune() keeps the table near retentionMs only when it runs, so check the row count and any pruning backlog before you apply the step. Existing deliveries keep the age they already had.

The 6-to-7 step adds the index webhook_deliveries_replays, which prune() needs to delete history quickly (#153). Building it blocks writes to the deliveries table while it reads the table once. On a large table, build it first outside a transaction, with exactly this definition, then apply the step as usual:

CREATE INDEX CONCURRENTLY IF NOT EXISTS webhook_deliveries_replays
  ON "public"."webhook_deliveries"(scope_key, replay_of) WHERE replay_of IS NOT NULL;

The step then keeps that index. If the concurrent build fails, it leaves an invalid index, and the step refuses to run until you drop it with DROP INDEX "public"."webhook_deliveries_replays".

Rollout order

  1. Deploy nothing yet. Apply the upgrade SQL with your migration runner in one transaction. Processes on the previous package keep running when the release notes say the upgrade is compatible with them. The 3 to 4, 4 to 5, 5 to 6, and 6 to 7 upgrades are. With Better Auth management, also run your Better Auth migration in this step. 0.2.0 added the optional pauseReason field to the endpoint model, and 0.5.0 adds the optional encryptedHeaders field.
  2. Restart producers on the new package. Their check() now passes.
  3. Restart workers and cleanup jobs on the new package. A restarted process on the old package fails fast with SCHEMA_MISMATCH, so finish the rollout rather than mixing versions for long.

A process that skips check() may run unsupported queries against a schema it doesn't know. CI rehearses this order against the previously published package. Every option is in the migration reference.

On this page