Fixed import cycle
continuous-integration/drone/push Build is failing Details

This commit is contained in:
kolaente 2019-03-27 07:55:33 +01:00
parent a91b881d58
commit 22e2a72fb9
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
5 changed files with 39 additions and 56 deletions

View File

@ -18,11 +18,9 @@ package db
import (
"code.vikunja.io/api/pkg/log"
"encoding/gob"
"fmt"
"github.com/go-xorm/core"
"github.com/go-xorm/xorm"
xrc "github.com/go-xorm/xorm-redis-cache"
"github.com/spf13/viper"
_ "github.com/go-sql-driver/mysql" // Because.
@ -55,21 +53,6 @@ func CreateDBEngine() (engine *xorm.Engine, err error) {
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.ShowSQL(viper.GetString("log.database") != "off")

View File

@ -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{},
}
}

View File

@ -19,6 +19,7 @@ package migration
import (
"code.vikunja.io/api/pkg/db"
"code.vikunja.io/api/pkg/log"
"code.vikunja.io/api/pkg/models"
"github.com/go-xorm/xorm"
"github.com/spf13/viper"
"sort"
@ -136,6 +137,6 @@ func dropTableColum(x *xorm.Engine, tableName, col string) error {
func initSchema(tx *xorm.Engine) error {
return tx.Sync2(
db.GetTables(),
models.GetTables()...,
)
}

View File

@ -19,8 +19,10 @@ package models
import (
"code.vikunja.io/api/pkg/db"
"code.vikunja.io/api/pkg/log"
"encoding/gob"
_ "github.com/go-sql-driver/mysql" // Because.
"github.com/go-xorm/xorm"
xrc "github.com/go-xorm/xorm-redis-cache"
_ "github.com/mattn/go-sqlite3" // Because.
"github.com/spf13/viper"
)
@ -29,6 +31,24 @@ var (
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
func SetEngine() (err error) {
x, err = db.CreateDBEngine()
@ -37,6 +57,22 @@ func SetEngine() (err error) {
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
}

View File

@ -66,7 +66,7 @@ func createTestEngine(fixturesDir string) error {
x.SetMapper(core.GonicMapper{})
// 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)
}