diff --git a/db/migrations/mirror/000021_add_moshicam_schema.up.sql b/db/migrations/mirror/000021_add_moshicam_schema.up.sql new file mode 100644 index 000000000..600bc1b4e --- /dev/null +++ b/db/migrations/mirror/000021_add_moshicam_schema.up.sql @@ -0,0 +1,26 @@ +/* {% require_sudo %} */ + +-- Create schema +create schema if not exists moshicam; + +-- Limit all access to defined roles +revoke all on schema moshicam from public; + +alter default privileges for role access_rw in schema moshicam grant select on tables to access_ro; +alter default privileges for role access_rw in schema moshicam grant usage on sequences to access_ro; +alter default privileges for role access_rw in schema moshicam grant execute on functions to access_ro; + +-------------------------------------------------------------------------------------- +-- access_rw: has read/write access to all current and future objects in all schemas +-------------------------------------------------------------------------------------- +-- Grant usage + create privileges on all schemas +grant all on schema moshicam to access_rw; + +-- Make sure access_rw owns all schemas +alter schema moshicam owner to access_rw; + +-- Grant read/write privileges on all current objects in the schemas +grant all on all tables in schema moshicam to access_rw; +grant all on all sequences in schema moshicam to access_rw; +grant all on all functions in schema moshicam to access_rw; + diff --git a/db/migrations/mirror/000022_ksuid_function.up.sql b/db/migrations/mirror/000022_ksuid_function.up.sql new file mode 100644 index 000000000..e3b97f8e3 --- /dev/null +++ b/db/migrations/mirror/000022_ksuid_function.up.sql @@ -0,0 +1,50 @@ +/* {% require_sudo %} */ + +CREATE EXTENSION IF NOT EXISTS pgcrypto; + +create or replace function ksuid() returns text as $$ +declare + v_time timestamp with time zone := null; + v_seconds numeric(50) := null; + v_numeric numeric(50) := null; + v_epoch numeric(50) = 1400000000; -- 2014-05-13T16:53:20Z + v_payload bytea := null; + v_base62 text := ''; + v_alphabet char array[62] := array[ + '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', + 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', + 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', + 'U', 'V', 'W', 'X', 'Y', 'Z', + 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', + 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', + 'u', 'v', 'w', 'x', 'y', 'z']; + i integer := 0; +begin + + -- Get the current time + v_time := clock_timestamp(); + + -- Extract the epoch seconds + v_seconds := EXTRACT(EPOCH FROM v_time) - v_epoch; + + -- Generate a KSUID in a numeric variable + v_numeric := v_seconds * pow(2::numeric(50), 128); -- 32 bits for seconds + + -- Add 128 random bits to it + v_payload := gen_random_bytes(16); + while i < 16 loop + i := i + 1; + v_numeric := v_numeric + (get_byte(v_payload, i - 1)::numeric(50) * pow(2::numeric(50), (16 - i) * 8)); + end loop; + + -- Encode it to base-62 + while v_numeric <> 0 loop + v_base62 := v_base62 || v_alphabet[mod(v_numeric, 62) + 1]; + v_numeric := div(v_numeric, 62); + end loop; + v_base62 := reverse(v_base62); + v_base62 := lpad(v_base62, 27, '0'); + + return v_base62; + +end $$ language plpgsql; diff --git a/db/migrations/mirror/000023_add_token_tags.up.sql b/db/migrations/mirror/000023_add_token_tags.up.sql new file mode 100644 index 000000000..a0aba58a7 --- /dev/null +++ b/db/migrations/mirror/000023_add_token_tags.up.sql @@ -0,0 +1,11 @@ +create table if not exists moshicam.token_tags ( + id varchar(255) not null primary key, + deleted boolean default false not null, + last_updated timestamp with time zone default current_timestamp not null, + created_at timestamp with time zone default current_timestamp not null, + contract_address text not null, + token_id decimal not null, + tag_name text not null +); +create unique index if not exists token_tags_contract_token_tag_idx on moshicam.token_tags(tag_name, contract_address, token_id) where not deleted; +create index if not exists token_tags_contract_token_idx on moshicam.token_tags(contract_address, token_id) where not deleted;