Skip to content

🎧 Your Personal Streaming Service

Enable SQL migrations

Deluanc3cc7dee017b
diff --git a/Makefile b/Makefile
index f00e2fd60..9740abb99 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 067458287..39bb062c3 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 0a7c3e47a..cea44974f 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 000000000..ca957faf8
--- /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 212671217..e0f033880 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 402532ddc..6c7f58c09 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")