fix: (sql) lower case addresses for easier joins

- add schema docs
This commit is contained in:
Mohamed Sohail 2022-05-05 14:56:22 +03:00
parent 1c65a11460
commit 659007e563
Signed by: kamikazechaser
GPG Key ID: 7DD45520C01CD85D
3 changed files with 6 additions and 6 deletions

BIN
docs/cic-dw-schema.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 88 KiB

View File

@ -80,7 +80,7 @@ SELECT 1, blockchain_address, 'cic_ussd.account.block_chain_address remote curso
-- bootstrap first tx row -- bootstrap first tx row
INSERT INTO transactions (tx_hash, block_number, tx_index, token_address, sender_address, recipient_address, tx_value, date_block, tx_type, success) INSERT INTO transactions (tx_hash, block_number, tx_index, token_address, sender_address, recipient_address, tx_value, date_block, tx_type, success)
SELECT tx.tx_hash, tx.block_number, tx.tx_index, tx.source_token, tx.sender, tx.recipient, tx.from_value, tx.date_block, concat(tag.domain, '_', tag.value) AS tx_type, tx.success SELECT tx.tx_hash, tx.block_number, tx.tx_index, LOWER(tx.source_token), LOWER(tx.sender), LOWER(tx.recipient), tx.from_value, tx.date_block, concat(tag.domain, '_', tag.value) AS tx_type, tx.success
FROM cic_cache.tx FROM cic_cache.tx
INNER JOIN cic_cache.tag_tx_link ON tx.id = cic_cache.tag_tx_link.tx_id INNER JOIN cic_cache.tag_tx_link ON tx.id = cic_cache.tag_tx_link.tx_id
INNER JOIN cic_cache.tag ON cic_cache.tag_tx_link.tag_id = cic_cache.tag.id INNER JOIN cic_cache.tag ON cic_cache.tag_tx_link.tag_id = cic_cache.tag.id

View File

@ -9,7 +9,7 @@ WITH current_ussd_cursor AS (
INSERT INTO users (phone_number, blockchain_address, date_registered, failed_pin_attempts, ussd_account_status) INSERT INTO users (phone_number, blockchain_address, date_registered, failed_pin_attempts, ussd_account_status)
SELECT cic_ussd.account.phone_number, cic_ussd.account.blockchain_address, cic_ussd.account.created, cic_ussd.account.failed_pin_attempts, cic_ussd.account.status SELECT cic_ussd.account.phone_number, cic_ussd.account.blockchain_address, cic_ussd.account.created, cic_ussd.account.failed_pin_attempts, cic_ussd.account.status
FROM cic_ussd.account WHERE cic_ussd.account.id > (SELECT id FROM current_ussd_cursor) ORDER BY cic_ussd.account.id ASC LIMIT 200; FROM cic_ussd.account WHERE cic_ussd.account.id > (SELECT id FROM current_ussd_cursor) ORDER BY cic_ussd.account.id ASC LIMIT 300;
UPDATE cursors SET cursor_pos = (SELECT blockchain_address FROM users ORDER BY id DESC LIMIT 1) WHERE cursors.id = 1; UPDATE cursors SET cursor_pos = (SELECT blockchain_address FROM users ORDER BY id DESC LIMIT 1) WHERE cursors.id = 1;
@ -17,13 +17,13 @@ UPDATE cursors SET cursor_pos = (SELECT blockchain_address FROM users ORDER BY i
-- This db transaction will auto scroll the cic_cache remote adding values as per the limit and auto-updating the cursor -- This db transaction will auto scroll the cic_cache remote adding values as per the limit and auto-updating the cursor
-- The tx_hash is used as the cursor to retrieve the corresponding id since the id is not guaranteed to be sequential -- The tx_hash is used as the cursor to retrieve the corresponding id since the id is not guaranteed to be sequential
WITH current_cache_cursor AS ( WITH current_cache_cursor AS (
SELECT id FROM cic_cache.tx WHERE tx_hash = (SELECT cursor_pos FROM cursors WHERE id = 2) SELECT id FROM cic_cache.tx WHERE LOWER(tx_hash) = (SELECT cursor_pos FROM cursors WHERE id = 2)
) )
INSERT INTO transactions (tx_hash, block_number, tx_index, token_address, sender_address, recipient_address, tx_value, date_block, tx_type, success) INSERT INTO transactions (tx_hash, block_number, tx_index, token_address, sender_address, recipient_address, tx_value, date_block, tx_type, success)
SELECT cic_cache.tx.tx_hash, cic_cache.tx.block_number, cic_cache.tx.tx_index, cic_cache.tx.source_token, cic_cache.tx.sender, cic_cache.tx.recipient, cic_cache.tx.from_value, cic_cache.tx.date_block, concat(cic_cache.tag.domain, '_', cic_cache.tag.value) AS tx_type, cic_cache.tx.success SELECT cic_cache.tx.tx_hash, cic_cache.tx.block_number, cic_cache.tx.tx_index, LOWER(cic_cache.tx.source_token), LOWER(cic_cache.tx.sender), LOWER(cic_cache.tx.recipient), cic_cache.tx.from_value, cic_cache.tx.date_block, concat(cic_cache.tag.domain, '_', cic_cache.tag.value) AS tx_type, cic_cache.tx.success
FROM cic_cache.tx INNER JOIN cic_cache.tag_tx_link ON cic_cache.tx.id = cic_cache.tag_tx_link.tx_id INNER JOIN cic_cache.tag ON cic_cache.tag_tx_link.tag_id = cic_cache.tag.id FROM cic_cache.tx INNER JOIN cic_cache.tag_tx_link ON cic_cache.tx.id = cic_cache.tag_tx_link.tx_id INNER JOIN cic_cache.tag ON cic_cache.tag_tx_link.tag_id = cic_cache.tag.id
WHERE cic_cache.tx.id > (SELECT id FROM current_cache_cursor) ORDER BY cic_cache.tx.id ASC LIMIT 200; WHERE cic_cache.tx.id > (SELECT id FROM current_cache_cursor) ORDER BY cic_cache.tx.id ASC LIMIT 300;
UPDATE cursors SET cursor_pos = (SELECT tx_hash FROM transactions ORDER BY id DESC LIMIT 1) WHERE cursors.id = 2; UPDATE cursors SET cursor_pos = (SELECT tx_hash FROM transactions ORDER BY id DESC LIMIT 1) WHERE cursors.id = 2;
@ -34,7 +34,7 @@ SELECT cursor_pos from cursors WHERE id = $1;
-- name: insert-token-data -- name: insert-token-data
-- Insert new token -- Insert new token
INSERT INTO tokens (token_address, token_name, token_symbol, token_decimals) VALUES INSERT INTO tokens (token_address, token_name, token_symbol, token_decimals) VALUES
($1, $2, $3, $4); (LOWER($1), $2, $3, $4);
-- name: update-cursor -- name: update-cursor
-- Generic cursor update -- Generic cursor update