Memos is an open-source, self-hosted note-taking app that brings your everyday thoughts together in a personal timeline. Your data stays yours, and you choose what to keep private or share.
Go dockerfossgomarkdownmemomicroblognote-takingnotesopen-sourceown-your-datareactself-hostedsqlitetimeline
chore: improve resource internal_path migrator (#2698)
Lincoln Nogueira369b8af109a1
diff --git a/bin/memos/main.go b/bin/memos/main.goindex 4c67e76b7..5ffbfabfb 100644--- a/bin/memos/main.go+++ b/bin/memos/main.go@@ -59,11 +59,14 @@ var (}store := store.New(dbDriver, profile)- if err := store.MigrateResourceInternalPath(ctx); err != nil {- cancel()- log.Error("failed to migrate resource internal path", zap.Error(err))- return- }++ go func() {+ if err := store.MigrateResourceInternalPath(ctx); err != nil {+ cancel()+ log.Error("failed to migrate resource internal path", zap.Error(err))+ return+ }+ }()s, err := server.NewServer(ctx, profile, store)if err != nil {diff --git a/store/migrator.go b/store/migrator.goindex 9caa4615d..9b9995afb 100644--- a/store/migrator.go+++ b/store/migrator.go@@ -2,10 +2,15 @@ package storeimport ("context"- "path/filepath"+ "fmt"+ "os"+ "time"+"strings""github.com/pkg/errors"++ "github.com/usememos/memos/internal/log")// MigrateResourceInternalPath migrates resource internal path from absolute path to relative path.@@ -15,30 +20,37 @@ func (s *Store) MigrateResourceInternalPath(ctx context.Context) error {return errors.Wrap(err, "failed to list resources")}+ dataPath := strings.ReplaceAll(s.Profile.Data, `\`, "/")+ migrateStartTime := time.Now()+ migratedCount := 0for _, resource := range resources {if resource.InternalPath == "" {continue}- internalPath := resource.InternalPath- if filepath.IsAbs(internalPath) {- if !strings.HasPrefix(internalPath, s.Profile.Data) {- // Invalid internal path, skip.- continue- }- internalPath = strings.TrimPrefix(internalPath, s.Profile.Data)- for strings.HasPrefix(internalPath, "/") {- internalPath = strings.TrimPrefix(internalPath, "/")- }- _, err := s.UpdateResource(ctx, &UpdateResource{- ID: resource.ID,- InternalPath: &internalPath,- })- if err != nil {- return errors.Wrap(err, "failed to update resource")- }+ internalPath := strings.ReplaceAll(resource.InternalPath, `\`, "/")+ if !strings.HasPrefix(internalPath, dataPath) {+ continue}++ internalPath = strings.TrimPrefix(internalPath, dataPath)++ for os.IsPathSeparator(internalPath[0]) {+ internalPath = internalPath[1:]+ }++ _, err := s.UpdateResource(ctx, &UpdateResource{+ ID: resource.ID,+ InternalPath: &internalPath,+ })+ if err != nil {+ return errors.Wrap(err, "failed to update local resource path")+ }+ migratedCount++}+ if migratedCount > 0 {+ log.Info(fmt.Sprintf("migrated %d local resource paths in %s", migratedCount, time.Since(migrateStartTime)))+ }return nil}diff --git a/test/store/migrator_test.go b/test/store/migrator_test.gonew file mode 100644index 000000000..a971f28f8--- /dev/null+++ b/test/store/migrator_test.go@@ -0,0 +1,56 @@+package teststore++import (+ "context"+ "testing"++ "github.com/stretchr/testify/require"++ "github.com/usememos/memos/store"+)++func TestMigrateResourceInternalPath(t *testing.T) {+ ctx := context.Background()+ ts := NewTestingStore(ctx, t)+ user, err := createTestingHostUser(ctx, ts)+ require.NoError(t, err)++ testCases := []map[string]string{+ {+ ts.Profile.Data + "/assets/test.jpg": "assets/test.jpg",+ },+ {+ ts.Profile.Data + `\assets\test.jpg`: "assets/test.jpg",+ },+ {+ "/unhandled/path/test.jpg": "/unhandled/path/test.jpg",+ },+ {+ `C:\unhandled\path\assets\test.jpg`: `C:\unhandled\path\assets\test.jpg`,+ },+ }++ for _, testCase := range testCases {+ for input, expectedOutput := range testCase {+ resourceCreate := &store.Resource{+ CreatorID: user.ID,+ InternalPath: input,+ }+ createdResource, err := ts.CreateResource(ctx, resourceCreate)+ require.NoError(t, err)++ err = ts.MigrateResourceInternalPath(ctx)+ require.NoError(t, err)++ findResource := &store.FindResource{+ ID: &createdResource.ID,+ }+ resource, err := ts.GetResource(ctx, findResource)+ require.NoError(t, err)++ require.Equal(t, expectedOutput, resource.InternalPath)+ }+ }++ ts.Close()+}