Fixed merge conflicts
This commit is contained in:
No files matched your search
@@ -54,22 +54,6 @@ func NewMock() (*DB, error) {
|
||||
return New(":memory:", mockOptions())
|
||||
}
|
||||
|
||||
func (db *DB) GetSetting(key string) (string, error) {
|
||||
var setting Setting
|
||||
if err := db.Where("key=?", key).First(&setting).Error; err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return "", err
|
||||
}
|
||||
return setting.Value, nil
|
||||
}
|
||||
|
||||
func (db *DB) SetSetting(key, value string) error {
|
||||
return db.
|
||||
Where("key=?", key).
|
||||
Assign(Setting{Key: key, Value: value}).
|
||||
FirstOrCreate(&Setting{}).
|
||||
Error
|
||||
}
|
||||
|
||||
func (db *DB) InsertBulkLeftMany(table string, head []string, left int, col []int) error {
|
||||
if len(col) == 0 {
|
||||
return nil
|
||||
@@ -137,3 +121,27 @@ func (db *DB) TransactionChunked(data []int64, cb ChunkFunc) error {
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
type SettingKey string
|
||||
|
||||
const (
|
||||
LastFMAPIKey SettingKey = "lastfm_api_key" //nolint:gosec
|
||||
LastFMSecret SettingKey = "lastfm_secret"
|
||||
LastScanTime SettingKey = "last_scan_time"
|
||||
)
|
||||
|
||||
func (db *DB) GetSetting(key SettingKey) (string, error) {
|
||||
var setting Setting
|
||||
if err := db.Where("key=?", key).First(&setting).Error; err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return "", err
|
||||
}
|
||||
return setting.Value, nil
|
||||
}
|
||||
|
||||
func (db *DB) SetSetting(key SettingKey, value string) error {
|
||||
return db.
|
||||
Where("key=?", key).
|
||||
Assign(Setting{Key: key, Value: value}).
|
||||
FirstOrCreate(&Setting{}).
|
||||
Error
|
||||
}
|
||||
+1
-1
@@ -21,7 +21,7 @@ func randKey() string {
|
||||
}
|
||||
|
||||
func TestGetSetting(t *testing.T) {
|
||||
key := randKey()
|
||||
key := SettingKey(randKey())
|
||||
value := "howdy"
|
||||
|
||||
require := require.New(t)
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"fmt"
|
||||
"log"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/jinzhu/gorm"
|
||||
@@ -55,6 +56,9 @@ func (db *DB) Migrate(ctx MigrationContext) error {
|
||||
construct(ctx, "202211111057", migratePlaylistsQueuesToFullID),
|
||||
construct(ctx, "202304221528", migratePlaylistsToM3U),
|
||||
construct(ctx, "202305301718", migratePlayCountToLength),
|
||||
construct(ctx, "202307281628", migrateAlbumArtistsMany2Many),
|
||||
construct(ctx, "202309070009", migrateDeleteArtistCoverField),
|
||||
construct(ctx, "202309131743", migrateArtistInfo),
|
||||
}
|
||||
|
||||
return gormigrate.
|
||||
@@ -538,3 +542,74 @@ func migratePlayCountToLength(tx *gorm.DB, _ MigrationContext) error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func migrateAlbumArtistsMany2Many(tx *gorm.DB, _ MigrationContext) error {
|
||||
// gorms seems to want to create the table automatically without ON DELETE rules
|
||||
step := tx.DropTableIfExists(AlbumArtist{})
|
||||
if err := step.Error; err != nil {
|
||||
return fmt.Errorf("step drop prev: %w", err)
|
||||
}
|
||||
|
||||
step = tx.AutoMigrate(
|
||||
AlbumArtist{},
|
||||
Album{},
|
||||
Artist{},
|
||||
)
|
||||
if err := step.Error; err != nil {
|
||||
return fmt.Errorf("step auto migrate: %w", err)
|
||||
}
|
||||
|
||||
if tx.Dialect().HasColumn("albums", "tag_artist_id") {
|
||||
tx = tx.LogMode(false)
|
||||
step = tx.Exec(`
|
||||
INSERT INTO album_artists (album_id, artist_id)
|
||||
SELECT id album_id, tag_artist_id artist_id
|
||||
FROM albums
|
||||
WHERE tag_artist_id IS NOT NULL;
|
||||
`)
|
||||
if err := step.Error; err != nil && !strings.Contains(err.Error(), "no such column") {
|
||||
return fmt.Errorf("step insert from albums: %w", err)
|
||||
}
|
||||
|
||||
step = tx.Exec(`DROP INDEX idx_albums_tag_artist_id`)
|
||||
if err := step.Error; err != nil {
|
||||
return fmt.Errorf("step drop index: %w", err)
|
||||
}
|
||||
|
||||
step = tx.Exec(`ALTER TABLE albums DROP COLUMN tag_artist_id;`)
|
||||
if err := step.Error; err != nil {
|
||||
return fmt.Errorf("step drop albums tag artist id: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
if tx.Dialect().HasColumn("tracks", "artist_id") {
|
||||
step = tx.Exec(`ALTER TABLE tracks DROP COLUMN artist_id;`)
|
||||
if err := step.Error; err != nil {
|
||||
return fmt.Errorf("step drop track tag artist: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func migrateDeleteArtistCoverField(tx *gorm.DB, _ MigrationContext) error {
|
||||
if !tx.Dialect().HasColumn("artists", "cover") {
|
||||
return nil
|
||||
}
|
||||
|
||||
step := tx.Exec(`
|
||||
ALTER TABLE artists DROP COLUMN cover;
|
||||
`)
|
||||
if err := step.Error; err != nil {
|
||||
return fmt.Errorf("step drop: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func migrateArtistInfo(tx *gorm.DB, _ MigrationContext) error {
|
||||
return tx.AutoMigrate(
|
||||
ArtistInfo{},
|
||||
).
|
||||
Error
|
||||
}
|
||||
+57
-25
@@ -7,8 +7,10 @@ package db
|
||||
// https://www.db-fiddle.com/f/wJ7z8L7mu6ZKaYmWk1xr1p/5
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -31,7 +33,7 @@ func splitIDs(in, sep string) []specid.ID {
|
||||
return ret
|
||||
}
|
||||
|
||||
func joinIds(in []specid.ID, sep string) string {
|
||||
func join[T fmt.Stringer](in []T, sep string) string {
|
||||
if in == nil {
|
||||
return ""
|
||||
}
|
||||
@@ -46,12 +48,12 @@ type Artist struct {
|
||||
ID int `gorm:"primary_key"`
|
||||
Name string `gorm:"not null; unique_index"`
|
||||
NameUDec string `sql:"default: null"`
|
||||
Albums []*Album `gorm:"foreignkey:TagArtistID"`
|
||||
Albums []*Album `gorm:"many2many:album_artists"`
|
||||
AlbumCount int `sql:"-"`
|
||||
Cover string `sql:"default: null"`
|
||||
ArtistStar *ArtistStar
|
||||
ArtistRating *ArtistRating
|
||||
AverageRating float64 `sql:"default: null"`
|
||||
AverageRating float64 `sql:"default: null"`
|
||||
Info *ArtistInfo `gorm:"foreignkey:id"`
|
||||
}
|
||||
|
||||
func (a *Artist) SID() *specid.ID {
|
||||
@@ -89,9 +91,7 @@ type Track struct {
|
||||
Filename string `gorm:"not null; unique_index:idx_folder_filename" sql:"default: null"`
|
||||
FilenameUDec string `sql:"default: null"`
|
||||
Album *Album
|
||||
AlbumID int `gorm:"not null; unique_index:idx_folder_filename" sql:"default: null; type:int REFERENCES albums(id) ON DELETE CASCADE"`
|
||||
Artist *Artist
|
||||
ArtistID int `gorm:"not null" sql:"default: null; type:int REFERENCES artists(id) ON DELETE CASCADE"`
|
||||
AlbumID int `gorm:"not null; unique_index:idx_folder_filename" sql:"default: null; type:int REFERENCES albums(id) ON DELETE CASCADE"`
|
||||
Genres []*Genre `gorm:"many2many:track_genres"`
|
||||
Size int `sql:"default: null"`
|
||||
Length int `sql:"default: null"`
|
||||
@@ -118,10 +118,6 @@ func (t *Track) AlbumSID() *specid.ID {
|
||||
return &specid.ID{Type: specid.Album, Value: t.AlbumID}
|
||||
}
|
||||
|
||||
func (t *Track) ArtistSID() *specid.ID {
|
||||
return &specid.ID{Type: specid.Artist, Value: t.ArtistID}
|
||||
}
|
||||
|
||||
func (t *Track) Ext() string {
|
||||
return filepath.Ext(t.Filename)
|
||||
}
|
||||
@@ -178,8 +174,8 @@ type User struct {
|
||||
}
|
||||
|
||||
type Setting struct {
|
||||
Key string `gorm:"not null; primary_key; auto_increment:false" sql:"default: null"`
|
||||
Value string `sql:"default: null"`
|
||||
Key SettingKey `gorm:"not null; primary_key; auto_increment:false" sql:"default: null"`
|
||||
Value string `sql:"default: null"`
|
||||
}
|
||||
|
||||
type Play struct {
|
||||
@@ -190,7 +186,7 @@ type Play struct {
|
||||
AlbumID int `gorm:"not null; index" sql:"default: null; type:int REFERENCES albums(id) ON DELETE CASCADE"`
|
||||
Time time.Time `sql:"default: null"`
|
||||
Count int
|
||||
Length int
|
||||
Length int
|
||||
}
|
||||
|
||||
type Album struct {
|
||||
@@ -202,16 +198,15 @@ type Album struct {
|
||||
RightPath string `gorm:"not null; unique_index:idx_album_abs_path" sql:"default: null"`
|
||||
RightPathUDec string `sql:"default: null"`
|
||||
Parent *Album
|
||||
ParentID int `sql:"default: null; type:int REFERENCES albums(id) ON DELETE CASCADE"`
|
||||
RootDir string `gorm:"unique_index:idx_album_abs_path" sql:"default: null"`
|
||||
Genres []*Genre `gorm:"many2many:album_genres"`
|
||||
Cover string `sql:"default: null"`
|
||||
TagArtist *Artist
|
||||
TagArtistID int `gorm:"index" sql:"default: null; type:int REFERENCES artists(id) ON DELETE CASCADE"`
|
||||
TagTitle string `sql:"default: null"`
|
||||
TagTitleUDec string `sql:"default: null"`
|
||||
TagBrainzID string `sql:"default: null"`
|
||||
TagYear int `sql:"default: null"`
|
||||
ParentID int `sql:"default: null; type:int REFERENCES albums(id) ON DELETE CASCADE"`
|
||||
RootDir string `gorm:"unique_index:idx_album_abs_path" sql:"default: null"`
|
||||
Genres []*Genre `gorm:"many2many:album_genres"`
|
||||
Cover string `sql:"default: null"`
|
||||
Artists []*Artist `gorm:"many2many:album_artists"`
|
||||
TagTitle string `sql:"default: null"`
|
||||
TagTitleUDec string `sql:"default: null"`
|
||||
TagBrainzID string `sql:"default: null"`
|
||||
TagYear int `sql:"default: null"`
|
||||
Tracks []*Track
|
||||
ChildCount int `sql:"-"`
|
||||
Duration int `sql:"-"`
|
||||
@@ -243,6 +238,18 @@ func (a *Album) GenreStrings() []string {
|
||||
return strs
|
||||
}
|
||||
|
||||
func (a *Album) ArtistsStrings() []string {
|
||||
var artists = append([]*Artist(nil), a.Artists...)
|
||||
sort.Slice(artists, func(i, j int) bool {
|
||||
return artists[i].ID < artists[j].ID
|
||||
})
|
||||
strs := make([]string, 0, len(artists))
|
||||
for _, artist := range artists {
|
||||
strs = append(strs, artist.Name)
|
||||
}
|
||||
return strs
|
||||
}
|
||||
|
||||
type PlayQueue struct {
|
||||
ID int `gorm:"primary_key"`
|
||||
CreatedAt time.Time
|
||||
@@ -265,7 +272,7 @@ func (p *PlayQueue) GetItems() []specid.ID {
|
||||
}
|
||||
|
||||
func (p *PlayQueue) SetItems(items []specid.ID) {
|
||||
p.Items = joinIds(items, ",")
|
||||
p.Items = join(items, ",")
|
||||
}
|
||||
|
||||
type TranscodePreference struct {
|
||||
@@ -275,6 +282,13 @@ type TranscodePreference struct {
|
||||
Profile string `gorm:"not null" sql:"default: null"`
|
||||
}
|
||||
|
||||
type AlbumArtist struct {
|
||||
Album *Album
|
||||
AlbumID int `gorm:"not null; unique_index:idx_album_id_artist_id" sql:"default: null; type:int REFERENCES albums(id) ON DELETE CASCADE"`
|
||||
Artist *Artist
|
||||
ArtistID int `gorm:"not null; unique_index:idx_album_id_artist_id" sql:"default: null; type:int REFERENCES artists(id) ON DELETE CASCADE"`
|
||||
}
|
||||
|
||||
type TrackGenre struct {
|
||||
Track *Track
|
||||
TrackID int `gorm:"not null; unique_index:idx_track_id_genre_id" sql:"default: null; type:int REFERENCES tracks(id) ON DELETE CASCADE"`
|
||||
@@ -429,3 +443,21 @@ type InternetRadioStation struct {
|
||||
func (ir *InternetRadioStation) SID() *specid.ID {
|
||||
return &specid.ID{Type: specid.InternetRadioStation, Value: ir.ID}
|
||||
}
|
||||
|
||||
type ArtistInfo struct {
|
||||
ID int `gorm:"primary_key" sql:"type:int REFERENCES artists(id) ON DELETE CASCADE"`
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time `gorm:"index"`
|
||||
Biography string
|
||||
MusicBrainzID string
|
||||
LastFMURL string
|
||||
ImageURL string
|
||||
SimilarArtists string
|
||||
TopTracks string
|
||||
}
|
||||
|
||||
func (p *ArtistInfo) GetSimilarArtists() []string { return strings.Split(p.SimilarArtists, ";") }
|
||||
func (p *ArtistInfo) SetSimilarArtists(items []string) { p.SimilarArtists = strings.Join(items, ";") }
|
||||
|
||||
func (p *ArtistInfo) GetTopTracks() []string { return strings.Split(p.TopTracks, ";") }
|
||||
func (p *ArtistInfo) SetTopTracks(items []string) { p.TopTracks = strings.Join(items, ";") }
|
||||
Reference in New Issue
Block a user