From a9959b42f73c3f91eff6a684d5f7d842f1be2c2c Mon Sep 17 00:00:00 2001 From: Frank Elsinga Date: Wed, 13 Mar 2024 21:19:41 +0100 Subject: [PATCH] added the correct migration for `dish_ratings` FK-binding --- server/backend/migration/20240311000000.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/server/backend/migration/20240311000000.go b/server/backend/migration/20240311000000.go index dd082ef9..59cd41af 100644 --- a/server/backend/migration/20240311000000.go +++ b/server/backend/migration/20240311000000.go @@ -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 }, }