mirror of
https://github.com/navidrome/navidrome.git
synced 2026-06-19 07:37:15 +00:00
32ac53dc9f
Thread the context.Context that goose.UpContext already passes into every migration through to all DB calls: tx.Exec/Query/QueryRow become tx.ExecContext/QueryContext/QueryRowContext with ctx. The shared helpers in migration.go (notice, forceFullRescan, isDBInitialized) gain a ctx parameter and all call sites are updated. No-op migration functions use blank params (_ context.Context, _ *sql.Tx). This is a behavior-preserving change: the SQL, arguments, and ordering of every migration are unchanged; only cancellation/deadline propagation is added. Add a forbidigo lint rule scoped to db/migrations/ that forbids the non-context tx.Exec/Query/QueryRow forms, preventing regression. Signed-off-by: Deluan <deluan@navidrome.org>
45 lines
1.3 KiB
Go
45 lines
1.3 KiB
Go
package migrations
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
|
|
"github.com/navidrome/navidrome/consts"
|
|
"github.com/navidrome/navidrome/model/id"
|
|
"github.com/pressly/goose/v3"
|
|
)
|
|
|
|
func init() {
|
|
goose.AddMigrationContext(upEnsureDefaultTranscodings, downEnsureDefaultTranscodings)
|
|
}
|
|
|
|
func upEnsureDefaultTranscodings(ctx context.Context, tx *sql.Tx) error {
|
|
// Older installations may be missing default transcodings that were added
|
|
// after the initial seeding (e.g., aac was added later than mp3/opus).
|
|
// Insert any missing defaults without touching user-customized entries.
|
|
// Check both target_format and name since both have UNIQUE constraints,
|
|
// and older entries may have a different target_format (e.g., 'oga' vs 'opus')
|
|
// but the same name.
|
|
for _, t := range consts.DefaultTranscodings {
|
|
var count int
|
|
err := tx.QueryRowContext(ctx, "SELECT COUNT(*) FROM transcoding WHERE target_format = ? OR name = ?", t.TargetFormat, t.Name).Scan(&count)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if count == 0 {
|
|
_, err = tx.ExecContext(ctx,
|
|
"INSERT INTO transcoding (id, name, target_format, default_bit_rate, command) VALUES (?, ?, ?, ?, ?)",
|
|
id.NewRandom(), t.Name, t.TargetFormat, t.DefaultBitRate, t.Command,
|
|
)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func downEnsureDefaultTranscodings(_ context.Context, _ *sql.Tx) error {
|
|
return nil
|
|
}
|