Skip to content

Commit

Permalink
update DB sql schemas
Browse files Browse the repository at this point in the history
  • Loading branch information
SpaghettiOverload committed Aug 23, 2023
1 parent aaadd70 commit b47a2a8
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 33 deletions.
36 changes: 24 additions & 12 deletions database/scheme/14-cosmwasm.sql
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
CREATE TABLE cosmwasm_store
(
transaction_hash TEXT NOT NULL REFERENCES transaction (hash),
transaction_hash TEXT NOT NULL,
partition_id BIGINT NOT NULL,
index BIGINT NOT NULL,
sender TEXT NOT NULL,
instantiate_permission JSONB DEFAULT '{}'::JSONB,
result_code_id TEXT,
success BOOLEAN NOT NULL,
PRIMARY KEY(transaction_hash, index)
PRIMARY KEY(transaction_hash, index),
FOREIGN KEY(transaction_hash, partition_id) REFERENCES transaction (hash, partition_id)
);

CREATE INDEX cosmwasm_store_sender_index ON cosmwasm_store (sender);
CREATE INDEX cosmwasm_store_result_code_id_index ON cosmwasm_store (result_code_id);

CREATE TABLE cosmwasm_instantiate
(
transaction_hash TEXT NOT NULL REFERENCES transaction (hash),
transaction_hash TEXT NOT NULL,
partition_id BIGINT NOT NULL,
index BIGINT NOT NULL,
admin TEXT,
funds JSONB DEFAULT '[]'::JSONB,
Expand All @@ -23,7 +26,8 @@ CREATE TABLE cosmwasm_instantiate
code_id TEXT NOT NULL,
result_contract_address TEXT,
success BOOLEAN NOT NULL,
PRIMARY KEY(transaction_hash, index)
PRIMARY KEY(transaction_hash, index),
FOREIGN KEY(transaction_hash, partition_id) REFERENCES transaction (hash, partition_id)
);

CREATE INDEX cosmwasm_instantiate_label_index ON cosmwasm_instantiate (label);
Expand All @@ -33,15 +37,17 @@ CREATE INDEX cosmwasm_instantiate_result_contract_address_index ON cosmwasm_inst

CREATE TABLE cosmwasm_execute
(
transaction_hash TEXT NOT NULL REFERENCES transaction (hash),
transaction_hash TEXT NOT NULL,
partition_id BIGINT NOT NULL,
index BIGINT NOT NULL,
method TEXT NOT NULL,
arguments JSONB DEFAULT '{}'::JSONB,
funds JSONB DEFAULT '[]'::JSONB,
sender TEXT NOT NULL,
contract TEXT NOT NULL,
success BOOLEAN NOT NULL,
PRIMARY KEY(transaction_hash, index)
PRIMARY KEY(transaction_hash, index),
FOREIGN KEY(transaction_hash, partition_id) REFERENCES transaction (hash, partition_id)
);

CREATE INDEX cosmwasm_execute_method_index ON cosmwasm_execute (method);
Expand All @@ -50,14 +56,16 @@ CREATE INDEX cosmwasm_execute_contract_index ON cosmwasm_execute (contract);

CREATE TABLE cosmwasm_migrate
(
transaction_hash TEXT NOT NULL REFERENCES transaction (hash),
transaction_hash TEXT NOT NULL,
partition_id BIGINT NOT NULL,
index BIGINT NOT NULL,
sender TEXT NOT NULL,
contract TEXT NOT NULL,
code_id TEXT NOT NULL,
arguments JSONB DEFAULT '{}'::JSONB,
success BOOLEAN NOT NULL,
PRIMARY KEY(transaction_hash, index)
PRIMARY KEY(transaction_hash, index),
FOREIGN KEY(transaction_hash, partition_id) REFERENCES transaction (hash, partition_id)
);

CREATE INDEX cosmwasm_migrate_sender_index ON cosmwasm_migrate (sender);
Expand All @@ -66,13 +74,15 @@ CREATE INDEX cosmwasm_migrate_code_id_index ON cosmwasm_migrate (code_id);

CREATE TABLE cosmwasm_update_admin
(
transaction_hash TEXT NOT NULL REFERENCES transaction (hash),
transaction_hash TEXT NOT NULL,
partition_id BIGINT NOT NULL,
index BIGINT NOT NULL,
sender TEXT NOT NULL,
contract TEXT NOT NULL,
new_admin TEXT NOT NULL,
success BOOLEAN NOT NULL,
PRIMARY KEY(transaction_hash, index)
PRIMARY KEY(transaction_hash, index),
FOREIGN KEY(transaction_hash, partition_id) REFERENCES transaction (hash, partition_id)
);

CREATE INDEX cosmwasm_update_admin_sender_index ON cosmwasm_update_admin (sender);
Expand All @@ -81,12 +91,14 @@ CREATE INDEX cosmwasm_update_admin_new_admin_index ON cosmwasm_update_admin (new

CREATE TABLE cosmwasm_clear_admin
(
transaction_hash TEXT NOT NULL REFERENCES transaction (hash),
transaction_hash TEXT NOT NULL,
partition_id BIGINT NOT NULL,
index BIGINT NOT NULL,
sender TEXT NOT NULL,
contract TEXT NOT NULL,
success BOOLEAN NOT NULL,
PRIMARY KEY(transaction_hash, index)
PRIMARY KEY(transaction_hash, index),
FOREIGN KEY(transaction_hash, partition_id) REFERENCES transaction (hash, partition_id)
);

CREATE INDEX cosmwasm_clear_admin_sender_index ON cosmwasm_clear_admin (sender);
Expand Down
8 changes: 5 additions & 3 deletions database/scheme/15-gravity.sql
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ CREATE TABLE gravity_transaction
receiver TEXT NOT NULL,
votes INTEGER NOT NULL,
consensus BOOLEAN NOT NULL,
transaction_hash TEXT NOT NULL REFERENCES transaction (hash),
transaction_hash TEXT NOT NULL,
partition_id BIGINT NOT NULL,
height BIGINT NOT NULL REFERENCES block (height),
PRIMARY KEY(attestation_id, orchestrator)
PRIMARY KEY(attestation_id, orchestrator),
FOREIGN KEY(transaction_hash, partition_id) REFERENCES transaction (hash, partition_id)
);

CREATE INDEX gravity_transaction_receiver_index ON gravity_transaction (receiver);
Expand All @@ -29,7 +31,7 @@ CREATE FUNCTION gravity_messages_by_address(
"offset" BIGINT = 0)
RETURNS SETOF message AS
$$
SELECT m.transaction_hash, m.index, m.type, m.value, m.involved_accounts_addresses
SELECT m.transaction_hash, m.index, m.type, m.value, m.involved_accounts_addresses, m.partition_id, m.height
FROM message m
JOIN gravity_transaction t on m.transaction_hash = t.transaction_hash
WHERE t.receiver = receiver_addr AND t.orchestrator = ANY(m.involved_accounts_addresses)
Expand Down
12 changes: 8 additions & 4 deletions database/scheme/18-nft_module.sql
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@

CREATE TABLE nft_denom
(
transaction_hash TEXT NOT NULL REFERENCES transaction (hash),
transaction_hash TEXT NOT NULL,
partition_id BIGINT NOT NULL,
id TEXT NOT NULL,
name TEXT NOT NULL,
schema TEXT NOT NULL,
symbol TEXT NOT NULL,
owner TEXT NOT NULL,
contract_address_signer TEXT NOT NULL,
PRIMARY KEY(id)
PRIMARY KEY(id),
FOREIGN KEY(transaction_hash, partition_id) REFERENCES transaction (hash, partition_id)
);

CREATE INDEX nft_denom_owner_index ON nft_denom (owner);

CREATE TABLE nft_nft
(
transaction_hash TEXT NOT NULL REFERENCES transaction (hash),
transaction_hash TEXT NOT NULL,
partition_id BIGINT NOT NULL,
id BIGINT NOT NULL,
denom_id TEXT NOT NULL REFERENCES nft_denom (id),
name TEXT NOT NULL,
Expand All @@ -26,7 +29,8 @@ CREATE TABLE nft_nft
sender TEXT NOT NULL,
contract_address_signer TEXT NOT NULL,
burned BOOLEAN DEFAULT FALSE,
PRIMARY KEY(id, denom_id)
PRIMARY KEY(id, denom_id),
FOREIGN KEY(transaction_hash, partition_id) REFERENCES transaction (hash, partition_id)
);

CREATE INDEX nft_nft_owner_index ON nft_nft (owner);
Expand Down
10 changes: 6 additions & 4 deletions database/scheme/19-distinct_message_query_func.sql
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
CREATE TABLE distinct_message
(
transaction_hash TEXT NOT NULL REFERENCES transaction (hash),
transaction_hash TEXT NOT NULL,
height BIGINT,
index BIGINT,
type TEXT,
value JSONB,
involved_accounts_addresses TEXT[]
involved_accounts_addresses TEXT[],
partition_id BIGINT NOT NULL,
FOREIGN KEY(transaction_hash, partition_id) REFERENCES transaction (hash, partition_id)
);

CREATE FUNCTION messages_by_address_distinct_on_tx_hash(
Expand All @@ -15,11 +17,11 @@ CREATE FUNCTION messages_by_address_distinct_on_tx_hash(
"offset" BIGINT = 0)
RETURNS SETOF distinct_message AS
$$
SELECT DISTINCT ON(height, message.transaction_hash) message.transaction_hash, height, message.index, message.type, message.value, message.involved_accounts_addresses
SELECT DISTINCT ON(message.height, message.transaction_hash) message.transaction_hash, message.height, message.index, message.type, message.value, message.involved_accounts_addresses, message.partition_id
FROM message
JOIN transaction t on message.transaction_hash = t.hash
WHERE (cardinality(types) = 0 OR type = ANY (types))
AND addresses && involved_accounts_addresses
ORDER BY height DESC, message.transaction_hash DESC
ORDER BY message.height DESC, message.transaction_hash DESC
LIMIT "limit" OFFSET "offset"
$$ LANGUAGE sql STABLE;
6 changes: 4 additions & 2 deletions database/scheme/20-group_module.sql
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,10 @@ CREATE TABLE group_proposal
messages JSONB NOT NULL,
height BIGINT NOT NULL REFERENCES block (height),
submit_time TIMESTAMP WITHOUT TIME ZONE NOT NULL,
transaction_hash TEXT NULL REFERENCES transaction (hash),
member_count INT NOT NULL
transaction_hash TEXT NULL,
member_count INT NOT NULL,
partition_id BIGINT NOT NULL,
FOREIGN KEY(transaction_hash, partition_id) REFERENCES transaction (hash, partition_id)
);

CREATE INDEX group_proposal_status_index ON group_proposal (group_id) WHERE status = 'PROPOSAL_STATUS_SUBMITTED';
Expand Down
24 changes: 16 additions & 8 deletions database/scheme/21-marketplace_module.sql
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,34 @@ $$ LANGUAGE sql STABLE;

CREATE TABLE marketplace_collection
(
transaction_hash TEXT NOT NULL REFERENCES transaction (hash),
transaction_hash TEXT NOT NULL,
partition_id BIGINT NOT NULL,
id BIGINT NOT NULL,
denom_id TEXT NOT NULL REFERENCES nft_denom (id),
mint_royalties TEXT NOT NULL,
resale_royalties TEXT NOT NULL,
verified BOOLEAN NOT NULL,
creator TEXT NOT NULL,
PRIMARY KEY(id)
PRIMARY KEY(id),
FOREIGN KEY(transaction_hash, partition_id) REFERENCES transaction (hash, partition_id)
);

CREATE INDEX marketplace_collection_denom_id_index ON marketplace_collection (denom_id);
CREATE INDEX marketplace_collection_creator_index ON marketplace_collection (creator);

CREATE TABLE marketplace_nft
(
transaction_hash TEXT NOT NULL REFERENCES transaction (hash),
transaction_hash TEXT NOT NULL,
partition_id BIGINT NOT NULL,
id BIGINT,
uid TEXT,
token_id BIGINT NOT NULL,
denom_id TEXT NOT NULL REFERENCES nft_denom (id),
price DECIMAL,
creator TEXT NOT NULL,
PRIMARY KEY(token_id, denom_id),
FOREIGN KEY (token_id, denom_id) REFERENCES nft_nft(id, denom_id)
FOREIGN KEY (token_id, denom_id) REFERENCES nft_nft(id, denom_id),
FOREIGN KEY(transaction_hash, partition_id) REFERENCES transaction (hash, partition_id)
);

CREATE INDEX marketplace_nft_id_index ON marketplace_nft (id);
Expand All @@ -53,7 +57,8 @@ CREATE INDEX marketplace_nft_uid_index ON marketplace_nft (uid);

CREATE TABLE marketplace_nft_buy_history
(
transaction_hash TEXT NOT NULL REFERENCES transaction (hash),
transaction_hash TEXT NOT NULL,
partition_id BIGINT NOT NULL,
token_id BIGINT NOT NULL,
denom_id TEXT NOT NULL REFERENCES nft_denom (id),
price DECIMAL NOT NULL,
Expand All @@ -62,7 +67,8 @@ CREATE TABLE marketplace_nft_buy_history
usd_price DECIMAL NOT NULL,
btc_price DECIMAL NOT NULL,
timestamp BIGINT NOT NULL,
FOREIGN KEY (token_id, denom_id) REFERENCES nft_nft(id, denom_id)
FOREIGN KEY (token_id, denom_id) REFERENCES nft_nft(id, denom_id),
FOREIGN KEY(transaction_hash, partition_id) REFERENCES transaction (hash, partition_id)
);

CREATE INDEX marketplace_nft_buy_history_token_id_denom_id_index ON marketplace_nft_buy_history (token_id, denom_id);
Expand All @@ -74,9 +80,11 @@ CREATE INDEX marketplace_nft_buy_history_timestamp_index ON marketplace_nft_buy_
CREATE TABLE nft_transfer_history
(
id BIGINT NOT NULL,
transaction_hash TEXT NOT NULL REFERENCES transaction (hash),
transaction_hash TEXT NOT NULL,
partition_id BIGINT NOT NULL,
denom_id TEXT NOT NULL REFERENCES nft_denom (id),
old_owner TEXT NOT NULL,
new_owner TEXT NOT NULL,
timestamp BIGINT NOT NULL
timestamp BIGINT NOT NULL,
FOREIGN KEY(transaction_hash, partition_id) REFERENCES transaction (hash, partition_id)
);

0 comments on commit b47a2a8

Please sign in to comment.