diff --git a/server/backend/cron/movies.go b/server/backend/cron/movies.go index cbac4ad9..4820eaf6 100644 --- a/server/backend/cron/movies.go +++ b/server/backend/cron/movies.go @@ -55,7 +55,7 @@ func (c *CronService) movieCron(cronjob *model.Crontab) error { for _, item := range channel.Items { logFields := log.Fields{"link": item.Link, "title": item.Title, "date": item.PubDate, "location": item.Location, "url": item.Enclosure.Url} var exists bool - if err := c.db.Model(model.Kino{}).Select("count(*) > 0").Where("link = ?", item.Link).Find(&exists).Error; err != nil { + if err := c.db.Model(model.Movie{}).Select("count(*) > 0").Where("link = ?", item.Link).Find(&exists).Error; err != nil { log.WithError(err).WithFields(logFields).Error("Cound lot check if movie already exists") continue } @@ -95,7 +95,7 @@ func (c *CronService) movieCron(cronjob *model.Crontab) error { } // save the result of the previous steps (🎉) - movie := model.Kino{ + movie := model.Movie{ Date: date, Title: item.Title, Year: imdbMovie.Year, diff --git a/server/backend/migration/2023090510000000.go b/server/backend/migration/2023090510000000.go new file mode 100644 index 00000000..92453e62 --- /dev/null +++ b/server/backend/migration/2023090510000000.go @@ -0,0 +1,71 @@ +package migration + +import ( + "github.com/go-gormigrate/gormigrate/v2" + "gorm.io/gorm" +) + +// Movie stores all movies +type Movie struct { + Id int32 `gorm:"primary_key;AUTO_INCREMENT;column:id;type:int;not null;"` + Cover Files `gorm:"column:cover;type:int;not null"` +} + +// Kino stores all movies +type Kino struct { + Cover Files `gorm:"column:cover;type:int;not null"` + Trailer string `gorm:"column:trailer;type:text;"` +} + +// TableName sets the insert table name for this struct type +func (n *Kino) TableName() string { + return "kino" +} + +type Files struct{} + +// TableName sets the insert table name for this struct type +func (f *Files) TableName() string { + return "files" +} + +// migrate2023090510000000 +// removes the unused trailer column +// makes the Cover FK into a not null field +// renames kino -> movie +// fixes the id being named kino +func (m TumDBMigrator) migrate2023090510000000() *gormigrate.Migration { + return &gormigrate.Migration{ + ID: "migrate2023090510000000", + Migrate: func(tx *gorm.DB) error { + // fix the movie table + if err := tx.Migrator().RenameTable(&Kino{}, &Movie{}); err != nil { + return err + } + if err := tx.Migrator().DropColumn(&Movie{}, "trailer"); err != nil { + return err + } + if err := tx.Migrator().RenameColumn(&Movie{}, "kino", "id"); err != nil { + return err + } + if err := tx.Migrator().AlterColumn(&Movie{}, "cover"); err != nil { + return err + } + return nil + }, + + Rollback: func(tx *gorm.DB) error { + // rollback the kino table + if err := tx.Migrator().RenameTable(&Kino{}, &Movie{}); err != nil { + return err + } + if err := tx.Migrator().RenameColumn(&Movie{}, "id", "kino"); err != nil { + return err + } + if err := tx.AutoMigrate(Kino{}); err != nil { + return err + } + return nil + }, + } +} diff --git a/server/backend/migration/migration.go b/server/backend/migration/migration.go index eae29baf..acebca17 100644 --- a/server/backend/migration/migration.go +++ b/server/backend/migration/migration.go @@ -42,6 +42,7 @@ func (m TumDBMigrator) Migrate() error { m.migrate20221119131300(), m.migrate20221210000000(), m.migrate2023090410000000(), + m.migrate2023090510000000(), }) err := mig.Migrate() return err diff --git a/server/backend/movie.go b/server/backend/movie.go index 660ed63b..e66f809c 100644 --- a/server/backend/movie.go +++ b/server/backend/movie.go @@ -11,8 +11,8 @@ import ( ) func (s *CampusServer) GetMovies(_ context.Context, req *pb.GetMoviesRequest) (*pb.GetMoviesReply, error) { - var movies []model.Kino - if err := s.db.Joins("Files").Find(&movies, "kino > ?", req.LastId).Error; err != nil { + var movies []model.Movie + if err := s.db.Joins("Files").Find(&movies, "id > ?", req.LastId).Error; err != nil { log.WithError(err).Error("Error while fetching movies from database") return nil, status.Error(codes.Internal, "Error while fetching movies from database") } diff --git a/server/backend/movie_test.go b/server/backend/movie_test.go index 99ab8e9a..0961f378 100644 --- a/server/backend/movie_test.go +++ b/server/backend/movie_test.go @@ -78,11 +78,11 @@ var ( func (s *MovieSuite) Test_GetMoviesAll() { server := CampusServer{db: s.DB} - s.mock.ExpectQuery("SELECT `kino`.`kino`,`kino`.`date`,`kino`.`created`,`kino`.`title`,`kino`.`year`,`kino`.`runtime`,`kino`.`genre`,`kino`.`director`,`kino`.`actors`,`kino`.`rating`,`kino`.`description`,`kino`.`trailer`,`kino`.`cover`,`kino`.`link`,`Files`.`file` AS `Files__file`,`Files`.`name` AS `Files__name`,`Files`.`path` AS `Files__path`,`Files`.`downloads` AS `Files__downloads`,`Files`.`url` AS `Files__url`,`Files`.`downloaded` AS `Files__downloaded` FROM `kino` LEFT JOIN `files` `Files` ON `kino`.`cover` = `Files`.`file` WHERE kino > ?"). + s.mock.ExpectQuery("SELECT `movies`.`id`,`movies`.`date`,`movies`.`created`,`movies`.`title`,`movies`.`year`,`movies`.`runtime`,`movies`.`genre`,`movies`.`director`,`movies`.`actors`,`movies`.`rating`,`movies`.`description`,`movies`.`cover`,`movies`.`link`,`Files`.`file` AS `Files__file`,`Files`.`name` AS `Files__name`,`Files`.`path` AS `Files__path`,`Files`.`downloads` AS `Files__downloads`,`Files`.`url` AS `Files__url`,`Files`.`downloaded` AS `Files__downloaded` FROM `movies` LEFT JOIN `files` `Files` ON `movies`.`cover` = `Files`.`file` WHERE id > ?"). WithArgs(-1). - WillReturnRows(sqlmock.NewRows([]string{"kino", "date", "created", "title", "year", "runtime", "genre", "director", "actors", "rating", "description", "trailer", "cover", "link", "Files__file", "Files__name", "Files__path", "Files__downloads", "Files__url", "Files__downloaded"}). - AddRow(movie2.Id, movie2.Date.AsTime(), movie2.Created.AsTime(), movie2.Title, movie2.Year, movie2.Runtime, movie2.Genre, movie2.Director, movie2.Actors, movie2.ImdbRating, movie2.Description, nil, movie2.CoverID, movie2.Link, movie2.CoverID, movie2.CoverName, movie2.CoverPath, 1, "", 1). - AddRow(movie1.Id, movie1.Date.AsTime(), movie1.Created.AsTime(), movie1.Title, movie1.Year, movie1.Runtime, movie1.Genre, movie1.Director, movie1.Actors, movie1.ImdbRating, movie1.Description, nil, movie1.CoverID, movie1.Link, movie1.CoverID, movie1.CoverName, movie1.CoverPath, 1, "", 1)) + WillReturnRows(sqlmock.NewRows([]string{"id", "date", "created", "title", "year", "runtime", "genre", "director", "actors", "rating", "description", "cover", "link", "Files__file", "Files__name", "Files__path", "Files__downloads", "Files__url", "Files__downloaded"}). + AddRow(movie2.Id, movie2.Date.AsTime(), movie2.Created.AsTime(), movie2.Title, movie2.Year, movie2.Runtime, movie2.Genre, movie2.Director, movie2.Actors, movie2.ImdbRating, movie2.Description, movie2.CoverID, movie2.Link, movie2.CoverID, movie2.CoverName, movie2.CoverPath, 1, "", 1). + AddRow(movie1.Id, movie1.Date.AsTime(), movie1.Created.AsTime(), movie1.Title, movie1.Year, movie1.Runtime, movie1.Genre, movie1.Director, movie1.Actors, movie1.ImdbRating, movie1.Description, movie1.CoverID, movie1.Link, movie1.CoverID, movie1.CoverName, movie1.CoverPath, 1, "", 1)) response, err := server.GetMovies(context.Background(), &pb.GetMoviesRequest{LastId: -1}) require.NoError(s.T(), err) require.Equal(s.T(), &pb.GetMoviesReply{Movies: []*pb.MovieMsgElement{&movie2, &movie1}}, response) @@ -90,10 +90,10 @@ func (s *MovieSuite) Test_GetMoviesAll() { func (s *MovieSuite) Test_GetMoviesOne() { server := CampusServer{db: s.DB} - s.mock.ExpectQuery("SELECT `kino`.`kino`,`kino`.`date`,`kino`.`created`,`kino`.`title`,`kino`.`year`,`kino`.`runtime`,`kino`.`genre`,`kino`.`director`,`kino`.`actors`,`kino`.`rating`,`kino`.`description`,`kino`.`trailer`,`kino`.`cover`,`kino`.`link`,`Files`.`file` AS `Files__file`,`Files`.`name` AS `Files__name`,`Files`.`path` AS `Files__path`,`Files`.`downloads` AS `Files__downloads`,`Files`.`url` AS `Files__url`,`Files`.`downloaded` AS `Files__downloaded` FROM `kino` LEFT JOIN `files` `Files` ON `kino`.`cover` = `Files`.`file` WHERE kino > ?"). + s.mock.ExpectQuery("SELECT `movies`.`id`,`movies`.`date`,`movies`.`created`,`movies`.`title`,`movies`.`year`,`movies`.`runtime`,`movies`.`genre`,`movies`.`director`,`movies`.`actors`,`movies`.`rating`,`movies`.`description`,`movies`.`cover`,`movies`.`link`,`Files`.`file` AS `Files__file`,`Files`.`name` AS `Files__name`,`Files`.`path` AS `Files__path`,`Files`.`downloads` AS `Files__downloads`,`Files`.`url` AS `Files__url`,`Files`.`downloaded` AS `Files__downloaded` FROM `movies` LEFT JOIN `files` `Files` ON `movies`.`cover` = `Files`.`file` WHERE id > ?"). WithArgs(1). - WillReturnRows(sqlmock.NewRows([]string{"kino", "date", "created", "title", "year", "runtime", "genre", "director", "actors", "rating", "description", "trailer", "cover", "link", "Files__file", "Files__name", "Files__path", "Files__downloads", "Files__url", "Files__downloaded"}). - AddRow(movie1.Id, movie1.Date.AsTime(), movie1.Created.AsTime(), movie1.Title, movie1.Year, movie1.Runtime, movie1.Genre, movie1.Director, movie1.Actors, movie1.ImdbRating, movie1.Description, nil, movie1.CoverID, movie1.Link, movie1.CoverID, movie1.CoverName, movie1.CoverPath, 1, "", 1)) + WillReturnRows(sqlmock.NewRows([]string{"id", "date", "created", "title", "year", "runtime", "genre", "director", "actors", "rating", "description", "cover", "link", "Files__file", "Files__name", "Files__path", "Files__downloads", "Files__url", "Files__downloaded"}). + AddRow(movie1.Id, movie1.Date.AsTime(), movie1.Created.AsTime(), movie1.Title, movie1.Year, movie1.Runtime, movie1.Genre, movie1.Director, movie1.Actors, movie1.ImdbRating, movie1.Description, movie1.CoverID, movie1.Link, movie1.CoverID, movie1.CoverName, movie1.CoverPath, 1, "", 1)) response, err := server.GetMovies(context.Background(), &pb.GetMoviesRequest{LastId: 1}) require.NoError(s.T(), err) require.Equal(s.T(), &pb.GetMoviesReply{Movies: []*pb.MovieMsgElement{&movie1}}, response) @@ -101,9 +101,9 @@ func (s *MovieSuite) Test_GetMoviesOne() { func (s *MovieSuite) Test_GetMoviesNone() { server := CampusServer{db: s.DB} - s.mock.ExpectQuery("SELECT `kino`.`kino`,`kino`.`date`,`kino`.`created`,`kino`.`title`,`kino`.`year`,`kino`.`runtime`,`kino`.`genre`,`kino`.`director`,`kino`.`actors`,`kino`.`rating`,`kino`.`description`,`kino`.`trailer`,`kino`.`cover`,`kino`.`link`,`Files`.`file` AS `Files__file`,`Files`.`name` AS `Files__name`,`Files`.`path` AS `Files__path`,`Files`.`downloads` AS `Files__downloads`,`Files`.`url` AS `Files__url`,`Files`.`downloaded` AS `Files__downloaded` FROM `kino` LEFT JOIN `files` `Files` ON `kino`.`cover` = `Files`.`file` WHERE kino > ?"). + s.mock.ExpectQuery("SELECT `movies`.`id`,`movies`.`date`,`movies`.`created`,`movies`.`title`,`movies`.`year`,`movies`.`runtime`,`movies`.`genre`,`movies`.`director`,`movies`.`actors`,`movies`.`rating`,`movies`.`description`,`movies`.`cover`,`movies`.`link`,`Files`.`file` AS `Files__file`,`Files`.`name` AS `Files__name`,`Files`.`path` AS `Files__path`,`Files`.`downloads` AS `Files__downloads`,`Files`.`url` AS `Files__url`,`Files`.`downloaded` AS `Files__downloaded` FROM `movies` LEFT JOIN `files` `Files` ON `movies`.`cover` = `Files`.`file` WHERE id > ?"). WithArgs(42). - WillReturnRows(sqlmock.NewRows([]string{"kino", "date", "created", "title", "year", "runtime", "genre", "director", "actors", "rating", "description", "trailer", "cover", "link", "Files__file", "Files__name", "Files__path", "Files__downloads", "Files__url", "Files__downloaded"})) + WillReturnRows(sqlmock.NewRows([]string{"id", "date", "created", "title", "year", "runtime", "genre", "director", "actors", "rating", "description", "cover", "link", "Files__file", "Files__name", "Files__path", "Files__downloads", "Files__url", "Files__downloaded"})) response, err := server.GetMovies(context.Background(), &pb.GetMoviesRequest{LastId: 42}) require.NoError(s.T(), err) require.Equal(s.T(), &pb.GetMoviesReply{Movies: []*pb.MovieMsgElement(nil)}, response) diff --git a/server/model/kino.go b/server/model/kino.go deleted file mode 100644 index 6f7a7026..00000000 --- a/server/model/kino.go +++ /dev/null @@ -1,30 +0,0 @@ -package model - -import ( - "database/sql" - "time" -) - -// Kino stores all movies -type Kino struct { - Id int32 `gorm:"primary_key;AUTO_INCREMENT;column:kino;type:int;not null;"` - Date time.Time `gorm:"column:date;type:datetime;not null;"` - Created time.Time `gorm:"column:created;type:timestamp;not null;default:CURRENT_TIMESTAMP"` - Title string `gorm:"column:title;type:text;not null;"` - Year string `gorm:"column:year;type:varchar(4);not null;"` - Runtime string `gorm:"column:runtime;type:varchar(40);not null;"` - Genre string `gorm:"column:genre;type:varchar(100);not null;"` - Director string `gorm:"column:director;type:text;not null;"` - Actors string `gorm:"column:actors;type:text;not null;"` - ImdbRating string `gorm:"column:rating;type:varchar(4);not null;"` - Description string `gorm:"column:description;type:text;not null;"` - Trailer sql.NullString `gorm:"column:trailer"` - FilesID int32 `gorm:"column:cover"` - Files Files `gorm:"foreignKey:FilesID;references:file"` - Link string `gorm:"column:link;type:varchar(190);not null;unique;"` -} - -// TableName sets the insert table name for this struct type -func (n *Kino) TableName() string { - return "kino" -} diff --git a/server/model/movie.go b/server/model/movie.go new file mode 100644 index 00000000..69615cd2 --- /dev/null +++ b/server/model/movie.go @@ -0,0 +1,23 @@ +package model + +import ( + "time" +) + +// Movie stores all movies +type Movie struct { + Id int32 `gorm:"primary_key;AUTO_INCREMENT;column:id;type:int;not null;"` + Date time.Time `gorm:"column:date;type:datetime;not null;"` + Created time.Time `gorm:"column:created;type:timestamp;not null;default:CURRENT_TIMESTAMP"` + Title string `gorm:"column:title;type:text;not null;"` + Year string `gorm:"column:year;type:varchar(4);not null;"` + Runtime string `gorm:"column:runtime;type:varchar(40);not null;"` + Genre string `gorm:"column:genre;type:varchar(100);not null;"` + Director string `gorm:"column:director;type:text;not null;"` + Actors string `gorm:"column:actors;type:text;not null;"` + ImdbRating string `gorm:"column:rating;type:varchar(4);not null;"` + Description string `gorm:"column:description;type:text;not null;"` + FilesID int32 `gorm:"column:cover;not null"` + Files Files `gorm:"foreignKey:FilesID;references:file;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"` + Link string `gorm:"column:link;type:varchar(190);not null;unique;"` +}