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 12, 2023
1 parent 1d38f81 commit 35210ca
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 0 deletions.
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 @@ -41,6 +41,7 @@ func (m TumDBMigrator) Migrate() error {
m.migrate20220713000000(),
m.migrate20221119131300(),
m.migrate20221210000000(),
m.migrate2023090510000000(),
})
err := mig.Migrate()
return err
Expand Down
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 35210ca

Please sign in to comment.