Skip to content

Commit

Permalink
Merge branch 'feat/1.1.1/state' into test
Browse files Browse the repository at this point in the history
  • Loading branch information
LinkinStars committed Jul 20, 2023
2 parents 43f7d64 + a3b033b commit 5774446
Show file tree
Hide file tree
Showing 29 changed files with 501 additions and 283 deletions.
3 changes: 1 addition & 2 deletions internal/controller/search_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,10 @@ func (sc *SearchController) Search(ctx *gin.Context) {
}
dto.UserID = middleware.GetLoginUserIDFromContext(ctx)

resp, total, extra, err := sc.searchService.Search(ctx, &dto)
resp, total, err := sc.searchService.Search(ctx, &dto)

handler.HandleResponse(ctx, err, schema.SearchListResp{
Total: total,
SearchResp: resp,
Extra: extra,
})
}
18 changes: 11 additions & 7 deletions internal/migrations/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ const minDBVersion = 0
type Migration interface {
Version() string
Description() string
Migrate(*xorm.Engine) error
Migrate(ctx context.Context, x *xorm.Engine) error
ShouldCleanCache() bool
}

type migration struct {
version string
description string
migrate func(*xorm.Engine) error
migrate func(ctx context.Context, x *xorm.Engine) error
shouldCleanCache bool
}

Expand All @@ -37,8 +37,8 @@ func (m *migration) Description() string {
}

// Migrate executes the migration
func (m *migration) Migrate(x *xorm.Engine) error {
return m.migrate(x)
func (m *migration) Migrate(ctx context.Context, x *xorm.Engine) error {
return m.migrate(ctx, x)
}

// ShouldCleanCache should clean the cache
Expand All @@ -47,12 +47,12 @@ func (m *migration) ShouldCleanCache() bool {
}

// NewMigration creates a new migration
func NewMigration(version, desc string, fn func(*xorm.Engine) error, shouldCleanCache bool) Migration {
func NewMigration(version, desc string, fn func(ctx context.Context, x *xorm.Engine) error, shouldCleanCache bool) Migration {
return &migration{version: version, description: desc, migrate: fn, shouldCleanCache: shouldCleanCache}
}

// Use noopMigration when there is a migration that has been no-oped
var noopMigration = func(_ *xorm.Engine) error { return nil }
var noopMigration = func(_ context.Context, _ *xorm.Engine) error { return nil }

var migrations = []Migration{
// 0->1
Expand All @@ -72,6 +72,10 @@ var migrations = []Migration{
NewMigration("v1.1.0", "add gravatar base url", updateCount, true),
}

func GetMigrations() []Migration {
return migrations
}

// GetCurrentDBVersion returns the current db version
func GetCurrentDBVersion(engine *xorm.Engine) (int64, error) {
if err := engine.Sync(new(entity.Version)); err != nil {
Expand Down Expand Up @@ -130,7 +134,7 @@ func Migrate(dbConf *data.Database, cacheConf *data.CacheConf, upgradeToSpecific
currentDBVersion, currentDBVersion+1, expectedVersion)
migrationFunc := migrations[currentDBVersion]
fmt.Printf("[migrate] try to migrate Answer version %s, description: %s\n", migrationFunc.Version(), migrationFunc.Description())
if err := migrationFunc.Migrate(engine); err != nil {
if err := migrationFunc.Migrate(context.Background(), engine); err != nil {
fmt.Printf("[migrate] migrate to db version %d failed: %s\n", currentDBVersion+1, err.Error())
return err
}
Expand Down
5 changes: 3 additions & 2 deletions internal/migrations/v1.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package migrations

import (
"context"
"xorm.io/xorm"
)

func addUserLanguage(x *xorm.Engine) error {
func addUserLanguage(ctx context.Context, x *xorm.Engine) error {
type User struct {
ID string `xorm:"not null pk autoincr BIGINT(20) id"`
Username string `xorm:"not null default '' VARCHAR(50) UNIQUE username"`
Language string `xorm:"not null default '' VARCHAR(100) language"`
}
return x.Sync(new(User))
return x.Context(ctx).Sync(new(User))
}
13 changes: 7 additions & 6 deletions internal/migrations/v10.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package migrations

import (
"context"
"encoding/json"
"fmt"

Expand All @@ -11,11 +12,11 @@ import (
"xorm.io/xorm"
)

func addLoginLimitations(x *xorm.Engine) error {
func addLoginLimitations(ctx context.Context, x *xorm.Engine) error {
loginSiteInfo := &entity.SiteInfo{
Type: constant.SiteTypeLogin,
}
exist, err := x.Get(loginSiteInfo)
exist, err := x.Context(ctx).Get(loginSiteInfo)
if err != nil {
return fmt.Errorf("get config failed: %w", err)
}
Expand All @@ -26,7 +27,7 @@ func addLoginLimitations(x *xorm.Engine) error {
content.AllowEmailDomains = make([]string, 0)
data, _ := json.Marshal(content)
loginSiteInfo.Content = string(data)
_, err = x.ID(loginSiteInfo.ID).Cols("content").Update(loginSiteInfo)
_, err = x.Context(ctx).ID(loginSiteInfo.ID).Cols("content").Update(loginSiteInfo)
if err != nil {
return fmt.Errorf("update site info failed: %w", err)
}
Expand All @@ -35,7 +36,7 @@ func addLoginLimitations(x *xorm.Engine) error {
interfaceSiteInfo := &entity.SiteInfo{
Type: constant.SiteTypeInterface,
}
exist, err = x.Get(interfaceSiteInfo)
exist, err = x.Context(ctx).Get(interfaceSiteInfo)
if err != nil {
return fmt.Errorf("get config failed: %w", err)
}
Expand All @@ -52,7 +53,7 @@ func addLoginLimitations(x *xorm.Engine) error {
}
data, _ := json.Marshal(siteUsers)

exist, err = x.Get(&entity.SiteInfo{Type: constant.SiteTypeUsers})
exist, err = x.Context(ctx).Get(&entity.SiteInfo{Type: constant.SiteTypeUsers})
if err != nil {
return fmt.Errorf("get config failed: %w", err)
}
Expand All @@ -62,7 +63,7 @@ func addLoginLimitations(x *xorm.Engine) error {
Content: string(data),
Status: 1,
}
_, err = x.InsertOne(usersSiteInfo)
_, err = x.Context(ctx).Insert(usersSiteInfo)
if err != nil {
return fmt.Errorf("insert site info failed: %w", err)
}
Expand Down
10 changes: 5 additions & 5 deletions internal/migrations/v11.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package migrations

import (
"context"
"fmt"

"github.com/answerdev/answer/internal/entity"
"github.com/segmentfault/pacman/log"
"xorm.io/xorm"
)

func updateRolePinAndHideFeatures(x *xorm.Engine) error {

func updateRolePinAndHideFeatures(ctx context.Context, x *xorm.Engine) error {
defaultConfigTable := []*entity.Config{
{ID: 119, Key: "question.pin", Value: `0`},
{ID: 120, Key: "question.unpin", Value: `0`},
Expand All @@ -21,18 +21,18 @@ func updateRolePinAndHideFeatures(x *xorm.Engine) error {
{ID: 126, Key: "rank.question.hide", Value: `-1`},
}
for _, c := range defaultConfigTable {
exist, err := x.Get(&entity.Config{ID: c.ID})
exist, err := x.Context(ctx).Get(&entity.Config{ID: c.ID})
if err != nil {
return fmt.Errorf("get config failed: %w", err)
}
if exist {
if _, err = x.Update(c, &entity.Config{ID: c.ID}); err != nil {
if _, err = x.Context(ctx).Update(c, &entity.Config{ID: c.ID}); err != nil {
log.Errorf("update %+v config failed: %s", c, err)
return fmt.Errorf("update config failed: %w", err)
}
continue
}
if _, err = x.Insert(&entity.Config{ID: c.ID, Key: c.Key, Value: c.Value}); err != nil {
if _, err = x.Context(ctx).Insert(&entity.Config{ID: c.ID, Key: c.Key, Value: c.Value}); err != nil {
log.Errorf("insert %+v config failed: %s", c, err)
return fmt.Errorf("add config failed: %w", err)
}
Expand Down
7 changes: 4 additions & 3 deletions internal/migrations/v12.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package migrations

import (
"context"
"fmt"
"time"

Expand Down Expand Up @@ -37,9 +38,9 @@ func (QuestionPostTime) TableName() string {
return "question"
}

func updateQuestionPostTime(x *xorm.Engine) error {
func updateQuestionPostTime(ctx context.Context, x *xorm.Engine) error {
questionList := make([]QuestionPostTime, 0)
err := x.Find(&questionList, &entity.Question{})
err := x.Context(ctx).Find(&questionList, &entity.Question{})
if err != nil {
return fmt.Errorf("get questions failed: %w", err)
}
Expand All @@ -50,7 +51,7 @@ func updateQuestionPostTime(x *xorm.Engine) error {
} else if !item.CreatedAt.IsZero() {
item.PostUpdateTime = item.CreatedAt
}
if _, err = x.Update(item, &QuestionPostTime{ID: item.ID}); err != nil {
if _, err = x.Context(ctx).Update(item, &QuestionPostTime{ID: item.ID}); err != nil {
log.Errorf("update %+v config failed: %s", item, err)
return fmt.Errorf("update question failed: %w", err)
}
Expand Down
Loading

0 comments on commit 5774446

Please sign in to comment.