-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Unique
dish.{name, cafeteriaID}
(#325)
* introduced an index and made sure that the dishes are unique * added the correct migration for `dish_ratings` FK-binding * modified the models.DishRating struct as well with the appropriate change
- Loading branch information
1 parent
9f7a0b5
commit 8b7f232
Showing
7 changed files
with
79 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package migration | ||
|
||
import ( | ||
"github.com/go-gormigrate/gormigrate/v2" | ||
"gorm.io/gorm" | ||
) | ||
|
||
// migrate20240311000000 | ||
// made sure that dishes have the correct indexes | ||
// changed how `dish_rating` is bound to `dish` | ||
func migrate20240311000000() *gormigrate.Migration { | ||
return &gormigrate.Migration{ | ||
ID: "20240311000000", | ||
Migrate: func(tx *gorm.DB) error { | ||
// make sure that dish_rating is FK-bound to dish | ||
if err := tx.Exec("alter table dish_rating modify dishID int").Error; err != nil { | ||
return err | ||
} | ||
if err := tx.Exec("alter table dish_rating add constraint dish_rating_dish_dish_fk foreign key (dishID) references dish (dish) on update cascade 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_rating is FK-bound to dishes | ||
if err := tx.Exec("alter table dish_rating drop constraint dish_rating_dish_dish_fk").Error; err != nil { | ||
return err | ||
} | ||
if err := tx.Exec("alter table dish_rating modify dishID int not null").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 | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters