Skip to content

Commit

Permalink
chore: tweak store methods name
Browse files Browse the repository at this point in the history
  • Loading branch information
boojack committed Apr 17, 2024
1 parent 00d25b1 commit 14d4cfd
Show file tree
Hide file tree
Showing 17 changed files with 68 additions and 198 deletions.
2 changes: 1 addition & 1 deletion internal/jobs/presign_link.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func findObjectStorage(ctx context.Context, dataStore *store.Store) (*s3.Client,
if workspaceStorageSetting.StorageType != storepb.WorkspaceStorageSetting_STORAGE_TYPE_EXTERNAL || workspaceStorageSetting.ActivedExternalStorageId == nil {
return nil, nil
}
storage, err := dataStore.GetStorageV1(ctx, &store.FindStorage{ID: workspaceStorageSetting.ActivedExternalStorageId})
storage, err := dataStore.GetStorage(ctx, &store.FindStorage{ID: workspaceStorageSetting.ActivedExternalStorageId})
if err != nil {
return nil, errors.Wrap(err, "Failed to find storage")
}
Expand Down
2 changes: 1 addition & 1 deletion server/route/api/v2/auth_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (s *APIV2Service) SignIn(ctx context.Context, request *apiv2pb.SignInReques
}

func (s *APIV2Service) SignInWithSSO(ctx context.Context, request *apiv2pb.SignInWithSSORequest) (*apiv2pb.SignInWithSSOResponse, error) {
identityProvider, err := s.Store.GetIdentityProviderV1(ctx, &store.FindIdentityProvider{
identityProvider, err := s.Store.GetIdentityProvider(ctx, &store.FindIdentityProvider{
ID: &request.IdpId,
})
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions server/route/api/v2/idp_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func (s *APIV2Service) CreateIdentityProvider(ctx context.Context, request *apiv
return nil, status.Errorf(codes.PermissionDenied, "permission denied")
}

identityProvider, err := s.Store.CreateIdentityProviderV1(ctx, convertIdentityProviderToStore(request.IdentityProvider))
identityProvider, err := s.Store.CreateIdentityProvider(ctx, convertIdentityProviderToStore(request.IdentityProvider))
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to create identity provider, error: %+v", err)
}
Expand All @@ -31,7 +31,7 @@ func (s *APIV2Service) CreateIdentityProvider(ctx context.Context, request *apiv
}

func (s *APIV2Service) ListIdentityProviders(ctx context.Context, _ *apiv2pb.ListIdentityProvidersRequest) (*apiv2pb.ListIdentityProvidersResponse, error) {
identityProviders, err := s.Store.ListIdentityProvidersV1(ctx, &store.FindIdentityProvider{})
identityProviders, err := s.Store.ListIdentityProviders(ctx, &store.FindIdentityProvider{})
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to list identity providers, error: %+v", err)
}
Expand All @@ -50,7 +50,7 @@ func (s *APIV2Service) GetIdentityProvider(ctx context.Context, request *apiv2pb
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "invalid identity provider name: %v", err)
}
identityProvider, err := s.Store.GetIdentityProviderV1(ctx, &store.FindIdentityProvider{
identityProvider, err := s.Store.GetIdentityProvider(ctx, &store.FindIdentityProvider{
ID: &id,
})
if err != nil {
Expand Down Expand Up @@ -86,7 +86,7 @@ func (s *APIV2Service) UpdateIdentityProvider(ctx context.Context, request *apiv
}
}

identityProvider, err := s.Store.UpdateIdentityProviderV1(ctx, update)
identityProvider, err := s.Store.UpdateIdentityProvider(ctx, update)
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to update identity provider, error: %+v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion server/route/api/v2/resource_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ func SaveResourceBlob(ctx context.Context, s *store.Store, create *store.Resourc
if workspaceStorageSetting.ActivedExternalStorageId == nil {
return errors.Errorf("No actived external storage found")
}
storage, err := s.GetStorageV1(ctx, &store.FindStorage{ID: workspaceStorageSetting.ActivedExternalStorageId})
storage, err := s.GetStorage(ctx, &store.FindStorage{ID: workspaceStorageSetting.ActivedExternalStorageId})
if err != nil {
return errors.Wrap(err, "Failed to find actived external storage")
}
Expand Down
8 changes: 4 additions & 4 deletions server/route/api/v2/storage_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func (s *APIV2Service) CreateStorage(ctx context.Context, request *apiv2pb.Creat
return nil, status.Errorf(codes.PermissionDenied, "permission denied")
}

storage, err := s.Store.CreateStorageV1(ctx, convertStorageToStore(request.Storage))
storage, err := s.Store.CreateStorage(ctx, convertStorageToStore(request.Storage))
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to create storage, error: %+v", err)
}
Expand All @@ -30,7 +30,7 @@ func (s *APIV2Service) CreateStorage(ctx context.Context, request *apiv2pb.Creat
}

func (s *APIV2Service) ListStorages(ctx context.Context, _ *apiv2pb.ListStoragesRequest) (*apiv2pb.ListStoragesResponse, error) {
storages, err := s.Store.ListStoragesV1(ctx, &store.FindStorage{})
storages, err := s.Store.ListStorages(ctx, &store.FindStorage{})
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to list storages, error: %+v", err)
}
Expand All @@ -45,7 +45,7 @@ func (s *APIV2Service) ListStorages(ctx context.Context, _ *apiv2pb.ListStorages
}

func (s *APIV2Service) GetStorage(ctx context.Context, request *apiv2pb.GetStorageRequest) (*apiv2pb.GetStorageResponse, error) {
storage, err := s.Store.GetStorageV1(ctx, &store.FindStorage{
storage, err := s.Store.GetStorage(ctx, &store.FindStorage{
ID: &request.Id,
})
if err != nil {
Expand Down Expand Up @@ -77,7 +77,7 @@ func (s *APIV2Service) UpdateStorage(ctx context.Context, request *apiv2pb.Updat
}
}

storage, err := s.Store.UpdateStorageV1(ctx, update)
storage, err := s.Store.UpdateStorage(ctx, update)
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to update storage, error: %+v", err)
}
Expand Down
6 changes: 3 additions & 3 deletions server/route/api/v2/workspace_setting_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

func (s *APIV2Service) ListWorkspaceSettings(ctx context.Context, _ *apiv2pb.ListWorkspaceSettingsRequest) (*apiv2pb.ListWorkspaceSettingsResponse, error) {
workspaceSettings, err := s.Store.ListWorkspaceSettingsV1(ctx, &store.FindWorkspaceSetting{})
workspaceSettings, err := s.Store.ListWorkspaceSettings(ctx, &store.FindWorkspaceSetting{})
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to get workspace setting: %v", err)
}
Expand All @@ -36,7 +36,7 @@ func (s *APIV2Service) GetWorkspaceSetting(ctx context.Context, request *apiv2pb
return nil, status.Errorf(codes.InvalidArgument, "invalid workspace setting name: %v", err)
}
settingKey := storepb.WorkspaceSettingKey(storepb.WorkspaceSettingKey_value[settingKeyString])
workspaceSetting, err := s.Store.GetWorkspaceSettingV1(ctx, &store.FindWorkspaceSetting{
workspaceSetting, err := s.Store.GetWorkspaceSetting(ctx, &store.FindWorkspaceSetting{
Name: settingKey.String(),
})
if err != nil {
Expand Down Expand Up @@ -64,7 +64,7 @@ func (s *APIV2Service) SetWorkspaceSetting(ctx context.Context, request *apiv2pb
return nil, status.Errorf(codes.PermissionDenied, "permission denied")
}

if _, err := s.Store.UpsertWorkspaceSettingV1(ctx, convertWorkspaceSettingToStore(request.Setting)); err != nil {
if _, err := s.Store.UpsertWorkspaceSetting(ctx, convertWorkspaceSettingToStore(request.Setting)); err != nil {
return nil, status.Errorf(codes.Internal, "failed to upsert workspace setting: %v", err)
}

Expand Down
2 changes: 1 addition & 1 deletion server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func (s *Server) getOrUpsertWorkspaceBasicSetting(ctx context.Context) (*storepb
modified = true
}
if modified {
workspaceSetting, err := s.Store.UpsertWorkspaceSettingV1(ctx, &storepb.WorkspaceSetting{
workspaceSetting, err := s.Store.UpsertWorkspaceSetting(ctx, &storepb.WorkspaceSetting{
Key: storepb.WorkspaceSettingKey_WORKSPACE_SETTING_BASIC,
Value: &storepb.WorkspaceSetting_BasicSetting{BasicSetting: workspaceBasicSetting},
})
Expand Down
2 changes: 1 addition & 1 deletion store/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ import (
"fmt"
)

func getUserSettingV1CacheKey(userID int32, key string) string {
func getUserSettingCacheKey(userID int32, key string) string {
return fmt.Sprintf("%d-%s-v1", userID, key)
}
18 changes: 9 additions & 9 deletions store/idp.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type DeleteIdentityProvider struct {
ID int32
}

func (s *Store) CreateIdentityProviderV1(ctx context.Context, create *storepb.IdentityProvider) (*storepb.IdentityProvider, error) {
func (s *Store) CreateIdentityProvider(ctx context.Context, create *storepb.IdentityProvider) (*storepb.IdentityProvider, error) {
raw, err := convertIdentityProviderToRaw(create)
if err != nil {
return nil, err
Expand All @@ -46,11 +46,11 @@ func (s *Store) CreateIdentityProviderV1(ctx context.Context, create *storepb.Id
if err != nil {
return nil, err
}
s.idpV1Cache.Store(identityProvider.Id, identityProvider)
s.idpCache.Store(identityProvider.Id, identityProvider)
return identityProvider, nil
}

func (s *Store) ListIdentityProvidersV1(ctx context.Context, find *FindIdentityProvider) ([]*storepb.IdentityProvider, error) {
func (s *Store) ListIdentityProviders(ctx context.Context, find *FindIdentityProvider) ([]*storepb.IdentityProvider, error) {
list, err := s.driver.ListIdentityProviders(ctx, find)
if err != nil {
return nil, err
Expand All @@ -62,19 +62,19 @@ func (s *Store) ListIdentityProvidersV1(ctx context.Context, find *FindIdentityP
if err != nil {
return nil, err
}
s.idpV1Cache.Store(identityProvider.Id, identityProvider)
s.idpCache.Store(identityProvider.Id, identityProvider)
}
return identityProviders, nil
}

func (s *Store) GetIdentityProviderV1(ctx context.Context, find *FindIdentityProvider) (*storepb.IdentityProvider, error) {
func (s *Store) GetIdentityProvider(ctx context.Context, find *FindIdentityProvider) (*storepb.IdentityProvider, error) {
if find.ID != nil {
if cache, ok := s.idpV1Cache.Load(*find.ID); ok {
if cache, ok := s.idpCache.Load(*find.ID); ok {
return cache.(*storepb.IdentityProvider), nil
}
}

list, err := s.ListIdentityProvidersV1(ctx, find)
list, err := s.ListIdentityProviders(ctx, find)
if err != nil {
return nil, err
}
Expand All @@ -97,7 +97,7 @@ type UpdateIdentityProviderV1 struct {
Config *storepb.IdentityProviderConfig
}

func (s *Store) UpdateIdentityProviderV1(ctx context.Context, update *UpdateIdentityProviderV1) (*storepb.IdentityProvider, error) {
func (s *Store) UpdateIdentityProvider(ctx context.Context, update *UpdateIdentityProviderV1) (*storepb.IdentityProvider, error) {
updateRaw := &UpdateIdentityProvider{
ID: update.ID,
}
Expand All @@ -123,7 +123,7 @@ func (s *Store) UpdateIdentityProviderV1(ctx context.Context, update *UpdateIden
if err != nil {
return nil, err
}
s.idpV1Cache.Store(identityProvider.Id, identityProvider)
s.idpCache.Store(identityProvider.Id, identityProvider)
return identityProvider, nil
}

Expand Down
76 changes: 0 additions & 76 deletions store/migrator.go
Original file line number Diff line number Diff line change
@@ -1,77 +1 @@
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")
}

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
// nolint
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" {
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.DeleteWorkspaceSetting(ctx, &DeleteWorkspaceSetting{
Name: workspaceSetting.Name,
}); err != nil {
return errors.Wrap(err, fmt.Sprintf("failed to delete workspace setting: %s", workspaceSetting.Name))
}
}
}

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")
}

if _, err := s.UpsertWorkspaceSettingV1(ctx, &storepb.WorkspaceSetting{
Key: storepb.WorkspaceSettingKey_WORKSPACE_SETTING_GENERAL,
Value: &storepb.WorkspaceSetting_GeneralSetting{GeneralSetting: workspaceGeneralSetting},
}); err != nil {
return errors.Wrap(err, "failed to upsert workspace general setting")
}

return nil
}
42 changes: 9 additions & 33 deletions store/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,35 +30,7 @@ type DeleteStorage struct {
ID int32
}

func (s *Store) CreateStorage(ctx context.Context, create *Storage) (*Storage, error) {
return s.driver.CreateStorage(ctx, create)
}

func (s *Store) ListStorages(ctx context.Context, find *FindStorage) ([]*Storage, error) {
return s.driver.ListStorages(ctx, find)
}

func (s *Store) GetStorage(ctx context.Context, find *FindStorage) (*Storage, error) {
list, err := s.ListStorages(ctx, find)
if err != nil {
return nil, err
}
if len(list) == 0 {
return nil, nil
}

return list[0], nil
}

func (s *Store) UpdateStorage(ctx context.Context, update *UpdateStorage) (*Storage, error) {
return s.driver.UpdateStorage(ctx, update)
}

func (s *Store) DeleteStorage(ctx context.Context, delete *DeleteStorage) error {
return s.driver.DeleteStorage(ctx, delete)
}

func (s *Store) CreateStorageV1(ctx context.Context, create *storepb.Storage) (*storepb.Storage, error) {
func (s *Store) CreateStorage(ctx context.Context, create *storepb.Storage) (*storepb.Storage, error) {
storageRaw := &Storage{
Name: create.Name,
Type: create.Type.String(),
Expand All @@ -83,7 +55,7 @@ func (s *Store) CreateStorageV1(ctx context.Context, create *storepb.Storage) (*
return storage, nil
}

func (s *Store) ListStoragesV1(ctx context.Context, find *FindStorage) ([]*storepb.Storage, error) {
func (s *Store) ListStorages(ctx context.Context, find *FindStorage) ([]*storepb.Storage, error) {
list, err := s.driver.ListStorages(ctx, find)
if err != nil {
return nil, err
Expand All @@ -100,8 +72,8 @@ func (s *Store) ListStoragesV1(ctx context.Context, find *FindStorage) ([]*store
return storages, nil
}

func (s *Store) GetStorageV1(ctx context.Context, find *FindStorage) (*storepb.Storage, error) {
list, err := s.ListStoragesV1(ctx, find)
func (s *Store) GetStorage(ctx context.Context, find *FindStorage) (*storepb.Storage, error) {
list, err := s.ListStorages(ctx, find)
if err != nil {
return nil, err
}
Expand All @@ -119,7 +91,7 @@ type UpdateStorageV1 struct {
Config *storepb.StorageConfig
}

func (s *Store) UpdateStorageV1(ctx context.Context, update *UpdateStorageV1) (*storepb.Storage, error) {
func (s *Store) UpdateStorage(ctx context.Context, update *UpdateStorageV1) (*storepb.Storage, error) {
updateRaw := &UpdateStorage{
ID: update.ID,
}
Expand All @@ -144,6 +116,10 @@ func (s *Store) UpdateStorageV1(ctx context.Context, update *UpdateStorageV1) (*
return storage, nil
}

func (s *Store) DeleteStorage(ctx context.Context, delete *DeleteStorage) error {
return s.driver.DeleteStorage(ctx, delete)
}

func convertStorageFromRaw(storageRaw *Storage) (*storepb.Storage, error) {
storage := &storepb.Storage{
Id: storageRaw.ID,
Expand Down
6 changes: 1 addition & 5 deletions store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ type Store struct {
workspaceSettingCache sync.Map // map[string]*storepb.WorkspaceSetting
userCache sync.Map // map[int]*User
userSettingCache sync.Map // map[string]*UserSetting
idpCache sync.Map // map[int]*IdentityProvider
idpV1Cache sync.Map // map[int]*storepb.IdentityProvider
idpCache sync.Map // map[int]*storepb.IdentityProvider
}

// New creates a new instance of Store.
Expand All @@ -27,9 +26,6 @@ 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
}

Expand Down
Loading

0 comments on commit 14d4cfd

Please sign in to comment.