DB Migrations #67
17
pkg/db/db.go
17
pkg/db/db.go
|
@ -18,11 +18,9 @@ package db
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"code.vikunja.io/api/pkg/log"
|
"code.vikunja.io/api/pkg/log"
|
||||||
"encoding/gob"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/go-xorm/core"
|
"github.com/go-xorm/core"
|
||||||
"github.com/go-xorm/xorm"
|
"github.com/go-xorm/xorm"
|
||||||
xrc "github.com/go-xorm/xorm-redis-cache"
|
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
|
|
||||||
_ "github.com/go-sql-driver/mysql" // Because.
|
_ "github.com/go-sql-driver/mysql" // Because.
|
||||||
|
@ -55,21 +53,6 @@ func CreateDBEngine() (engine *xorm.Engine, err error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Cache
|
|
||||||
if viper.GetBool("cache.enabled") {
|
|
||||||
switch viper.GetString("cache.type") {
|
|
||||||
case "memory":
|
|
||||||
cacher := xorm.NewLRUCacher(xorm.NewMemoryStore(), viper.GetInt("cache.maxelementsize"))
|
|
||||||
engine.SetDefaultCacher(cacher)
|
|
||||||
case "redis":
|
|
||||||
cacher := xrc.NewRedisCacher(viper.GetString("redis.host"), viper.GetString("redis.password"), xrc.DEFAULT_EXPIRATION, engine.Logger())
|
|
||||||
engine.SetDefaultCacher(cacher)
|
|
||||||
gob.Register(GetTables())
|
|
||||||
default:
|
|
||||||
log.Log.Info("Did not find a valid cache type. Caching disabled. Please refer to the docs for poosible cache types.")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
engine.SetMapper(core.GonicMapper{})
|
engine.SetMapper(core.GonicMapper{})
|
||||||
|
|
||||||
engine.ShowSQL(viper.GetString("log.database") != "off")
|
engine.ShowSQL(viper.GetString("log.database") != "off")
|
||||||
|
|
|
@ -1,37 +0,0 @@
|
||||||
// Vikunja is a todo-list application to facilitate your life.
|
|
||||||
// Copyright 2019 Vikunja and contributors. All rights reserved.
|
|
||||||
//
|
|
||||||
// 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.
|
|
||||||
//
|
|
||||||
// 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.
|
|
||||||
//
|
|
||||||
// You should have received a copy of the GNU General Public License
|
|
||||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
||||||
|
|
||||||
package db
|
|
||||||
|
|
||||||
import "code.vikunja.io/api/pkg/models"
|
|
||||||
|
|
||||||
func GetTables() []interface{} {
|
|
||||||
return []interface{}{
|
|
||||||
&models.User{},
|
|
||||||
&models.List{},
|
|
||||||
&models.ListTask{},
|
|
||||||
&models.Team{},
|
|
||||||
&models.TeamMember{},
|
|
||||||
&models.TeamList{},
|
|
||||||
&models.TeamNamespace{},
|
|
||||||
&models.Namespace{},
|
|
||||||
&models.ListUser{},
|
|
||||||
&models.NamespaceUser{},
|
|
||||||
&models.ListTaskAssginee{},
|
|
||||||
&models.Label{},
|
|
||||||
&models.LabelTask{},
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -19,6 +19,7 @@ package migration
|
||||||
import (
|
import (
|
||||||
"code.vikunja.io/api/pkg/db"
|
"code.vikunja.io/api/pkg/db"
|
||||||
"code.vikunja.io/api/pkg/log"
|
"code.vikunja.io/api/pkg/log"
|
||||||
|
"code.vikunja.io/api/pkg/models"
|
||||||
"github.com/go-xorm/xorm"
|
"github.com/go-xorm/xorm"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
"sort"
|
"sort"
|
||||||
|
@ -136,6 +137,6 @@ func dropTableColum(x *xorm.Engine, tableName, col string) error {
|
||||||
|
|
||||||
func initSchema(tx *xorm.Engine) error {
|
func initSchema(tx *xorm.Engine) error {
|
||||||
return tx.Sync2(
|
return tx.Sync2(
|
||||||
db.GetTables(),
|
models.GetTables()...,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,8 +19,10 @@ package models
|
||||||
import (
|
import (
|
||||||
"code.vikunja.io/api/pkg/db"
|
"code.vikunja.io/api/pkg/db"
|
||||||
"code.vikunja.io/api/pkg/log"
|
"code.vikunja.io/api/pkg/log"
|
||||||
|
"encoding/gob"
|
||||||
_ "github.com/go-sql-driver/mysql" // Because.
|
_ "github.com/go-sql-driver/mysql" // Because.
|
||||||
"github.com/go-xorm/xorm"
|
"github.com/go-xorm/xorm"
|
||||||
|
xrc "github.com/go-xorm/xorm-redis-cache"
|
||||||
_ "github.com/mattn/go-sqlite3" // Because.
|
_ "github.com/mattn/go-sqlite3" // Because.
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
|
@ -29,6 +31,24 @@ var (
|
||||||
x *xorm.Engine
|
x *xorm.Engine
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func GetTables() []interface{} {
|
||||||
|
return []interface{}{
|
||||||
|
&User{},
|
||||||
|
&List{},
|
||||||
|
&ListTask{},
|
||||||
|
&Team{},
|
||||||
|
&TeamMember{},
|
||||||
|
&TeamList{},
|
||||||
|
&TeamNamespace{},
|
||||||
|
&Namespace{},
|
||||||
|
&ListUser{},
|
||||||
|
&NamespaceUser{},
|
||||||
|
&ListTaskAssginee{},
|
||||||
|
&Label{},
|
||||||
|
&LabelTask{},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// SetEngine sets the xorm.Engine
|
// SetEngine sets the xorm.Engine
|
||||||
func SetEngine() (err error) {
|
func SetEngine() (err error) {
|
||||||
x, err = db.CreateDBEngine()
|
x, err = db.CreateDBEngine()
|
||||||
|
@ -37,6 +57,22 @@ func SetEngine() (err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Cache
|
||||||
|
// We have to initialize the cache here to avoid import cycles
|
||||||
|
if viper.GetBool("cache.enabled") {
|
||||||
|
switch viper.GetString("cache.type") {
|
||||||
|
case "memory":
|
||||||
|
cacher := xorm.NewLRUCacher(xorm.NewMemoryStore(), viper.GetInt("cache.maxelementsize"))
|
||||||
|
x.SetDefaultCacher(cacher)
|
||||||
|
case "redis":
|
||||||
|
cacher := xrc.NewRedisCacher(viper.GetString("redis.host"), viper.GetString("redis.password"), xrc.DEFAULT_EXPIRATION, x.Logger())
|
||||||
|
x.SetDefaultCacher(cacher)
|
||||||
|
gob.Register(GetTables())
|
||||||
|
default:
|
||||||
|
log.Log.Info("Did not find a valid cache type. Caching disabled. Please refer to the docs for poosible cache types.")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -66,7 +66,7 @@ func createTestEngine(fixturesDir string) error {
|
||||||
x.SetMapper(core.GonicMapper{})
|
x.SetMapper(core.GonicMapper{})
|
||||||
|
|
||||||
// Sync dat shit
|
// Sync dat shit
|
||||||
if err := x.StoreEngine("InnoDB").Sync2(tables...); err != nil {
|
if err := x.StoreEngine("InnoDB").Sync2(GetTables()...); err != nil {
|
||||||
return fmt.Errorf("sync database struct error: %v", err)
|
return fmt.Errorf("sync database struct error: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user