diff --git a/internal/migrations/migrations.go b/internal/migrations/migrations.go index 1e4dfc7e..522fbdb0 100644 --- a/internal/migrations/migrations.go +++ b/internal/migrations/migrations.go @@ -59,6 +59,7 @@ var migrations = []Migration{ NewMigration("add user pin hide features", addRolePinAndHideFeatures, true), NewMigration("update accept answer rank", updateAcceptAnswerRank, true), NewMigration("update user pin hide features", updateRolePinAndHideFeatures, true), + NewMigration("update question post time", updateQuestionPostTime, true), } // GetCurrentDBVersion returns the current db version diff --git a/internal/migrations/v12.go b/internal/migrations/v12.go new file mode 100644 index 00000000..34b6c0c4 --- /dev/null +++ b/internal/migrations/v12.go @@ -0,0 +1,33 @@ +package migrations + +import ( + "fmt" + + "github.com/answerdev/answer/internal/entity" + "github.com/segmentfault/pacman/log" + "xorm.io/xorm" +) + +func updateQuestionPostTime(x *xorm.Engine) error { + questionList := make([]entity.Question, 0) + err := x.Find(&questionList, &entity.Question{}) + if err != nil { + return fmt.Errorf("get questions failed: %w", err) + } + for _, item := range questionList { + if item.PostUpdateTime.IsZero() { + if !item.UpdatedAt.IsZero() { + item.PostUpdateTime = item.UpdatedAt + } else if !item.CreatedAt.IsZero() { + item.PostUpdateTime = item.CreatedAt + } + if _, err = x.Update(item, &entity.Question{ID: item.ID}); err != nil { + log.Errorf("update %+v config failed: %s", item, err) + return fmt.Errorf("update question failed: %w", err) + } + } + + } + + return nil +}