Skip to content

Commit

Permalink
only delete host sector from hosts with provided key
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisSchinnerl committed Dec 19, 2024
1 parent 793a15f commit 3ecebe8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
16 changes: 14 additions & 2 deletions stores/metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3436,8 +3436,10 @@ func TestDeleteHostSector(t *testing.T) {
MinShards: 1,
Shards: []object.Sector{
{
Contracts: map[types.PublicKey][]types.FileContractID{hk1: fcids},
Root: root,
Contracts: map[types.PublicKey][]types.FileContractID{
hk1: fcids,
},
Root: root,
},
},
})
Expand All @@ -3447,6 +3449,11 @@ func TestDeleteHostSector(t *testing.T) {
t.Fatal("expected 4 contract-sector links", n)
}

// Make sure 2 hostSector entries exist.
if n := ss.Count("host_sectors"); n != 2 {
t.Fatal("expected 2 host-sector links", n)
}

// Prune the sector from hk1.
if n, err := ss.DeleteHostSector(context.Background(), hk1, root); err != nil {
t.Fatal(err)
Expand All @@ -3459,6 +3466,11 @@ func TestDeleteHostSector(t *testing.T) {
t.Fatal("expected 2 contract-sector links", n)
}

// Make sure 1 hostSector entry exists.
if n := ss.Count("host_sectors"); n != 1 {
t.Fatal("expected 1 host-sector link", n)
}

// Find the slab. It should have an invalid health.
var slabID int64
var validUntil int64
Expand Down
9 changes: 8 additions & 1 deletion stores/sql/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,14 @@ func DeleteHostSector(ctx context.Context, tx sql.Tx, hk types.PublicKey, root t
}

// remove sector from host_sectors
_, err = tx.Exec(ctx, "DELETE FROM host_sectors WHERE db_sector_id = ?", sectorID)
_, err = tx.Exec(ctx, `
DELETE FROM host_sectors
WHERE db_sector_id = ? AND db_host_id IN (
SELECT h.id
FROM hosts h
WHERE h.public_key = ?
)
`, sectorID, PublicKey(hk))
if err != nil {
return 0, fmt.Errorf("failed to delete host sector: %w", err)
}
Expand Down

0 comments on commit 3ecebe8

Please sign in to comment.