MCPcopy Index your code
hub / github.com/dnote/dnote / migrateToV3

Function migrateToV3

pkg/cli/migrate/legacy.go:556–620  ·  view source on GitHub ↗

migrateToV3 generates actions for existing dnote

(ctx context.DnoteCtx)

Source from the content-addressed store, hash-verified

554
555// migrateToV3 generates actions for existing dnote
556func migrateToV3(ctx context.DnoteCtx) error {
557 notePath := fmt.Sprintf("%s/dnote", ctx.Paths.LegacyDnote)
558 actionsPath := fmt.Sprintf("%s/actions", ctx.Paths.LegacyDnote)
559
560 b, err := os.ReadFile(notePath)
561 if err != nil {
562 return errors.Wrap(err, "Failed to read the note file")
563 }
564
565 var dnote migrateToV3Dnote
566
567 err = json.Unmarshal(b, &dnote)
568 if err != nil {
569 return errors.Wrap(err, "Failed to unmarshal existing dnote into JSON")
570 }
571
572 var actions []migrateToV3Action
573
574 for bookName, book := range dnote {
575 // Find the minimum added_on timestamp from the notes that belong to the book
576 // to give timstamp to the add_book action.
577 // Logically add_book must have happened no later than the first add_note
578 // to the book in order for sync to work.
579 minTs := time.Now().Unix()
580 for _, note := range book.Notes {
581 if note.AddedOn < minTs {
582 minTs = note.AddedOn
583 }
584 }
585
586 action := migrateToV3Action{
587 Type: migrateToV3ActionAddBook,
588 Data: map[string]interface{}{
589 "book_name": bookName,
590 },
591 Timestamp: minTs,
592 }
593 actions = append(actions, action)
594
595 for _, note := range book.Notes {
596 action := migrateToV3Action{
597 Type: migrateToV3ActionAddNote,
598 Data: map[string]interface{}{
599 "note_uuid": note.UUID,
600 "book_name": book.Name,
601 "content": note.Content,
602 },
603 Timestamp: note.AddedOn,
604 }
605 actions = append(actions, action)
606 }
607 }
608
609 a, err := json.Marshal(actions)
610 if err != nil {
611 return errors.Wrap(err, "Failed to marshal actions into JSON")
612 }
613

Callers 2

performMigrationFunction · 0.85
TestMigrateToV3Function · 0.85

Calls 1

NowMethod · 0.65

Tested by 1

TestMigrateToV3Function · 0.68