From f292238e7f28dca891e06dbbe68a2fc2d09edc1c Mon Sep 17 00:00:00 2001 From: WithoutPants <53250216+WithoutPants@users.noreply.github.com> Date: Tue, 21 Sep 2021 21:57:50 +1000 Subject: [PATCH] Only close DB if not nil (#1749) --- pkg/database/database.go | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/pkg/database/database.go b/pkg/database/database.go index 017a3497a..87298fcbf 100644 --- a/pkg/database/database.go +++ b/pkg/database/database.go @@ -21,7 +21,7 @@ import ( ) var DB *sqlx.DB -var WriteMu *sync.Mutex +var WriteMu sync.Mutex var dbPath string var appSchemaVersion uint = 28 var databaseSchemaVersion uint @@ -84,7 +84,6 @@ func Initialize(databasePath string) error { const disableForeignKeys = false DB = open(databasePath, disableForeignKeys) - WriteMu = &sync.Mutex{} if err := runCustomMigrations(); err != nil { return err @@ -97,7 +96,15 @@ func Close() error { WriteMu.Lock() defer WriteMu.Unlock() - return DB.Close() + if DB != nil { + if err := DB.Close(); err != nil { + return err + } + + DB = nil + } + + return nil } func open(databasePath string, disableForeignKeys bool) *sqlx.DB {