db: add genres table and genre id column to album and tracks
This commit is contained in:
1
db/db.go
1
db/db.go
@@ -40,6 +40,7 @@ func New(path string) (*DB, error) {
|
||||
&migrationInitSchema,
|
||||
&migrationCreateInitUser,
|
||||
&migrationMergePlaylist,
|
||||
&migrationAddGenre,
|
||||
})
|
||||
if err = migr.Migrate(); err != nil {
|
||||
return nil, errors.Wrap(err, "migrating to latest version")
|
||||
|
||||
@@ -67,3 +67,15 @@ var migrationMergePlaylist = gormigrate.Migration{
|
||||
Error
|
||||
},
|
||||
}
|
||||
|
||||
var migrationAddGenre = gormigrate.Migration{
|
||||
ID: "202003020000",
|
||||
Migrate: func(tx *gorm.DB) error {
|
||||
return tx.AutoMigrate(
|
||||
Genre{},
|
||||
Album{},
|
||||
Track{},
|
||||
).
|
||||
Error
|
||||
},
|
||||
}
|
||||
|
||||
15
db/model.go
15
db/model.go
@@ -49,6 +49,15 @@ func (a *Artist) IndexName() string {
|
||||
return a.Name
|
||||
}
|
||||
|
||||
type Genre struct {
|
||||
ID int `gorm:"primary_ket"`
|
||||
Name string `gorm:"not null; unique_index"`
|
||||
Albums []*Album `gorm:"foreignkey:TagGenreID"`
|
||||
AlbumCount int `sql:"-"`
|
||||
Tracks []*Track `gorm:"foreignkey:TagGenreID"`
|
||||
TrackCount int `sql:"-"`
|
||||
}
|
||||
|
||||
type Track struct {
|
||||
ID int `gorm:"primary_key"`
|
||||
CreatedAt time.Time
|
||||
@@ -67,6 +76,8 @@ type Track struct {
|
||||
TagTrackArtist string `sql:"default: null"`
|
||||
TagTrackNumber int `sql:"default: null"`
|
||||
TagDiscNumber int `sql:"default: null"`
|
||||
TagGenre *Genre
|
||||
TagGenreID int `sql:"default: null; type:int REFERENCES genres(id) ON DELETE CASCADE"`
|
||||
}
|
||||
|
||||
func (t *Track) Ext() string {
|
||||
@@ -117,7 +128,9 @@ type Album struct {
|
||||
ParentID int `sql:"default: null; type:int REFERENCES albums(id) ON DELETE CASCADE"`
|
||||
Cover string `sql:"default: null"`
|
||||
TagArtist *Artist
|
||||
TagArtistID int `sql:"default: null; type:int REFERENCES artists(id) ON DELETE CASCADE"`
|
||||
TagArtistID int `sql:"default: null; type:int REFERENCES artists(id) ON DELETE CASCADE"`
|
||||
TagGenre *Genre
|
||||
TagGenreID int `sql:"default: null; type:int REFERENCES genres(id) ON DELETE CASCADE"`
|
||||
TagTitle string `sql:"default: null"`
|
||||
TagTitleUDec string `sql:"default: null"`
|
||||
TagBrainzID string `sql:"default: null"`
|
||||
|
||||
Reference in New Issue
Block a user