2020-02-07 17:27:45 +01:00
|
|
|
// Vikunja is a to-do list application to facilitate your life.
|
2021-02-02 20:19:13 +01:00
|
|
|
// Copyright 2018-2021 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
|
2020-12-23 16:41:52 +01:00
|
|
|
// it under the terms of the GNU Affero General Public Licensee as published by
|
2019-12-04 20:39:56 +01:00
|
|
|
// 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
|
2020-12-23 16:41:52 +01:00
|
|
|
// GNU Affero General Public Licensee for more details.
|
2019-03-29 18:54:35 +01:00
|
|
|
//
|
2020-12-23 16:41:52 +01:00
|
|
|
// You should have received a copy of the GNU Affero General Public Licensee
|
2019-12-04 20:39:56 +01:00
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2019-03-29 18:54:35 +01:00
|
|
|
|
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
2019-10-16 22:52:29 +02:00
|
|
|
"encoding/gob"
|
2019-03-29 18:54:35 +01:00
|
|
|
"fmt"
|
2020-02-16 22:42:04 +01:00
|
|
|
"net/url"
|
2020-06-23 11:21:42 +02:00
|
|
|
"os"
|
2019-05-25 07:49:52 +02:00
|
|
|
"strconv"
|
2020-02-16 22:42:04 +01:00
|
|
|
"strings"
|
2019-05-25 07:49:52 +02:00
|
|
|
"time"
|
2020-10-11 22:10:03 +02:00
|
|
|
|
|
|
|
"code.vikunja.io/api/pkg/config"
|
|
|
|
"code.vikunja.io/api/pkg/log"
|
|
|
|
xrc "gitea.com/xorm/xorm-redis-cache"
|
2020-02-14 17:34:25 +01:00
|
|
|
"xorm.io/core"
|
|
|
|
"xorm.io/xorm"
|
2020-04-12 19:29:24 +02:00
|
|
|
"xorm.io/xorm/caches"
|
2020-12-23 16:32:28 +01:00
|
|
|
"xorm.io/xorm/schemas"
|
2019-10-16 22:52:29 +02:00
|
|
|
|
2019-03-29 18:54:35 +01:00
|
|
|
_ "github.com/go-sql-driver/mysql" // Because.
|
2020-02-16 22:42:04 +01:00
|
|
|
_ "github.com/lib/pq" // Because.
|
2019-03-29 18:54:35 +01:00
|
|
|
_ "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
|
2020-10-11 22:10:03 +02:00
|
|
|
switch config.DatabaseType.GetString() {
|
|
|
|
case "mysql":
|
2019-03-29 18:54:35 +01:00
|
|
|
engine, err = initMysqlEngine()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-10-11 22:10:03 +02:00
|
|
|
case "postgres":
|
2020-02-16 22:42:04 +01:00
|
|
|
engine, err = initPostgresEngine()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-10-11 22:10:03 +02:00
|
|
|
case "sqlite":
|
2019-03-29 18:54:35 +01:00
|
|
|
// Otherwise use sqlite
|
|
|
|
engine, err = initSqliteEngine()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-10-11 22:10:03 +02:00
|
|
|
default:
|
2020-06-27 19:04:01 +02:00
|
|
|
log.Fatalf("Unknown database type %s", config.DatabaseType.GetString())
|
2019-03-29 18:54:35 +01:00
|
|
|
}
|
|
|
|
|
2020-06-27 19:04:01 +02:00
|
|
|
engine.SetTZLocation(config.GetTimeZone()) // Vikunja's timezone
|
|
|
|
loc, err := time.LoadLocation("GMT") // The db data timezone
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Error parsing time zone: %s", err)
|
|
|
|
}
|
|
|
|
engine.SetTZDatabase(loc)
|
2019-03-29 18:54:35 +01:00
|
|
|
engine.SetMapper(core.GonicMapper{})
|
2020-04-19 09:27:28 +02:00
|
|
|
logger := log.NewXormLogger("")
|
2020-02-14 17:34:25 +01:00
|
|
|
engine.SetLogger(logger)
|
2019-03-29 18:54:35 +01:00
|
|
|
|
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":
|
2020-04-12 19:29:24 +02:00
|
|
|
cacher := caches.NewLRUCacher(caches.NewMemoryStore(), config.CacheMaxElementSize.GetInt())
|
2019-10-16 22:52:29 +02:00
|
|
|
engine.SetDefaultCacher(cacher)
|
|
|
|
case "redis":
|
2020-10-10 18:53:59 +02:00
|
|
|
cacher := xrc.NewRedisCacher(config.RedisHost.GetString(), config.RedisPassword.GetString(), xrc.DEFAULT_EXPIRATION, engine.Logger())
|
2019-10-16 22:52:29 +02:00
|
|
|
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(
|
2021-05-08 16:54:55 +02:00
|
|
|
"%s:%s@tcp(%s)/%s?charset=utf8mb4&parseTime=true&tls=%s",
|
2019-07-06 22:12:26 +02:00
|
|
|
config.DatabaseUser.GetString(),
|
|
|
|
config.DatabasePassword.GetString(),
|
|
|
|
config.DatabaseHost.GetString(),
|
2021-05-08 16:54:55 +02:00
|
|
|
config.DatabaseDatabase.GetString(),
|
|
|
|
config.DatabaseTLS.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
|
|
|
|
}
|
|
|
|
|
2020-02-16 22:42:04 +01:00
|
|
|
// parsePostgreSQLHostPort parses given input in various forms defined in
|
|
|
|
// https://www.postgresql.org/docs/current/static/libpq-connect.html#LIBPQ-CONNSTRING
|
|
|
|
// and returns proper host and port number.
|
|
|
|
func parsePostgreSQLHostPort(info string) (string, string) {
|
|
|
|
host, port := "127.0.0.1", "5432"
|
|
|
|
if strings.Contains(info, ":") && !strings.HasSuffix(info, "]") {
|
|
|
|
idx := strings.LastIndex(info, ":")
|
|
|
|
host = info[:idx]
|
|
|
|
port = info[idx+1:]
|
|
|
|
} else if len(info) > 0 {
|
|
|
|
host = info
|
|
|
|
}
|
|
|
|
return host, port
|
|
|
|
}
|
|
|
|
|
|
|
|
func initPostgresEngine() (engine *xorm.Engine, err error) {
|
|
|
|
host, port := parsePostgreSQLHostPort(config.DatabaseHost.GetString())
|
|
|
|
connStr := fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=%s",
|
|
|
|
host,
|
|
|
|
port,
|
|
|
|
url.PathEscape(config.DatabaseUser.GetString()),
|
|
|
|
url.PathEscape(config.DatabasePassword.GetString()),
|
|
|
|
config.DatabaseDatabase.GetString(),
|
|
|
|
config.DatabaseSslMode.GetString(),
|
|
|
|
)
|
|
|
|
|
|
|
|
engine, err = xorm.NewEngine("postgres", connStr)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
engine.SetMaxOpenConns(config.DatabaseMaxOpenConnections.GetInt())
|
|
|
|
engine.SetMaxIdleConns(config.DatabaseMaxIdleConnections.GetInt())
|
|
|
|
max, err := time.ParseDuration(strconv.Itoa(config.DatabaseMaxConnectionLifetime.GetInt()) + `ms`)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
engine.SetConnMaxLifetime(max)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-03-29 18:54:35 +01:00
|
|
|
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"
|
|
|
|
}
|
|
|
|
|
2020-06-23 11:21:42 +02:00
|
|
|
// Try opening the db file to return a better error message if that does not work
|
2020-06-27 16:34:53 +02:00
|
|
|
var exists = true
|
|
|
|
if _, err := os.Stat(path); err != nil {
|
|
|
|
exists = !os.IsNotExist(err)
|
|
|
|
}
|
2020-06-23 11:21:42 +02:00
|
|
|
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)
|
|
|
|
}
|
2020-06-27 16:34:53 +02:00
|
|
|
_ = 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
|
|
|
|
}
|
2020-06-23 11:21:42 +02:00
|
|
|
|
2019-03-29 18:54:35 +01:00
|
|
|
return xorm.NewEngine("sqlite3", path)
|
|
|
|
}
|
2020-06-21 17:30:48 +02:00
|
|
|
|
|
|
|
// WipeEverything wipes all tables and their data. Use with caution...
|
|
|
|
func WipeEverything() error {
|
|
|
|
|
|
|
|
tables, err := x.DBMetas()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, t := range tables {
|
|
|
|
if err := x.DropTables(t.Name); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2020-12-23 16:32:28 +01:00
|
|
|
|
|
|
|
// NewSession creates a new xorm session
|
|
|
|
func NewSession() *xorm.Session {
|
|
|
|
return x.NewSession()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Type returns the db type of the currently configured db
|
|
|
|
func Type() schemas.DBType {
|
|
|
|
return x.Dialect().URI().DBType
|
|
|
|
}
|