mirror of
https://github.com/usememos/memos.git
synced 2025-06-05 22:09:59 +02:00
chore: add workspace setting migrator
This commit is contained in:
@ -102,7 +102,7 @@ func (s *APIV1Service) GetSystemStatus(c echo.Context) error {
|
|||||||
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find system setting list").SetInternal(err)
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find system setting list").SetInternal(err)
|
||||||
}
|
}
|
||||||
for _, systemSetting := range systemSettingList {
|
for _, systemSetting := range systemSettingList {
|
||||||
if systemSetting.Name == SystemSettingServerIDName.String() || systemSetting.Name == SystemSettingSecretSessionName.String() || systemSetting.Name == SystemSettingTelegramBotTokenName.String() || systemSetting.Name == SystemSettingInstanceURLName.String() {
|
if systemSetting.Name == SystemSettingServerIDName.String() || systemSetting.Name == SystemSettingSecretSessionName.String() || systemSetting.Name == SystemSettingTelegramBotTokenName.String() {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -41,8 +41,6 @@ const (
|
|||||||
SystemSettingTelegramBotTokenName SystemSettingName = "telegram-bot-token"
|
SystemSettingTelegramBotTokenName SystemSettingName = "telegram-bot-token"
|
||||||
// SystemSettingMemoDisplayWithUpdatedTsName is the name of memo display with updated ts.
|
// SystemSettingMemoDisplayWithUpdatedTsName is the name of memo display with updated ts.
|
||||||
SystemSettingMemoDisplayWithUpdatedTsName SystemSettingName = "memo-display-with-updated-ts"
|
SystemSettingMemoDisplayWithUpdatedTsName SystemSettingName = "memo-display-with-updated-ts"
|
||||||
// SystemSettingInstanceURLName is the name of instance url setting.
|
|
||||||
SystemSettingInstanceURLName SystemSettingName = "instance-url"
|
|
||||||
)
|
)
|
||||||
const systemSettingUnmarshalError = `failed to unmarshal value from system setting "%v"`
|
const systemSettingUnmarshalError = `failed to unmarshal value from system setting "%v"`
|
||||||
|
|
||||||
@ -288,7 +286,6 @@ func (upsert UpsertSystemSettingRequest) Validate() error {
|
|||||||
if err := json.Unmarshal([]byte(upsert.Value), &value); err != nil {
|
if err := json.Unmarshal([]byte(upsert.Value), &value); err != nil {
|
||||||
return errors.Errorf(systemSettingUnmarshalError, settingName)
|
return errors.Errorf(systemSettingUnmarshalError, settingName)
|
||||||
}
|
}
|
||||||
case SystemSettingInstanceURLName:
|
|
||||||
default:
|
default:
|
||||||
return errors.New("invalid system setting name")
|
return errors.New("invalid system setting name")
|
||||||
}
|
}
|
||||||
|
@ -173,7 +173,7 @@ func (s *APIV2Service) doSignIn(ctx context.Context, user *store.User, expireTim
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *APIV2Service) SignUp(ctx context.Context, request *apiv2pb.SignUpRequest) (*apiv2pb.SignUpResponse, error) {
|
func (s *APIV2Service) SignUp(ctx context.Context, request *apiv2pb.SignUpRequest) (*apiv2pb.SignUpResponse, error) {
|
||||||
workspaceGeneralSetting, err := s.GetWorkspaceGeneralSetting(ctx)
|
workspaceGeneralSetting, err := s.Store.GetWorkspaceGeneralSetting(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, status.Errorf(codes.Internal, fmt.Sprintf("failed to get workspace setting, err: %s", err))
|
return nil, status.Errorf(codes.Internal, fmt.Sprintf("failed to get workspace setting, err: %s", err))
|
||||||
}
|
}
|
||||||
@ -251,7 +251,7 @@ func (s *APIV2Service) buildAccessTokenCookie(ctx context.Context, accessToken s
|
|||||||
} else {
|
} else {
|
||||||
attrs = append(attrs, "Expires="+expireTime.Format(time.RFC1123))
|
attrs = append(attrs, "Expires="+expireTime.Format(time.RFC1123))
|
||||||
}
|
}
|
||||||
workspaceGeneralSetting, err := s.GetWorkspaceGeneralSetting(ctx)
|
workspaceGeneralSetting, err := s.Store.GetWorkspaceGeneralSetting(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", errors.Wrap(err, "failed to get workspace setting")
|
return "", errors.Wrap(err, "failed to get workspace setting")
|
||||||
}
|
}
|
||||||
|
@ -4,10 +4,8 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
|
||||||
"google.golang.org/grpc/codes"
|
"google.golang.org/grpc/codes"
|
||||||
"google.golang.org/grpc/status"
|
"google.golang.org/grpc/status"
|
||||||
"google.golang.org/protobuf/encoding/protojson"
|
|
||||||
|
|
||||||
apiv2pb "github.com/usememos/memos/proto/gen/api/v2"
|
apiv2pb "github.com/usememos/memos/proto/gen/api/v2"
|
||||||
storepb "github.com/usememos/memos/proto/gen/store"
|
storepb "github.com/usememos/memos/proto/gen/store"
|
||||||
@ -51,22 +49,6 @@ func (s *APIV2Service) SetWorkspaceSetting(ctx context.Context, request *apiv2pb
|
|||||||
return &apiv2pb.SetWorkspaceSettingResponse{}, nil
|
return &apiv2pb.SetWorkspaceSettingResponse{}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *APIV2Service) GetWorkspaceGeneralSetting(ctx context.Context) (*storepb.WorkspaceGeneralSetting, error) {
|
|
||||||
workspaceSetting, err := s.Store.GetWorkspaceSetting(ctx, &store.FindWorkspaceSetting{
|
|
||||||
Name: storepb.WorkspaceSettingKey_WORKSPACE_SETTING_GENERAL.String(),
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return nil, errors.Wrap(err, "failed to get workspace setting")
|
|
||||||
}
|
|
||||||
workspaceGeneralSetting := &storepb.WorkspaceGeneralSetting{}
|
|
||||||
if workspaceSetting != nil {
|
|
||||||
if err := protojson.Unmarshal([]byte(workspaceSetting.Value), workspaceGeneralSetting); err != nil {
|
|
||||||
return nil, errors.Wrap(err, "failed to unmarshal workspace setting")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return workspaceGeneralSetting, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func convertWorkspaceSettingFromStore(setting *storepb.WorkspaceSetting) *apiv2pb.WorkspaceSetting {
|
func convertWorkspaceSettingFromStore(setting *storepb.WorkspaceSetting) *apiv2pb.WorkspaceSetting {
|
||||||
return &apiv2pb.WorkspaceSetting{
|
return &apiv2pb.WorkspaceSetting{
|
||||||
Name: fmt.Sprintf("%s%s", WorkspaceSettingNamePrefix, setting.Key.String()),
|
Name: fmt.Sprintf("%s%s", WorkspaceSettingNamePrefix, setting.Key.String()),
|
||||||
|
@ -61,6 +61,12 @@ var (
|
|||||||
}
|
}
|
||||||
|
|
||||||
storeInstance := store.New(dbDriver, profile)
|
storeInstance := store.New(dbDriver, profile)
|
||||||
|
if err := storeInstance.MigrateManually(ctx); err != nil {
|
||||||
|
cancel()
|
||||||
|
log.Error("failed to migrate manually", zap.Error(err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
s, err := server.NewServer(ctx, profile, storeInstance)
|
s, err := server.NewServer(ctx, profile, storeInstance)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
cancel()
|
cancel()
|
||||||
|
@ -13,7 +13,6 @@ import (
|
|||||||
"github.com/yourselfhosted/gomark/parser/tokenizer"
|
"github.com/yourselfhosted/gomark/parser/tokenizer"
|
||||||
"github.com/yourselfhosted/gomark/renderer"
|
"github.com/yourselfhosted/gomark/renderer"
|
||||||
|
|
||||||
apiv1 "github.com/usememos/memos/api/v1"
|
|
||||||
"github.com/usememos/memos/internal/util"
|
"github.com/usememos/memos/internal/util"
|
||||||
"github.com/usememos/memos/server/profile"
|
"github.com/usememos/memos/server/profile"
|
||||||
"github.com/usememos/memos/store"
|
"github.com/usememos/memos/store"
|
||||||
@ -81,13 +80,11 @@ func (s *FrontendService) registerRoutes(e *echo.Echo) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *FrontendService) registerFileRoutes(ctx context.Context, e *echo.Echo) {
|
func (s *FrontendService) registerFileRoutes(ctx context.Context, e *echo.Echo) {
|
||||||
instanceURLSetting, err := s.Store.GetWorkspaceSetting(ctx, &store.FindWorkspaceSetting{
|
workspaceGeneralSetting, err := s.Store.GetWorkspaceGeneralSetting(ctx)
|
||||||
Name: apiv1.SystemSettingInstanceURLName.String(),
|
if err != nil {
|
||||||
})
|
|
||||||
if err != nil || instanceURLSetting == nil {
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
instanceURL := instanceURLSetting.Value
|
instanceURL := workspaceGeneralSetting.GetInstanceUrl()
|
||||||
if instanceURL == "" {
|
if instanceURL == "" {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
58
store/migrator.go
Normal file
58
store/migrator.go
Normal file
@ -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
|
||||||
|
}
|
@ -26,6 +26,13 @@ func New(driver Driver, profile *profile.Profile) *Store {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *Store) MigrateManually(ctx context.Context) error {
|
||||||
|
if err := s.MigrateWorkspaceSetting(ctx); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (s *Store) Vacuum(ctx context.Context) error {
|
func (s *Store) Vacuum(ctx context.Context) error {
|
||||||
return s.driver.Vacuum(ctx)
|
return s.driver.Vacuum(ctx)
|
||||||
}
|
}
|
||||||
|
@ -99,3 +99,18 @@ func (s *Store) GetWorkspaceSettingV1(ctx context.Context, find *FindWorkspaceSe
|
|||||||
s.workspaceSettingV1Cache.Store(workspaceSetting.Key.String(), workspaceSetting)
|
s.workspaceSettingV1Cache.Store(workspaceSetting.Key.String(), workspaceSetting)
|
||||||
return workspaceSetting, nil
|
return workspaceSetting, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *Store) GetWorkspaceGeneralSetting(ctx context.Context) (*storepb.WorkspaceGeneralSetting, error) {
|
||||||
|
workspaceSetting, err := s.GetWorkspaceSettingV1(ctx, &FindWorkspaceSettingV1{
|
||||||
|
Key: storepb.WorkspaceSettingKey_WORKSPACE_SETTING_GENERAL,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.Wrap(err, "failed to get workspace setting")
|
||||||
|
}
|
||||||
|
|
||||||
|
workspaceGeneralSetting := &storepb.WorkspaceGeneralSetting{}
|
||||||
|
if workspaceSetting != nil {
|
||||||
|
workspaceGeneralSetting = workspaceSetting.GetGeneral()
|
||||||
|
}
|
||||||
|
return workspaceGeneralSetting, nil
|
||||||
|
}
|
||||||
|
@ -6,40 +6,10 @@ import (
|
|||||||
|
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
apiv1 "github.com/usememos/memos/api/v1"
|
|
||||||
storepb "github.com/usememos/memos/proto/gen/store"
|
storepb "github.com/usememos/memos/proto/gen/store"
|
||||||
"github.com/usememos/memos/store"
|
"github.com/usememos/memos/store"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestWorkspaceSettingStore(t *testing.T) {
|
|
||||||
ctx := context.Background()
|
|
||||||
ts := NewTestingStore(ctx, t)
|
|
||||||
_, err := ts.UpsertWorkspaceSetting(ctx, &store.WorkspaceSetting{
|
|
||||||
Name: apiv1.SystemSettingServerIDName.String(),
|
|
||||||
Value: "test_server_id",
|
|
||||||
})
|
|
||||||
require.NoError(t, err)
|
|
||||||
_, err = ts.UpsertWorkspaceSetting(ctx, &store.WorkspaceSetting{
|
|
||||||
Name: apiv1.SystemSettingSecretSessionName.String(),
|
|
||||||
Value: "test_secret_session_name",
|
|
||||||
})
|
|
||||||
require.NoError(t, err)
|
|
||||||
_, err = ts.UpsertWorkspaceSetting(ctx, &store.WorkspaceSetting{
|
|
||||||
Name: apiv1.SystemSettingAllowSignUpName.String(),
|
|
||||||
Value: "true",
|
|
||||||
})
|
|
||||||
require.NoError(t, err)
|
|
||||||
_, err = ts.UpsertWorkspaceSetting(ctx, &store.WorkspaceSetting{
|
|
||||||
Name: apiv1.SystemSettingLocalStoragePathName.String(),
|
|
||||||
Value: "/tmp/memos",
|
|
||||||
})
|
|
||||||
require.NoError(t, err)
|
|
||||||
list, err := ts.ListWorkspaceSettings(ctx, &store.FindWorkspaceSetting{})
|
|
||||||
require.NoError(t, err)
|
|
||||||
require.Equal(t, 4, len(list))
|
|
||||||
ts.Close()
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestWorkspaceSettingV1Store(t *testing.T) {
|
func TestWorkspaceSettingV1Store(t *testing.T) {
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
ts := NewTestingStore(ctx, t)
|
ts := NewTestingStore(ctx, t)
|
||||||
|
Reference in New Issue
Block a user