From 731b6e1acb728e8056236a9bced9ba31263770ed Mon Sep 17 00:00:00 2001 From: trigramdev9 <98334141+trigramdev9@users.noreply.github.com> Date: Tue, 8 Feb 2022 02:47:03 -0700 Subject: [PATCH] feat: schema updates for admin site (#947) Co-authored-by: Joe Spencer --- packages/db/postgres/tables.sql | 39 +++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/packages/db/postgres/tables.sql b/packages/db/postgres/tables.sql index 2edd6257e4..46a5798376 100644 --- a/packages/db/postgres/tables.sql +++ b/packages/db/postgres/tables.sql @@ -1,3 +1,13 @@ +-- Auth key blocked status type is the type of blocking that has occurred on the api +-- key. These are primarily used by the admin app. +CREATE TYPE auth_key_blocked_status_type AS ENUM +( + -- The api key is blocked. + 'Blocked', + -- The api key is unblocked. + 'Unblocked' +); + -- A user of web3.storage. CREATE TABLE IF NOT EXISTS public.user ( @@ -34,6 +44,16 @@ CREATE TABLE IF NOT EXISTS auth_key CREATE INDEX IF NOT EXISTS auth_key_user_id_idx ON auth_key (user_id); +CREATE TABLE IF NOT EXISTS auth_key_history +( + id BIGSERIAL PRIMARY KEY, + status auth_key_blocked_status_type NOT NULL, + reason TEXT NOT NULL, + auth_key_id BIGSERIAL NOT NULL REFERENCES auth_key (id), + inserted_at TIMESTAMP WITH TIME ZONE DEFAULT timezone('utc'::text, now()) NOT NULL, + deleted_at TIMESTAMP WITH TIME ZONE +); + -- Details of the root of a file/directory stored on web3.storage. CREATE TABLE IF NOT EXISTS content ( @@ -53,7 +73,7 @@ CREATE UNIQUE INDEX content_cid_with_size_idx ON content (cid) INCLUDE (dag_size DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM pg_type WHERE typname = 'pin_status_type') THEN - + -- IPFS Cluster tracker status values. -- https://github.com/ipfs/ipfs-cluster/blob/54c3608899754412861e69ee81ca8f676f7e294b/api/types.go#L52-L83 -- TODO: nft.storage only using a subset of these: https://github.com/ipfs-shipyard/nft.storage/blob/main/packages/api/db/tables.sql#L2-L7 @@ -248,4 +268,19 @@ CREATE TABLE IF NOT EXISTS pinning_authorization user_id BIGINT NOT NULL REFERENCES public.user (id), inserted_at TIMESTAMP WITH TIME ZONE DEFAULT timezone('utc'::text, now()) NOT NULL, deleted_at TIMESTAMP WITH TIME ZONE -) +); + +CREATE VIEW admin_search as +select + u.id::text as user_id, + u.email as email, + ak.secret as token, + ak.id::text as token_id, + ak.deleted_at as deleted_at, + akh.inserted_at as reason_inserted_at, + akh.reason as reason, + akh.status as status +from public.user u +right join auth_key ak on ak.user_id = u.id +full outer join (select * from auth_key_history where deleted_at is null) as akh on akh.auth_key_id = ak.id +where ak.deleted_at is NULL or ak.deleted_at is not NULL and akh.status is not NULL;