From ba3d6ae64c10310f6b2fbe3001372ddd42708a7d Mon Sep 17 00:00:00 2001 From: Matt Baer Date: Sat, 8 Dec 2018 13:34:29 -0500 Subject: [PATCH] Support custom SQLite database file name Ref T529 --- .gitignore | 2 +- app.go | 6 +++++- config/config.go | 1 + 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 6f45e83..f1e3331 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,4 @@ build config.ini -writefreely.db \ No newline at end of file +*.db diff --git a/app.go b/app.go index 3974009..feb5dd7 100644 --- a/app.go +++ b/app.go @@ -486,7 +486,11 @@ func connectToDatabase(app *app) { db, err = sql.Open(app.cfg.Database.Type, fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=true&loc=%s", app.cfg.Database.User, app.cfg.Database.Password, app.cfg.Database.Host, app.cfg.Database.Port, app.cfg.Database.Database, url.QueryEscape(time.Local.String()))) db.SetMaxOpenConns(50) } else if app.cfg.Database.Type == "sqlite3" { - db, err = sql.Open("sqlite3", "./writefreely.db?parseTime=true&cached=shared") + if app.cfg.Database.FileName == "" { + log.Error("SQLite database filename value in config.ini is empty.") + os.Exit(1) + } + db, err = sql.Open("sqlite3", app.cfg.Database.FileName+"?parseTime=true&cached=shared") db.SetMaxOpenConns(1) } else { log.Error("Invalid database type '%s'. Only 'mysql' and 'sqlite3' are supported right now.", app.cfg.Database.Type) diff --git a/config/config.go b/config/config.go index 438dc15..fad258d 100644 --- a/config/config.go +++ b/config/config.go @@ -22,6 +22,7 @@ type ( DatabaseCfg struct { Type string `ini:"type"` + FileName string `ini:"filename"` User string `ini:"username"` Password string `ini:"password"` Database string `ini:"database"`