-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add heaviest chain materialized view (#97)
* feat: add heaviest chain materialized view * Change migration number * fix: Update view name for consistency with derived tables * chore: I like capital SQL Co-authored-by: Mike Greenberg <[email protected]>
- Loading branch information
Showing
2 changed files
with
43 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package migrations | ||
|
||
import ( | ||
"github.com/go-pg/migrations/v8" | ||
) | ||
|
||
// Schema version 8 adds view over derived_conensus_chain_view | ||
|
||
func init() { | ||
up := batch(` | ||
CREATE MATERIALIZED VIEW IF NOT EXISTS derived_conensus_chain_view AS | ||
WITH RECURSIVE consensus_chain AS ( | ||
SELECT | ||
b.cid, | ||
b.height, | ||
b.miner, | ||
b.timestamp, | ||
b.parent_state_root, | ||
b.win_count | ||
FROM block_headers b | ||
WHERE b.parent_state_root = (SELECT parent_state_root FROM block_headers ORDER BY height desc, parent_weight DESC LIMIT 1) | ||
UNION | ||
SELECT | ||
p.cid, | ||
p.height, | ||
p.miner, | ||
p.timestamp, | ||
p.parent_state_root, | ||
p.win_count | ||
FROM block_headers p | ||
INNER JOIN block_parents pb ON p.cid = pb.parent | ||
INNER JOIN consensus_chain c ON c.cid = pb.block | ||
) SELECT * FROM consensus_chain | ||
WITH NO DATA; | ||
`) | ||
|
||
down := batch(` | ||
DROP MATERIALIZED VIEW IF EXISTS derived_conensus_chain_view; | ||
`) | ||
|
||
migrations.MustRegisterTx(up, down) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters