From 38faffa9078f28b9941fef1bf169c7d5520587d2 Mon Sep 17 00:00:00 2001 From: Deluan Date: Fri, 28 Feb 2020 14:00:41 -0500 Subject: [PATCH] feat: `notice` function to notify (in logs) about important changes in migrations --- ...20200220143731_change_duration_to_float.go | 4 +- db/migration/migration.go | 46 +++++++++++++++++++ 2 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 db/migration/migration.go diff --git a/db/migration/20200220143731_change_duration_to_float.go b/db/migration/20200220143731_change_duration_to_float.go index 99a265745..288a792b9 100644 --- a/db/migration/20200220143731_change_duration_to_float.go +++ b/db/migration/20200220143731_change_duration_to_float.go @@ -2,7 +2,7 @@ package migration import ( "database/sql" - "github.com/deluan/navidrome/log" + "github.com/pressly/goose" ) @@ -11,7 +11,7 @@ func init() { } func Up20200220143731(tx *sql.Tx) error { - log.Warn("This migration will force the next scan to be a full rescan!") + notice(tx, "This migration will force the next scan to be a full rescan!") _, err := tx.Exec(` create table media_file_dg_tmp ( diff --git a/db/migration/migration.go b/db/migration/migration.go new file mode 100644 index 000000000..191722456 --- /dev/null +++ b/db/migration/migration.go @@ -0,0 +1,46 @@ +package migration + +import ( + "database/sql" + "fmt" + "sync" + + "github.com/deluan/navidrome/consts" +) + +// Use this in migrations that need to communicate something important (braking changes, forced reindexes, etc...) +func notice(tx *sql.Tx, msg string) { + if isDBInitialized(tx) { + fmt.Printf(` +************************************************************************************* +NOTICE: %s +************************************************************************************* + +`, msg) + } +} + +var once sync.Once + +func isDBInitialized(tx *sql.Tx) (initialized bool) { + once.Do(func() { + rows, err := tx.Query("select count(*) from property where id='" + consts.InitialSetupFlagKey + "'") + checkErr(err) + initialized = checkCount(rows) > 0 + }) + return initialized +} + +func checkCount(rows *sql.Rows) (count int) { + for rows.Next() { + err := rows.Scan(&count) + checkErr(err) + } + return count +} + +func checkErr(err error) { + if err != nil { + panic(err) + } +}