From 51d58d39822718caf90e8ee276eee6b021714f01 Mon Sep 17 00:00:00 2001 From: Steven Date: Wed, 21 Feb 2024 23:43:18 +0800 Subject: [PATCH] chore: add workspace setting migrator --- api/v1/system.go | 2 +- api/v1/system_setting.go | 3 -- api/v2/auth_service.go | 4 +- api/v2/workspace_setting_service.go | 18 --------- bin/memos/main.go | 6 +++ server/frontend/frontend.go | 9 ++--- store/migrator.go | 58 ++++++++++++++++++++++++++++ store/store.go | 7 ++++ store/workspace_setting.go | 15 +++++++ test/store/workspace_setting_test.go | 30 -------------- 10 files changed, 92 insertions(+), 60 deletions(-) create mode 100644 store/migrator.go diff --git a/api/v1/system.go b/api/v1/system.go index d3e0984ff..10bc642fe 100644 --- a/api/v1/system.go +++ b/api/v1/system.go @@ -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) } 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 } diff --git a/api/v1/system_setting.go b/api/v1/system_setting.go index 1733b2402..951b8e325 100644 --- a/api/v1/system_setting.go +++ b/api/v1/system_setting.go @@ -41,8 +41,6 @@ const ( SystemSettingTelegramBotTokenName SystemSettingName = "telegram-bot-token" // SystemSettingMemoDisplayWithUpdatedTsName is the name of 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"` @@ -288,7 +286,6 @@ func (upsert UpsertSystemSettingRequest) Validate() error { if err := json.Unmarshal([]byte(upsert.Value), &value); err != nil { return errors.Errorf(systemSettingUnmarshalError, settingName) } - case SystemSettingInstanceURLName: default: return errors.New("invalid system setting name") } diff --git a/api/v2/auth_service.go b/api/v2/auth_service.go index 4a680c426..ec2d4052a 100644 --- a/api/v2/auth_service.go +++ b/api/v2/auth_service.go @@ -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) { - workspaceGeneralSetting, err := s.GetWorkspaceGeneralSetting(ctx) + workspaceGeneralSetting, err := s.Store.GetWorkspaceGeneralSetting(ctx) if err != nil { 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 { attrs = append(attrs, "Expires="+expireTime.Format(time.RFC1123)) } - workspaceGeneralSetting, err := s.GetWorkspaceGeneralSetting(ctx) + workspaceGeneralSetting, err := s.Store.GetWorkspaceGeneralSetting(ctx) if err != nil { return "", errors.Wrap(err, "failed to get workspace setting") } diff --git a/api/v2/workspace_setting_service.go b/api/v2/workspace_setting_service.go index 94dbd578c..8b3941997 100644 --- a/api/v2/workspace_setting_service.go +++ b/api/v2/workspace_setting_service.go @@ -4,10 +4,8 @@ import ( "context" "fmt" - "github.com/pkg/errors" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" - "google.golang.org/protobuf/encoding/protojson" apiv2pb "github.com/usememos/memos/proto/gen/api/v2" 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 } -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 { return &apiv2pb.WorkspaceSetting{ Name: fmt.Sprintf("%s%s", WorkspaceSettingNamePrefix, setting.Key.String()), diff --git a/bin/memos/main.go b/bin/memos/main.go index d84e9394b..1af88838b 100644 --- a/bin/memos/main.go +++ b/bin/memos/main.go @@ -61,6 +61,12 @@ var ( } 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) if err != nil { cancel() diff --git a/server/frontend/frontend.go b/server/frontend/frontend.go index b4a7aff49..02c56860d 100644 --- a/server/frontend/frontend.go +++ b/server/frontend/frontend.go @@ -13,7 +13,6 @@ import ( "github.com/yourselfhosted/gomark/parser/tokenizer" "github.com/yourselfhosted/gomark/renderer" - apiv1 "github.com/usememos/memos/api/v1" "github.com/usememos/memos/internal/util" "github.com/usememos/memos/server/profile" "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) { - instanceURLSetting, err := s.Store.GetWorkspaceSetting(ctx, &store.FindWorkspaceSetting{ - Name: apiv1.SystemSettingInstanceURLName.String(), - }) - if err != nil || instanceURLSetting == nil { + workspaceGeneralSetting, err := s.Store.GetWorkspaceGeneralSetting(ctx) + if err != nil { return } - instanceURL := instanceURLSetting.Value + instanceURL := workspaceGeneralSetting.GetInstanceUrl() if instanceURL == "" { return } diff --git a/store/migrator.go b/store/migrator.go new file mode 100644 index 000000000..2186598f7 --- /dev/null +++ b/store/migrator.go @@ -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 +} diff --git a/store/store.go b/store/store.go index 9c72d33a8..5ee65604b 100644 --- a/store/store.go +++ b/store/store.go @@ -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 { return s.driver.Vacuum(ctx) } diff --git a/store/workspace_setting.go b/store/workspace_setting.go index 4e17ab0ac..39b717680 100644 --- a/store/workspace_setting.go +++ b/store/workspace_setting.go @@ -99,3 +99,18 @@ func (s *Store) GetWorkspaceSettingV1(ctx context.Context, find *FindWorkspaceSe s.workspaceSettingV1Cache.Store(workspaceSetting.Key.String(), workspaceSetting) 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 +} diff --git a/test/store/workspace_setting_test.go b/test/store/workspace_setting_test.go index 86e8cfcd9..d791c61d0 100644 --- a/test/store/workspace_setting_test.go +++ b/test/store/workspace_setting_test.go @@ -6,40 +6,10 @@ import ( "github.com/stretchr/testify/require" - apiv1 "github.com/usememos/memos/api/v1" storepb "github.com/usememos/memos/proto/gen/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) { ctx := context.Background() ts := NewTestingStore(ctx, t)