diff --git a/store/migrations.go b/store/migrations.go index 43d8c646e62d7a26f40b3e19fed2d7ddfafb3e96..4cce5e7fcdb13a2dee91a46313bb642f0b04aed6 100644 --- a/store/migrations.go +++ b/store/migrations.go @@ -33,33 +33,6 @@ var ( Migrate: func(tx *gorm.DB) error { return nil }, Rollback: func(tx *gorm.DB) error { return nil }, }, - { - ID: "201905160033", - Migrate: func(tx *gorm.DB) error { - type PlaylistEntry struct { - ID uint64 `json:"-" gorm:"primaryKey"` - PlaylistID uint64 `json:"-" gorm:"not null;index;uniqueIndex:unique_playlist_line_numbers"` - LineNum uint `json:"-" gorm:"not null;uniqueIndex:unique_playlist_line_numbers"` - URI string `json:"uri" gorm:"size:1024"` - File *File `json:"file,omitempty" gorm:"associationAutoUpdate:false;associationAutoCreate:false"` - FileID *uint64 `json:"-" gorm:"index"` - } - - type Playlist struct { - ID uint64 `json:"id" gorm:"primaryKey"` - CreatedAt time.Time `json:"created"` - UpdatedAt time.Time `json:"updated"` - Description string `json:"description"` - ShowName string `json:"show" gorm:"not null;index"` - Show Show `json:"-" gorm:"associationForeignKey:Name"` - Entries []PlaylistEntry `json:"entries,omitempty"` - } - return tx.AutoMigrate(&Playlist{}) - }, - Rollback: func(tx *gorm.DB) error { - return tx.Migrator().DropColumn(&Playlist{}, "description") - }, - }, { ID: "201905291602", Migrate: func(tx *gorm.DB) error { @@ -115,36 +88,6 @@ var ( return tx.Migrator().DropConstraint(&ImportLog{}, "file_id") }, }, - { - ID: "201908110945", - Migrate: func(tx *gorm.DB) error { - - type PlaylistEntry struct { - ID uint64 `json:"-" gorm:"primaryKey"` - PlaylistID uint64 `json:"-" gorm:"not null;index;uniqueIndex:unique_playlist_line_numbers"` - LineNum uint `json:"-" gorm:"not null;uniqueIndex:unique_playlist_line_numbers"` - URI string `json:"uri" gorm:"size:1024"` - File *File `json:"file,omitempty" gorm:"associationAutoUpdate:false;associationAutoCreate:false"` - FileID *uint64 `json:"-" gorm:"index"` - } - - type Playlist struct { - ID uint64 `json:"id" gorm:"primaryKey"` - CreatedAt time.Time `json:"created"` - UpdatedAt time.Time `json:"updated"` - Description string `json:"description"` - PlayoutMode string `json:"playoutMode"` - ShowName string `json:"show" gorm:"not null;index"` - Show Show `json:"-" gorm:"associationForeignKey:Name"` - Entries []PlaylistEntry `json:"entries,omitempty"` - } - - return tx.AutoMigrate(&Playlist{}) - }, - Rollback: func(tx *gorm.DB) error { - return tx.Migrator().DropColumn(&Playlist{}, "playout_mode") - }, - }, { ID: "201908150104", Migrate: func(tx *gorm.DB) error { @@ -188,30 +131,6 @@ var ( return tx.Migrator().DropColumn(&File{}, "metadata__isrc") }, }, - { - ID: "202006130203", - Migrate: func(tx *gorm.DB) error { - - type PlaylistEntry struct { - ID uint64 `json:"-" gorm:"primaryKey"` - PlaylistID uint64 `json:"-" gorm:"not null;index;uniqueIndex:unique_playlist_line_numbers"` - LineNum uint `json:"-" gorm:"not null;uniqueIndex:unique_playlist_line_numbers"` - URI string `json:"uri" gorm:"size:1024"` - Duration *time.Duration `json:"duration,omitempty"` - File *File `json:"file,omitempty" gorm:"associationAutoUpdate:false;associationAutoCreate:false"` - FileID *uint64 `json:"-" gorm:"index"` - } - - // actually all playlists would need to be verified if they still fit the new constraint - // that only allows a single non-file entry with duration == NULL per playlist. - // However, we are still pre-first-release and all migrations will likely be squashed before that - // release anyway... - return tx.AutoMigrate(&PlaylistEntry{}) - }, - Rollback: func(tx *gorm.DB) error { - return tx.Migrator().DropColumn(&PlaylistEntry{}, "duration") - }, - }, { ID: "202309141500", Migrate: func(tx *gorm.DB) error { @@ -227,21 +146,7 @@ var ( Duration float64 `json:"duration"` } - if err := tx.Migrator().AlterColumn(&File{}, "duration"); err != nil { - return err - } - - type PlaylistEntry struct { - ID uint64 `json:"-" gorm:"primaryKey"` - PlaylistID uint64 `json:"-" gorm:"not null;index;uniqueIndex:unique_playlist_line_numbers"` - LineNum uint `json:"-" gorm:"not null;uniqueIndex:unique_playlist_line_numbers"` - URI string `json:"uri" gorm:"size:1024"` - Duration *float64 `json:"duration,omitempty"` - File *File `json:"file,omitempty" gorm:"associationAutoUpdate:false;associationAutoCreate:false"` - FileID *uint64 `json:"-" gorm:"index"` - } - - return tx.Migrator().AlterColumn(&PlaylistEntry{}, "duration") + return tx.Migrator().AlterColumn(&File{}, "duration") }, Rollback: func(tx *gorm.DB) error { type File struct { @@ -256,11 +161,7 @@ var ( Duration time.Duration `json:"duration"` } - if err := tx.Migrator().AlterColumn(&File{}, "duration"); err != nil { - return err - } - - return tx.Migrator().AlterColumn(&Playlist{}, "duration") + return tx.Migrator().AlterColumn(&File{}, "duration") }, }, { @@ -282,41 +183,26 @@ var ( Size uint64 `json:"size"` Duration float64 `json:"duration"` } - type Playlist struct { - ID uint64 `json:"id" gorm:"primaryKey"` - CreatedAt time.Time `json:"created"` - UpdatedAt time.Time `json:"updated"` - Description string `json:"description"` - PlayoutMode string `json:"playoutMode" gorm:"not null;default:'linear'"` - ShowID uint64 `json:"showId" gorm:"not null;index"` - Show Show `json:"-" gorm:"associationForeignKey:ID"` - Entries []PlaylistEntry `json:"entries"` - } if err := tx.Migrator().DropConstraint(&File{}, "show_name"); err != nil { return err } - if err := tx.Migrator().DropConstraint(&Playlist{}, "show_name"); err != nil { - return err - } - return tx.AutoMigrate(&Show{}, &File{}, &Playlist{}) + + return tx.AutoMigrate(&Show{}, &File{}) }, Rollback: func(tx *gorm.DB) error { if err := tx.Migrator().DropColumn(&Show{}, "id"); err != nil { return err } - if err := tx.Migrator().DropColumn(&File{}, "show_id"); err != nil { - return err - } - return tx.Migrator().DropColumn(&Playlist{}, "show_id") + return tx.Migrator().DropColumn(&File{}, "show_id") }, }, } ) func initialMigration(tx *gorm.DB) (err error) { - if err = tx.AutoMigrate(&Show{}, &File{}, &ImportLog{}, &Playlist{}, &PlaylistEntry{}); err != nil { + if err = tx.AutoMigrate(&Show{}, &File{}, &ImportLog{}); err != nil { return err } @@ -331,30 +217,7 @@ func initialMigration(tx *gorm.DB) (err error) { if err = tx.Exec("ALTER TABLE import_logs DROP CONSTRAINT fk_import_logs_file").Error; err != nil { return err } - if err = tx.Exec("ALTER TABLE import_logs ADD CONSTRAINT fk_import_logs_file FOREIGN KEY (file_id) REFERENCES files(id) ON UPDATE CASCADE ON DELETE CASCADE ").Error; err != nil { - return err - } - if err = tx.Exec("ALTER TABLE playlists DROP CONSTRAINT fk_playlists_show").Error; err != nil { - return err - } - if err = tx.Exec("ALTER TABLE playlists ADD CONSTRAINT fk_playlists_show FOREIGN KEY (show_id) REFERENCES shows(id) ON UPDATE CASCADE ON DELETE CASCADE ").Error; err != nil { - return err - } - if err = tx.Exec("ALTER TABLE playlist_entries DROP CONSTRAINT fk_playlist_entries_file").Error; err != nil { - return err - } - if err = tx.Exec("ALTER TABLE playlist_entries ADD CONSTRAINT fk_playlist_entries_file FOREIGN KEY (file_id) REFERENCES files(id) ON UPDATE CASCADE ON DELETE RESTRICT ").Error; err != nil { - return err - } - if err = tx.Exec("ALTER TABLE playlist_entries DROP CONSTRAINT fk_playlists_entries").Error; err != nil { - return err - } - if err = tx.Exec("ALTER TABLE playlist_entries ADD CONSTRAINT fk_playlists_entries FOREIGN KEY (playlist_id) REFERENCES playlists(id) ON UPDATE CASCADE ON DELETE CASCADE ").Error; err != nil { - return err - } - - return nil - + return tx.Exec("ALTER TABLE import_logs ADD CONSTRAINT fk_import_logs_file FOREIGN KEY (file_id) REFERENCES files(id) ON UPDATE CASCADE ON DELETE CASCADE ").Error } func (st *Store) initDBModel(cfg DBConfig) (err error) {