From 22b222ad320e9933f01d9c59fc5b59b4fb05fa53 Mon Sep 17 00:00:00 2001 From: Paolo Chillari Date: Thu, 18 Nov 2021 15:05:58 +0000 Subject: [PATCH] fix: prevent table.sql to fail if a type already exists * fix: create new types only if they do not exist * chore: housekeeping --- packages/db/postgres/tables.sql | 94 ++++++++++++++++++--------------- 1 file changed, 51 insertions(+), 43 deletions(-) diff --git a/packages/db/postgres/tables.sql b/packages/db/postgres/tables.sql index 56153310ee..b0f87495ed 100644 --- a/packages/db/postgres/tables.sql +++ b/packages/db/postgres/tables.sql @@ -49,37 +49,57 @@ CREATE INDEX IF NOT EXISTS content_updated_at_idx ON content (updated_at); -- TODO: Sync with @ribasushi as we can start using this as the primary key CREATE UNIQUE INDEX content_cid_with_size_idx ON content (cid) INCLUDE (dag_size); --- 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 -CREATE TYPE pin_status_type AS ENUM -( - -- Should never see this value. When used as a filter. It means "all". - 'Undefined', - -- The cluster node is offline or not responding. - 'ClusterError', - -- An error occurred pinning. - 'PinError', - -- An error occurred unpinning. - 'UnpinError', - -- The IPFS daemon has pinned the item. - 'Pinned', - -- The IPFS daemon is currently pinning the item. - 'Pinning', - -- The IPFS daemon is currently unpinning the item. - 'Unpinning', - -- The IPFS daemon is not pinning the item. - 'Unpinned', - -- The IPFS daemon is not pinning the item but it is being tracked. - 'Remote', - -- The item has been queued for pinning on the IPFS daemon. - 'PinQueued', - -- The item has been queued for unpinning on the IPFS daemon. - 'UnpinQueued', - -- The IPFS daemon is not pinning the item through this CID but it is tracked - -- in a cluster dag - 'Sharded' -); +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 + CREATE TYPE pin_status_type AS ENUM + ( + -- Should never see this value. When used as a filter. It means "all". + 'Undefined', + -- The cluster node is offline or not responding. + 'ClusterError', + -- An error occurred pinning. + 'PinError', + -- An error occurred unpinning. + 'UnpinError', + -- The IPFS daemon has pinned the item. + 'Pinned', + -- The IPFS daemon is currently pinning the item. + 'Pinning', + -- The IPFS daemon is currently unpinning the item. + 'Unpinning', + -- The IPFS daemon is not pinning the item. + 'Unpinned', + -- The IPFS daemon is not pinning the item but it is being tracked. + 'Remote', + -- The item has been queued for pinning on the IPFS daemon. + 'PinQueued', + -- The item has been queued for unpinning on the IPFS daemon. + 'UnpinQueued', + -- The IPFS daemon is not pinning the item through this CID but it is tracked + -- in a cluster dag + 'Sharded' + ); + END IF; + IF NOT EXISTS (SELECT 1 FROM pg_type WHERE typname = 'upload_type') THEN + -- Upload type is the type of received upload data. + CREATE TYPE upload_type AS ENUM + ( + -- A CAR file upload. + 'Car', + -- Files uploaded and converted into a CAR file. + 'Upload', + -- A raw blob upload in the request body. + 'Blob', + -- A multi file upload using a multipart request. + 'Multipart' + ); + END IF; +END$$; -- An IPFS node that is pinning content. CREATE TABLE IF NOT EXISTS pin_location @@ -114,18 +134,6 @@ CREATE INDEX IF NOT EXISTS pin_updated_at_idx ON pin (updated_at); CREATE INDEX IF NOT EXISTS pin_status_idx ON pin (status); CREATE INDEX IF NOT EXISTS pin_composite_pinned_at_idx ON pin (content_cid, updated_at) WHERE status = 'Pinned'; --- Upload type is the type of received upload data. -CREATE TYPE upload_type AS ENUM -( - -- A CAR file upload. - 'Car', - -- Files uploaded and converted into a CAR file. - 'Upload', - -- A raw blob upload in the request body. - 'Blob', - -- A multi file upload using a multipart request. - 'Multipart' -); -- An upload created by a user. CREATE TABLE IF NOT EXISTS upload