-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdd.sql
More file actions
64 lines (56 loc) · 2.31 KB
/
Copy pathdd.sql
File metadata and controls
64 lines (56 loc) · 2.31 KB
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
58
59
60
61
62
63
64
-- fmsg-webapi database extensions.
--
-- This file extends the fmsgd schema at:
-- https://github.com/markmnl/fmsgd/blob/main/dd.sql
--
-- fmsg-webapi assumes fmsgd is the fmsg host implementation and uses the same
-- PostgreSQL database.
CREATE TABLE IF NOT EXISTS fmsg_api_sub_account (
owner_addr varchar(255) NOT NULL,
agent varchar(64) NOT NULL,
sub_addr varchar(255),
grant_type text NOT NULL DEFAULT 'derived_sub_account',
display_name text,
key_id varchar(64),
key_hash bytea,
allowed_cidrs cidr[],
key_expires_at timestamptz,
max_sub_accounts int NOT NULL DEFAULT 5,
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now(),
PRIMARY KEY (owner_addr, agent),
UNIQUE (key_id),
CHECK (max_sub_accounts > 0),
CHECK (grant_type IN ('derived_sub_account', 'delegated_identity')),
CHECK (
(agent = '' AND sub_addr IS NULL AND display_name IS NULL AND key_id IS NULL AND key_hash IS NULL AND allowed_cidrs IS NULL AND key_expires_at IS NULL)
OR
(agent <> '' AND sub_addr IS NOT NULL AND key_id IS NOT NULL AND key_hash IS NOT NULL AND allowed_cidrs IS NOT NULL AND cardinality(allowed_cidrs) > 0 AND key_expires_at IS NOT NULL)
),
CHECK (agent = '' OR agent NOT LIKE '%\_%' ESCAPE '\')
);
ALTER TABLE fmsg_api_sub_account
ADD COLUMN IF NOT EXISTS grant_type text NOT NULL DEFAULT 'derived_sub_account';
ALTER TABLE fmsg_api_sub_account
ADD COLUMN IF NOT EXISTS display_name text;
ALTER TABLE fmsg_api_sub_account
DROP CONSTRAINT IF EXISTS fmsg_api_sub_account_sub_addr_key;
DO $$
BEGIN
IF NOT EXISTS (
SELECT 1
FROM pg_constraint
WHERE conname = 'fmsg_api_sub_account_grant_type_check'
) THEN
ALTER TABLE fmsg_api_sub_account
ADD CONSTRAINT fmsg_api_sub_account_grant_type_check
CHECK (grant_type IN ('derived_sub_account', 'delegated_identity'));
END IF;
END $$;
CREATE INDEX IF NOT EXISTS fmsg_api_sub_account_owner_idx
ON fmsg_api_sub_account ((lower(owner_addr)));
CREATE INDEX IF NOT EXISTS fmsg_api_sub_account_sub_idx
ON fmsg_api_sub_account ((lower(sub_addr)));
CREATE UNIQUE INDEX IF NOT EXISTS fmsg_api_sub_account_owner_sub_unique
ON fmsg_api_sub_account ((lower(owner_addr)), (lower(sub_addr)))
WHERE agent <> '';