code.vegaprotocol.io/vega@v0.79.0/datanode/sqlstore/migrations/0107_ensure_distinct_game_id.sql (about) 1 -- +goose Up 2 3 drop materialized view if exists game_stats; 4 drop materialized view if exists game_stats_current; 5 drop view if exists game_individual_rankings; 6 7 create view game_individual_rankings as 8 with individual_games as ( 9 -- get the games where the entity scope is individuals 10 select distinct game_id 11 from transfers 12 where dispatch_strategy ->> 'entity_scope' = '1' 13 and game_id is not null), 14 individual_totals as ( 15 -- calculate the total rewards for each individual in each individual entity scoped game 16 select t.game_id, 17 t.epoch_id, 18 t.asset_id, 19 t.party_id, 20 sum(t.total_rewards) as total_rewards, 21 sum(total_rewards_quantum) as total_rewards_quantum 22 from game_reward_totals t 23 join individual_games i on t.game_id = i.game_id 24 group by t.game_id, t.epoch_id, t.asset_id, t.party_id), 25 individual_rankings as ( 26 -- rank the individuals for each game at each epoch 27 select game_id, 28 epoch_id, 29 party_id, 30 total_rewards_quantum, 31 rank() over ( 32 partition by game_id, epoch_id order by total_rewards_quantum desc 33 ) as rank 34 from individual_totals) 35 select it.game_id, 36 it.epoch_id, 37 it.party_id, 38 it.total_rewards, 39 ir.total_rewards_quantum, 40 ir.rank 41 from individual_totals it 42 join individual_rankings ir 43 on it.game_id = ir.game_id and it.epoch_id = ir.epoch_id and it.party_id = ir.party_id; 44 45 create materialized view game_stats as 46 with 47 game_epochs as ( 48 select distinct 49 game_id, epoch_id 50 from rewards 51 where 52 game_id is not null 53 ), 54 dispatch_strategies AS ( 55 SELECT DISTINCT 56 ON (game_id) game_id, dispatch_strategy 57 FROM transfers 58 WHERE 59 transfer_type = 'Recurring' 60 ORDER BY game_id, vega_time DESC 61 ), 62 game_rewards as ( 63 select 64 r.*, t.dispatch_strategy, tm.team_id, tmr.rank as member_rank, tr.rank as team_rank, tmr.total_rewards, tmr.total_rewards_quantum, 65 tr.total_rewards as team_total_rewards, tr.total_rewards_quantum as team_total_rewards_quantum, 'ENTITY_SCOPE_TEAMS' as entity_scope 66 from 67 rewards r 68 join game_epochs ge on r.game_id = ge.game_id 69 and r.epoch_id = ge.epoch_id 70 JOIN dispatch_strategies t ON r.game_id = t.game_id 71 AND t.dispatch_strategy ->> 'entity_scope' = '2' 72 join team_members tm on r.party_id = tm.party_id 73 left join game_team_rankings tr on r.game_id = tr.game_id 74 and r.epoch_id = tr.epoch_id 75 and tm.team_id = tr.team_id 76 left join game_team_member_rankings tmr on r.game_id = tmr.game_id 77 and r.epoch_id = tmr.epoch_id 78 and tm.team_id = tmr.team_id 79 and r.party_id = tmr.party_id 80 union all 81 select 82 r.*, t.dispatch_strategy, null as team_id, tmr.rank as member_rank, null as team_rank, tmr.total_rewards, tmr.total_rewards_quantum, 83 null as team_total_rewards, null as team_total_rewards_quantum, 'ENTITY_SCOPE_INDIVIDUALS' as entity_scope 84 from 85 rewards r 86 join game_epochs ge on r.game_id = ge.game_id 87 and r.epoch_id = ge.epoch_id 88 JOIN dispatch_strategies t ON r.game_id = t.game_id 89 AND t.dispatch_strategy ->> 'entity_scope' = '1' 90 left join game_individual_rankings tmr on r.game_id = tmr.game_id 91 and r.epoch_id = tmr.epoch_id 92 and r.party_id = tmr.party_id 93 ) 94 select * 95 from game_rewards 96 with data; 97 98 create materialized view game_stats_current as 99 with 100 game_epochs as ( 101 select game_id, max(epoch_id) as epoch_id 102 from rewards 103 where 104 game_id is not null 105 group by 106 game_id 107 ), 108 dispatch_strategies AS ( 109 SELECT DISTINCT 110 ON (game_id) game_id, dispatch_strategy 111 FROM transfers 112 WHERE 113 transfer_type = 'Recurring' 114 ORDER BY game_id, vega_time DESC 115 ), 116 game_rewards as ( 117 select 118 r.*, t.dispatch_strategy, tm.team_id, tmr.rank as member_rank, tr.rank as team_rank, tmr.total_rewards, tmr.total_rewards_quantum, 119 tr.total_rewards as team_total_rewards, tr.total_rewards_quantum as team_total_rewards_quantum, 'ENTITY_SCOPE_TEAMS' as entity_scope 120 from 121 rewards r 122 join game_epochs ge on r.game_id = ge.game_id 123 and r.epoch_id = ge.epoch_id 124 JOIN dispatch_strategies t ON r.game_id = t.game_id 125 AND t.dispatch_strategy ->> 'entity_scope' = '2' 126 join team_members tm on r.party_id = tm.party_id 127 left join game_team_rankings tr on r.game_id = tr.game_id 128 and r.epoch_id = tr.epoch_id 129 and tm.team_id = tr.team_id 130 left join game_team_member_rankings tmr on r.game_id = tmr.game_id 131 and r.epoch_id = tmr.epoch_id 132 and tm.team_id = tmr.team_id 133 and r.party_id = tmr.party_id 134 union all 135 select 136 r.*, t.dispatch_strategy, null as team_id, tmr.rank as member_rank, null as team_rank, tmr.total_rewards, tmr.total_rewards_quantum, 137 null as team_total_rewards, null as team_total_rewards_quantum, 'ENTITY_SCOPE_INDIVIDUALS' as entity_scope 138 from 139 rewards r 140 join game_epochs ge on r.game_id = ge.game_id 141 and r.epoch_id = ge.epoch_id 142 JOIN dispatch_strategies t ON r.game_id = t.game_id 143 AND t.dispatch_strategy ->> 'entity_scope' = '1' 144 left join game_individual_rankings tmr on r.game_id = tmr.game_id 145 and r.epoch_id = tmr.epoch_id 146 and r.party_id = tmr.party_id 147 ) 148 select * 149 from game_rewards 150 with data; 151 152 -- +goose Down 153 154 -- We are fixing a bug, and there is no point of reinstating a bug, we do nothing.