migrateToV5 migrates actions
(ctx context.DnoteCtx)
| 674 | |
| 675 | // migrateToV5 migrates actions |
| 676 | func 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) |