diff --git a/stores/metadata_test.go b/stores/metadata_test.go index 90a386667..9ccc84c50 100644 --- a/stores/metadata_test.go +++ b/stores/metadata_test.go @@ -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, }, }, }) @@ -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) @@ -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 diff --git a/stores/sql/main.go b/stores/sql/main.go index 5e624c25c..df3683415 100644 --- a/stores/sql/main.go +++ b/stores/sql/main.go @@ -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) }