From 89c195bfb1ccfb39ad19c4fadc65b2b36da32615 Mon Sep 17 00:00:00 2001 From: Frank Elsinga Date: Mon, 11 Mar 2024 02:32:02 +0100 Subject: [PATCH] introduced an index and made sure that the dishes are unique --- server/backend/cron/dish_name_download.go | 28 ++++++++++++---------- server/backend/migration/20240311000000.go | 20 ++++++++++++++++ server/backend/migration/migration.go | 28 +--------------------- server/model/dish.go | 4 ++-- 4 files changed, 38 insertions(+), 42 deletions(-) create mode 100644 server/backend/migration/20240311000000.go diff --git a/server/backend/cron/dish_name_download.go b/server/backend/cron/dish_name_download.go index 947321f1..a2efe896 100644 --- a/server/backend/cron/dish_name_download.go +++ b/server/backend/cron/dish_name_download.go @@ -2,6 +2,7 @@ package cron import ( "encoding/json" + "errors" "fmt" "net/http" "strings" @@ -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), diff --git a/server/backend/migration/20240311000000.go b/server/backend/migration/20240311000000.go new file mode 100644 index 00000000..2f4b2b20 --- /dev/null +++ b/server/backend/migration/20240311000000.go @@ -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 + }, + } +} diff --git a/server/backend/migration/migration.go b/server/backend/migration/migration.go index b031a60d..4bd06f29 100644 --- a/server/backend/migration/migration.go +++ b/server/backend/migration/migration.go @@ -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 } @@ -71,6 +44,7 @@ func manualMigrate(db *gorm.DB) error { migrate20240102000000(), migrate20240103000000(), migrate20240207000000(), + migrate20240311000000(), } return gormigrate.New(db, gormigrateOptions, migrations).Migrate() } diff --git a/server/model/dish.go b/server/model/dish.go index 7ab5c4d7..72b89fa7 100644 --- a/server/model/dish.go +++ b/server/model/dish.go @@ -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