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

Function migrateToV5

pkg/cli/migrate/legacy.go:676–743  ·  view source on GitHub ↗

migrateToV5 migrates actions

(ctx context.DnoteCtx)

Source from the content-addressed store, hash-verified

674
675// migrateToV5 migrates actions
676func migrateToV5(ctx context.DnoteCtx) error {
677 actionsPath := fmt.Sprintf("%s/actions", ctx.Paths.LegacyDnote)
678
679 b, err := os.ReadFile(actionsPath)
680 if err != nil {
681 return errors.Wrap(err, "reading the actions file")
682 }
683
684 var actions []migrateToV5PreAction
685 err = json.Unmarshal(b, &actions)
686 if err != nil {
687 return errors.Wrap(err, "unmarshalling actions from JSON")
688 }
689
690 result := []migrateToV5PostAction{}
691
692 for _, action := range actions {
693 var data json.RawMessage
694
695 switch action.Type {
696 case migrateToV5ActionEditNote:
697 var oldData migrateToV5PreEditNoteData
698 if err = json.Unmarshal(action.Data, &oldData); err != nil {
699 return errors.Wrapf(err, "unmarshalling old data of an edit note action %d", action.ID)
700 }
701
702 migratedData := migrateToV5PostEditNoteData{
703 NoteUUID: oldData.NoteUUID,
704 FromBook: oldData.BookName,
705 Content: oldData.Content,
706 }
707 b, err = json.Marshal(migratedData)
708 if err != nil {
709 return errors.Wrap(err, "marshalling data")
710 }
711
712 data = b
713 default:
714 data = action.Data
715 }
716
717 actionUUID, err := genUUID()
718 if err != nil {
719 return errors.Wrap(err, "generating UUID")
720 }
721
722 migrated := migrateToV5PostAction{
723 UUID: actionUUID,
724 Schema: 1,
725 Type: action.Type,
726 Data: data,
727 Timestamp: action.Timestamp,
728 }
729
730 result = append(result, migrated)
731 }
732
733 a, err := json.Marshal(result)

Callers 2

performMigrationFunction · 0.85
TestMigrateToV5Function · 0.85

Calls 1

genUUIDFunction · 0.85

Tested by 1

TestMigrateToV5Function · 0.68