Better migration locking behaviour

This commit is contained in:
2019-10-22 13:41:21 +02:00
parent 5aced1b8b1
commit 6ab4c1e4d3
6 changed files with 32 additions and 2 deletions

View File

@@ -2,4 +2,12 @@ CREATE TABLE IF NOT EXISTS migrations (
id integer GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
name text UNIQUE NOT NULL,
applied_at timestamptz NOT NULL DEFAULT CURRENT_TIMESTAMP
)
);
CREATE TABLE IF NOT EXISTS migrations_lock (
id integer PRIMARY KEY,
is_locked boolean NOT NULL DEFAULT false
);
INSERT INTO migrations_lock (id, is_locked) VALUES (1, false)
ON CONFLICT DO NOTHING;

3
sql/migrations/lock.sql Normal file
View File

@@ -0,0 +1,3 @@
WITH rows as (
UPDATE migrations_lock SET is_locked = true WHERE is_locked = false RETURNING 1
) SELECT COUNT(*) FROM rows;

View File

@@ -0,0 +1 @@
UPDATE migrations_lock SET is_locked = false WHERE is_locked = true;