Skip to content

Commit

Permalink
added the correct migration for dish_ratings FK-binding
Browse files Browse the repository at this point in the history
  • Loading branch information
CommanderStorm committed Mar 13, 2024
1 parent 5d23caf commit a9959b4
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions server/backend/migration/20240311000000.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,35 @@ import (

// migrate20240311000000
// made sure that dishes have the correct indexes
// changed how `dish_ratings` is bound to `dish`
func migrate20240311000000() *gormigrate.Migration {
return &gormigrate.Migration{
ID: "20240311000000",
Migrate: func(tx *gorm.DB) error {
// make sure that dish_ratings are FK-bound to dishes
if err := tx.Exec(`alter table dish_rating
add constraint dish_rating_dish_dish_fk
foreign key (dishID) references dish (dish)
on delete cascade;`).Error; err != nil {
return err
}
// because dishes already have a cafeteria, storing this again is not necessary
if err := tx.Exec(`alter table dish_rating drop column cafeteriaID`).Error; err != nil {
return err
}
// uniqueness
return tx.Exec("create unique index dish_name_cafeteriaID_uindex on dish (name, cafeteriaID)").Error
},
Rollback: func(tx *gorm.DB) error {
// make sure that dish_ratings are FK-bound to dishes
if err := tx.Exec(`alter table dish_rating drop constraint dish_rating_dish_dish_fk`).Error; err != nil {
return err
}
// because dishes already have a cafeteria, storing this agiain is not nessesary
if err := tx.Exec(`alter table dish_rating add column cafeteriaID int not null`).Error; err != nil {
return err
}
// uniqueness
return tx.Exec("drop index dish_name_cafeteriaID_uindex on dish").Error
},
}
Expand Down

0 comments on commit a9959b4

Please sign in to comment.