store artist album appearances

This commit is contained in:
sentriz
2023-11-06 23:40:00 +00:00
parent 03cc1aedad
commit 98eb1066d8
4 changed files with 80 additions and 17 deletions

View File

@@ -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"`

View File

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