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

Function migrateToV7

pkg/cli/migrate/legacy.go:801–862  ·  view source on GitHub ↗

migrateToV7 migrates data of edit_note action to the proper version which is EditNoteDataV2. Due to a bug, edit logged actions with schema version '2' but with a data of EditNoteDataV1. https://github.com/dnote/dnote/pkg/cli/issues/107

(ctx context.DnoteCtx)

Source from the content-addressed store, hash-verified

799// EditNoteDataV2. Due to a bug, edit logged actions with schema version '2'
800// but with a data of EditNoteDataV1. https://github.com/dnote/dnote/pkg/cli/issues/107
801func migrateToV7(ctx context.DnoteCtx) error {
802 actionPath := fmt.Sprintf("%s/actions", ctx.Paths.LegacyDnote)
803
804 b, err := os.ReadFile(actionPath)
805 if err != nil {
806 return errors.Wrap(err, "reading actions file")
807 }
808
809 var preActions []migrateToV7Action
810 postActions := []migrateToV7Action{}
811 err = json.Unmarshal(b, &preActions)
812 if err != nil {
813 return errors.Wrap(err, "unmarshalling existing actions")
814 }
815
816 for _, action := range preActions {
817 var newAction migrateToV7Action
818
819 if action.Type == migrateToV7ActionTypeEditNote {
820 var oldData migrateToV7EditNoteDataV1
821 if e := json.Unmarshal(action.Data, &oldData); e != nil {
822 return errors.Wrapf(e, "unmarshalling data of action with uuid %s", action.Data)
823 }
824
825 newData := migrateToV7EditNoteDataV2{
826 NoteUUID: oldData.NoteUUID,
827 FromBook: oldData.FromBook,
828 ToBook: nil,
829 Content: &oldData.Content,
830 Public: nil,
831 }
832 d, e := json.Marshal(newData)
833 if e != nil {
834 return errors.Wrapf(e, "marshalling new data of action with uuid %s", action.Data)
835 }
836
837 newAction = migrateToV7Action{
838 UUID: action.UUID,
839 Schema: action.Schema,
840 Type: action.Type,
841 Timestamp: action.Timestamp,
842 Data: d,
843 }
844 } else {
845 newAction = action
846 }
847
848 postActions = append(postActions, newAction)
849 }
850
851 d, err := json.Marshal(postActions)
852 if err != nil {
853 return errors.Wrap(err, "marshalling new actions")
854 }
855
856 err = os.WriteFile(actionPath, d, 0644)
857 if err != nil {
858 return errors.Wrap(err, "writing new actions to a file")

Callers 2

performMigrationFunction · 0.85
TestMigrateToV7Function · 0.85

Calls

no outgoing calls

Tested by 1

TestMigrateToV7Function · 0.68