feat: `notice` function to notify (in logs) about important changes in migrations

This commit is contained in:
Deluan 2020-02-28 14:00:41 -05:00
parent 65a792be3a
commit 38faffa907
2 changed files with 48 additions and 2 deletions

View File

@ -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
(

46
db/migration/migration.go Normal file
View File

@ -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)
}
}