Move transcodings initialization to a migration

This will make it run only once, not every
time the transcoding table is empty
This commit is contained in:
Deluan 2020-07-06 23:48:43 -04:00
parent fdc38b5ca5
commit f0a5df7cd7
2 changed files with 43 additions and 29 deletions

View File

@ -0,0 +1,43 @@
package migration
import (
"database/sql"
"github.com/deluan/navidrome/consts"
"github.com/google/uuid"
"github.com/pressly/goose"
)
func init() {
goose.AddMigration(upAddDefaultTranscodings, downAddDefaultTranscodings)
}
func upAddDefaultTranscodings(tx *sql.Tx) error {
row := tx.QueryRow("SELECT COUNT(*) FROM transcoding")
var count int
err := row.Scan(&count)
if err != nil {
return err
}
if count > 0 {
return nil
}
stmt, err := tx.Prepare("insert into transcoding (id, name, target_format, default_bit_rate, command) values (?, ?, ?, ?, ?)")
if err != nil {
return err
}
for _, t := range consts.DefaultTranscodings {
r, _ := uuid.NewRandom()
_, err := stmt.Exec(r.String(), t["name"], t["targetFormat"], t["defaultBitRate"], t["command"])
if err != nil {
return err
}
}
return nil
}
func downAddDefaultTranscodings(tx *sql.Tx) error {
return nil
}

View File

@ -2,7 +2,6 @@ package server
import (
"context"
"encoding/json"
"fmt"
"time"
@ -15,10 +14,6 @@ import (
func initialSetup(ds model.DataStore) {
_ = ds.WithTx(func(tx model.DataStore) error {
if err := createDefaultTranscodings(ds); err != nil {
return err
}
properties := ds.Property(context.TODO())
_, err := properties.Get(consts.InitialSetupFlagKey)
if err == nil {
@ -84,27 +79,3 @@ func createJWTSecret(ds model.DataStore) error {
}
return err
}
func createDefaultTranscodings(ds model.DataStore) error {
transcodings := ds.Transcoding(context.TODO())
c, _ := transcodings.CountAll()
if c != 0 {
return nil
}
for _, d := range consts.DefaultTranscodings {
var j []byte
var err error
if j, err = json.Marshal(d); err != nil {
return err
}
var t model.Transcoding
if err = json.Unmarshal(j, &t); err != nil {
return err
}
log.Info("Creating default transcoding config", "name", t.Name)
if err = transcodings.Put(&t); err != nil {
return err
}
}
return nil
}