Browse Source
- The evolutions compacted. - Domain types created. - Foreign keys added. - Support for the P2WPKH address format added.prometheus-integration
Alexis Hernandez
7 years ago
10 changed files with 176 additions and 138 deletions
@ -1,32 +1,31 @@ |
|||
|
|||
# --- !Ups |
|||
|
|||
CREATE TABLE blocks( |
|||
blockhash VARCHAR(64) NOT NULL, |
|||
previous_blockhash VARCHAR(64) NULL, |
|||
next_blockhash VARCHAR(64) NULL, |
|||
merkle_root VARCHAR(64) NULL, |
|||
tpos_contract VARCHAR(64) NULL, |
|||
size INT NOT NULL, |
|||
height INT NOT NULL, |
|||
version INT NOT NULL, |
|||
time BIGINT NOT NULL, |
|||
median_time BIGINT NOT NULL, |
|||
nonce INT NOT NULL, |
|||
bits VARCHAR(50) NOT NULL, |
|||
chainwork VARCHAR(80) NOT NULL, |
|||
difficulty DECIMAL(30, 20), |
|||
-- constraints |
|||
CONSTRAINT blocks_blockhash_pk PRIMARY KEY (blockhash), |
|||
CONSTRAINT blocks_blockhash_format CHECK (blockhash ~ '^[a-f0-9]{64}$'), |
|||
CONSTRAINT blocks_previous_blockhash_format CHECK (previous_blockhash ~ '^[a-f0-9]{64}$'), |
|||
CONSTRAINT blocks_next_blockhash_format CHECK (next_blockhash ~ '^[a-f0-9]{64}$'), |
|||
CONSTRAINT blocks_tpos_contract_format CHECK (tpos_contract ~ '^[a-f0-9]{64}$') |
|||
-- custom types are defined to simplify the validations and simplify updates to the validations. |
|||
|
|||
CREATE DOMAIN BLOCKHASH_TYPE AS TEXT |
|||
CHECK ( |
|||
VALUE ~ '^[a-f0-9]{64}$' |
|||
); |
|||
|
|||
CREATE DOMAIN TXID_TYPE AS TEXT |
|||
CHECK ( |
|||
VALUE ~ '^[a-f0-9]{64}$' |
|||
); |
|||
|
|||
CREATE INDEX blocks_height_index ON blocks USING BTREE (height); |
|||
CREATE INDEX blocks_time_index ON blocks USING BTREE (time); |
|||
CREATE DOMAIN ADDRESS_TYPE AS TEXT |
|||
CHECK ( |
|||
VALUE ~ '(^[A-Za-z0-9]{34}$)|(^[A-Za-z0-9]{42}$)' |
|||
); |
|||
|
|||
CREATE DOMAIN NON_NEGATIVE_INT_TYPE AS INT |
|||
CHECK ( |
|||
VALUE >= 0 |
|||
); |
|||
|
|||
# --- !Downs |
|||
|
|||
DROP TABLE blocks; |
|||
DROP DOMAIN NON_NEGATIVE_INT_TYPE; |
|||
DROP DOMAIN ADDRESS_TYPE; |
|||
DROP DOMAIN TXID_TYPE; |
|||
DROP DOMAIN BLOCKHASH_TYPE; |
|||
|
@ -1,32 +1,32 @@ |
|||
|
|||
# --- !Ups |
|||
|
|||
-- we pre-compute the balances while storing transactions in order to perform |
|||
-- simpler queries while requiring addresses and the available amounts. |
|||
CREATE TABLE balances( |
|||
address VARCHAR(34) NOT NULL, |
|||
received DECIMAL(30, 15) NOT NULL, |
|||
spent DECIMAL(30, 15) NOT NULL, |
|||
available DECIMAL(30, 15) NOT NULL, |
|||
-- the next_blockhash doesn't have a foreign key on purpose, it can cause issues while deleting blocks. |
|||
CREATE TABLE blocks( |
|||
blockhash BLOCKHASH_TYPE NOT NULL, |
|||
previous_blockhash BLOCKHASH_TYPE NULL, |
|||
next_blockhash BLOCKHASH_TYPE NULL, |
|||
merkle_root TEXT NOT NULL, |
|||
tpos_contract TXID_TYPE NULL, |
|||
size NON_NEGATIVE_INT_TYPE NOT NULL, |
|||
height NON_NEGATIVE_INT_TYPE NOT NULL, |
|||
version INT NOT NULL, |
|||
time BIGINT NOT NULL, |
|||
median_time BIGINT NOT NULL, |
|||
nonce INT NOT NULL, |
|||
bits TEXT NOT NULL, |
|||
chainwork TEXT NOT NULL, |
|||
difficulty DECIMAL(30, 20) NOT NULL, |
|||
-- constraints |
|||
CONSTRAINT balances_address_pk PRIMARY KEY (address), |
|||
CONSTRAINT balances_address_format CHECK (address ~ '[a-zA-Z0-9]{34}$') |
|||
CONSTRAINT blocks_blockhash_pk PRIMARY KEY (blockhash), |
|||
CONSTRAINT blocks_height_unique UNIQUE (height), |
|||
CONSTRAINT blocks_previous_blockhash_fk FOREIGN KEY (previous_blockhash) REFERENCES blocks (blockhash) |
|||
); |
|||
|
|||
CREATE INDEX balances_available_index ON balances (available); |
|||
|
|||
|
|||
-- there are certain addresses that we need to hide from public, like the one |
|||
-- used for the coin swap. |
|||
CREATE TABLE hidden_addresses( |
|||
address VARCHAR(34) NOT NULL, |
|||
-- constraints |
|||
CONSTRAINT hidden_addresses_address_pk PRIMARY KEY (address), |
|||
CONSTRAINT hidden_addresses_address_format CHECK (address ~ '[a-zA-Z0-9]{34}$') |
|||
); |
|||
CREATE INDEX blocks_time_index ON blocks USING BTREE (time); |
|||
|
|||
|
|||
# --- !Downs |
|||
|
|||
DROP TABLE hidden_addresses; |
|||
DROP TABLE balances; |
|||
DROP INDEX blocks_time_index; |
|||
DROP TABLE blocks; |
|||
|
@ -1,49 +1,30 @@ |
|||
|
|||
# --- !Ups |
|||
|
|||
CREATE TABLE transactions( |
|||
txid VARCHAR(64) NOT NULL, |
|||
blockhash VARCHAR(64) NOT NULL, |
|||
time BIGINT NOT NULL, |
|||
size INT NOT NULL, |
|||
-- we pre-compute the balances while storing transactions in order to perform |
|||
-- simpler queries while requiring addresses and the available amounts. |
|||
CREATE TABLE balances( |
|||
address ADDRESS_TYPE NOT NULL, |
|||
received DECIMAL(30, 15) NOT NULL, |
|||
spent DECIMAL(30, 15) NOT NULL, |
|||
-- constraints |
|||
CONSTRAINT transactions_txid_pk PRIMARY KEY (txid), |
|||
CONSTRAINT transactions_txid_format CHECK (txid ~ '^[a-f0-9]{64}$'), |
|||
CONSTRAINT transactions_blockhash_format CHECK (blockhash ~ '^[a-f0-9]{64}$') |
|||
CONSTRAINT balances_address_pk PRIMARY KEY (address) |
|||
); |
|||
|
|||
CREATE INDEX transactions_blockhash_index ON transactions (blockhash); |
|||
CREATE INDEX balances_available_index ON balances USING BTREE ((received - spent)); |
|||
|
|||
|
|||
CREATE TABLE transaction_inputs( |
|||
txid VARCHAR(64) NOT NULL, |
|||
index INT NOT NULL, |
|||
value DECIMAL(30, 15) NULL, |
|||
address VARCHAR(34) NULL, |
|||
-- there are certain addresses that we need to hide from the public, |
|||
-- like the one used for the coin swap. |
|||
CREATE TABLE hidden_addresses( |
|||
address ADDRESS_TYPE NOT NULL, |
|||
-- constraints |
|||
CONSTRAINT transaction_inputs_txid_index_pk PRIMARY KEY (txid, index), |
|||
CONSTRAINT transaction_inputs_txid_format CHECK (txid ~ '^[a-f0-9]{64}$'), |
|||
CONSTRAINT transaction_inputs_address_format CHECK (address ~ '[a-zA-Z0-9]{34}$') |
|||
); |
|||
|
|||
CREATE TABLE transaction_outputs( |
|||
txid VARCHAR(64) NOT NULL, |
|||
index INT NOT NULL, |
|||
value DECIMAL(30, 15) NOT NULL, |
|||
address VARCHAR(34) NULL, |
|||
tpos_owner_address VARCHAR(34) NULL, |
|||
tpos_merchant_address VARCHAR(34) NULL, |
|||
-- constraints |
|||
CONSTRAINT transaction_outputs_txid_index_pk PRIMARY KEY (txid, index), |
|||
CONSTRAINT transaction_outputs_txid_format CHECK (txid ~ '^[a-f0-9]{64}$'), |
|||
CONSTRAINT transaction_outputs_address_format CHECK (address ~ '[a-zA-Z0-9]{34}$'), |
|||
CONSTRAINT transaction_outputs_tpos_owner_address_format CHECK (address ~ '[a-zA-Z0-9]{34}$'), |
|||
CONSTRAINT transaction_outputs_tpos_merchant_address_format CHECK (address ~ '[a-zA-Z0-9]{34}$') |
|||
CONSTRAINT hidden_addresses_address_pk PRIMARY KEY (address) |
|||
); |
|||
|
|||
|
|||
# --- !Downs |
|||
|
|||
DROP TABLE transaction_outputs; |
|||
DROP TABLE transaction_inputs; |
|||
DROP TABLE transactions; |
|||
DROP TABLE hidden_addresses; |
|||
DROP INDEX balances_available_index; |
|||
DROP TABLE balances; |
|||
|
@ -1,14 +1,56 @@ |
|||
|
|||
# --- !Ups |
|||
|
|||
DROP INDEX blocks_height_index; |
|||
CREATE TABLE transactions( |
|||
txid TXID_TYPE NOT NULL, |
|||
blockhash BLOCKHASH_TYPE NOT NULL, |
|||
time BIGINT NOT NULL, |
|||
size NON_NEGATIVE_INT_TYPE NOT NULL, |
|||
-- constraints |
|||
CONSTRAINT transactions_txid_pk PRIMARY KEY (txid), |
|||
CONSTRAINT transactions_blockhash_fk FOREIGN KEY (blockhash) REFERENCES blocks (blockhash) |
|||
); |
|||
|
|||
CREATE INDEX transactions_blockhash_index ON transactions USING BTREE (blockhash); |
|||
CREATE INDEX transactions_time_index ON transactions USING BTREE (time); |
|||
|
|||
|
|||
CREATE TABLE transaction_inputs( |
|||
txid TXID_TYPE NOT NULL, |
|||
index NON_NEGATIVE_INT_TYPE NOT NULL, |
|||
value DECIMAL(30, 15) NULL, |
|||
address ADDRESS_TYPE NULL, |
|||
-- constraints |
|||
CONSTRAINT transaction_inputs_txid_index_pk PRIMARY KEY (txid, index), |
|||
CONSTRAINT transaction_inputs_txid_fk FOREIGN KEY (txid) REFERENCES transactions (txid) |
|||
); |
|||
|
|||
CREATE INDEX transaction_inputs_address_index ON transaction_inputs USING BTREE (address); |
|||
|
|||
|
|||
CREATE TABLE transaction_outputs( |
|||
txid TXID_TYPE NOT NULL, |
|||
index NON_NEGATIVE_INT_TYPE NOT NULL, |
|||
value DECIMAL(30, 15) NOT NULL, |
|||
address ADDRESS_TYPE NULL, |
|||
tpos_owner_address ADDRESS_TYPE NULL, |
|||
tpos_merchant_address ADDRESS_TYPE NULL, |
|||
-- constraints |
|||
CONSTRAINT transaction_outputs_txid_index_pk PRIMARY KEY (txid, index), |
|||
CONSTRAINT transaction_outputs_txid_fk FOREIGN KEY (txid) REFERENCES transactions (txid) |
|||
); |
|||
|
|||
CREATE INDEX transaction_outputs_address_index ON transaction_outputs USING BTREE (address); |
|||
|
|||
ALTER TABLE blocks |
|||
ADD CONSTRAINT blocks_height_unique UNIQUE (height); |
|||
|
|||
# --- !Downs |
|||
|
|||
ALTER TABLE blocks |
|||
DROP CONSTRAINT blocks_height_unique; |
|||
DROP INDEX transaction_outputs_address_index; |
|||
DROP TABLE transaction_outputs; |
|||
|
|||
DROP INDEX transaction_inputs_address_index; |
|||
DROP TABLE transaction_inputs; |
|||
|
|||
CREATE INDEX blocks_height_index ON blocks USING BTREE (height); |
|||
DROP INDEX transactions_blockhash_index; |
|||
DROP INDEX transactions_time_index; |
|||
DROP TABLE transactions; |
|||
|
@ -1,20 +0,0 @@ |
|||
|
|||
# --- !Ups |
|||
|
|||
DROP INDEX balances_available_index; |
|||
|
|||
ALTER TABLE balances |
|||
DROP COLUMN available; |
|||
|
|||
CREATE INDEX balances_available_index ON balances ((received - spent)); |
|||
|
|||
|
|||
# --- !Downs |
|||
|
|||
DROP INDEX balances_available_index; |
|||
|
|||
-- in case this down is applied, the table needs to be rebuilt. |
|||
ALTER TABLE balances |
|||
ADD COLUMN available DECIMAL(30, 15) NOT NULL DEFAULT 0; |
|||
|
|||
CREATE INDEX balances_available_index ON balances (available); |
@ -1,13 +0,0 @@ |
|||
|
|||
# --- !Ups |
|||
|
|||
CREATE INDEX transaction_inputs_address_index ON transaction_inputs (address); |
|||
|
|||
CREATE INDEX transaction_outputs_address_index ON transaction_outputs (address); |
|||
|
|||
|
|||
# --- !Downs |
|||
|
|||
DROP INDEX transaction_outputs_address_index; |
|||
|
|||
DROP INDEX transaction_inputs_address_index; |
Loading…
Reference in new issue