Skip to content

Commit

Permalink
introduced an index and made sure that the dishes are unique
Browse files Browse the repository at this point in the history
  • Loading branch information
CommanderStorm committed Mar 11, 2024
1 parent a0e877b commit 89c195b
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 42 deletions.
28 changes: 15 additions & 13 deletions server/backend/cron/dish_name_download.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cron

import (
"encoding/json"
"errors"
"fmt"
"net/http"
"strings"
Expand Down Expand Up @@ -95,25 +96,26 @@ func downloadDailyDishes(c *CronService) {
CafeteriaID: v.Cafeteria,
}

var count int64
var dishId int64
if err := c.db.Model(&model.Dish{}).
Where("name = ? AND cafeteriaID = ?", dish.Name, dish.CafeteriaID).
Select("dish").
First(&dishId).
Count(&count).Error; err != nil {
log.WithError(err).Error("Error while checking whether this is already in database")
}
if count == 0 {
var dbDish model.Dish
if err := c.db.First(&dbDish, "name = ? AND cafeteriaID = ?", dish.Name, dish.CafeteriaID).Error; err != nil && errors.Is(err, gorm.ErrRecordNotFound) {
if err := c.db.Create(&dish).Error; err != nil {
log.WithError(err).Error("Error while creating new CanteenDish entry with name {}. CanteenDish won't be saved", dish.Name)
log.WithError(err).WithField("name", dish.Name).Error("Error while creating new CanteenDish entry. CanteenDish won't be saved")
}
addDishTagsToMapping(dish.Dish, dish.Name, c.db)
dishId = dish.Dish
dbDish = dish
} else if err != nil {
log.WithError(err).Error("Error while checking whether the dish is already in database")
}

if dbDish.Type != dish.Type {
if err := c.db.Where("dish = ?", dbDish.Dish).Updates(&dish).Error; err != nil {
log.WithError(err).WithField("from", dish.Type).WithField("to", dish.Type).Error("Error while updating dish to new type")
}
}

if weekliesWereAdded == 0 {
errCreate := c.db.Create(&model.DishesOfTheWeek{
DishID: dishId,
DishID: dbDish.Dish,
Year: int32(year),
Week: int32(week),
Day: int32(weekDayIndex),
Expand Down
20 changes: 20 additions & 0 deletions server/backend/migration/20240311000000.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package migration

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

// migrate20240311000000
// made sure that dishes have the correct indexes
func migrate20240311000000() *gormigrate.Migration {
return &gormigrate.Migration{
ID: "20240311000000",
Migrate: func(tx *gorm.DB) error {
return tx.Raw("create unique index dish_name_cafeteriaID_uindex on dish (name, cafeteriaID)").Error
},
Rollback: func(tx *gorm.DB) error {
return tx.Raw("drop index dish_name_cafeteriaID_uindex on dish").Error
},
}
}
28 changes: 1 addition & 27 deletions server/backend/migration/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,7 @@ import (

func autoMigrate(db *gorm.DB) error {
err := db.AutoMigrate(
&model.Cafeteria{},
&model.CafeteriaRating{},
&model.CafeteriaRatingTag{},
&model.CafeteriaRatingTagOption{},
&model.CanteenHeadCount{},
&model.Crontab{},
&model.Device{},
&model.Dish{},
&model.DishNameTag{},
&model.DishNameTagOption{},
&model.DishNameTagOptionExcluded{},
&model.DishNameTagOptionIncluded{},
&model.DishRating{},
&model.DishRatingTag{},
&model.DishRatingTagOption{},
&model.DishToDishNameTag{},
&model.DishesOfTheWeek{},
&model.Feedback{},
&model.File{},
&model.Kino{},
&model.NewExamResultsSubscriber{},
&model.News{},
&model.NewsAlert{},
&model.NewsSource{},
&model.Notification{},
&model.NotificationConfirmation{},
&model.NotificationType{},
&model.UpdateNote{},
)
return err
}
Expand Down Expand Up @@ -71,6 +44,7 @@ func manualMigrate(db *gorm.DB) error {
migrate20240102000000(),
migrate20240103000000(),
migrate20240207000000(),
migrate20240311000000(),
}
return gormigrate.New(db, gormigrateOptions, migrations).Migrate()
}
Expand Down
4 changes: 2 additions & 2 deletions server/model/dish.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ var (
// Dish represents one dish fin a specific cafeteria
type Dish struct {
Dish int64 `gorm:"primary_key;AUTO_INCREMENT;column:dish;type:int;not null;" json:"dish"`
Name string `gorm:"column:name;type:text;not null;" json:"name" `
Name string `gorm:"column:name;type:text;not null;uniqueIndex:dish_name_cafeteriaID_uindex" json:"name" `
Type string `gorm:"column:type;type:text;not null;" json:"type" `
CafeteriaID int64 `gorm:"column:cafeteriaID;foreignKey:cafeteria;type:int;not null;" json:"cafeteriaID"`
CafeteriaID int64 `gorm:"column:cafeteriaID;foreignKey:cafeteria;type:int;not null;uniqueIndex:dish_name_cafeteriaID_uindex" json:"cafeteriaID"`
}

// TableName sets the insert table name for this struct type
Expand Down

0 comments on commit 89c195b

Please sign in to comment.