Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: improve resource internal_path migrator #2698

Merged
merged 2 commits into from
Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions bin/memos/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
48 changes: 30 additions & 18 deletions store/migrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@ package store

import (
"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.
Expand All @@ -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 := 0
for _, 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
}
56 changes: 56 additions & 0 deletions test/store/migrator_test.go
Original file line number Diff line number Diff line change
@@ -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()
}