Support custom SQLite database file name

Ref T529
This commit is contained in:
Matt Baer 2018-12-08 13:34:29 -05:00
parent c6851fee50
commit ba3d6ae64c
3 changed files with 7 additions and 2 deletions

2
.gitignore vendored
View File

@ -4,4 +4,4 @@
build build
config.ini config.ini
writefreely.db *.db

6
app.go
View File

@ -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, 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) db.SetMaxOpenConns(50)
} else if app.cfg.Database.Type == "sqlite3" { } 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) db.SetMaxOpenConns(1)
} else { } else {
log.Error("Invalid database type '%s'. Only 'mysql' and 'sqlite3' are supported right now.", app.cfg.Database.Type) log.Error("Invalid database type '%s'. Only 'mysql' and 'sqlite3' are supported right now.", app.cfg.Database.Type)

View File

@ -22,6 +22,7 @@ type (
DatabaseCfg struct { DatabaseCfg struct {
Type string `ini:"type"` Type string `ini:"type"`
FileName string `ini:"filename"`
User string `ini:"username"` User string `ini:"username"`
Password string `ini:"password"` Password string `ini:"password"`
Database string `ini:"database"` Database string `ini:"database"`