From c3cc7dee017b80e5f2ca63e415bcc97ead766cf6 Mon Sep 17 00:00:00 2001 From: Deluan Date: Tue, 4 Apr 2023 10:30:28 -0400 Subject: [PATCH] Enable SQL migrations --- Makefile | 9 +++++++-- cmd/root.go | 2 +- db/db.go | 13 ++++++++++--- db/migration/placeholder.sql | 3 +++ persistence/persistence_suite_test.go | 2 +- scanner/scanner_suite_test.go | 2 +- 6 files changed, 23 insertions(+), 8 deletions(-) create mode 100644 db/migration/placeholder.sql diff --git a/Makefile b/Makefile index f00e2fd6..9740abb9 100644 --- a/Makefile +++ b/Makefile @@ -52,8 +52,13 @@ snapshots: ##@Development Update (GoLang) Snapshot tests UPDATE_SNAPSHOTS=true go run github.com/onsi/ginkgo/v2/ginkgo@latest ./server/subsonic/... .PHONY: snapshots -migration: ##@Development Create an empty migration file - @if [ -z "${name}" ]; then echo "Usage: make migration name=name_of_migration_file"; exit 1; fi +migration-sql: ##@Development Create an empty SQL migration file + @if [ -z "${name}" ]; then echo "Usage: make migration-sql name=name_of_migration_file"; exit 1; fi + go run github.com/pressly/goose/v3/cmd/goose@latest -dir db/migration create ${name} sql +.PHONY: migration + +migration-go: ##@Development Create an empty Go migration file + @if [ -z "${name}" ]; then echo "Usage: make migration-go name=name_of_migration_file"; exit 1; fi go run github.com/pressly/goose/v3/cmd/goose@latest -dir db/migration create ${name} .PHONY: migration diff --git a/cmd/root.go b/cmd/root.go index 06745828..39bb062c 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -61,7 +61,7 @@ func preRun() { } func runNavidrome() { - db.EnsureLatestVersion() + db.Init() defer func() { if err := db.Close(); err != nil { log.Error("Error closing DB", err) diff --git a/db/db.go b/db/db.go index 0a7c3e47..cea44974 100644 --- a/db/db.go +++ b/db/db.go @@ -2,6 +2,7 @@ package db import ( "database/sql" + "embed" "fmt" _ "github.com/mattn/go-sqlite3" @@ -17,6 +18,11 @@ var ( Path string ) +//go:embed migration/*.sql +var embedMigrations embed.FS + +const migrationsFolder = "migration" + func Db() *sql.DB { return singleton.GetInstance(func() *sql.DB { Path = conf.Server.DbPath @@ -38,7 +44,7 @@ func Close() error { return Db().Close() } -func EnsureLatestVersion() { +func Init() { db := Db() // Disable foreign_keys to allow re-creating tables in migrations @@ -55,18 +61,19 @@ func EnsureLatestVersion() { gooseLogger := &logAdapter{silent: isSchemaEmpty(db)} goose.SetLogger(gooseLogger) + goose.SetBaseFS(embedMigrations) err = goose.SetDialect(Driver) if err != nil { log.Fatal("Invalid DB driver", "driver", Driver, err) } - err = goose.Run("up", db, "./") + err = goose.Up(db, migrationsFolder) if err != nil { log.Fatal("Failed to apply new migrations", err) } } -func isSchemaEmpty(db *sql.DB) bool { // nolint:interfacer +func isSchemaEmpty(db *sql.DB) bool { rows, err := db.Query("SELECT name FROM sqlite_master WHERE type='table' AND name='goose_db_version';") // nolint:rowserrcheck if err != nil { log.Fatal("Database could not be opened!", err) diff --git a/db/migration/placeholder.sql b/db/migration/placeholder.sql new file mode 100644 index 00000000..ca957faf --- /dev/null +++ b/db/migration/placeholder.sql @@ -0,0 +1,3 @@ +-- This can be removed once we have at least one SQL migration. +-- It is here to avoid an error in the linter: +-- db/db.go:23:4: invalid go:embed: build system did not supply embed configuration (typecheck) \ No newline at end of file diff --git a/persistence/persistence_suite_test.go b/persistence/persistence_suite_test.go index 21267121..e0f03388 100644 --- a/persistence/persistence_suite_test.go +++ b/persistence/persistence_suite_test.go @@ -24,7 +24,7 @@ func TestPersistence(t *testing.T) { //conf.Server.DbPath = "./test-123.db" conf.Server.DbPath = "file::memory:?cache=shared" _ = orm.RegisterDataBase("default", db.Driver, conf.Server.DbPath) - db.EnsureLatestVersion() + db.Init() log.SetLevel(log.LevelError) RegisterFailHandler(Fail) RunSpecs(t, "Persistence Suite") diff --git a/scanner/scanner_suite_test.go b/scanner/scanner_suite_test.go index 402532dd..6c7f58c0 100644 --- a/scanner/scanner_suite_test.go +++ b/scanner/scanner_suite_test.go @@ -16,7 +16,7 @@ func TestScanner(t *testing.T) { tests.Init(t, true) conf.Server.DbPath = "file::memory:?cache=shared" _ = orm.RegisterDataBase("default", db.Driver, conf.Server.DbPath) - db.EnsureLatestVersion() + db.Init() log.SetLevel(log.LevelFatal) RegisterFailHandler(Fail) RunSpecs(t, "Scanner Suite")