Skip to content

Commit

Permalink
renaming kino -> movie of the db
Browse files Browse the repository at this point in the history
  • Loading branch information
CommanderStorm committed Sep 5, 2023
1 parent 78583f1 commit 88506dd
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 43 deletions.
4 changes: 2 additions & 2 deletions server/backend/cron/movies.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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,
Expand Down
71 changes: 71 additions & 0 deletions server/backend/migration/2023090510000000.go
Original file line number Diff line number Diff line change
@@ -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
},
}
}
1 change: 1 addition & 0 deletions server/backend/migration/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func (m TumDBMigrator) Migrate() error {
m.migrate20221119131300(),
m.migrate20221210000000(),
m.migrate2023090410000000(),
m.migrate2023090510000000(),
})
err := mig.Migrate()
return err
Expand Down
4 changes: 2 additions & 2 deletions server/backend/movie.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
18 changes: 9 additions & 9 deletions server/backend/movie_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,32 +78,32 @@ 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)
}

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)
}

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)
Expand Down
30 changes: 0 additions & 30 deletions server/model/kino.go

This file was deleted.

23 changes: 23 additions & 0 deletions server/model/movie.go
Original file line number Diff line number Diff line change
@@ -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;"`
}

0 comments on commit 88506dd

Please sign in to comment.