Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Table name cleanup #419

Closed
wants to merge 22 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 20 additions & 19 deletions server/backend/cafeteria.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (s *CampusServer) ListCanteenRatings(ctx context.Context, input *pb.ListCan
// queryLastCafeteriaRatingsWithLimit
// Queries the actual ratings for a cafeteria and attaches the tag ratings which belong to the ratings
func queryLastCafeteriaRatingsWithLimit(input *pb.ListCanteenRatingsRequest, cafeteriaID int32, tx *gorm.DB) []*pb.SingleRatingReply {
var ratings []model.CafeteriaRating
var ratings []model.CanteenRating
var err error

var limit = int(input.Limit)
Expand All @@ -81,7 +81,8 @@ func queryLastCafeteriaRatingsWithLimit(input *pb.ListCanteenRatingsRequest, caf
} else {
to = input.To.AsTime()
}
err = tx.Order("timestamp desc, cafeteriaRating desc").
err = tx.
Order("timestamp desc, cafeteriaRating desc").
Limit(limit).
Find(&ratings, "cafeteriaID = ? AND timestamp < ? AND timestamp > ?", cafeteriaID, to, from).Error
} else {
Expand Down Expand Up @@ -233,25 +234,25 @@ func queryTags(cafeteriaID int32, dishID int32, ratingType model.ModelType, tx *
var results []queryRatingTag
var err error
if ratingType == model.DISH {
err = tx.Table("dish_rating_tag_option options").
err = tx.Table("dish_rating_tag_options options").
Joins("JOIN dish_rating_tag_statistics results ON options.dishRatingTagOption = results.tagID").
Select("options.dishRatingTagOption as tagId, results.average as avg, "+
"results.min as min, results.max as max, results.std as std").
Where("results.cafeteriaID = ? AND results.dishID = ?", cafeteriaID, dishID).
Scan(&results).Error
} else if ratingType == model.CAFETERIA {
err = tx.Table("cafeteria_rating_tag_option options").
err = tx.Table("cafeteria_rating_tag_options options").
Joins("JOIN cafeteria_rating_tag_statistics results ON options.cafeteriaRatingTagOption = results.tagID").
Select("options.cafeteriaRatingTagOption as tagId, results.average as avg, "+
"results.min as min, results.max as max, results.std as std").
Where("results.cafeteriaID = ?", cafeteriaID).
Scan(&results).Error
} else { //Query for name tags
err = tx.Table("dish_to_dish_name_tag mapping").
err = tx.Table("dish_to_dish_name_tags mapping").
Where("mapping.dishID = ?", dishID).
Select("mapping.nameTagID as tag").
Joins("JOIN dish_name_tag_statistics results ON mapping.nameTagID = results.tagID").
Joins("JOIN dish_name_tag_option options ON mapping.nameTagID = options.dishNameTagOption").
Joins("JOIN dish_name_tag_options options ON mapping.nameTagID = options.dishNameTagOption").
Select("mapping.nameTagID as tagId, results.average as avg, " +
"results.min as min, results.max as max, results.std as std").
Scan(&results).Error
Expand Down Expand Up @@ -282,13 +283,13 @@ func queryTagRatingsOverviewForRating(dishID int64, ratingType model.ModelType,
var results []*pb.RatingTagNewRequest
var err error
if ratingType == model.DISH {
err = tx.Table("dish_rating_tag_option options").
Joins("JOIN dish_rating_tag rating ON options.dishRatingTagOption = rating.tagID").
err = tx.Table("dish_rating_tag_options options").
Joins("JOIN dish_rating_tags rating ON options.dishRatingTagOption = rating.tagID").
Select("dishRatingTagOption as tagId, points, parentRating").
Find(&results, "parentRating = ?", dishID).Error
} else {
err = tx.Table("cafeteria_rating_tag_option options").
Joins("JOIN cafeteria_rating_tag rating ON options.cafeteriaRatingTagOption = rating.tagID").
err = tx.Table("cafeteria_rating_tag_options options").
Joins("JOIN cafeteria_rating_tags rating ON options.cafeteriaRatingTagOption = rating.tagID").
Select("cafeteriaRatingTagOption as tagId, points, correspondingRating").
Find(&results, "correspondingRating = ?", dishID).Error
}
Expand All @@ -312,7 +313,7 @@ func (s *CampusServer) CreateCanteenRating(ctx context.Context, input *pb.Create
}

resPath := imageWrapper(input.Image, "cafeterias", cafeteriaID)
rating := model.CafeteriaRating{
rating := model.CanteenRating{
Comment: input.Comment,
Points: input.Points,
CafeteriaID: cafeteriaID,
Expand Down Expand Up @@ -457,10 +458,10 @@ func inputSanitizationForNewRatingElements(rating int32, comment string, cafeter
return -1, status.Error(codes.InvalidArgument, "Comments must not contain @ symbols in order to prevent misuse. Rating has not been saved.")
}

var result *model.Cafeteria
var result *model.Canteen
if res := tx.First(&result, "name LIKE ?", cafeteriaName); errors.Is(res.Error, gorm.ErrRecordNotFound) || res.RowsAffected == 0 {
log.WithError(res.Error).Error("Error while querying the cafeteria id by name: ", cafeteriaName)
return -1, status.Error(codes.InvalidArgument, "Cafeteria does not exist. Rating has not been saved.")
return -1, status.Error(codes.InvalidArgument, "Canteen does not exist. Rating has not been saved.")
}

return result.Cafeteria, nil
Expand All @@ -484,7 +485,7 @@ func storeRatingTags(parentRatingID int64, tags []*pb.RatingTag, tagType model.M
Where("dishRatingTagOption LIKE ?", currentTag.TagId).
Count(&count).Error
} else {
err = tx.Model(&model.CafeteriaRatingTagOption{}).
err = tx.Model(&model.CanteenRatingTagOption{}).
Where("cafeteriaRatingTagOption LIKE ?", currentTag.TagId).
Count(&count).Error
}
Expand Down Expand Up @@ -534,13 +535,13 @@ func getModelStoreTag(tagType model.ModelType, tx *gorm.DB) *gorm.DB {
if tagType == model.DISH {
return tx.Model(&model.DishRatingTag{})
} else {
return tx.Model(&model.CafeteriaRatingTag{})
return tx.Model(&model.CanteenRatingTag{})
}
}

func getIDForCafeteriaName(name string, tx *gorm.DB) int32 {
var result int32 = -1
err := tx.Model(&model.Cafeteria{}).
err := tx.Model(&model.Canteen{}).
Where("name LIKE ?", name).
Select("cafeteria").
Scan(&result).Error
Expand Down Expand Up @@ -609,7 +610,7 @@ func (s *CampusServer) GetAvailableCafeteriaTags(ctx context.Context, _ *pb.List
var result []*pb.TagsOverview
var requestStatus error = nil
err := s.db.WithContext(ctx).
Model(&model.CafeteriaRatingTagOption{}).
Model(&model.CanteenRatingTagOption{}).
Select("DE as de, EN as en, cafeteriaRatingsTagOption as TagId").
Find(&result).Error
if err != nil {
Expand All @@ -628,7 +629,7 @@ func (s *CampusServer) GetCafeterias(ctx context.Context, _ *pb.ListCanteensRequ
var result []*pb.Canteen
var requestStatus error = nil
if err := s.db.WithContext(ctx).
Model(&model.Cafeteria{}).
Model(&model.Canteen{}).
Select("cafeteria as id,address,latitude,longitude").
Scan(&result).Error; err != nil {
log.WithError(err).Error("while loading Cafeterias from database.")
Expand Down Expand Up @@ -657,7 +658,7 @@ func (s *CampusServer) ListDishes(ctx context.Context, req *pb.ListDishesRequest
cafeteriaName := strings.ReplaceAll(strings.ToUpper(req.CanteenId), "-", "_")

err := s.db.WithContext(ctx).
Table("dishes_of_the_week weekly").
Table("dishes_of_the_weeks weekly").
Where("weekly.day = ? AND weekly.week = ? and weekly.year = ?", req.Day, req.Week, req.Year).
Select("weekly.dishID").
Joins("JOIN dish d ON d.dish = weekly.dishID").
Expand Down
10 changes: 5 additions & 5 deletions server/backend/cron/dish_name_download.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (c *CronService) dishNameDownloadCron() error {
}

func downloadDailyDishes(c *CronService) {
var results []model.Cafeteria
var results []model.Canteen
if err := c.db.Find(&results).Error; err != nil {
log.WithError(err).Error("Error while querying all cafeteria names from the database.")
return
Expand Down Expand Up @@ -145,13 +145,13 @@ func downloadCanteenNames(c *CronService) {
}

for _, cafeteriaName := range cafeteriaNames {
mensa := model.Cafeteria{
mensa := model.Canteen{
Name: cafeteriaName.Name,
Address: cafeteriaName.Location.Address,
Latitude: cafeteriaName.Location.Latitude,
Longitude: cafeteriaName.Location.Longitude,
}
var cafeteriaResult model.Cafeteria
var cafeteriaResult model.Canteen
if err := c.db.First(&cafeteriaResult, "name = ?", cafeteriaName.Name).Error; err != nil {
if err := c.db.Create(&mensa).Error; err != nil {
log.WithError(err).Error("Error while creating the db entry for the cafeteria ", cafeteriaName.Name)
Expand All @@ -170,15 +170,15 @@ func downloadCanteenNames(c *CronService) {
func addDishTagsToMapping(dishID int64, dishName string, db *gorm.DB) {
lowercaseDish := strings.ToLower(dishName)
var includedTags []int64
if err := db.Model(&model.DishNameTagOptionIncluded{}).
if err := db.Model(&model.IncludedDishNameTagOption{}).
Where("? LIKE CONCAT('%', expression ,'%')", lowercaseDish).
Select("nameTagID").
Scan(&includedTags).Error; err != nil {
log.WithError(err).Error("Error while querying all included expressions for the CanteenDish: ", lowercaseDish)
}

var excludedTags []int64
if err := db.Model(&model.DishNameTagOptionExcluded{}).
if err := db.Model(&model.ExcludedDishNameTagOption{}).
Where("? LIKE CONCAT('%', expression ,'%')", lowercaseDish).
Select("nameTagID").
Scan(&excludedTags).Error; err != nil {
Expand Down
4 changes: 2 additions & 2 deletions server/backend/cron/movies.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func (c *CronService) movieCron() error {
}
log.Trace("parsing upcoming feed")
var allMovieLinks []string
if err := c.db.Model(&model.Kino{}).
if err := c.db.Model(&model.Movie{}).
Distinct().
Pluck("Link", &allMovieLinks).Error; err != nil {
return err
Expand Down Expand Up @@ -70,7 +70,7 @@ func (c *CronService) movieCron() error {
log.WithFields(logFields).WithError(err).Error("error while finding imdb id")
continue
}
movie := model.Kino{
movie := model.Movie{
Date: date,
Title: item.Title,
Location: null.StringFrom(item.Location),
Expand Down
31 changes: 0 additions & 31 deletions server/backend/migration/20230825000000.go

This file was deleted.

79 changes: 79 additions & 0 deletions server/backend/migration/20230912000000.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package migration

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

// migrate20230912000000
// Removes ticketsales from the db
func migrate20230912000000() *gormigrate.Migration {
return &gormigrate.Migration{
ID: "20230912000000",
Migrate: func(tx *gorm.DB) error {
// order intentional to avoid foreign key constraint errors
for _, tbl := range []string{"ticket_history", "ticket_type", "ticket_payment", "ticket_admin2group", "ticket_admin", "event", "ticket_group"} {
if err := tx.Migrator().DropTable(tbl); err != nil {
return err
}
}
return nil
},

Rollback: func(tx *gorm.DB) error {
//ticket_group
if err := tx.Exec(`create table ticket_group (ticket_group int auto_increment primary key,description text not null);`).Error; err != nil {
return err
}
//event
if err := tx.Exec("create table event(event int auto_increment primary key, news int null, kino int null, file int null, title varchar(100) not null, description text not null, locality varchar(200) not null, link varchar(200) null, start datetime null, end datetime null, ticket_group int default 1 null, constraint fkEventFile foreign key (file) references files (file) on update cascade on delete set null, constraint fkEventGroup foreign key (ticket_group) references ticket_group (ticket_group), constraint fkKino foreign key (kino) references kino (kino) on update cascade on delete set null, constraint fkNews foreign key (news) references news (news) on update cascade on delete set null);").Error; err != nil {
return err
}
if err := tx.Exec("create index file on event (file);").Error; err != nil {
return err
}
if err := tx.Exec("create fulltext index searchTitle on event (title);").Error; err != nil {
return err
}
//ticket_admin
if err := tx.Exec("create table ticket_admin(ticket_admin int auto_increment primary key, `key` text not null, created timestamp default current_timestamp() not null, active tinyint(1) default 0 not null, comment text null);").Error; err != nil {
return err
}
//ticket_admin2group
if err := tx.Exec("create table ticket_admin2group( ticket_admin2group int auto_increment primary key, ticket_admin int not null, ticket_group int not null, constraint fkTicketAdmin foreign key (ticket_admin) references ticket_admin (ticket_admin) on update cascade on delete cascade, constraint fkTicketGroup foreign key (ticket_group) references ticket_group (ticket_group) on update cascade on delete cascade);").Error; err != nil {
return err
}
if err := tx.Exec("create index ticket_admin on ticket_admin2group (ticket_admin);").Error; err != nil {
return err
}
if err := tx.Exec("create index ticket_group on ticket_admin2group (ticket_group);").Error; err != nil {
return err
}
//ticket_payment
if err := tx.Exec("create table ticket_payment( ticket_payment int auto_increment primary key, name varchar(50) not null, min_amount int null, max_amount int null, config text not null);").Error; err != nil {
return err
}
//ticket_type
if err := tx.Exec("create table ticket_type( ticket_type int auto_increment primary key, event int not null, ticket_payment int not null, price double not null, contingent int not null, description varchar(100) not null, constraint fkEvent foreign key (event) references event (event) on update cascade on delete cascade, constraint fkPayment foreign key (ticket_payment) references ticket_payment (ticket_payment) on update cascade);").Error; err != nil {
return err
}
if err := tx.Exec("create index event on ticket_type (event);").Error; err != nil {
return err
}
if err := tx.Exec("create index ticket_payment on ticket_type (ticket_payment);").Error; err != nil {
return err
}
//ticket_history
if err := tx.Exec("create table ticket_history( ticket_history int auto_increment primary key, member int not null, ticket_payment int null, ticket_type int not null, purchase datetime null, redemption datetime null, created timestamp default current_timestamp() not null, code char(128) not null, constraint fkMember foreign key (member) references member (member) on update cascade on delete cascade, constraint fkTicketPayment foreign key (ticket_payment) references ticket_payment (ticket_payment) on update cascade, constraint fkTicketType foreign key (ticket_type) references ticket_type (ticket_type) on update cascade);").Error; err != nil {
return err
}
if err := tx.Exec("create index member on ticket_history (member);").Error; err != nil {
return err
}
if err := tx.Exec("create index ticket_payment on ticket_history (ticket_payment);").Error; err != nil {
return err
}
return tx.Exec("create index ticket_type on ticket_history (ticket_type);").Error
},
}
}
2 changes: 1 addition & 1 deletion server/backend/migration/20231003000000.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ func addNotIncluded(parentId int64, db *gorm.DB, v nameTag) {
Expression: expression,
NameTagID: parentId}).Error
if err != nil {
log.WithError(err).WithFields(fields).Error("Unable to create new DishNameTagOptionExcluded")
log.WithError(err).WithFields(fields).Error("Unable to create new ExcludedDishNameTagOption")
}
}
}
Expand Down
58 changes: 58 additions & 0 deletions server/backend/migration/20231024000000.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package migration

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

type wrongTableName struct {
Original string
New string
}

var wrongTableNames = []wrongTableName{
{"crontab", "crontabs"},
{"kino", "movies"},
{"cafeteria", "canteens"},
{"canteen_head_count", "canteen_head_counts"},
{"cafeteria_rating", "canteen_ratings"},
{"cafeteria_rating_tag", "canteen_rating_tags"},
{"cafeteria_rating_tag_option", "canteen_rating_tag_options"},
{"dish", "dishes"},
{"dish_rating", "dish_ratings"},
{"dish_rating_tag", "dish_rating_tags"},
{"dish_rating_tag_option", "dish_rating_tag_options"},
{"dish_name_tag", "dish_name_tags"},
{"dish_name_tag_option", "dish_name_tag_options"},
{"dish_name_tag_option_excluded", "excluded_dish_name_tag_options"},
{"dish_name_tag_option_included", "included_dish_name_tag_option"},
{"dish_to_dish_name_tag", "dish_to_dish_name_tags"},
{"dishes_of_the_week", "dishes_of_the_weeks"},
{"update_note", "update_notes"},
{"newsSource", "news_sources"},
{"news_alert", "news_alerts"},
}

// migrate20231024000000
// - replaces all instances of misleadingly named tables with the correct ones
func migrate20231024000000() *gormigrate.Migration {
return &gormigrate.Migration{
ID: "20231024000000",
Migrate: func(tx *gorm.DB) error {
for _, table := range wrongTableNames {
if err := tx.Migrator().RenameTable(table.Original, table.New); err != nil {
return err
}
}
return nil
},
Rollback: func(tx *gorm.DB) error {
for _, table := range wrongTableNames {
if err := tx.Migrator().RenameTable(table.New, table.Original); err != nil {
return err
}
}
return nil
},
}
}
Loading
Loading