Migration lock row recipe
Two deploy pipelines migrating the same database at once is how schemas half-apply. A lock row in the database makes runners serialize, with no external infrastructure.
The task
Two deploy jobs started together and both began altering the same schema. The runners need a mutual lock that lives in the database rather than in a separate coordinator.
The snippet
Written for PostgreSQL. Versions for SQL Server and MySQL are under Variations.
CREATE TABLE IF NOT EXISTS migration_lock (
lock_name text PRIMARY KEY,
acquired_at timestamptz NOT NULL DEFAULT now(),
owner text NOT NULL
);
-- try to claim the lock: a row back means we got it,
-- no row means someone else holds it
INSERT INTO migration_lock (lock_name, owner)
VALUES ('default', 'runner-7')
ON CONFLICT (lock_name) DO NOTHING
RETURNING owner, acquired_at;
-- see who holds it and since when
SELECT owner, acquired_at FROM migration_lock WHERE lock_name = 'default';
-- release when done
DELETE FROM migration_lock WHERE lock_name = 'default' AND owner = 'runner-7';
Как это работает
The INSERT is the lock: the primary key lets exactly one runner's row in, and for everyone else ON CONFLICT DO NOTHING inserts nothing, so RETURNING comes back empty. Run the INSERT in its own committed transaction so other runners can see the row. Releasing is the DELETE, guarded by owner so a runner cannot remove someone else's claim. A crashed runner never deletes its row, so treat claims older than a timeout as abandoned. Run each migration in a transaction where the engine allows it; MySQL commits DDL implicitly, so a failed migration there can stop halfway.
Variations
MySQL
MySQLINSERT IGNORE INTO migration_lock (lock_name, owner)
VALUES ('default', 'runner-7');
SELECT ROW_COUNT(); -- 1 = claimed, 0 = already held
-- release:
DELETE FROM migration_lock
WHERE lock_name = 'default' AND owner = 'runner-7';
SQL Server
SQL Server-- UPDLOCK, HOLDLOCK stops two runners passing the NOT EXISTS
-- check at the same moment; the PK on LockName backs it up
INSERT INTO dbo.MigrationLock (LockName, Owner)
SELECT 'default', 'runner-7'
WHERE NOT EXISTS (
SELECT 1 FROM dbo.MigrationLock WITH (UPDLOCK, HOLDLOCK)
WHERE LockName = 'default');
SELECT @@ROWCOUNT AS claimed; -- 1 = claimed, 0 = held elsewhere
Questions that come up
Why not use GET_LOCK or sp_getapplock instead?
Often you should. GET_LOCK, sp_getapplock and PostgreSQL's pg_advisory_lock are released automatically when the session ends, so a crashed runner can never leave a stale lock. They only last as long as one connection, though, and tell you little about the holder beyond a session id. A lock row records who took it and when, and survives across connections, at the cost of cleaning up after crashed runners yourself.
Keep reading
Related on SQLly
Run these with autocomplete that knows your tables
SQLly is a native SQL workbench for macOS, Windows and Linux. Paste a snippet into a live connection and you get schema-aware IntelliSense, a plan viewer and guards against unsafe edits.