mirror of
https://github.com/navidrome/navidrome.git
synced 2025-04-04 13:07:36 +03:00
Add a "upgrading schema" log message to the DB initialization when there are pending migrations.
This commit is contained in:
parent
fa7cc40d23
commit
5f9b6b632d
1 changed files with 28 additions and 1 deletions
29
db/db.go
29
db/db.go
|
@ -60,19 +60,46 @@ func Init() {
|
||||||
}
|
}
|
||||||
|
|
||||||
gooseLogger := &logAdapter{silent: isSchemaEmpty(db)}
|
gooseLogger := &logAdapter{silent: isSchemaEmpty(db)}
|
||||||
goose.SetLogger(gooseLogger)
|
|
||||||
goose.SetBaseFS(embedMigrations)
|
goose.SetBaseFS(embedMigrations)
|
||||||
|
|
||||||
err = goose.SetDialect(Driver)
|
err = goose.SetDialect(Driver)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("Invalid DB driver", "driver", Driver, err)
|
log.Fatal("Invalid DB driver", "driver", Driver, err)
|
||||||
}
|
}
|
||||||
|
if !isSchemaEmpty(db) && hasPendingMigrations(db, migrationsFolder) {
|
||||||
|
log.Info("Upgrading DB Schema to latest version")
|
||||||
|
}
|
||||||
|
goose.SetLogger(gooseLogger)
|
||||||
err = goose.Up(db, migrationsFolder)
|
err = goose.Up(db, migrationsFolder)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal("Failed to apply new migrations", err)
|
log.Fatal("Failed to apply new migrations", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type statusLogger struct{ numPending int }
|
||||||
|
|
||||||
|
func (*statusLogger) Fatalf(format string, v ...interface{}) { log.Fatal(fmt.Sprintf(format, v...)) }
|
||||||
|
func (l *statusLogger) Printf(format string, v ...interface{}) {
|
||||||
|
if len(v) < 1 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if v0, ok := v[0].(string); !ok {
|
||||||
|
return
|
||||||
|
} else if v0 == "Pending" {
|
||||||
|
l.numPending++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func hasPendingMigrations(db *sql.DB, folder string) bool {
|
||||||
|
l := &statusLogger{}
|
||||||
|
goose.SetLogger(l)
|
||||||
|
err := goose.Status(db, folder)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal("Failed to check for pending migrations", err)
|
||||||
|
}
|
||||||
|
return l.numPending > 0
|
||||||
|
}
|
||||||
|
|
||||||
func isSchemaEmpty(db *sql.DB) bool {
|
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
|
rows, err := db.Query("SELECT name FROM sqlite_master WHERE type='table' AND name='goose_db_version';") // nolint:rowserrcheck
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue