diff --git a/db/db.go b/db/db.go index 640861b89..dc6576d3e 100644 --- a/db/db.go +++ b/db/db.go @@ -42,7 +42,6 @@ func Db() *sql.DB { func EnsureLatestVersion() { db := Db() - goose.SetLogger(&logAdapter{}) // Disable foreign_keys to allow re-creating tables in migrations _, err := db.Exec("PRAGMA foreign_keys=off") @@ -56,6 +55,9 @@ func EnsureLatestVersion() { log.Error("Error disabling foreign_keys", err) } + gooseLogger := &logAdapter{silent: isSchemaEmpty(db)} + goose.SetLogger(gooseLogger) + err = goose.SetDialect(Driver) if err != nil { log.Error("Invalid DB driver", "driver", Driver, err) @@ -68,7 +70,19 @@ func EnsureLatestVersion() { } } -type logAdapter struct{} +func isSchemaEmpty(db *sql.DB) bool { // nolint:interfacer + rows, err := db.Query("SELECT name FROM sqlite_master WHERE type='table' AND name='goose_db_version';") // nolint:rowserrcheck + if err != nil { + log.Error("Database could not be opened!", err) + os.Exit(1) + } + defer rows.Close() + return !rows.Next() +} + +type logAdapter struct { + silent bool +} func (l *logAdapter) Fatal(v ...interface{}) { log.Error(fmt.Sprint(v...)) @@ -81,13 +95,19 @@ func (l *logAdapter) Fatalf(format string, v ...interface{}) { } func (l *logAdapter) Print(v ...interface{}) { - log.Info(fmt.Sprint(v...)) + if !l.silent { + log.Info(fmt.Sprint(v...)) + } } func (l *logAdapter) Println(v ...interface{}) { - log.Info(fmt.Sprintln(v...)) + if !l.silent { + log.Info(fmt.Sprintln(v...)) + } } func (l *logAdapter) Printf(format string, v ...interface{}) { - log.Info(fmt.Sprintf(format, v...)) + if !l.silent { + log.Info(fmt.Sprintf(format, v...)) + } } diff --git a/db/db_test.go b/db/db_test.go new file mode 100644 index 000000000..d24934884 --- /dev/null +++ b/db/db_test.go @@ -0,0 +1,36 @@ +package db + +import ( + "database/sql" + "testing" + + "github.com/navidrome/navidrome/log" + "github.com/navidrome/navidrome/tests" + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" +) + +func TestDB(t *testing.T) { + tests.Init(t, false) + log.SetLevel(log.LevelCritical) + RegisterFailHandler(Fail) + RunSpecs(t, "DB Suite") +} + +var _ = Describe("isSchemaEmpty", func() { + var db *sql.DB + BeforeEach(func() { + path := "file::memory:" + db, _ = sql.Open(Driver, path) + }) + + It("returns false if the goose metadata table is found", func() { + _, err := db.Exec("create table goose_db_version (id primary key);") + Expect(err).ToNot(HaveOccurred()) + Expect(isSchemaEmpty(db)).To(BeFalse()) + }) + + It("returns true if the schema is brand new", func() { + Expect(isSchemaEmpty(db)).To(BeTrue()) + }) +})