2020-02-07 17:27:45 +01:00
|
|
|
// Vikunja is a to-do list application to facilitate your life.
|
2020-01-09 18:33:22 +01:00
|
|
|
// Copyright 2018-2020 Vikunja and contributors. All rights reserved.
|
2019-03-29 18:54:35 +01:00
|
|
|
//
|
2019-12-04 20:39:56 +01:00
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
2019-03-29 18:54:35 +01:00
|
|
|
//
|
2019-12-04 20:39:56 +01:00
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
2019-03-29 18:54:35 +01:00
|
|
|
//
|
2019-12-04 20:39:56 +01:00
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2019-03-29 18:54:35 +01:00
|
|
|
|
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
|
|
|
"code.vikunja.io/api/pkg/config"
|
|
|
|
"code.vikunja.io/api/pkg/log"
|
2019-10-16 22:52:29 +02:00
|
|
|
"encoding/gob"
|
2019-03-29 18:54:35 +01:00
|
|
|
"fmt"
|
|
|
|
"github.com/go-xorm/core"
|
|
|
|
"github.com/go-xorm/xorm"
|
2019-05-25 07:49:52 +02:00
|
|
|
"strconv"
|
|
|
|
"time"
|
2019-03-29 18:54:35 +01:00
|
|
|
|
2019-10-16 22:52:29 +02:00
|
|
|
xrc "github.com/go-xorm/xorm-redis-cache"
|
|
|
|
|
2019-03-29 18:54:35 +01:00
|
|
|
_ "github.com/go-sql-driver/mysql" // Because.
|
|
|
|
_ "github.com/mattn/go-sqlite3" // Because.
|
|
|
|
)
|
|
|
|
|
2019-10-16 22:52:29 +02:00
|
|
|
// We only want one instance of the engine, so we can reate it once and reuse it
|
|
|
|
var x *xorm.Engine
|
|
|
|
|
2019-03-29 18:54:35 +01:00
|
|
|
// CreateDBEngine initializes a db engine from the config
|
|
|
|
func CreateDBEngine() (engine *xorm.Engine, err error) {
|
2019-10-16 22:52:29 +02:00
|
|
|
|
|
|
|
if x != nil {
|
|
|
|
return x, nil
|
|
|
|
}
|
|
|
|
|
2019-03-29 18:54:35 +01:00
|
|
|
// If the database type is not set, this likely means we need to initialize the config first
|
2019-07-06 22:12:26 +02:00
|
|
|
if config.DatabaseType.GetString() == "" {
|
2019-03-29 18:54:35 +01:00
|
|
|
config.InitConfig()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use Mysql if set
|
2019-07-06 22:12:26 +02:00
|
|
|
if config.DatabaseType.GetString() == "mysql" {
|
2019-03-29 18:54:35 +01:00
|
|
|
engine, err = initMysqlEngine()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Otherwise use sqlite
|
|
|
|
engine, err = initSqliteEngine()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
engine.SetMapper(core.GonicMapper{})
|
2019-07-06 22:12:26 +02:00
|
|
|
engine.ShowSQL(config.LogDatabase.GetString() != "off")
|
2019-03-29 18:54:35 +01:00
|
|
|
engine.SetLogger(xorm.NewSimpleLogger(log.GetLogWriter("database")))
|
|
|
|
|
2019-10-16 22:52:29 +02:00
|
|
|
// Cache
|
|
|
|
// We have to initialize the cache here to avoid import cycles
|
|
|
|
if config.CacheEnabled.GetBool() {
|
|
|
|
switch config.CacheType.GetString() {
|
|
|
|
case "memory":
|
|
|
|
cacher := xorm.NewLRUCacher(xorm.NewMemoryStore(), config.CacheMaxElementSize.GetInt())
|
|
|
|
engine.SetDefaultCacher(cacher)
|
|
|
|
case "redis":
|
|
|
|
cacher := xrc.NewRedisCacher(config.RedisEnabled.GetString(), config.RedisPassword.GetString(), xrc.DEFAULT_EXPIRATION, engine.Logger())
|
|
|
|
engine.SetDefaultCacher(cacher)
|
|
|
|
default:
|
|
|
|
log.Info("Did not find a valid cache type. Caching disabled. Please refer to the docs for poosible cache types.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
x = engine
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// RegisterTableStructsForCache registers tables in gob encoding for redis cache
|
|
|
|
func RegisterTableStructsForCache(val interface{}) {
|
|
|
|
gob.Register(val)
|
|
|
|
}
|
|
|
|
|
2019-03-29 18:54:35 +01:00
|
|
|
func initMysqlEngine() (engine *xorm.Engine, err error) {
|
2019-12-07 21:14:13 +01:00
|
|
|
// We're using utf8mb here instead of just utf8 because we want to use non-BMP characters.
|
|
|
|
// See https://stackoverflow.com/a/30074553/10924593 for more info.
|
2019-03-29 18:54:35 +01:00
|
|
|
connStr := fmt.Sprintf(
|
2019-12-07 21:14:13 +01:00
|
|
|
"%s:%s@tcp(%s)/%s?charset=utf8mb4&parseTime=true",
|
2019-07-06 22:12:26 +02:00
|
|
|
config.DatabaseUser.GetString(),
|
|
|
|
config.DatabasePassword.GetString(),
|
|
|
|
config.DatabaseHost.GetString(),
|
|
|
|
config.DatabaseDatabase.GetString())
|
2019-03-29 18:54:35 +01:00
|
|
|
engine, err = xorm.NewEngine("mysql", connStr)
|
2019-05-25 07:49:52 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2019-07-06 22:12:26 +02:00
|
|
|
engine.SetMaxOpenConns(config.DatabaseMaxOpenConnections.GetInt())
|
|
|
|
engine.SetMaxIdleConns(config.DatabaseMaxIdleConnections.GetInt())
|
|
|
|
max, err := time.ParseDuration(strconv.Itoa(config.DatabaseMaxConnectionLifetime.GetInt()) + `ms`)
|
2019-05-25 07:49:52 +02:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
engine.SetConnMaxLifetime(max)
|
2019-03-29 18:54:35 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func initSqliteEngine() (engine *xorm.Engine, err error) {
|
2019-07-06 22:12:26 +02:00
|
|
|
path := config.DatabasePath.GetString()
|
2019-03-29 18:54:35 +01:00
|
|
|
if path == "" {
|
|
|
|
path = "./db.db"
|
|
|
|
}
|
|
|
|
|
|
|
|
return xorm.NewEngine("sqlite3", path)
|
|
|
|
}
|