mirror of
https://github.com/navidrome/navidrome.git
synced 2026-12-03 18:36:56 +00:00
Pipeline: Test, Lint, Build / Get version info (push) Canceled after 0s
Pipeline: Test, Lint, Build / Lint Go code (push) Canceled after 0s
Pipeline: Test, Lint, Build / Validate DB migrations (push) Canceled after 0s
Pipeline: Test, Lint, Build / Test Go code (push) Canceled after 0s
Pipeline: Test, Lint, Build / Test Go plugins (push) Canceled after 0s
Pipeline: Test, Lint, Build / Report coverage (push) Canceled after 0s
Pipeline: Test, Lint, Build / Test Go code (Windows) (push) Canceled after 0s
Pipeline: Test, Lint, Build / Test JS code (push) Canceled after 0s
Pipeline: Test, Lint, Build / Lint i18n files (push) Canceled after 0s
Pipeline: Test, Lint, Build / Check Docker configuration (push) Canceled after 0s
Pipeline: Test, Lint, Build / Build (darwin/amd64) (push) Canceled after 0s
Pipeline: Test, Lint, Build / Build (darwin/arm64) (push) Canceled after 0s
Pipeline: Test, Lint, Build / Build (linux/386) (push) Canceled after 0s
Pipeline: Test, Lint, Build / Build (linux/amd64) (push) Canceled after 0s
Pipeline: Test, Lint, Build / Build (linux/arm/v5) (push) Canceled after 0s
Pipeline: Test, Lint, Build / Build (linux/arm/v6) (push) Canceled after 0s
Pipeline: Test, Lint, Build / Build (linux/arm/v7) (push) Canceled after 0s
Pipeline: Test, Lint, Build / Build (linux/arm64) (push) Canceled after 0s
Pipeline: Test, Lint, Build / Build (linux/riscv64) (push) Canceled after 0s
Pipeline: Test, Lint, Build / Build (windows/386) (push) Canceled after 0s
Pipeline: Test, Lint, Build / Build (windows/amd64) (push) Canceled after 0s
Pipeline: Test, Lint, Build / Push to GHCR (push) Canceled after 0s
Pipeline: Test, Lint, Build / Push to Docker Hub () (push) Canceled after 0s
Pipeline: Test, Lint, Build / Cleanup digest artifacts (push) Canceled after 0s
Pipeline: Test, Lint, Build / Build Windows installers (push) Canceled after 0s
Pipeline: Test, Lint, Build / Package/Release (push) Canceled after 0s
Pipeline: Test, Lint, Build / Upload Linux PKG (push) Canceled after 0s
49 lines
1.4 KiB
Go
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
|
|
}
|