memos/store/migrator.go

78 lines
2.6 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")
}
workspaceBasicSetting, err := s.GetWorkspaceBasicSetting(ctx)
if err != nil {
return errors.Wrap(err, "failed to get workspace basic setting")
}
2024-02-21 23:43:18 +08:00
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 == "server-id" {
workspaceBasicSetting.ServerId = workspaceSetting.Value
} else if workspaceSetting.Name == "secret-session" {
workspaceBasicSetting.SecretKey = workspaceSetting.Value
} else 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_BASIC,
Value: &storepb.WorkspaceSetting_BasicSetting{BasicSetting: workspaceBasicSetting},
}); err != nil {
return errors.Wrap(err, "failed to upsert workspace basic setting")
}
2024-02-21 23:43:18 +08:00
if _, err := s.UpsertWorkspaceSettingV1(ctx, &storepb.WorkspaceSetting{
Key: storepb.WorkspaceSettingKey_WORKSPACE_SETTING_GENERAL,
2024-04-10 20:05:17 +08:00
Value: &storepb.WorkspaceSetting_GeneralSetting{GeneralSetting: workspaceGeneralSetting},
2024-02-21 23:43:18 +08:00
}); err != nil {
return errors.Wrap(err, "failed to upsert workspace general setting")
}
return nil
}