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(migrations): Clean up cache if this migration needed
LinkinStar2b83f21584f3
diff --git a/cmd/answer/command.go b/cmd/answer/command.goindex 5d026f77..1748f4b7 100644--- a/cmd/answer/command.go+++ b/cmd/answer/command.go@@ -96,7 +96,7 @@ To run answer, use:fmt.Println("read config failed: ", err.Error())return}- if err = migrations.Migrate(c.Data.Database); err != nil {+ if err = migrations.Migrate(c.Data.Database, c.Data.Cache); err != nil {fmt.Println("migrate failed: ", err.Error())return}diff --git a/internal/migrations/migrations.go b/internal/migrations/migrations.goindex 44aaec56..bbe0a958 100644--- a/internal/migrations/migrations.go+++ b/internal/migrations/migrations.go@@ -1,6 +1,7 @@package migrationsimport (+ "context""fmt""github.com/answerdev/answer/internal/base/data"@@ -14,11 +15,13 @@ const minDBVersion = 0 // answer 1.0.0type Migration interface {Description() stringMigrate(*xorm.Engine) error+ ShouldCleanCache() bool}type migration struct {- description string- migrate func(*xorm.Engine) error+ description string+ migrate func(*xorm.Engine) error+ shouldCleanCache bool}// Description returns the migration's description@@ -31,9 +34,14 @@ func (m *migration) Migrate(x *xorm.Engine) error {return m.migrate(x)}+// ShouldCleanCache should clean the cache+func (m *migration) ShouldCleanCache() bool {+ return m.shouldCleanCache+}+// NewMigration creates a new migration-func NewMigration(desc string, fn func(*xorm.Engine) error) Migration {- return &migration{description: desc, migrate: fn}+func NewMigration(desc string, fn func(*xorm.Engine) error, shouldCleanCache bool) Migration {+ return &migration{description: desc, migrate: fn, shouldCleanCache: shouldCleanCache}}// Use noopMigration when there is a migration that has been no-oped@@ -41,12 +49,12 @@ var noopMigration = func(_ *xorm.Engine) error { return nil }var migrations = []Migration{// 0->1- NewMigration("this is first version, no operation", noopMigration),- NewMigration("add user language", addUserLanguage),- NewMigration("add recommend and reserved tag fields", addTagRecommendedAndReserved),- NewMigration("add activity timeline", addActivityTimeline),- NewMigration("add user role", addRoleFeatures),- NewMigration("add theme and private mode", addThemeAndPrivateMode),+ NewMigration("this is first version, no operation", noopMigration, false),+ NewMigration("add user language", addUserLanguage, false),+ NewMigration("add recommend and reserved tag fields", addTagRecommendedAndReserved, false),+ NewMigration("add activity timeline", addActivityTimeline, false),+ NewMigration("add user role", addRoleFeatures, false),+ NewMigration("add theme and private mode", addThemeAndPrivateMode, true),}// GetCurrentDBVersion returns the current db version@@ -76,8 +84,12 @@ func ExpectedVersion() int64 {}// Migrate database to current version-func Migrate(dataConf *data.Database) error {- engine, err := data.NewDB(false, dataConf)+func Migrate(dbConf *data.Database, cacheConf *data.CacheConf) error {+ cache, cacheCleanup, err := data.NewCache(cacheConf)+ if err != nil {+ fmt.Println("new check failed:", err.Error())+ }+ engine, err := data.NewDB(false, dbConf)if err != nil {fmt.Println("new database failed: ", err.Error())return err@@ -98,6 +110,11 @@ func Migrate(dataConf *data.Database) error {fmt.Printf("[migrate] migrate to db version %d failed: %s\n", currentDBVersion+1, err.Error())return err}+ if migrationFunc.ShouldCleanCache() {+ if err := cache.Flush(context.Background()); err != nil {+ fmt.Printf("[migrate] flush cache failed: %s\n", err.Error())+ }+ }fmt.Printf("[migrate] migrate to db version %d success\n", currentDBVersion+1)if _, err := engine.Update(&entity.Version{ID: 1, VersionNumber: currentDBVersion + 1}); err != nil {fmt.Printf("[migrate] migrate to db version %d, update failed: %s", currentDBVersion+1, err.Error())@@ -105,5 +122,8 @@ func Migrate(dataConf *data.Database) error {}currentDBVersion++}+ if cache != nil {+ cacheCleanup()+ }return nil}