diff --git a/backend-rust/CHANGELOG.md b/backend-rust/CHANGELOG.md index db2c50ed5..e27bbe6af 100644 --- a/backend-rust/CHANGELOG.md +++ b/backend-rust/CHANGELOG.md @@ -29,6 +29,7 @@ Database schema version: 2 - Contract rejected event skips in the correct way. - Fix issue where `ContractUpdated::message` attempted to parse empty messages, resulting in parsing error messages instead of `null`. - Issue making `avgFinalizationTime` field of `Query::block_metrics` always return `null`. +- Issue making `Query::block_metrics` included a bucket for a period in the future. ## [0.1.19] - 2025-01-30 diff --git a/backend-rust/src/migrations/m0002-block-cumulative-fin-time-index.sql b/backend-rust/src/migrations/m0002-block-cumulative-fin-time-index.sql index d3d097a23..7123d10d8 100644 --- a/backend-rust/src/migrations/m0002-block-cumulative-fin-time-index.sql +++ b/backend-rust/src/migrations/m0002-block-cumulative-fin-time-index.sql @@ -10,3 +10,18 @@ ALTER TABLE blocks ALTER COLUMN hash SET DATA TYPE VARCHAR(64); CREATE INDEX blocks_hash_gin_trgm_idx ON blocks USING gin(hash gin_trgm_ops); -- Important for quickly calculating the delegated stake to a baker pool. CREATE INDEX delegated_target_baker_id_index ON accounts(delegated_target_baker_id); +-- Function for generating a table where each row is a bucket. +-- Used by metrics queries. +-- This is replacing the current `date_bin_series` in a backwards compatible way, fixing issue where +-- a future bucket always got included. +CREATE OR REPLACE FUNCTION date_bin_series(bucket_size interval, starting TIMESTAMPTZ, ending TIMESTAMPTZ) +RETURNS TABLE(bucket_start TIMESTAMPTZ, bucket_end TIMESTAMPTZ) AS $$ + SELECT + bucket_start, + bucket_start + bucket_size + FROM generate_series( + date_bin(bucket_size, starting, TIMESTAMPTZ '2001-01-01'), + date_bin(bucket_size, ending, TIMESTAMPTZ '2001-01-01'), + bucket_size + ) as bucket_start; +$$ LANGUAGE sql;