mirror of
https://github.com/navidrome/navidrome.git
synced 2025-06-06 02:13:29 +03:00
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:
parent
fdc38b5ca5
commit
f0a5df7cd7
43
db/migration/20200706231659_add_default_transcodings.go
Normal file
43
db/migration/20200706231659_add_default_transcodings.go
Normal 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
|
||||||
|
}
|
@ -2,7 +2,6 @@ package server
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -15,10 +14,6 @@ import (
|
|||||||
|
|
||||||
func initialSetup(ds model.DataStore) {
|
func initialSetup(ds model.DataStore) {
|
||||||
_ = ds.WithTx(func(tx model.DataStore) error {
|
_ = ds.WithTx(func(tx model.DataStore) error {
|
||||||
if err := createDefaultTranscodings(ds); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
properties := ds.Property(context.TODO())
|
properties := ds.Property(context.TODO())
|
||||||
_, err := properties.Get(consts.InitialSetupFlagKey)
|
_, err := properties.Get(consts.InitialSetupFlagKey)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
@ -84,27 +79,3 @@ func createJWTSecret(ds model.DataStore) error {
|
|||||||
}
|
}
|
||||||
return err
|
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
|
|
||||||
}
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user