Skip to content

Commit

Permalink
When inserting new pool_info_cache record, check for most recent off …
Browse files Browse the repository at this point in the history
…chain pool data record matching the url/hash of the most recent update, to deal with the case where pool updates are made but no new off chain pool data records get created by dbsync
  • Loading branch information
gregbgithub committed Aug 2, 2024
1 parent 60ae393 commit d406f8b
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions files/grest/rpc/01_cached_tables/pool_info_cache.sql
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,20 @@ LANGUAGE plpgsql
AS $$
DECLARE
_latest_pool_update_id integer;
_calc_meta_id bigint;
BEGIN
IF (tg_table_name = 'pool_update') THEN
IF (tg_op = 'INSERT') THEN

-- for url/hash of most recent update, find last off_chain data record
SELECT coalesce(max(ocpd.pmr_id), new.meta_id) INTO _calc_meta_id
FROM off_chain_pool_data ocpd
INNER JOIN pool_metadata_ref pmr ON ocpd.pmr_id = pmr.id
INNER JOIN pool_metadata_ref pmr2 ON pmr2.id = new.meta_id AND
pmr2.pool_id = pmr.pool_id AND
pmr2.url = pmr.url AND
pmr2.hash = pmr.hash;

PERFORM grest.pool_info_insert(
new.id,
new.registered_tx_id,
Expand All @@ -219,7 +230,7 @@ BEGIN
new.pledge,
new.deposit,
new.reward_addr_id,
new.meta_id
_calc_meta_id
);
ELSIF (tg_op = 'DELETE') THEN
DELETE FROM grest.pool_info_cache
Expand Down Expand Up @@ -302,13 +313,24 @@ FOR EACH ROW EXECUTE FUNCTION grest.pool_info_retire_status();
DO $$
DECLARE
_latest_pool_info_tx_id bigint;
_calc_meta_id bigint;
rec RECORD;
BEGIN
SELECT COALESCE(MAX(tx_id), 0) INTO _latest_pool_info_tx_id FROM grest.pool_info_cache;

FOR rec IN (
SELECT * FROM public.pool_update AS pu WHERE pu.registered_tx_id > _latest_pool_info_tx_id
) LOOP

-- for url/hash of most recent update, find last off_chain data record
SELECT coalesce(max(ocpd.pmr_id), rec.meta_id) INTO _calc_meta_id
FROM off_chain_pool_data ocpd
INNER JOIN pool_metadata_ref pmr ON ocpd.pmr_id = pmr.id
INNER JOIN pool_metadata_ref pmr2 ON pmr2.id = rec.meta_id AND
pmr2.pool_id = pmr.pool_id AND
pmr2.url = pmr.url AND
pmr2.hash = pmr.hash;

PERFORM grest.pool_info_insert(
rec.id,
rec.registered_tx_id,
Expand All @@ -320,7 +342,7 @@ BEGIN
rec.pledge,
rec.deposit,
rec.reward_addr_id,
rec.meta_id
_calc_meta_id
);
END LOOP;

Expand Down

0 comments on commit d406f8b

Please sign in to comment.