diff --git a/model/migrate_test.go b/model/migrate_test.go index 62491bb5..2c2ec627 100644 --- a/model/migrate_test.go +++ b/model/migrate_test.go @@ -77,6 +77,17 @@ func TestFKSetNullOnDelete(t *testing.T) { }) } +func TestCarBlockFKIndexes(t *testing.T) { + testutil.All(t, func(ctx context.Context, t *testing.T, db *gorm.DB) { + require.True(t, db.Migrator().HasIndex(&model.CarBlock{}, "CarID"), + "car_blocks.car_id must be indexed for FK cascade performance") + require.True(t, db.Migrator().HasIndex(&model.CarBlock{}, "FileID"), + "car_blocks.file_id must be indexed for FK cascade performance") + require.True(t, db.Migrator().HasIndex(&model.CarBlock{}, "CID"), + "car_blocks.cid must be indexed for gateway lookups") + }) +} + func TestInferPieceTypes(t *testing.T) { testutil.All(t, func(ctx context.Context, t *testing.T, db *gorm.DB) { prep := model.Preparation{Name: "test", MaxSize: 1024, PieceSize: 1024} diff --git a/model/preparation.go b/model/preparation.go index 7a448435..fd19ab1a 100644 --- a/model/preparation.go +++ b/model/preparation.go @@ -304,7 +304,7 @@ type CarBlock struct { // Associations - SET NULL for fast prep deletion, async cleanup CarID *CarID `cbor:"-" gorm:"index" json:"carId"` Car *Car `cbor:"-" gorm:"foreignKey:CarID;constraint:OnDelete:SET NULL" json:"car,omitempty" swaggerignore:"true"` - FileID *FileID `cbor:"7,keyasint,omitempty" json:"fileId"` + FileID *FileID `cbor:"7,keyasint,omitempty" gorm:"index" json:"fileId"` File *File `cbor:"-" gorm:"foreignKey:FileID;constraint:OnDelete:SET NULL" json:"file,omitempty" swaggerignore:"true"` }