feat: add data dir command function
This commit is contained in:
parent
35195c8e09
commit
b99150c081
|
@ -74,7 +74,10 @@ func initApplication(debug bool, serverConf *conf.Server, dbConf *data.Database,
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
langController := controller.NewLangController(i18nTranslator)
|
langController := controller.NewLangController(i18nTranslator)
|
||||||
engine := data.NewDB(debug, dbConf)
|
engine, err := data.NewDB(debug, dbConf)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
cache, cleanup, err := data.NewCache(cacheConf)
|
cache, cleanup, err := data.NewCache(cacheConf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
|
|
|
@ -9,6 +9,7 @@ import (
|
||||||
"github.com/segmentfault/pacman/log"
|
"github.com/segmentfault/pacman/log"
|
||||||
"xorm.io/core"
|
"xorm.io/core"
|
||||||
"xorm.io/xorm"
|
"xorm.io/xorm"
|
||||||
|
ormlog "xorm.io/xorm/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Data data
|
// Data data
|
||||||
|
@ -27,18 +28,20 @@ func NewData(db *xorm.Engine, cache cache.Cache) (*Data, func(), error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewDB new database instance
|
// NewDB new database instance
|
||||||
func NewDB(debug bool, dataConf *Database) *xorm.Engine {
|
func NewDB(debug bool, dataConf *Database) (*xorm.Engine, error) {
|
||||||
engine, err := xorm.NewEngine("mysql", dataConf.Connection)
|
engine, err := xorm.NewEngine("mysql", dataConf.Connection)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
return nil, err
|
||||||
}
|
|
||||||
|
|
||||||
if err = engine.Ping(); err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if debug {
|
if debug {
|
||||||
engine.ShowSQL(true)
|
engine.ShowSQL(true)
|
||||||
|
} else {
|
||||||
|
engine.SetLogLevel(ormlog.LOG_ERR)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err = engine.Ping(); err != nil {
|
||||||
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if dataConf.MaxIdleConn > 0 {
|
if dataConf.MaxIdleConn > 0 {
|
||||||
|
@ -51,7 +54,7 @@ func NewDB(debug bool, dataConf *Database) *xorm.Engine {
|
||||||
engine.SetConnMaxLifetime(time.Duration(dataConf.ConnMaxLifeTime) * time.Second)
|
engine.SetConnMaxLifetime(time.Duration(dataConf.ConnMaxLifeTime) * time.Second)
|
||||||
}
|
}
|
||||||
engine.SetColumnMapper(core.GonicMapper{})
|
engine.SetColumnMapper(core.GonicMapper{})
|
||||||
return engine
|
return engine, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewCache new cache instance
|
// NewCache new cache instance
|
||||||
|
|
|
@ -20,7 +20,7 @@ var (
|
||||||
func init() {
|
func init() {
|
||||||
s, _ := os.LookupEnv("TESTDATA-DB-CONNECTION")
|
s, _ := os.LookupEnv("TESTDATA-DB-CONNECTION")
|
||||||
cache, _, _ := data.NewCache(log.Getlog(), &data.CacheConf{})
|
cache, _, _ := data.NewCache(log.Getlog(), &data.CacheConf{})
|
||||||
dataSource, _, _ = data.NewData(log.Getlog(), data.NewDB(true, &data.Database{
|
dataSource, _, _ = data.NewData(log.Getlog(), data.≈NewDB(true, &data.Database{
|
||||||
Connection: s,
|
Connection: s,
|
||||||
}), cache)
|
}), cache)
|
||||||
log = log.Getlog()
|
log = log.Getlog()
|
||||||
|
|
|
@ -1,49 +0,0 @@
|
||||||
package revision
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"fmt"
|
|
||||||
"os"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/segmentfault/answer/internal/base/data"
|
|
||||||
"github.com/segmentfault/answer/internal/entity"
|
|
||||||
repo2 "github.com/segmentfault/answer/internal/repo"
|
|
||||||
"github.com/segmentfault/answer/internal/repo/unique"
|
|
||||||
"github.com/segmentfault/pacman/log"
|
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
dataSource *data.Data
|
|
||||||
log log.log
|
|
||||||
)
|
|
||||||
|
|
||||||
func Init() {
|
|
||||||
s, _ := os.LookupEnv("TESTDATA-DB-CONNECTION")
|
|
||||||
fmt.Println(s)
|
|
||||||
cache, _, _ := data.NewCache(log.Getlog(), &data.CacheConf{})
|
|
||||||
dataSource, _, _ = data.NewData(log.Getlog(), data.NewDB(true, &data.Database{
|
|
||||||
Connection: s,
|
|
||||||
}), cache)
|
|
||||||
log = log.Getlog()
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestRevisionRepo_AddRevision(t *testing.T) {
|
|
||||||
Init()
|
|
||||||
ctx := context.Background()
|
|
||||||
uniqueIDRepo := unique.NewUniqueIDRepo(log, dataSource)
|
|
||||||
questionRepo := repo2.NewQuestionRepo(log, dataSource, uniqueIDRepo)
|
|
||||||
question, _, _ := questionRepo.GetQuestion(ctx, "10010000000000048")
|
|
||||||
repo := NewRevisionRepo(log, dataSource, uniqueIDRepo)
|
|
||||||
revision := &entity.Revision{
|
|
||||||
UserID: question.UserID,
|
|
||||||
ObjectType: 0,
|
|
||||||
ObjectID: question.ID,
|
|
||||||
Title: question.Title,
|
|
||||||
Content: question.OriginalText,
|
|
||||||
Status: 1,
|
|
||||||
}
|
|
||||||
err := repo.AddRevision(ctx, revision, true)
|
|
||||||
assert.NoError(t, err)
|
|
||||||
}
|
|
|
@ -17,3 +17,11 @@ func CreatePathIsNotExist(path string) (bool, error) {
|
||||||
}
|
}
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func CheckPathExist(path string) bool {
|
||||||
|
_, err := os.Stat(path)
|
||||||
|
if err == nil {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue