transcode pref: use uniq user & client, not client & profile
closes #48
This commit is contained in:
1
db/db.go
1
db/db.go
@@ -42,6 +42,7 @@ func New(path string) (*DB, error) {
|
|||||||
&migrationMergePlaylist,
|
&migrationMergePlaylist,
|
||||||
&migrationCreateTranscode,
|
&migrationCreateTranscode,
|
||||||
&migrationAddGenre,
|
&migrationAddGenre,
|
||||||
|
&migrationUpdateTranscodePrefIDX,
|
||||||
})
|
})
|
||||||
if err = migr.Migrate(); err != nil {
|
if err = migr.Migrate(); err != nil {
|
||||||
return nil, errors.Wrap(err, "migrating to latest version")
|
return nil, errors.Wrap(err, "migrating to latest version")
|
||||||
|
|||||||
@@ -2,6 +2,8 @@
|
|||||||
package db
|
package db
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
"github.com/jinzhu/gorm"
|
"github.com/jinzhu/gorm"
|
||||||
"gopkg.in/gormigrate.v1"
|
"gopkg.in/gormigrate.v1"
|
||||||
)
|
)
|
||||||
@@ -91,3 +93,42 @@ var migrationAddGenre = gormigrate.Migration{
|
|||||||
Error
|
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
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|||||||
@@ -203,7 +203,7 @@ func (p *PlayQueue) SetItems(items []int) {
|
|||||||
|
|
||||||
type TranscodePreference struct {
|
type TranscodePreference struct {
|
||||||
User *User
|
User *User
|
||||||
UserID int `sql:"default: null; type:int REFERENCES users(id) ON DELETE CASCADE"`
|
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_client_profile" sql:"default: null"`
|
Client string `gorm:"not null; unique_index:idx_user_id_client" sql:"default: null"`
|
||||||
Profile string `gorm:"not null; unique_index:idx_client_profile" sql:"default: null"`
|
Profile string `gorm:"not null" sql:"default: null"`
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user