From ebaaaff4f1c3941741359f0a328d8dfdd3987eaf Mon Sep 17 00:00:00 2001 From: LinkinStar Date: Wed, 1 Feb 2023 17:19:27 +0800 Subject: [PATCH] fix(plugin): add plugin migrations --- internal/migrations/v7.go | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 internal/migrations/v7.go diff --git a/internal/migrations/v7.go b/internal/migrations/v7.go new file mode 100644 index 00000000..2faf6389 --- /dev/null +++ b/internal/migrations/v7.go @@ -0,0 +1,34 @@ +package migrations + +import ( + "fmt" + + "github.com/answerdev/answer/internal/entity" + "github.com/segmentfault/pacman/log" + "xorm.io/xorm" +) + +func addPlugin(x *xorm.Engine) error { + defaultConfigTable := []*entity.Config{ + {ID: 118, Key: "plugin.status", Value: `{}`}, + } + for _, c := range defaultConfigTable { + exist, err := x.Get(&entity.Config{ID: c.ID, Key: c.Key}) + 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}); 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 x.Sync(new(entity.PluginConfig)) +}