migrateToV8 migrates dnote data to sqlite database
(ctx context.DnoteCtx)
| 863 | |
| 864 | // migrateToV8 migrates dnote data to sqlite database |
| 865 | func migrateToV8(ctx context.DnoteCtx) error { |
| 866 | tx, err := ctx.DB.Begin() |
| 867 | if err != nil { |
| 868 | return errors.Wrap(err, "beginning a transaction") |
| 869 | } |
| 870 | |
| 871 | // 1. Migrate the the dnote file |
| 872 | dnoteFilePath := fmt.Sprintf("%s/dnote", ctx.Paths.LegacyDnote) |
| 873 | b, err := os.ReadFile(dnoteFilePath) |
| 874 | if err != nil { |
| 875 | return errors.Wrap(err, "reading the notes") |
| 876 | } |
| 877 | |
| 878 | var dnote migrateToV8Dnote |
| 879 | err = json.Unmarshal(b, &dnote) |
| 880 | if err != nil { |
| 881 | return errors.Wrap(err, "unmarshalling notes to JSON") |
| 882 | } |
| 883 | |
| 884 | for bookName, book := range dnote { |
| 885 | bookUUIDResult, err := uuid.NewRandom() |
| 886 | if err != nil { |
| 887 | return errors.Wrap(err, "generating uuid") |
| 888 | } |
| 889 | |
| 890 | bookUUID := bookUUIDResult.String() |
| 891 | |
| 892 | _, err = tx.Exec(`INSERT INTO books (uuid, label) VALUES (?, ?)`, bookUUID, bookName) |
| 893 | if err != nil { |
| 894 | tx.Rollback() |
| 895 | return errors.Wrapf(err, "inserting book %s", book.Name) |
| 896 | } |
| 897 | |
| 898 | for _, note := range book.Notes { |
| 899 | _, err = tx.Exec(`INSERT INTO notes |
| 900 | (uuid, book_uuid, content, added_on, edited_on, public) |
| 901 | VALUES (?, ?, ?, ?, ?, ?) |
| 902 | `, note.UUID, bookUUID, note.Content, note.AddedOn, note.EditedOn, note.Public) |
| 903 | |
| 904 | if err != nil { |
| 905 | tx.Rollback() |
| 906 | return errors.Wrapf(err, "inserting the note %s", note.UUID) |
| 907 | } |
| 908 | } |
| 909 | } |
| 910 | |
| 911 | // 2. Migrate the actions file |
| 912 | actionsPath := fmt.Sprintf("%s/actions", ctx.Paths.LegacyDnote) |
| 913 | b, err = os.ReadFile(actionsPath) |
| 914 | if err != nil { |
| 915 | return errors.Wrap(err, "reading the actions") |
| 916 | } |
| 917 | |
| 918 | var actions []migrateToV8Action |
| 919 | err = json.Unmarshal(b, &actions) |
| 920 | if err != nil { |
| 921 | return errors.Wrap(err, "unmarshalling actions from JSON") |
| 922 | } |