mirror of https://github.com/usememos/memos
chore: add workspace setting migrator
parent
4378816e44
commit
51d58d3982
@ -0,0 +1,58 @@
|
|||||||
|
package store
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/pkg/errors"
|
||||||
|
|
||||||
|
storepb "github.com/usememos/memos/proto/gen/store"
|
||||||
|
)
|
||||||
|
|
||||||
|
// MigrateWorkspaceSetting migrates workspace setting from v1 to v2.
|
||||||
|
func (s *Store) MigrateWorkspaceSetting(ctx context.Context) error {
|
||||||
|
workspaceSettings, err := s.ListWorkspaceSettings(ctx, &FindWorkspaceSetting{})
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrap(err, "failed to list workspace settings")
|
||||||
|
}
|
||||||
|
|
||||||
|
workspaceGeneralSetting, err := s.GetWorkspaceGeneralSetting(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrap(err, "failed to get workspace general setting")
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, workspaceSetting := range workspaceSettings {
|
||||||
|
matched := true
|
||||||
|
var baseValue any
|
||||||
|
json.Unmarshal([]byte(workspaceSetting.Value), &baseValue)
|
||||||
|
if workspaceSetting.Name == "allow-signup" {
|
||||||
|
workspaceGeneralSetting.DisallowSignup = baseValue.(bool)
|
||||||
|
} else if workspaceSetting.Name == "disable-password-login" {
|
||||||
|
workspaceGeneralSetting.DisallowPasswordLogin = baseValue.(bool)
|
||||||
|
} else if workspaceSetting.Name == "additional-style" {
|
||||||
|
workspaceGeneralSetting.AdditionalStyle = baseValue.(string)
|
||||||
|
} else if workspaceSetting.Name == "additional-script" {
|
||||||
|
workspaceGeneralSetting.AdditionalScript = baseValue.(string)
|
||||||
|
} else if workspaceSetting.Name == "instance-url" {
|
||||||
|
workspaceGeneralSetting.InstanceUrl = workspaceSetting.Value
|
||||||
|
} else {
|
||||||
|
matched = false
|
||||||
|
}
|
||||||
|
|
||||||
|
if matched {
|
||||||
|
if _, err := s.driver.GetDB().ExecContext(ctx, fmt.Sprintf("DELETE FROM system_setting WHERE name = '%s'", workspaceSetting.Name)); err != nil {
|
||||||
|
return errors.Wrap(err, "failed to delete workspace setting")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := s.UpsertWorkspaceSettingV1(ctx, &storepb.WorkspaceSetting{
|
||||||
|
Key: storepb.WorkspaceSettingKey_WORKSPACE_SETTING_GENERAL,
|
||||||
|
Value: &storepb.WorkspaceSetting_General{General: workspaceGeneralSetting},
|
||||||
|
}); err != nil {
|
||||||
|
return errors.Wrap(err, "failed to upsert workspace general setting")
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
Loading…
Reference in New Issue