Skip to main content

Function: syncWithAdvisoryLock()

syncWithAdvisoryLock(__namedParameters): Promise<void>

Defined in: syncWithAdvisoryLock.ts:66

Serializes a boot-time sequelize.sync() across concurrently-starting application instances using a Postgres session-level advisory lock (pg_advisory_lock).

Any app that runs sequelize.sync({ alter: true }) on boot behind more than one instance (rolling deploys, auto-scale-out, instance refresh) faces a race: the DDL runs concurrently against the same database and can deadlock, error, or leave the schema inconsistent. This helper ensures only one instance runs the sync at a time; the others block until the holder finishes and then run against the already-migrated schema (a no-op).

The lock is acquired and released on a single dedicated connection — a naive sequelize.query() lock/unlock pair can land on different pooled backends and try to unlock a lock the current connection never held. The lock is always released on both the success and failure paths.

Parameters

ParameterType
__namedParametersSyncWithAdvisoryLockOptions

Returns

Promise<void>

Example

await syncWithAdvisoryLock({
sequelize,
key: 0x50a7_5c_00, // stable, caller-chosen 64-bit key
sync: { alter: true },
});