Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

core: add progress during migration & backup restore

Abdullah Atta112497b1b62a
diff --git a/apps/web/src/common/index.js b/apps/web/src/common/index.js
index 7f368ab66..bfe3d6234 100644
--- a/apps/web/src/common/index.js
+++ b/apps/web/src/common/index.js
@@ -36,6 +36,8 @@ import { showFilePicker } from "../components/editor/picker";
import { logger } from "../utils/logger";
import { PATHS } from "@notesnook/desktop/paths";
import saveFile from "../commands/save-file";
+import { TaskManager } from "./task-manager";
+import { EVENTS } from "@notesnook/core/common";
export const CREATE_BUTTON_MAP = {
notes: {
@@ -140,11 +142,25 @@ export async function restoreBackupFile(backup) {
return !error;
});
} else {
- await showLoadingDialog({
+ await TaskManager.startTask({
title: "Restoring backup",
- subtitle:
- "Please do NOT close your browser or shut down your PC until the process completes.",
- action: () => restore(backup)
+ subtitle: "This might take a while",
+ type: "modal",
+ action: (report) => {
+ db.eventManager.subscribe(
+ EVENTS.migrationProgress,
+ ({ collection, total, current }) => {
+ report({
+ text: `Restoring ${collection}...`,
+ current,
+ total
+ });
+ }
+ );
+
+ report({ text: `Restoring...` });
+ return restore(backup);
+ }
});
}
}
diff --git a/apps/web/src/components/dialogs/migration-dialog.tsx b/apps/web/src/components/dialogs/migration-dialog.tsx
index 8bb10c079..d12731329 100644
--- a/apps/web/src/components/dialogs/migration-dialog.tsx
+++ b/apps/web/src/components/dialogs/migration-dialog.tsx
@@ -17,56 +17,121 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
+import { EVENTS } from "@notesnook/core/common";
import { Text } from "@theme-ui/components";
+import { useCallback, useEffect, useState } from "react";
import { createBackup } from "../../common";
import { db } from "../../common/db";
import { Perform } from "../../common/dialog-controller";
import { TaskManager } from "../../common/task-manager";
+import { isDesktop } from "../../utils/platform";
import Dialog from "./dialog";
+type MigrationProgressEvent = {
+ collection: string;
+ total: number;
+ current: number;
+};
+
export type MigrationDialogProps = {
onClose: Perform;
};
export default function MigrationDialog(props: MigrationDialogProps) {
- return (
- <Dialog
- width={500}
- isOpen={true}
- title={"Database migration required"}
- description={
- "Due to new features we need to migrate your data to a newer version."
- }
- positiveButton={{
- text: "Backup and migrate",
- onClick: async () => {
- await createBackup(true);
- await TaskManager.startTask({
- type: "modal",
- title: `Migrating your database`,
- subtitle:
- "Please do NOT close your browser/app during the migration process.",
- action: (task) => {
- task({ text: `Please wait...` });
- return db.migrations?.migrate();
- }
- });
+ const [error, setError] = useState<Error>();
+ const [isProcessing, setIsProcessing] = useState(false);
+
+ const startMigration = useCallback(async () => {
+ setIsProcessing(true);
+
+ await createBackup();
+
+ await TaskManager.startTask({
+ type: "modal",
+ title: `Applying changes...`,
+ subtitle: "This might take while.",
+ action: async (task) => {
+ db.eventManager.subscribe(
+ EVENTS.migrationProgress,
+ ({ collection, total, current }: MigrationProgressEvent) => {
+ task({
+ text: `Processing ${collection}...`,
+ current,
+ total
+ });
+ }
+ );
+ task({ text: `Processing...` });
+ try {
+ await db.migrations?.migrate();
+
props.onClose(true);
+ } catch (e) {
+ if (e instanceof Error) setError(e);
}
- }}
- >
- <Text variant={"subtitle"}>Read before continuing:</Text>
- <Text as="ol" sx={{ paddingInlineStart: 20, mt: 1 }}>
- <Text as="li" variant={"body"}>
- It is <b>required</b> that you <b>download &amp; save a backup</b> of
- your data.
+ }
+ });
+ }, [props]);
+
+ useEffect(() => {
+ if (isDesktop()) {
+ (async () => {
+ await startMigration();
+ })();
+ }
+ }, [startMigration]);
+
+ if (error) {
+ return (
+ <Dialog
+ isOpen={true}
+ title={"There was an error"}
+ description={""}
+ positiveButton={{
+ text: "Try again",
+ onClick: startMigration
+ }}
+ >
+ <Text
+ as="p"
+ variant={"error"}
+ sx={{
+ bg: "errorBg",
+ p: 1,
+ borderRadius: "default",
+ wordWrap: "normal",
+ overflowWrap: "break-word"
+ }}
+ >
+ {error.stack}
</Text>
- <Text as="li" variant={"body"}>
- If you face any issues feel free to reach out to us via{" "}
+ <Text as="p" variant={"subBody"} sx={{ mt: 2 }}>
+ If this continues to happen, please reach out to us via{" "}
<a href="https://discord.com/invite/zQBK97EE22">Discord</a> or email
us at{" "}
</Text>
+ </Dialog>
+ );
+ }
+
+ if (isDesktop() || isProcessing) return null;
+
+ return (
+ <Dialog
+ width={500}
+ isOpen={true}
+ title={"Save a backup of your notes"}
+ description={""}
+ positiveButton={{
+ text: "Save & continue",
+ onClick: startMigration
+ }}
+ >
+ <Text as="p" variant={"body"}>
+ {
+ "Thank you for updating Notesnook! We'll be applying some minor changes for a better note taking experience."
+ }
</Text>
</Dialog>
);
diff --git a/packages/core/common.js b/packages/core/common.js
index fe5a21a97..55ca2eee6 100644
--- a/packages/core/common.js
+++ b/packages/core/common.js
@@ -46,6 +46,14 @@ export function sendSyncProgressEvent(EV, type, total, current) {
});
}
+export function sendMigrationProgressEvent(EV, collection, total, current) {
+ EV.publish(EVENTS.migrationProgress, {
+ collection,
+ total,
+ current: current === undefined ? total : current
+ });
+}
+
export const CLIENT_ID = "notesnook";
export const CHECK_IDS = {
@@ -73,6 +81,7 @@ export const EVENTS = {
databaseUpdated: "db:updated",
databaseCollectionInitiated: "db:collectionInitiated",
appRefreshRequested: "app:refreshRequested",
+ migrationProgress: "migration:progress",
noteRemoved: "note:removed",
tokenRefreshed: "token:refreshed",
userUnauthorized: "user:unauthorized",
diff --git a/packages/core/database/migrator.js b/packages/core/database/migrator.js
index d5dabf795..b34ced79e 100644
--- a/packages/core/database/migrator.js
+++ b/packages/core/database/migrator.js
@@ -17,6 +17,7 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
+import { sendMigrationProgressEvent } from "../common";
import { migrateCollection, migrateItem } from "../migrations";
class Migrator {
@@ -24,6 +25,14 @@ class Migrator {
for (let collection of collections) {
if (!collection.index || !collection.dbCollection) continue;
+ if (collection.dbCollection.collectionName)
+ sendMigrationProgressEvent(
+ db.eventManager,
+ collection.dbCollection.collectionName,
+ 0,
+ 0
+ );
+
await migrateCollection(collection.dbCollection, version);
const index = (await collection.index()) || [];
@@ -34,6 +43,14 @@ class Migrator {
continue;
}
+ if (collection.dbCollection.collectionName)
+ sendMigrationProgressEvent(
+ db.eventManager,
+ collection.dbCollection.collectionName,
+ index.length,
+ i + 1
+ );
+
// check if item is permanently deleted or just a soft delete
if (item.deleted && !item.type) {
await collection.dbCollection?._collection?.addItem(item);