Skip to content

Commit

Permalink
added part of the missing dishrating-fks
Browse files Browse the repository at this point in the history
  • Loading branch information
CommanderStorm committed Mar 17, 2024
1 parent 1c441ba commit 38ddbff
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
51 changes: 51 additions & 0 deletions server/backend/migration/20240320000000.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
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",

Check failure on line 45 in server/backend/migration/20240320000000.go

View workflow job for this annotation

GitHub Actions / lint

printf: fmt.Sprintf call needs 4 args but has 7 args (govet)
fk.fromTable, fk.fromTable, fk.toTable, fk.toColumn, fk.fromColumn, 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 @@ -77,6 +77,7 @@ func manualMigrate(db *gorm.DB) error {
migrate20240317000000(),
migrate20240318000000(),
migrate20240319000000(),
migrate20240320000000(),
}
return gormigrate.New(db, gormigrateOptions, migrations).Migrate()
}
Expand Down

0 comments on commit 38ddbff

Please sign in to comment.