Skip to content

Commit

Permalink
sqlite: add sector_index migration
Browse files Browse the repository at this point in the history
  • Loading branch information
n8maninger committed Oct 4, 2023
1 parent 477b2e9 commit 0bd494d
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions persist/sqlite/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,26 @@ import (
"go.sia.tech/hostd/host/contracts"
)

// migrateVersion17 recalculates the indices of all contract sector roots.
// Fixes a bug where the indices were not being properly updated if more than
// one root was trimmed.
func migrateVersion17(tx txn) error {
const query = `
-- create a temp table that contains the new indices
CREATE TEMP TABLE temp_contract_sector_roots AS
SELECT * FROM (SELECT id, contract_id, root_index, ROW_NUMBER() OVER (PARTITION BY contract_id ORDER BY root_index ASC)-1 AS expected_root_index
FROM contract_sector_roots) a WHERE root_index <> expected_root_index ORDER BY contract_id, root_index ASC;
-- update the contract_sector_roots table with the new indices
UPDATE contract_sector_roots
SET root_index = (SELECT expected_root_index FROM temp_contract_sector_roots WHERE temp_contract_sector_roots.id = contract_sector_roots.id)
WHERE id IN (SELECT id FROM temp_contract_sector_roots);
-- drop the temp table
DROP TABLE temp_contract_sector_roots;`

_, err := tx.Exec(query)
return err
}

// migrateVersion16 recalculates the contract and physical sector metrics.
func migrateVersion16(tx txn) error {
// recalculate the contract sectors metric
Expand Down Expand Up @@ -463,4 +483,5 @@ var migrations = []func(tx txn) error{
migrateVersion14,
migrateVersion15,
migrateVersion16,
migrateVersion17,
}

0 comments on commit 0bd494d

Please sign in to comment.