From caee123f9d2c297a7b8cd92115a0ed609761f198 Mon Sep 17 00:00:00 2001 From: kolaente Date: Tue, 23 Jun 2020 11:21:42 +0200 Subject: [PATCH] Add better errors if the sqlite db file is not writable --- pkg/db/db.go | 8 ++++++++ pkg/migration/migration.go | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/pkg/db/db.go b/pkg/db/db.go index 457d87fb7..dd384e061 100644 --- a/pkg/db/db.go +++ b/pkg/db/db.go @@ -23,6 +23,7 @@ import ( "fmt" xrc "gitea.com/xorm/xorm-redis-cache" "net/url" + "os" "strconv" "strings" "time" @@ -166,6 +167,13 @@ func initSqliteEngine() (engine *xorm.Engine, err error) { path = "./db.db" } + // Try opening the db file to return a better error message if that does not work + 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. + return xorm.NewEngine("sqlite3", path) } diff --git a/pkg/migration/migration.go b/pkg/migration/migration.go index c6970bf8c..6d268f5aa 100644 --- a/pkg/migration/migration.go +++ b/pkg/migration/migration.go @@ -42,7 +42,7 @@ func initMigration(x *xorm.Engine) *xormigrate.Xormigrate { var err error x, err = db.CreateDBEngine() if err != nil { - log.Criticalf("Could not connect to db: %v", err.Error()) + log.Fatalf("Could not connect to db: %v", err.Error()) return nil } }