mirror of
https://github.com/navidrome/navidrome.git
synced 2026-11-19 15:23:36 +00:00
* chore(lint): ban panic in production code Add a forbidigo rule that rejects panic outside tests and test helpers (_test.go, tests/ and storagetest/). Its message says to return an error, and to use log.Fatal only for startup failures. The existing production panics are gone: - model.MustParseArtworkID was only used by core/artwork tests. It is now a Gomega helper in those test suites, and it also panicked with the parsed ID instead of the error. - isDBInitialized in db/migrations panicked on a query error and never closed its rows. It now uses QueryRowContext and logs the error. The two http.ErrAbortHandler panics in the stream and API v1 handlers stay, because net/http requires them to abort a response. They are marked with //nolint:forbidigo. cmd/wire_injectors.go is not linted, because of its wireinject build tag. * chore(lint): ban panic in tests too The panic ban excluded test files and test helpers, so agents could read it as permission to panic in tests. The rule now applies everywhere, and its message says what to use instead: return an error, or Expect(...)/Fail(...) in tests. Test panics are replaced with Gomega assertions, and the helpers that assert call GinkgoHelper() so failures point at the calling spec. The fake HTTP clients in the deezer tests and getDirEntry use Fail(...). storagetest.FakeFS.UpdateTags now asserts with Gomega instead of panicking. Seven test panics stay, marked //nolint:forbidigo: they exercise panic recovery or stand in for log.Fatal. * refactor(tests): share panicking test helpers Move mustParseArtworkID out of the two core/artwork suites into tests.MustParseArtworkID, and add conf.PanicOnLogFatal so the conf specs share one panicking logFatal stub and one //nolint. The MustPath spec now asserts the fatal message instead of capturing it. Also merge the back-to-back user and artist loops in the persistence suite setup.
106 lines
3.3 KiB
Go
106 lines
3.3 KiB
Go
package migrations
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"fmt"
|
|
"strings"
|
|
"sync"
|
|
|
|
"github.com/navidrome/navidrome/consts"
|
|
"github.com/navidrome/navidrome/log"
|
|
)
|
|
|
|
// Use this in migrations that need to communicate something important (breaking changes, forced reindexes, etc...)
|
|
func notice(ctx context.Context, tx *sql.Tx, msg string) {
|
|
if isDBInitialized(ctx, tx) {
|
|
line := strings.Repeat("*", len(msg)+8)
|
|
fmt.Printf("\n%s\nNOTICE: %s\n%s\n\n", line, msg, line)
|
|
}
|
|
}
|
|
|
|
// Call this in migrations that requires a full rescan
|
|
func forceFullRescan(ctx context.Context, tx *sql.Tx) error {
|
|
_, err := tx.ExecContext(ctx, fmt.Sprintf(`
|
|
INSERT OR REPLACE into property (id, value) values ('%s', '1');
|
|
`, consts.FullScanAfterMigrationFlagKey))
|
|
return err
|
|
}
|
|
|
|
// sq := Update(r.tableName).
|
|
// Set("last_scan_started_at", time.Now()).
|
|
// Set("full_scan_in_progress", fullScan).
|
|
// Where(Eq{"id": id})
|
|
|
|
var (
|
|
once sync.Once
|
|
initialized bool
|
|
)
|
|
|
|
func isDBInitialized(ctx context.Context, tx *sql.Tx) bool {
|
|
once.Do(func() {
|
|
var count int
|
|
err := tx.QueryRowContext(ctx, "select count(*) from property where id=?", consts.InitialSetupFlagKey).Scan(&count)
|
|
if err != nil {
|
|
log.Error(ctx, "Could not check if the database is initialized", err)
|
|
}
|
|
initialized = count > 0
|
|
})
|
|
return initialized
|
|
}
|
|
|
|
type (
|
|
execFunc func() error
|
|
execStmtFunc func(stmt string) execFunc
|
|
addColumnFunc func(tableName, columnName, columnType, defaultValue, initialValue string) execFunc
|
|
)
|
|
|
|
func createExecuteFunc(ctx context.Context, tx *sql.Tx) execStmtFunc {
|
|
return func(stmt string) execFunc {
|
|
return func() error {
|
|
_, err := tx.ExecContext(ctx, stmt)
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
|
|
// Hack way to add a new `not null` column to a table, setting the initial value for existing rows based on a
|
|
// SQL expression. It is done in 3 steps:
|
|
// 1. Add the column as nullable. Due to the way SQLite manipulates the DDL in memory, we need to add extra padding
|
|
// to the default value to avoid truncating it when changing the column to not null
|
|
// 2. Update the column with the initial value
|
|
// 3. Change the column to not null with the default value
|
|
//
|
|
// Based on https://stackoverflow.com/a/25917323
|
|
func createAddColumnFunc(ctx context.Context, tx *sql.Tx) addColumnFunc {
|
|
return func(tableName, columnName, columnType, defaultValue, initialValue string) execFunc {
|
|
return func() error {
|
|
// Format the `default null` value to have the same length as the final defaultValue
|
|
finalLen := len(fmt.Sprintf(`%s not`, defaultValue))
|
|
tempDefault := fmt.Sprintf(`default %s null`, strings.Repeat(" ", finalLen))
|
|
_, err := tx.ExecContext(ctx, fmt.Sprintf(`
|
|
alter table %s add column %s %s %s;`, tableName, columnName, columnType, tempDefault))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
_, err = tx.ExecContext(ctx, fmt.Sprintf(`
|
|
update %s set %s = %s where %[2]s is null;`, tableName, columnName, initialValue))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
_, err = tx.ExecContext(ctx, fmt.Sprintf(`
|
|
PRAGMA writable_schema = on;
|
|
UPDATE sqlite_master
|
|
SET sql = replace(sql, '%[1]s %[2]s %[5]s', '%[1]s %[2]s default %[3]s not null')
|
|
WHERE type = 'table'
|
|
AND name = '%[4]s';
|
|
PRAGMA writable_schema = off;
|
|
`, columnName, columnType, defaultValue, tableName, tempDefault))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return err
|
|
}
|
|
}
|
|
}
|