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