From e17cac854a83e0a93ac85f4729cb677207b65543 Mon Sep 17 00:00:00 2001 From: kolaente Date: Sat, 27 Jun 2020 16:34:53 +0200 Subject: [PATCH] Fix removing existing sqlite files --- pkg/db/db.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/pkg/db/db.go b/pkg/db/db.go index a3d6fffd..fedab1a2 100644 --- a/pkg/db/db.go +++ b/pkg/db/db.go @@ -168,12 +168,19 @@ func initSqliteEngine() (engine *xorm.Engine, err error) { } // Try opening the db file to return a better error message if that does not work + var exists = true + if _, err := os.Stat(path); err != nil { + exists = !os.IsNotExist(err) + } file, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE, 0) if err != nil { return nil, fmt.Errorf("could not open database file [uid=%d, gid=%d]: %s", os.Getuid(), os.Getgid(), err) } - _ = file.Close() // We directly close the file because we only want to check if it is writable. It will be reopened lazily later by xorm. - _ = os.Remove(path) // Remove the file to not prevent the db from creating another one + _ = file.Close() // We directly close the file because we only want to check if it is writable. It will be reopened lazily later by xorm. + + if !exists { + _ = os.Remove(path) // Remove the file to not prevent the db from creating another one + } return xorm.NewEngine("sqlite3", path) }