Skip to content

Commit

Permalink
fix: prevent table.sql to fail if a type already exists
Browse files Browse the repository at this point in the history
* fix: create new types only if they do not exist

* chore: housekeeping
  • Loading branch information
flea89 authored Nov 18, 2021
1 parent 46a3f1c commit 22b222a
Showing 1 changed file with 51 additions and 43 deletions.
94 changes: 51 additions & 43 deletions packages/db/postgres/tables.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 22b222a

Please sign in to comment.