store artist album appearances
This commit is contained in:
25
db/db.go
25
db/db.go
@@ -175,15 +175,17 @@ func (db *DB) SetSetting(key SettingKey, value string) error {
|
||||
}
|
||||
|
||||
type Artist struct {
|
||||
ID int `gorm:"primary_key"`
|
||||
Name string `gorm:"not null; unique_index"`
|
||||
NameUDec string `sql:"default: null"`
|
||||
Albums []*Album `gorm:"many2many:album_artists"`
|
||||
AlbumCount int `sql:"-"`
|
||||
ArtistStar *ArtistStar
|
||||
ArtistRating *ArtistRating
|
||||
AverageRating float64 `sql:"default: null"`
|
||||
Info *ArtistInfo `gorm:"foreignkey:id"`
|
||||
ID int `gorm:"primary_key"`
|
||||
Name string `gorm:"not null; unique_index"`
|
||||
NameUDec string `sql:"default: null"`
|
||||
Albums []*Album `gorm:"many2many:album_artists"`
|
||||
AlbumCount int `sql:"-"`
|
||||
Appearances []*Album `gorm:"many2many:artist_appearances"`
|
||||
AppearanceCount int `sql:"-"`
|
||||
ArtistStar *ArtistStar
|
||||
ArtistRating *ArtistRating
|
||||
AverageRating float64 `sql:"default: null"`
|
||||
Info *ArtistInfo `gorm:"foreignkey:id"`
|
||||
}
|
||||
|
||||
func (a *Artist) SID() *specid.ID {
|
||||
@@ -395,6 +397,11 @@ type TrackArtist struct {
|
||||
ArtistID int `gorm:"not null; unique_index:idx_track_id_artist_id" sql:"default: null; type:int REFERENCES artists(id) ON DELETE CASCADE"`
|
||||
}
|
||||
|
||||
type ArtistAppearances struct {
|
||||
ArtistID int `gorm:"not null; unique_index:idx_artist_id_album_id" sql:"default: null; type:int REFERENCES artists(id) ON DELETE CASCADE"`
|
||||
AlbumID int `gorm:"not null; unique_index:idx_artist_id_album_id" sql:"default: null; type:int REFERENCES albums(id) ON DELETE CASCADE"`
|
||||
}
|
||||
|
||||
type TrackGenre struct {
|
||||
TrackID int `gorm:"not null; unique_index:idx_track_id_genre_id" sql:"default: null; type:int REFERENCES tracks(id) ON DELETE CASCADE"`
|
||||
GenreID int `gorm:"not null; unique_index:idx_track_id_genre_id" sql:"default: null; type:int REFERENCES genres(id) ON DELETE CASCADE"`
|
||||
|
||||
@@ -68,6 +68,7 @@ func (db *DB) Migrate(ctx MigrationContext) error {
|
||||
construct(ctx, "202309161411", migratePlaylistsPaths),
|
||||
construct(ctx, "202310252205", migrateAlbumTagArtistString),
|
||||
construct(ctx, "202310281803", migrateTrackArtists),
|
||||
construct(ctx, "202311062259", migrateArtistAppearances),
|
||||
}
|
||||
|
||||
return gormigrate.
|
||||
@@ -744,3 +745,37 @@ func migrateTrackArtists(tx *gorm.DB, _ MigrationContext) error {
|
||||
}
|
||||
return tx.AutoMigrate(TrackArtist{}).Error
|
||||
}
|
||||
|
||||
func migrateArtistAppearances(tx *gorm.DB, _ MigrationContext) error {
|
||||
// gorms seems to want to create the table automatically without ON DELETE rules
|
||||
step := tx.DropTableIfExists(ArtistAppearances{})
|
||||
if err := step.Error; err != nil {
|
||||
return fmt.Errorf("step drop prev: %w", err)
|
||||
}
|
||||
|
||||
step = tx.AutoMigrate(ArtistAppearances{})
|
||||
if err := step.Error; err != nil {
|
||||
return fmt.Errorf("step auto migrate: %w", err)
|
||||
}
|
||||
|
||||
step = tx.Exec(`
|
||||
INSERT INTO artist_appearances (artist_id, album_id)
|
||||
SELECT artist_id, album_id
|
||||
FROM album_artists
|
||||
`)
|
||||
if err := step.Error; err != nil {
|
||||
return fmt.Errorf("step transfer album artists: %w", err)
|
||||
}
|
||||
|
||||
step = tx.Exec(`
|
||||
INSERT OR IGNORE INTO artist_appearances (artist_id, album_id)
|
||||
SELECT track_artists.artist_id, tracks.album_id
|
||||
FROM track_artists
|
||||
JOIN tracks ON tracks.id=track_artists.track_id
|
||||
`)
|
||||
if err := step.Error; err != nil {
|
||||
return fmt.Errorf("step transfer album artists: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user