From c7970999504f3d03e016b8a2abec6fc6955f06ce Mon Sep 17 00:00:00 2001 From: Steven Date: Tue, 2 Jan 2024 08:29:18 +0800 Subject: [PATCH] chore: update resource internal path migrator --- bin/memos/main.go | 6 +++ .../mysql/migration/prod/0.19/00_resource.sql | 19 -------- .../migration/prod/0.19/00_resource.sql | 19 -------- .../migration/prod/0.19/00_resource.sql | 19 -------- store/migrator.go | 44 +++++++++++++++++++ store/store.go | 2 +- 6 files changed, 51 insertions(+), 58 deletions(-) delete mode 100644 store/db/mysql/migration/prod/0.19/00_resource.sql delete mode 100644 store/db/postgres/migration/prod/0.19/00_resource.sql delete mode 100644 store/db/sqlite/migration/prod/0.19/00_resource.sql create mode 100644 store/migrator.go diff --git a/bin/memos/main.go b/bin/memos/main.go index 3720b276..4c67e76b 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 14ce267a..00000000 --- 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 fb4c1e7d..00000000 --- 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 7cdd0c10..00000000 --- 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 00000000..9caa4615 --- /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 03f785f2..dcaf36b6 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, } }