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: add resource name migrator

Steven5e7439464343
diff --git a/bin/memos/main.go b/bin/memos/main.go
index 5ffbfabfb..3a8a9d8b5 100644
--- a/bin/memos/main.go
+++ b/bin/memos/main.go
@@ -59,14 +59,11 @@ var (
}
store := store.New(dbDriver, profile)
-
- go func() {
- if err := store.MigrateResourceInternalPath(ctx); err != nil {
- cancel()
- log.Error("failed to migrate resource internal path", zap.Error(err))
- return
- }
- }()
+ if err := store.MigrateManually(ctx); err != nil {
+ cancel()
+ log.Error("failed to migrate manually", zap.Error(err))
+ return
+ }
s, err := server.NewServer(ctx, profile, store)
if err != nil {
diff --git a/store/db/sqlite/seed/10002__memo.sql b/store/db/sqlite/seed/10002__memo.sql
index 1c97ccca2..f905eed4c 100644
--- a/store/db/sqlite/seed/10002__memo.sql
+++ b/store/db/sqlite/seed/10002__memo.sql
@@ -8,7 +8,7 @@ INSERT INTO
VALUES
(
1,
- "hello",
+ "FqaZcg5H6EdGB9ke8kYUcy",
"#Hello πŸ‘‹ Welcome to memos.",
101
);
@@ -24,7 +24,7 @@ INSERT INTO
VALUES
(
2,
- "todo",
+ "DCo8442yRnXYPPcKSUAaEb",
'#TODO
- [x] Take more photos about **πŸŒ„ sunset**;
- [x] Clean the room;
@@ -44,7 +44,7 @@ INSERT INTO
VALUES
(
3,
- "links",
+ "ZvH7a6VWMuX5aArtECTj4N",
'**[Memos](https://github.com/usememos/memos)**: A lightweight, self-hosted memo hub. Open Source and Free forever.
**[Slash](https://github.com/yourselfhosted/slash)**: An open source, self-hosted bookmarks and link sharing platform. Save and share your links very easily.',
101,
@@ -62,7 +62,7 @@ INSERT INTO
VALUES
(
4,
- "todo2",
+ "2ad3WzUF4C6pTYXdm2nQC6",
'#TODO
- [x] Take more photos about **πŸŒ„ sunset**;
- [ ] Clean the classroom;
@@ -82,7 +82,7 @@ INSERT INTO
VALUES
(
5,
- "words",
+ "Pw2awZvxxLK4sPRtHmYuS7",
'δΈ‰δΊΊθ‘ŒοΌŒεΏ…ζœ‰ζˆ‘εΈˆη„‰οΌπŸ‘¨β€πŸ«',
102,
'PUBLIC'
diff --git a/store/db/sqlite/seed/10006__resource.sql b/store/db/sqlite/seed/10006__resource.sql
index d805fa61d..a810b75ff 100644
--- a/store/db/sqlite/seed/10006__resource.sql
+++ b/store/db/sqlite/seed/10006__resource.sql
@@ -1,4 +1,4 @@
INSERT INTO
resource (`resource_name`, `creator_id`, `filename`, `external_link`, `type`, `memo_id`)
VALUES
- ("slash-demo", 101, 'slash-demo.png', 'https://github.com/yourselfhosted/slash/blob/main/docs/assets/demo.png?raw=true', 'image/png', 3);
+ ("Pw2awZvxxLK4sPRtHmYuS7", 101, 'slash-demo.png', 'https://github.com/yourselfhosted/slash/blob/main/docs/assets/demo.png?raw=true', 'image/png', 3);
diff --git a/store/migrator.go b/store/migrator.go
index 003734e69..e1b82c378 100644
--- a/store/migrator.go
+++ b/store/migrator.go
@@ -8,6 +8,7 @@ import (
"strings"
+ "github.com/lithammer/shortuuid/v4"
"github.com/pkg/errors"
"github.com/usememos/memos/internal/log"
@@ -54,3 +55,66 @@ func (s *Store) MigrateResourceInternalPath(ctx context.Context) error {
}
return nil
}
+
+// MigrateResourceName migrates resource name from other format to short UUID.
+func (s *Store) MigrateResourceName(ctx context.Context) error {
+ memos, err := s.ListMemos(ctx, &FindMemo{})
+ if err != nil {
+ return errors.Wrap(err, "failed to list memos")
+ }
+ for _, memo := range memos {
+ if checkResourceName(memo.ResourceName) {
+ continue
+ }
+
+ resourceName := shortuuid.New()
+ err := s.UpdateMemo(ctx, &UpdateMemo{
+ ID: memo.ID,
+ ResourceName: &resourceName,
+ })
+ if err != nil {
+ return errors.Wrap(err, "failed to update memo")
+ }
+ }
+
+ resources, err := s.ListResources(ctx, &FindResource{})
+ if err != nil {
+ return errors.Wrap(err, "failed to list resources")
+ }
+ for _, resource := range resources {
+ if checkResourceName(resource.ResourceName) {
+ continue
+ }
+
+ resourceName := shortuuid.New()
+ _, err := s.UpdateResource(ctx, &UpdateResource{
+ ID: resource.ID,
+ ResourceName: &resourceName,
+ })
+ if err != nil {
+ return errors.Wrap(err, "failed to update resource")
+ }
+ }
+
+ return nil
+}
+
+func checkResourceName(resourceName string) bool {
+ // 22 is the length of shortuuid.
+ if len(resourceName) != 22 {
+ return false
+ }
+ for _, c := range resourceName {
+ if c >= '0' && c <= '9' {
+ continue
+ }
+ if c >= 'a' && c <= 'z' {
+ continue
+ }
+ if c >= 'A' && c <= 'Z' {
+ continue
+ }
+ return false
+ }
+ return true
+}
diff --git a/store/store.go b/store/store.go
index dcaf36b67..ddd3a903f 100644
--- a/store/store.go
+++ b/store/store.go
@@ -25,6 +25,16 @@ func New(driver Driver, profile *profile.Profile) *Store {
}
}
+func (s *Store) MigrateManually(ctx context.Context) error {
+ if err := s.MigrateResourceInternalPath(ctx); err != nil {
+ return err
+ }
+ if err := s.MigrateResourceName(ctx); err != nil {
+ return err
+ }
+ return nil
+}
+
func (s *Store) Vacuum(ctx context.Context) error {
return s.driver.Vacuum(ctx)
}