upgrade sqlite3
This commit is contained in:
88
db/db.go
88
db/db.go
@@ -1,88 +0,0 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/url"
|
||||
"os"
|
||||
|
||||
"github.com/jinzhu/gorm"
|
||||
"github.com/pkg/errors"
|
||||
"gopkg.in/gormigrate.v1"
|
||||
)
|
||||
|
||||
var (
|
||||
dbMaxOpenConns = 1
|
||||
dbOptions = url.Values{
|
||||
// with this, multiple connections share a single data and schema cache.
|
||||
// see https://www.sqlite.org/sharedcache.html
|
||||
"cache": []string{"shared"},
|
||||
// with this, the db sleeps for a little while when locked. can prevent
|
||||
// a SQLITE_BUSY. see https://www.sqlite.org/c3ref/busy_timeout.html
|
||||
"_busy_timeout": []string{"30000"},
|
||||
"_journal_mode": []string{"WAL"},
|
||||
}
|
||||
)
|
||||
|
||||
type DB struct {
|
||||
*gorm.DB
|
||||
}
|
||||
|
||||
func New(path string) (*DB, error) {
|
||||
pathAndArgs := fmt.Sprintf("%s?%s", path, dbOptions.Encode())
|
||||
db, err := gorm.Open("sqlite3", pathAndArgs)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "with gorm")
|
||||
}
|
||||
db.SetLogger(log.New(os.Stdout, "gorm ", 0))
|
||||
db.DB().SetMaxOpenConns(dbMaxOpenConns)
|
||||
migr := gormigrate.New(db, gormigrate.DefaultOptions, []*gormigrate.Migration{
|
||||
&migrationInitSchema,
|
||||
&migrationCreateInitUser,
|
||||
&migrationMergePlaylist,
|
||||
&migrationCreateTranscode,
|
||||
&migrationAddGenre,
|
||||
&migrationUpdateTranscodePrefIDX,
|
||||
})
|
||||
if err = migr.Migrate(); err != nil {
|
||||
return nil, errors.Wrap(err, "migrating to latest version")
|
||||
}
|
||||
return &DB{DB: db}, nil
|
||||
}
|
||||
|
||||
func NewMock() (*DB, error) {
|
||||
return New(":memory:")
|
||||
}
|
||||
|
||||
func (db *DB) GetSetting(key string) string {
|
||||
setting := &Setting{}
|
||||
db.
|
||||
Where("key=?", key).
|
||||
First(setting)
|
||||
return setting.Value
|
||||
}
|
||||
|
||||
func (db *DB) SetSetting(key, value string) {
|
||||
db.
|
||||
Where(Setting{Key: key}).
|
||||
Assign(Setting{Value: value}).
|
||||
FirstOrCreate(&Setting{})
|
||||
}
|
||||
|
||||
func (db *DB) GetUserFromName(name string) *User {
|
||||
user := &User{}
|
||||
err := db.
|
||||
Where("name=?", name).
|
||||
First(user).
|
||||
Error
|
||||
if gorm.IsRecordNotFoundError(err) {
|
||||
return nil
|
||||
}
|
||||
return user
|
||||
}
|
||||
|
||||
func (db *DB) WithTx(cb func(tx *gorm.DB)) {
|
||||
tx := db.Begin()
|
||||
defer tx.Commit()
|
||||
cb(tx)
|
||||
}
|
||||
@@ -1,46 +0,0 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"log"
|
||||
"math/rand"
|
||||
"testing"
|
||||
|
||||
_ "github.com/jinzhu/gorm/dialects/sqlite"
|
||||
)
|
||||
|
||||
var testDB *DB
|
||||
|
||||
func init() {
|
||||
var err error
|
||||
testDB, err = NewMock()
|
||||
if err != nil {
|
||||
log.Fatalf("error opening database: %v\n", err)
|
||||
}
|
||||
}
|
||||
|
||||
func randKey() string {
|
||||
letters := []rune("abcdef0123456789")
|
||||
b := make([]rune, 16)
|
||||
for i := range b {
|
||||
b[i] = letters[rand.Intn(len(letters))]
|
||||
}
|
||||
return string(b)
|
||||
}
|
||||
|
||||
func TestGetSetting(t *testing.T) {
|
||||
key := randKey()
|
||||
// new key
|
||||
expected := "hello"
|
||||
testDB.SetSetting(key, expected)
|
||||
actual := testDB.GetSetting(key)
|
||||
if actual != expected {
|
||||
t.Errorf("expected %q, got %q", expected, actual)
|
||||
}
|
||||
// existing key
|
||||
expected = "howdy"
|
||||
testDB.SetSetting(key, expected)
|
||||
actual = testDB.GetSetting(key)
|
||||
if actual != expected {
|
||||
t.Errorf("expected %q, got %q", expected, actual)
|
||||
}
|
||||
}
|
||||
134
db/migrations.go
134
db/migrations.go
@@ -1,134 +0,0 @@
|
||||
//nolint:deadcode,varcheck
|
||||
package db
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/jinzhu/gorm"
|
||||
"gopkg.in/gormigrate.v1"
|
||||
)
|
||||
|
||||
// $ date '+%Y%m%d%H%M'
|
||||
|
||||
// not really a migration
|
||||
var migrationInitSchema = gormigrate.Migration{
|
||||
ID: "202002192100",
|
||||
Migrate: func(tx *gorm.DB) error {
|
||||
return tx.AutoMigrate(
|
||||
Artist{},
|
||||
Track{},
|
||||
User{},
|
||||
Setting{},
|
||||
Play{},
|
||||
Album{},
|
||||
Playlist{},
|
||||
PlayQueue{},
|
||||
).
|
||||
Error
|
||||
},
|
||||
}
|
||||
|
||||
// not really a migration
|
||||
var migrationCreateInitUser = gormigrate.Migration{
|
||||
ID: "202002192019",
|
||||
Migrate: func(tx *gorm.DB) error {
|
||||
const (
|
||||
initUsername = "admin"
|
||||
initPassword = "admin"
|
||||
)
|
||||
err := tx.
|
||||
Where("name=?", initUsername).
|
||||
First(&User{}).
|
||||
Error
|
||||
if !gorm.IsRecordNotFoundError(err) {
|
||||
return nil
|
||||
}
|
||||
return tx.Create(&User{
|
||||
Name: initUsername,
|
||||
Password: initPassword,
|
||||
IsAdmin: true,
|
||||
}).
|
||||
Error
|
||||
},
|
||||
}
|
||||
|
||||
var migrationMergePlaylist = gormigrate.Migration{
|
||||
ID: "202002192222",
|
||||
Migrate: func(tx *gorm.DB) error {
|
||||
if !tx.HasTable("playlist_items") {
|
||||
return nil
|
||||
}
|
||||
return tx.Exec(`
|
||||
UPDATE playlists
|
||||
SET items=( SELECT group_concat(track_id) FROM (
|
||||
SELECT track_id
|
||||
FROM playlist_items
|
||||
WHERE playlist_items.playlist_id=playlists.id
|
||||
ORDER BY created_at
|
||||
) );
|
||||
DROP TABLE playlist_items;`,
|
||||
).
|
||||
Error
|
||||
},
|
||||
}
|
||||
|
||||
var migrationCreateTranscode = gormigrate.Migration{
|
||||
ID: "202003111222",
|
||||
Migrate: func(tx *gorm.DB) error {
|
||||
return tx.AutoMigrate(
|
||||
TranscodePreference{},
|
||||
).
|
||||
Error
|
||||
},
|
||||
}
|
||||
|
||||
var migrationAddGenre = gormigrate.Migration{
|
||||
ID: "202003121330",
|
||||
Migrate: func(tx *gorm.DB) error {
|
||||
return tx.AutoMigrate(
|
||||
Genre{},
|
||||
Album{},
|
||||
Track{},
|
||||
).
|
||||
Error
|
||||
},
|
||||
}
|
||||
|
||||
var migrationUpdateTranscodePrefIDX = gormigrate.Migration{
|
||||
ID: "202003241509",
|
||||
Migrate: func(tx *gorm.DB) error {
|
||||
var hasIDX int
|
||||
tx.
|
||||
Select("1").
|
||||
Table("sqlite_master").
|
||||
Where("type = ?", "index").
|
||||
Where("name = ?", "idx_user_id_client").
|
||||
Count(&hasIDX)
|
||||
if hasIDX == 1 {
|
||||
// index already exists
|
||||
return nil
|
||||
}
|
||||
step := tx.Exec(`
|
||||
ALTER TABLE transcode_preferences RENAME TO transcode_preferences_orig;
|
||||
`)
|
||||
if err := step.Error; err != nil {
|
||||
return fmt.Errorf("step rename: %w", err)
|
||||
}
|
||||
step = tx.AutoMigrate(
|
||||
TranscodePreference{},
|
||||
)
|
||||
if err := step.Error; err != nil {
|
||||
return fmt.Errorf("step create: %w", err)
|
||||
}
|
||||
step = tx.Exec(`
|
||||
INSERT INTO transcode_preferences (user_id, client, profile)
|
||||
SELECT user_id, client, profile
|
||||
FROM transcode_preferences_orig;
|
||||
DROP TABLE transcode_preferences_orig;
|
||||
`)
|
||||
if err := step.Error; err != nil {
|
||||
return fmt.Errorf("step copy: %w", err)
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}
|
||||
209
db/model.go
209
db/model.go
@@ -1,209 +0,0 @@
|
||||
//nolint:lll
|
||||
package db
|
||||
|
||||
import (
|
||||
"path"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"go.senan.xyz/gonic/mime"
|
||||
)
|
||||
|
||||
func splitInt(in, sep string) []int {
|
||||
if len(in) == 0 {
|
||||
return []int{}
|
||||
}
|
||||
parts := strings.Split(in, sep)
|
||||
ret := make([]int, 0, len(parts))
|
||||
for _, p := range parts {
|
||||
i, _ := strconv.Atoi(p)
|
||||
ret = append(ret, i)
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
func joinInt(in []int, sep string) string {
|
||||
if in == nil {
|
||||
return ""
|
||||
}
|
||||
strs := make([]string, 0, len(in))
|
||||
for _, i := range in {
|
||||
strs = append(strs, strconv.Itoa(i))
|
||||
}
|
||||
return strings.Join(strs, sep)
|
||||
}
|
||||
|
||||
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"`
|
||||
AlbumCount int `sql:"-"`
|
||||
}
|
||||
|
||||
func (a *Artist) IndexName() string {
|
||||
if len(a.NameUDec) > 0 {
|
||||
return a.NameUDec
|
||||
}
|
||||
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
|
||||
UpdatedAt time.Time
|
||||
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"`
|
||||
Size int `gorm:"not null" sql:"default: null"`
|
||||
Length int `sql:"default: null"`
|
||||
Bitrate int `sql:"default: null"`
|
||||
TagTitle string `sql:"default: null"`
|
||||
TagTitleUDec string `sql:"default: null"`
|
||||
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"`
|
||||
TagBrainzID string `sql:"default: null"`
|
||||
}
|
||||
|
||||
func (t *Track) Ext() string {
|
||||
longExt := path.Ext(t.Filename)
|
||||
if len(longExt) < 1 {
|
||||
return ""
|
||||
}
|
||||
return longExt[1:]
|
||||
}
|
||||
|
||||
func (t *Track) MIME() string {
|
||||
ext := t.Ext()
|
||||
return mime.Types[ext]
|
||||
}
|
||||
|
||||
func (t *Track) RelPath() string {
|
||||
if t.Album == nil {
|
||||
return ""
|
||||
}
|
||||
return path.Join(
|
||||
t.Album.LeftPath,
|
||||
t.Album.RightPath,
|
||||
t.Filename,
|
||||
)
|
||||
}
|
||||
|
||||
type User struct {
|
||||
ID int `gorm:"primary_key"`
|
||||
CreatedAt time.Time
|
||||
Name string `gorm:"not null; unique_index" sql:"default: null"`
|
||||
Password string `gorm:"not null" sql:"default: null"`
|
||||
LastFMSession string `sql:"default: null"`
|
||||
IsAdmin bool `sql:"default: null"`
|
||||
}
|
||||
|
||||
type Setting struct {
|
||||
Key string `gorm:"not null; primary_key; auto_increment:false" sql:"default: null"`
|
||||
Value string `sql:"default: null"`
|
||||
}
|
||||
|
||||
type Play struct {
|
||||
ID int `gorm:"primary_key"`
|
||||
User *User
|
||||
UserID int `gorm:"not null; index" sql:"default: null; type:int REFERENCES users(id) ON DELETE CASCADE"`
|
||||
Album *Album
|
||||
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
|
||||
}
|
||||
|
||||
type Album struct {
|
||||
ID int `gorm:"primary_key"`
|
||||
UpdatedAt time.Time
|
||||
ModifiedAt time.Time
|
||||
LeftPath string `gorm:"unique_index:idx_left_path_right_path"`
|
||||
RightPath string `gorm:"not null; unique_index:idx_left_path_right_path" sql:"default: null"`
|
||||
RightPathUDec string `sql:"default: null"`
|
||||
Parent *Album
|
||||
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"`
|
||||
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"`
|
||||
TagYear int `sql:"default: null"`
|
||||
Tracks []*Track
|
||||
ChildCount int `sql:"-"`
|
||||
ReceivedPaths bool `gorm:"-"`
|
||||
ReceivedTags bool `gorm:"-"`
|
||||
}
|
||||
|
||||
func (a *Album) IndexRightPath() string {
|
||||
if len(a.RightPathUDec) > 0 {
|
||||
return a.RightPathUDec
|
||||
}
|
||||
return a.RightPath
|
||||
}
|
||||
|
||||
type Playlist struct {
|
||||
ID int `gorm:"primary_key"`
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
User *User
|
||||
UserID int `sql:"default: null; type:int REFERENCES users(id) ON DELETE CASCADE"`
|
||||
Name string
|
||||
Comment string
|
||||
TrackCount int
|
||||
Items string
|
||||
}
|
||||
|
||||
func (p *Playlist) GetItems() []int {
|
||||
return splitInt(p.Items, ",")
|
||||
}
|
||||
|
||||
func (p *Playlist) SetItems(items []int) {
|
||||
p.Items = joinInt(items, ",")
|
||||
p.TrackCount = len(items)
|
||||
}
|
||||
|
||||
type PlayQueue struct {
|
||||
ID int `gorm:"primary_key"`
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
User *User
|
||||
UserID int `sql:"default: null; type:int REFERENCES users(id) ON DELETE CASCADE"`
|
||||
Current int
|
||||
Position int
|
||||
ChangedBy string
|
||||
Items string
|
||||
}
|
||||
|
||||
func (p *PlayQueue) GetItems() []int {
|
||||
return splitInt(p.Items, ",")
|
||||
}
|
||||
|
||||
func (p *PlayQueue) SetItems(items []int) {
|
||||
p.Items = joinInt(items, ",")
|
||||
}
|
||||
|
||||
type TranscodePreference struct {
|
||||
User *User
|
||||
UserID int `gorm:"not null; unique_index:idx_user_id_client" sql:"default: null; type:int REFERENCES users(id) ON DELETE CASCADE"`
|
||||
Client string `gorm:"not null; unique_index:idx_user_id_client" sql:"default: null"`
|
||||
Profile string `gorm:"not null" sql:"default: null"`
|
||||
}
|
||||
Reference in New Issue
Block a user