Make object recovery settlement-safe
This commit is contained in:
parent
cc23211e63
commit
cd83841520
1 changed files with 1054 additions and 90 deletions
33
sql/003_recovery_settlement.sql
Normal file
33
sql/003_recovery_settlement.sql
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
ALTER TABLE mutations
|
||||
ADD COLUMN IF NOT EXISTS dispatch_started_at TIMESTAMPTZ,
|
||||
ADD COLUMN IF NOT EXISTS request_deadline_at TIMESTAMPTZ,
|
||||
ADD COLUMN IF NOT EXISTS worker_lease_until TIMESTAMPTZ,
|
||||
ADD COLUMN IF NOT EXISTS settled_at TIMESTAMPTZ;
|
||||
|
||||
DO $$
|
||||
BEGIN
|
||||
IF EXISTS (
|
||||
SELECT 1
|
||||
FROM mutations
|
||||
WHERE external_state = 'pending'
|
||||
) THEN
|
||||
RAISE EXCEPTION
|
||||
'recovery settlement migration requires no pending mutations';
|
||||
END IF;
|
||||
END
|
||||
$$;
|
||||
|
||||
ALTER TABLE mutations
|
||||
ADD CONSTRAINT mutations_dispatch_timing_check CHECK (
|
||||
(external_state <> 'pending') OR
|
||||
(dispatch_started_at IS NOT NULL AND
|
||||
request_deadline_at IS NOT NULL AND
|
||||
worker_lease_until IS NOT NULL)
|
||||
) NOT VALID;
|
||||
|
||||
ALTER TABLE mutations
|
||||
VALIDATE CONSTRAINT mutations_dispatch_timing_check;
|
||||
|
||||
CREATE INDEX IF NOT EXISTS mutations_recovery_idx
|
||||
ON mutations (worker_lease_until, request_deadline_at, updated_at)
|
||||
WHERE state IN ('prepared', 'applying');
|
||||
Loading…
Reference in a new issue