mirror of https://github.com/usememos/memos.git
chore: add resource name migrator
This commit is contained in:
parent
f4e722c516
commit
5e74394643
|
|
@ -59,14 +59,11 @@ var (
|
||||||
}
|
}
|
||||||
|
|
||||||
store := store.New(dbDriver, profile)
|
store := store.New(dbDriver, profile)
|
||||||
|
if err := store.MigrateManually(ctx); err != nil {
|
||||||
go func() {
|
cancel()
|
||||||
if err := store.MigrateResourceInternalPath(ctx); err != nil {
|
log.Error("failed to migrate manually", zap.Error(err))
|
||||||
cancel()
|
return
|
||||||
log.Error("failed to migrate resource internal path", zap.Error(err))
|
}
|
||||||
return
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
s, err := server.NewServer(ctx, profile, store)
|
s, err := server.NewServer(ctx, profile, store)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,7 @@ INSERT INTO
|
||||||
VALUES
|
VALUES
|
||||||
(
|
(
|
||||||
1,
|
1,
|
||||||
"hello",
|
"FqaZcg5H6EdGB9ke8kYUcy",
|
||||||
"#Hello 👋 Welcome to memos.",
|
"#Hello 👋 Welcome to memos.",
|
||||||
101
|
101
|
||||||
);
|
);
|
||||||
|
|
@ -24,7 +24,7 @@ INSERT INTO
|
||||||
VALUES
|
VALUES
|
||||||
(
|
(
|
||||||
2,
|
2,
|
||||||
"todo",
|
"DCo8442yRnXYPPcKSUAaEb",
|
||||||
'#TODO
|
'#TODO
|
||||||
- [x] Take more photos about **🌄 sunset**;
|
- [x] Take more photos about **🌄 sunset**;
|
||||||
- [x] Clean the room;
|
- [x] Clean the room;
|
||||||
|
|
@ -44,7 +44,7 @@ INSERT INTO
|
||||||
VALUES
|
VALUES
|
||||||
(
|
(
|
||||||
3,
|
3,
|
||||||
"links",
|
"ZvH7a6VWMuX5aArtECTj4N",
|
||||||
'**[Memos](https://github.com/usememos/memos)**: A lightweight, self-hosted memo hub. Open Source and Free forever.
|
'**[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.',
|
**[Slash](https://github.com/yourselfhosted/slash)**: An open source, self-hosted bookmarks and link sharing platform. Save and share your links very easily.',
|
||||||
101,
|
101,
|
||||||
|
|
@ -62,7 +62,7 @@ INSERT INTO
|
||||||
VALUES
|
VALUES
|
||||||
(
|
(
|
||||||
4,
|
4,
|
||||||
"todo2",
|
"2ad3WzUF4C6pTYXdm2nQC6",
|
||||||
'#TODO
|
'#TODO
|
||||||
- [x] Take more photos about **🌄 sunset**;
|
- [x] Take more photos about **🌄 sunset**;
|
||||||
- [ ] Clean the classroom;
|
- [ ] Clean the classroom;
|
||||||
|
|
@ -82,7 +82,7 @@ INSERT INTO
|
||||||
VALUES
|
VALUES
|
||||||
(
|
(
|
||||||
5,
|
5,
|
||||||
"words",
|
"Pw2awZvxxLK4sPRtHmYuS7",
|
||||||
'三人行,必有我师焉!👨🏫',
|
'三人行,必有我师焉!👨🏫',
|
||||||
102,
|
102,
|
||||||
'PUBLIC'
|
'PUBLIC'
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
INSERT INTO
|
INSERT INTO
|
||||||
resource (`resource_name`, `creator_id`, `filename`, `external_link`, `type`, `memo_id`)
|
resource (`resource_name`, `creator_id`, `filename`, `external_link`, `type`, `memo_id`)
|
||||||
VALUES
|
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"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/lithammer/shortuuid/v4"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
|
|
||||||
"github.com/usememos/memos/internal/log"
|
"github.com/usememos/memos/internal/log"
|
||||||
|
|
@ -54,3 +55,66 @@ func (s *Store) MigrateResourceInternalPath(ctx context.Context) error {
|
||||||
}
|
}
|
||||||
return nil
|
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 {
|
func (s *Store) Vacuum(ctx context.Context) error {
|
||||||
return s.driver.Vacuum(ctx)
|
return s.driver.Vacuum(ctx)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue