Skip to content

A Q&A platform software for teams at any scales. Whether it's a community forum, help center, or knowledge management platform, you can always count on Apache Answer.

feat: add data dir command function

LinkinStarc59889939b6b
diff --git a/cmd/answer/wire_gen.go b/cmd/answer/wire_gen.go
index cd7e01d6..9097d841 100644
--- a/cmd/answer/wire_gen.go
+++ b/cmd/answer/wire_gen.go
@@ -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
diff --git a/internal/base/data/data.go b/internal/base/data/data.go
index 40fb7cf1..ccceef6e 100644
--- a/internal/base/data/data.go
+++ b/internal/base/data/data.go
@@ -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
diff --git a/internal/repo/comment/comment_repo_test.go b/internal/repo/comment/comment_repo_test.go
index 8a24e02c..463713b9 100644
--- a/internal/repo/comment/comment_repo_test.go
+++ b/internal/repo/comment/comment_repo_test.go
@@ -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()
diff --git a/internal/repo/revision/revision_repo_test.go b/internal/repo/revision/revision_repo_test.go
deleted file mode 100644
index 766107e1..00000000
--- a/internal/repo/revision/revision_repo_test.go
+++ /dev/null
@@ -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)
-}
diff --git a/pkg/dir/dir.go b/pkg/dir/dir.go
index fd6ee306..2a4d3519 100644
--- a/pkg/dir/dir.go
+++ b/pkg/dir/dir.go
@@ -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
+}