memos/store/migrator.go

62 lines
1.9 KiB
Go
Raw Normal View History

2024-02-21 23:43:18 +08:00
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
2024-02-21 23:45:12 +08:00
// nolint
2024-02-21 23:43:18 +08:00
json.Unmarshal([]byte(workspaceSetting.Value), &baseValue)
if workspaceSetting.Name == "allow-signup" {
2024-02-23 09:16:34 +08:00
workspaceGeneralSetting.DisallowSignup = !baseValue.(bool)
2024-02-21 23:43:18 +08:00
} 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.DeleteWorkspaceSetting(ctx, &DeleteWorkspaceSetting{
Name: workspaceSetting.Name,
}); err != nil {
return errors.Wrap(err, fmt.Sprintf("failed to delete workspace setting: %s", workspaceSetting.Name))
2024-02-21 23:43:18 +08:00
}
}
}
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
}