-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsetup.sql
57 lines (52 loc) · 1.7 KB
/
setup.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
CREATE TABLE IF NOT EXISTS proofs (
request_id UUID PRIMARY KEY,
proof_type SMALLINT NOT NULL,
status SMALLINT DEFAULT 0,
circuit_name VARCHAR(255) NOT NULL,
onchain BOOLEAN NOT NULL,
created_at TIMESTAMP WITH TIME ZONE,
witness_generated_at TIMESTAMP WITH TIME ZONE,
proof_generated_at TIMESTAMP WITH TIME ZONE,
proof JSON,
endpoint_type VARCHAR(128),
endpoint VARCHAR(128),
public_inputs TEXT[],
reason TEXT,
identifier VARCHAR(255)
);
CREATE OR REPLACE FUNCTION status_update_notify() RETURNS trigger AS $$
DECLARE
notification_payload JSON;
BEGIN
IF (TG_OP = 'UPDATE' AND NEW.status IS DISTINCT FROM OLD.status) OR TG_OP = 'INSERT' THEN
notification_payload = json_build_object(
'request_id', NEW.request_id,
'proof_type', NEW.proof_type,
'status', NEW.status,
'created_at', NEW.created_at,
'circuit_name', NEW.circuit_name,
'onchain', NEW.onchain,
'witness_generated_at', NEW.witness_generated_at,
'proof_generated_at', NEW.proof_generated_at,
'proof', NEW.proof,
'endpoint_type', NEW.endpoint_type,
'endpoint', NEW.endpoint,
'public_inputs', NEW.public_inputs,
'reason', NEW.reason,
'identifier', NEW.identifier
);
PERFORM pg_notify('status_update', notification_payload::text);
END IF;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
DROP TRIGGER IF EXISTS status_update_notify ON proofs;
CREATE TRIGGER status_update_notify
AFTER UPDATE ON proofs
FOR EACH ROW
EXECUTE PROCEDURE status_update_notify();
DROP TRIGGER IF EXISTS status_insert_notify ON proofs;
CREATE TRIGGER status_insert_notify
AFTER INSERT ON proofs
FOR EACH ROW
EXECUTE PROCEDURE status_update_notify();