diff --git a/internal/migrations/migrations.go b/internal/migrations/migrations.go index 787c72f4..1e4dfc7e 100644 --- a/internal/migrations/migrations.go +++ b/internal/migrations/migrations.go @@ -58,6 +58,7 @@ var migrations = []Migration{ NewMigration("add new answer notification", addNewAnswerNotification, true), NewMigration("add user pin hide features", addRolePinAndHideFeatures, true), NewMigration("update accept answer rank", updateAcceptAnswerRank, true), + NewMigration("update user pin hide features", updateRolePinAndHideFeatures, true), } // GetCurrentDBVersion returns the current db version diff --git a/internal/migrations/v11.go b/internal/migrations/v11.go new file mode 100644 index 00000000..a45509a5 --- /dev/null +++ b/internal/migrations/v11.go @@ -0,0 +1,38 @@ +package migrations + +import ( + "fmt" + + "github.com/answerdev/answer/internal/entity" + "github.com/segmentfault/pacman/log" + "xorm.io/xorm" +) + +func updateRolePinAndHideFeatures(x *xorm.Engine) error { + + defaultConfigTable := []*entity.Config{ + {ID: 119, Key: "rank.question.pin", Value: `-1`}, + {ID: 120, Key: "rank.question.unpin", Value: `-1`}, + {ID: 121, Key: "rank.question.show", Value: `-1`}, + {ID: 122, Key: "rank.question.hide", Value: `-1`}, + } + for _, c := range defaultConfigTable { + exist, err := x.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, Key: c.Key, Value: c.Value}); 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 { + log.Errorf("insert %+v config failed: %s", c, err) + return fmt.Errorf("add config failed: %w", err) + } + } + + return nil +}