Skip to content

armgit / memos

Public Imported from GitHub
0

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.

chore: update resource internal path migrator

Stevenc7970999504f
diff --git a/bin/memos/main.go b/bin/memos/main.go
index 3720b2764..4c67e76b7 100644
--- a/bin/memos/main.go
+++ b/bin/memos/main.go
@@ -59,6 +59,12 @@ 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
+ }
+
s, err := server.NewServer(ctx, profile, store)
if err != nil {
cancel()
diff --git a/store/db/mysql/migration/prod/0.19/00_resource.sql b/store/db/mysql/migration/prod/0.19/00_resource.sql
deleted file mode 100644
index 14ce267a8..000000000
--- a/store/db/mysql/migration/prod/0.19/00_resource.sql
+++ /dev/null
@@ -1,19 +0,0 @@
--- Make resource internal_path relative (to MEMOS_DATA) and replace backslash with slash
--- This is a best-effort approach, but even if it fails, it won't break assets from loading
-UPDATE resource
-SET
- internal_path = REPLACE (internal_path, '\\', '/')
-WHERE
- internal_path LIKE '%assets\\\%';
-
-UPDATE resource
-SET
- internal_path = REPLACE (
- internal_path,
- SUBSTR (
- internal_path,
- 1,
- INSTR (internal_path, '/assets')
- ),
- ''
- );
diff --git a/store/db/postgres/migration/prod/0.19/00_resource.sql b/store/db/postgres/migration/prod/0.19/00_resource.sql
deleted file mode 100644
index fb4c1e7db..000000000
--- a/store/db/postgres/migration/prod/0.19/00_resource.sql
+++ /dev/null
@@ -1,19 +0,0 @@
--- Make resource internal_path relative (to MEMOS_DATA) and replace backslash with slash
--- This is a best-effort approach, but even if it fails, it won't break assets from loading
-UPDATE resource
-SET
- internal_path = REPLACE (internal_path, '\', '/')
-WHERE
- internal_path LIKE '%assets\\%';
-
-UPDATE resource
-SET
- internal_path = REPLACE (
- internal_path,
- SUBSTRING(
- internal_path
- FROM
- 1 FOR POSITION('/assets' IN internal_path)
- ),
- ''
- );
diff --git a/store/db/sqlite/migration/prod/0.19/00_resource.sql b/store/db/sqlite/migration/prod/0.19/00_resource.sql
deleted file mode 100644
index 7cdd0c101..000000000
--- a/store/db/sqlite/migration/prod/0.19/00_resource.sql
+++ /dev/null
@@ -1,19 +0,0 @@
--- Make resource internal_path relative (to MEMOS_DATA) and replace backslash with slash
--- This is a best-effort approach, but even if it fails, it won't break assets from loading
-UPDATE resource
-SET
- internal_path = REPLACE (internal_path, '\', '/')
-WHERE
- internal_path LIKE '%assets\%';
-
-UPDATE resource
-SET
- internal_path = REPLACE (
- internal_path,
- SUBSTR (
- internal_path,
- 1,
- INSTR (internal_path, '/assets')
- ),
- ''
- );
diff --git a/store/migrator.go b/store/migrator.go
new file mode 100644
index 000000000..9caa4615d
--- /dev/null
+++ b/store/migrator.go
@@ -0,0 +1,44 @@
+package store
+
+import (
+ "context"
+ "path/filepath"
+ "strings"
+
+ "github.com/pkg/errors"
+)
+
+// MigrateResourceInternalPath migrates resource internal path from absolute path to relative path.
+func (s *Store) MigrateResourceInternalPath(ctx context.Context) error {
+ resources, err := s.ListResources(ctx, &FindResource{})
+ if err != nil {
+ return errors.Wrap(err, "failed to list resources")
+ }
+
+ 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")
+ }
+ }
+ }
+
+ return nil
+}
diff --git a/store/store.go b/store/store.go
index 03f785f2f..dcaf36b67 100644
--- a/store/store.go
+++ b/store/store.go
@@ -20,8 +20,8 @@ type Store struct {
// New creates a new instance of Store.
func New(driver Driver, profile *profile.Profile) *Store {
return &Store{
- Profile: profile,
driver: driver,
+ Profile: profile,
}
}