From 232e02f517e6e5aff75d644dc6d2a248c0a5acf4 Mon Sep 17 00:00:00 2001 From: blushi Date: Mon, 21 Oct 2024 09:54:45 +0200 Subject: [PATCH] feat: add support for storing multiple batch denoms per retirement --- index_retires.py | 20 +++++++++++++++----- migrations/README.md | 10 +++++----- migrations/committed/000003.sql | 12 ++++++++++++ migrations/schema_snapshot.sql | 6 ++++-- 4 files changed, 36 insertions(+), 12 deletions(-) create mode 100644 migrations/committed/000003.sql diff --git a/index_retires.py b/index_retires.py index a168ca8..2a2a9d1 100644 --- a/index_retires.py +++ b/index_retires.py @@ -29,20 +29,30 @@ def _index_retires(pg_conn, _client, _chain_num): normalize["chain_num"] = chain_num normalize["timestamp"] = timestamp normalize["tx_hash"] = tx_hash + normalize["batch_denoms"] = [] + normalize["amount"] = 0 + for entry in event: (_, _, _, _, key, value, _, _, _) = entry value = value.strip('"') if "v1alpha1.EventRetire" in entry[0]: if key == "amount": - normalize["amount"] = value + normalize["amount"] = normalize["amount"] + float(value) elif key == "batch_denom": - normalize["batch_denom"] = value + normalize["batch_denoms"].append(value) elif key == "location": normalize["jurisdiction"] = value elif key == "retirer": normalize["owner"] = value elif "v1.EventRetire" in entry[0]: - normalize[key] = value + if key == "amount": + normalize["amount"] = normalize["amount"] + float(value) + elif key == "batch_denom": + normalize["batch_denoms"].append(value) + elif key == "jurisdiction": + normalize["jurisdiction"] = value + elif key == "owner": + normalize["owner"] = value with pg_conn.cursor() as _cur: _cur.execute( """SELECT TRIM(BOTH '"' FROM (tx.data -> 'tx' -> 'body' -> 'memo')::text) AS memo FROM tx WHERE block_height=%s AND chain_num=%s AND tx_idx=%s""", @@ -54,7 +64,7 @@ def _index_retires(pg_conn, _client, _chain_num): retirement = ( normalize["type"], normalize["amount"], - normalize["batch_denom"], + normalize["batch_denoms"], normalize["jurisdiction"], normalize["owner"], normalize.get("reason", ""), @@ -66,7 +76,7 @@ def _index_retires(pg_conn, _client, _chain_num): normalize["tx_hash"], ) _cur.execute( - "INSERT INTO retirements (type, amount, batch_denom, jurisdiction, owner, reason, block_height, chain_num, tx_idx, msg_idx, timestamp, tx_hash) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)", + "INSERT INTO retirements (type, amount, batch_denoms, jurisdiction, owner, reason, block_height, chain_num, tx_idx, msg_idx, timestamp, tx_hash) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)", retirement, ) pg_conn.commit() diff --git a/migrations/README.md b/migrations/README.md index 1228d1e..759f29a 100644 --- a/migrations/README.md +++ b/migrations/README.md @@ -16,11 +16,11 @@ $ docker-compose up --build postgres Then, you must initialize the database: -``` -$ export DATABASE_URL="postgres://postgres:postgres@localhost:5432/indexer" -$ export SHADOW_DATABASE_URL="postgres://postgres:postgres@localhost:5432/indexer_shadow" -$ export ROOT_DATABASE_URL="postgres://postgres:postgres@localhost:5432/postgres" -$ yarn run graphile-migrate reset --erase +```sh +export DATABASE_URL="postgres://postgres:postgres@localhost:5432/indexer" +export SHADOW_DATABASE_URL="postgres://postgres:postgres@localhost:5432/indexer_shadow" +export ROOT_DATABASE_URL="postgres://postgres:postgres@localhost:5432/postgres" +yarn run graphile-migrate reset --erase ``` Now, we set up a watch process that will monitor `migrations/current.sql` for your changes as well as apply them to your local database: diff --git a/migrations/committed/000003.sql b/migrations/committed/000003.sql new file mode 100644 index 0000000..6f4ad10 --- /dev/null +++ b/migrations/committed/000003.sql @@ -0,0 +1,12 @@ +--! Previous: sha1:80425329666e5d20b5fbb7b6179f3d19acceeb76 +--! Hash: sha1:23379758e3a742625d5262c472693435196be95f + +ALTER TABLE public.retirements +ADD COLUMN IF NOT EXISTS batch_denoms text[] DEFAULT ARRAY[]::text[] NOT NULL; + +UPDATE public.retirements +SET batch_denoms = ARRAY[batch_denom]; + +-- TODO later once app fully migrated +-- ALTER TABLE public.retirements +-- DROP COLUMN IF EXISTS batch_denom; diff --git a/migrations/schema_snapshot.sql b/migrations/schema_snapshot.sql index e98a921..b731f25 100644 --- a/migrations/schema_snapshot.sql +++ b/migrations/schema_snapshot.sql @@ -3,11 +3,12 @@ -- -- Dumped from database version 14.9 (Debian 14.9-1.pgdg110+1) --- Dumped by pg_dump version 15.4 +-- Dumped by pg_dump version 17.0 SET statement_timeout = 0; SET lock_timeout = 0; SET idle_in_transaction_session_timeout = 0; +SET transaction_timeout = 0; SET client_encoding = 'UTF8'; SET standard_conforming_strings = on; SELECT pg_catalog.set_config('search_path', '', false); @@ -405,7 +406,8 @@ CREATE TABLE public.retirements ( chain_num smallint NOT NULL, tx_idx smallint NOT NULL, msg_idx smallint NOT NULL, - tx_hash text NOT NULL + tx_hash text NOT NULL, + batch_denoms text[] DEFAULT ARRAY[]::text[] NOT NULL );