chore: add resource name migrator

pull/2854/head
Steven 2 years ago
parent f4e722c516
commit 5e74394643

@ -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 {

@ -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'

@ -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);

@ -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
}

@ -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)
}

Loading…
Cancel
Save