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

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