Skip to content

Commit

Permalink
Adds the missing dishrating-FKs (#335)
Browse files Browse the repository at this point in the history
* added part of the missing dishrating-fks

* fixed build-error

* linting fix
  • Loading branch information
CommanderStorm authored Sep 24, 2024
1 parent e752e0a commit 762c664
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
52 changes: 52 additions & 0 deletions server/backend/migration/20240320000000.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package migration

import (
"fmt"

"github.com/go-gormigrate/gormigrate/v2"
"gorm.io/gorm"
)

type missingFK struct {
fromTable string
fromColumn string
toTable string
toColumn string
}

func missingDishFKs() []missingFK {
return []missingFK{
{"cafeteria_rating", "cafeteriaID", "cafeteria", "cafeteria"},
{"dish", "cafeteriaID", "cafeteria", "cafeteria"},
{"dish2dishflags", "dish", "dish", "dish"},
{"dish2dishflags", "flag", "dishflags", "flag"},
{"dish2mensa", "dish", "dish", "dish"},
{"dish2mensa", "mensa", "mensa", "mensa"},
{"dish_rating_tag", "parentRating", "dish_rating", "dishRating"},
{"dish_rating_tag", "tagID", "dish_rating_tag_option", "dishRatingTagOption"},
{"dish_to_dish_name_tag", "dishID", "dish", "dish"},
{"dishes_of_the_week", "dishID", "dish", "dish"},
}
}

// migrate20240320000000
// made sure that all dish ratings are connected with FK-Relationships
func migrate20240320000000() *gormigrate.Migration {
return &gormigrate.Migration{
ID: "20240320000000",
Migrate: func(tx *gorm.DB) error {
for _, fk := range missingDishFKs() {
tx.Exec(fmt.Sprintf("alter table `%s` add constraint %s_%s_%s_fk foreign key (`%s`) references `%s` (`%s`) on update cascade on delete cascade",
fk.fromTable, fk.fromTable, fk.toTable, fk.toColumn, fk.fromColumn, fk.toTable, fk.toColumn))
}
return nil
},
Rollback: func(tx *gorm.DB) error {
for _, fk := range missingDishFKs() {
tx.Exec(fmt.Sprintf("alter table `%s` drop foreign key %s_%s_%s_fk",
fk.fromTable, fk.fromTable, fk.toTable, fk.toColumn))
}
return nil
},
}
}
1 change: 1 addition & 0 deletions server/backend/migration/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ func manualMigrate(db *gorm.DB) error {
migrate20240317000000(),
migrate20240318000000(),
migrate20240319000000(),
migrate20240320000000(),
migrate20240327000000(),
migrate20240402000000(),
migrate20240405000000(),
Expand Down

0 comments on commit 762c664

Please sign in to comment.