transcode pref: use uniq user & client, not client & profile

closes #48
This commit is contained in:
sentriz
2020-03-24 14:48:30 +00:00
parent 52d2dbcce9
commit be3cf9a04e
3 changed files with 45 additions and 3 deletions

View File

@@ -42,6 +42,7 @@ func New(path string) (*DB, error) {
&migrationMergePlaylist,
&migrationCreateTranscode,
&migrationAddGenre,
&migrationUpdateTranscodePrefIDX,
})
if err = migr.Migrate(); err != nil {
return nil, errors.Wrap(err, "migrating to latest version")

View File

@@ -2,6 +2,8 @@
package db
import (
"fmt"
"github.com/jinzhu/gorm"
"gopkg.in/gormigrate.v1"
)
@@ -91,3 +93,42 @@ var migrationAddGenre = gormigrate.Migration{
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
},
}

View File

@@ -203,7 +203,7 @@ func (p *PlayQueue) SetItems(items []int) {
type TranscodePreference struct {
User *User
UserID int `sql:"default: null; type:int REFERENCES users(id) ON DELETE CASCADE"`
Client string `gorm:"not null; unique_index:idx_client_profile" sql:"default: null"`
Profile string `gorm:"not null; unique_index:idx_client_profile" sql:"default: null"`
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"`
}