feat: add data dir command function

This commit is contained in:
LinkinStar 2022-10-12 11:14:20 +08:00
parent 35195c8e09
commit b99150c081
5 changed files with 23 additions and 58 deletions

View File

@ -74,7 +74,10 @@ func initApplication(debug bool, serverConf *conf.Server, dbConf *data.Database,
return nil, nil, err
}
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)
if err != nil {
return nil, nil, err

View File

@ -9,6 +9,7 @@ import (
"github.com/segmentfault/pacman/log"
"xorm.io/core"
"xorm.io/xorm"
ormlog "xorm.io/xorm/log"
)
// Data data
@ -27,18 +28,20 @@ func NewData(db *xorm.Engine, cache cache.Cache) (*Data, func(), error) {
}
// 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)
if err != nil {
panic(err)
}
if err = engine.Ping(); err != nil {
panic(err)
return nil, err
}
if debug {
engine.ShowSQL(true)
} else {
engine.SetLogLevel(ormlog.LOG_ERR)
}
if err = engine.Ping(); err != nil {
return nil, err
}
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.SetColumnMapper(core.GonicMapper{})
return engine
return engine, nil
}
// NewCache new cache instance

View File

@ -20,7 +20,7 @@ var (
func init() {
s, _ := os.LookupEnv("TESTDATA-DB-CONNECTION")
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,
}), cache)
log = log.Getlog()

View File

@ -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)
}

View File

@ -17,3 +17,11 @@ func CreatePathIsNotExist(path string) (bool, error) {
}
return false, err
}
func CheckPathExist(path string) bool {
_, err := os.Stat(path)
if err == nil {
return true
}
return false
}