Skip to content

Commit

Permalink
Move Canteen Static Data in Migration (#251)
Browse files Browse the repository at this point in the history
* moved old functions in new migration

* remove unnecessary checks whether tag exists

* removed old initializer

* delete previous data + cron entries fix

* silenced logging of adding entries

* registered the migration andfixed a few typos

* fixed linting issues

* added code review feedback

* added state of models in migration

* Update server/backend/migration/20231003000000.go

Co-authored-by: Frank Elsinga <[email protected]>

* Update server/backend/migration/20231003000000.go

Co-authored-by: Frank Elsinga <[email protected]>

* Update server/backend/migration/20231003000000.go

Co-authored-by: Frank Elsinga <[email protected]>

* Update server/backend/migration/20231003000000.go

Co-authored-by: Frank Elsinga <[email protected]>

* Update server/backend/migration/20231003000000.go

Co-authored-by: Frank Elsinga <[email protected]>

* Update server/backend/migration/20231003000000.go

Co-authored-by: Frank Elsinga <[email protected]>

* Update server/backend/migration/20231003000000.go

Co-authored-by: Frank Elsinga <[email protected]>

* Update server/backend/migration/20231003000000.go

Co-authored-by: Frank Elsinga <[email protected]>

* Update server/backend/migration/20231003000000.go

Co-authored-by: Frank Elsinga <[email protected]>

* Update server/backend/migration/20231003000000.go

Co-authored-by: Frank Elsinga <[email protected]>

* Update server/backend/migration/20231003000000.go

Co-authored-by: Frank Elsinga <[email protected]>

* Update server/backend/migration/20231003000000.go

Co-authored-by: Frank Elsinga <[email protected]>

* create missing tables in migration

* fixed batch delete in migration

* set fix version of migrated structs

* formatting change

* formatting change

* removed unnecessary create models

* removed further instances of hasTable

---------

Co-authored-by: Frank Elsinga <[email protected]>
  • Loading branch information
tobiasjungmann and CommanderStorm authored Oct 9, 2023
1 parent ea68230 commit c9801de
Show file tree
Hide file tree
Showing 8 changed files with 250 additions and 245 deletions.
16 changes: 8 additions & 8 deletions server/backend/cafeteria.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ import (
"gorm.io/gorm"
)

type modelType int
type ModelType int

// Used to differentiate between the type of the model for different queries to reduce duplicated code.
const (
DISH modelType = 1
CAFETERIA modelType = 2
NAME modelType = 3
DISH ModelType = 1
CAFETERIA ModelType = 2
NAME ModelType = 3
)

// GetCafeteriaRatings RPC Endpoint
Expand Down Expand Up @@ -284,7 +284,7 @@ type queryRatingTag struct {
// queryTags
// Queries the average ratings for either cafeteriaRatingTags, dishRatingTags or NameTags.
// Since the db only stores IDs in the results, the tags must be joined to retrieve their names form the rating_options tables.
func queryTags(cafeteriaID int32, dishID int32, ratingType modelType, tx *gorm.DB) []*pb.RatingTagResult {
func queryTags(cafeteriaID int32, dishID int32, ratingType ModelType, tx *gorm.DB) []*pb.RatingTagResult {
var results []queryRatingTag
var err error
if ratingType == DISH {
Expand Down Expand Up @@ -333,7 +333,7 @@ func queryTags(cafeteriaID int32, dishID int32, ratingType modelType, tx *gorm.D

// queryTagRatingOverviewForRating
// Query all rating tags which belong to a specific rating given with an ID and return it as TagRatingOverviews
func queryTagRatingsOverviewForRating(dishID int64, ratingType modelType, tx *gorm.DB) []*pb.RatingTagNewRequest {
func queryTagRatingsOverviewForRating(dishID int64, ratingType ModelType, tx *gorm.DB) []*pb.RatingTagNewRequest {
var results []*pb.RatingTagNewRequest
var err error
if ratingType == DISH {
Expand Down Expand Up @@ -538,7 +538,7 @@ func inputSanitizationForNewRatingElements(rating int32, comment string, cafeter
// storeRatingTags
// Checks whether the rating-tag name is a valid option and if so,
// it will be saved with a reference to the rating
func storeRatingTags(parentRatingID int64, tags []*pb.RatingTag, tagType modelType, tx *gorm.DB) error {
func storeRatingTags(parentRatingID int64, tags []*pb.RatingTag, tagType ModelType, tx *gorm.DB) error {
var errorOccurred = ""
var warningOccurred = ""
if len(tags) > 0 {
Expand Down Expand Up @@ -599,7 +599,7 @@ func storeRatingTags(parentRatingID int64, tags []*pb.RatingTag, tagType modelTy

}

func getModelStoreTag(tagType modelType, tx *gorm.DB) *gorm.DB {
func getModelStoreTag(tagType ModelType, tx *gorm.DB) *gorm.DB {
if tagType == DISH {
return tx.Model(&model.DishRatingTag{})
} else {
Expand Down
227 changes: 0 additions & 227 deletions server/backend/cafeteria_rating_db_initializer.go

This file was deleted.

14 changes: 14 additions & 0 deletions server/backend/migration/20210709193000.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ import (
"gorm.io/gorm"
)

type File struct {
File int64 `gorm:"primary_key;AUTO_INCREMENT;column:file;type:int;" json:"file"`
Name string `gorm:"column:name;type:text;size:16777215;" json:"name"`
Path string `gorm:"column:path;type:text;size:16777215;" json:"path"`
Downloads int32 `gorm:"column:downloads;type:int;default:0;" json:"downloads"`
URL null.String `gorm:"column:url;default:null;" json:"url"` // URL of the files source (if any)
Downloaded null.Bool `gorm:"column:downloaded;type:boolean;default:1;" json:"downloaded"` // true when file is ready to be served, false when still being downloaded
}

// migrate20210709193000
// adds a "url" column to the database containing the url the file was downloaded from.
// adds a "finished" column to the database that indicates, that a files download is finished.
Expand All @@ -15,6 +24,11 @@ func (m TumDBMigrator) migrate20210709193000() *gormigrate.Migration {
return &gormigrate.Migration{
ID: "20210709193000",
Migrate: func(tx *gorm.DB) error {
if err := tx.AutoMigrate(
&File{},
); err != nil {
return err
}
type Files struct {
URL null.String `gorm:"column:url;default:null;" json:"url"` // URL of the file source (if any)
Downloaded null.Bool `gorm:"column:downloaded;type:boolean;default:1;" json:"downloaded"` // true when file is ready to be served, false when still being downloaded
Expand Down
19 changes: 16 additions & 3 deletions server/backend/migration/20230904100000.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,18 @@ import (
"gorm.io/gorm"
)

// NewsSource struct is a row record of the newsSource table in the tca database
type NewsSource struct {
Source int64 `gorm:"primary_key;AUTO_INCREMENT;column:source;type:int;"`
Title string `gorm:"column:title;type:text;size:16777215;"`
URL null.String `gorm:"column:url;type:text;size:16777215;"`
FileID int64 `gorm:"column:icon;not null;type:int;"`
File model.File `gorm:"foreignKey:FileID;references:file;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"`
Hook null.String `gorm:"column:hook;type:char;size:12;"`
}

// migrate20230904100000
// migrates the crontap from kino to movie crontab
// migrates the crontab from kino to movie crontab
func (m TumDBMigrator) migrate20230904100000() *gormigrate.Migration {
return &gormigrate.Migration{
ID: "20230904100000",
Expand All @@ -23,8 +33,11 @@ func (m TumDBMigrator) migrate20230904100000() *gormigrate.Migration {
if err := SafeEnumMigrate(tx, &model.Crontab{}, "type", "movie"); err != nil {
return err
}
if err := tx.AutoMigrate(&NewsSource{}); err != nil {
return err
}
// tu film news source is now inlined
if err := tx.Delete(&model.NewsSource{Source: 2}).Error; err != nil {
if err := tx.Delete(&NewsSource{Source: 2}).Error; err != nil {
return err
}
return tx.Create(&model.Crontab{
Expand All @@ -44,7 +57,7 @@ func (m TumDBMigrator) migrate20230904100000() *gormigrate.Migration {
if err := SafeEnumMigrate(tx, &model.Crontab{}, "type", "kino"); err != nil {
return err
}
if err := tx.Create(&model.NewsSource{
if err := tx.Create(&NewsSource{
Source: 2,
Title: "TU Film",
URL: null.StringFrom("http://www.tu-film.de/programm/index/upcoming.rss"),
Expand Down
Loading

0 comments on commit c9801de

Please sign in to comment.