Files
navidrome/plugins/migrate.go
T
Deluan f8ed972e4c chore(lint): add the ctxvariant linter
ctxvariant (tools/ctxvariant, a golangci-lint module plugin built via .custom-gcl.yml) reports a call to F when
FContext or FWithContext takes the same arguments after a leading ctx, plus stdlib calls whose ctx variant is named
differently (http.Get, net.Listen/Dial, DB.Begin, ...). It replaces the migration-only forbidigo SQL rules.
Not noctx: it misses closures in package-level vars (every Ginkgo spec) and packages without a direct import.
Fixes 702 call sites: 658 in tests, 14 in server code, 30 in migrations. The migrations now use ctx-aware calls;
they already ran every other query with the same ctx, so their behavior is the same.
2026-11-17 20:19:10 -05:00

49 lines
1.4 KiB
Go

package plugins
import (
"context"
"database/sql"
"fmt"
)
// migrateDB applies schema migrations to a SQLite database.
//
// Each entry in migrations is a single SQL statement. The current schema version
// is tracked using SQLite's built-in PRAGMA user_version. Only statements after
// the current version are executed, within a single transaction.
func migrateDB(ctx context.Context, db *sql.DB, migrations []string) error {
var version int
if err := db.QueryRowContext(ctx, `PRAGMA user_version`).Scan(&version); err != nil {
return fmt.Errorf("reading schema version: %w", err)
}
if version >= len(migrations) {
return nil
}
tx, err := db.BeginTx(ctx, nil)
if err != nil {
return fmt.Errorf("starting migration transaction: %w", err)
}
defer func() { _ = tx.Rollback() }()
for i := version; i < len(migrations); i++ {
if _, err := tx.ExecContext(ctx, migrations[i]); err != nil {
return fmt.Errorf("migration %d failed: %w", i+1, err)
}
}
// PRAGMA statements cannot be executed inside a transaction in some SQLite
// drivers, but with mattn/go-sqlite3 this works. We set it inside the tx
// so that a failed commit leaves the version unchanged.
if _, err := tx.ExecContext(ctx, fmt.Sprintf(`PRAGMA user_version = %d`, len(migrations))); err != nil {
return fmt.Errorf("updating schema version: %w", err)
}
if err := tx.Commit(); err != nil {
return fmt.Errorf("committing migrations: %w", err)
}
return nil
}