mirror of
https://github.com/streetwriters/notesnook.git
synced 2025-12-23 15:09:33 +01:00
feat: make migration on-demand
This commit is contained in:
@@ -1,20 +1,18 @@
|
|||||||
import { EventSourcePolyfill as EventSource } from "event-source-polyfill";
|
import { EventSourcePolyfill as EventSource } from "event-source-polyfill";
|
||||||
import { EVENTS } from "@streetwriters/notesnook-core/common";
|
import { NNStorage } from "../interfaces/storage";
|
||||||
import { TaskManager } from "./task-manager";
|
import { logger } from "../utils/logger";
|
||||||
import { initalize, logger } from "@streetwriters/notesnook-core/logger";
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @type {import("@streetwriters/notesnook-core/api").default}
|
* @type {import("@streetwriters/notesnook-core/api").default}
|
||||||
*/
|
*/
|
||||||
var db;
|
var db;
|
||||||
async function initializeDatabase(persistence) {
|
async function initializeDatabase(persistence) {
|
||||||
|
logger.measure("Database initialization");
|
||||||
|
|
||||||
const { default: Database } = await import(
|
const { default: Database } = await import(
|
||||||
"@streetwriters/notesnook-core/api"
|
"@streetwriters/notesnook-core/api"
|
||||||
);
|
);
|
||||||
const { NNStorage } = await import("../interfaces/storage");
|
|
||||||
const { default: FS } = await import("../interfaces/fs");
|
const { default: FS } = await import("../interfaces/fs");
|
||||||
|
|
||||||
initalize(new NNStorage("Logs", persistence));
|
|
||||||
db = new Database(new NNStorage("Notesnook", persistence), EventSource, FS);
|
db = new Database(new NNStorage("Notesnook", persistence), EventSource, FS);
|
||||||
|
|
||||||
// if (isTesting()) {
|
// if (isTesting()) {
|
||||||
@@ -39,22 +37,18 @@ async function initializeDatabase(persistence) {
|
|||||||
// });
|
// });
|
||||||
// }
|
// }
|
||||||
|
|
||||||
db.eventManager.subscribe(EVENTS.databaseMigrating, async ({ from, to }) => {
|
// db.eventManager.subscribe(EVENTS.databaseMigrating, async ({ from, to }) => {
|
||||||
await TaskManager.startTask({
|
|
||||||
type: "modal",
|
// });
|
||||||
title: `Migrating your database`,
|
|
||||||
subtitle:
|
|
||||||
"Please do not close your browser/app before the migration is done.",
|
|
||||||
action: (task) => {
|
|
||||||
task({ text: `Migrating database from v${from} to v${to}` });
|
|
||||||
return new Promise((resolve) => {
|
|
||||||
db.eventManager.subscribe(EVENTS.databaseMigrated, resolve);
|
|
||||||
});
|
|
||||||
},
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
await db.init();
|
await db.init();
|
||||||
|
|
||||||
|
logger.measure("Database initialization");
|
||||||
|
|
||||||
|
if (db.migrations.required()) {
|
||||||
|
const { showMigrationDialog } = await import("./dialog-controller");
|
||||||
|
await showMigrationDialog();
|
||||||
|
}
|
||||||
return db;
|
return db;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -278,6 +278,12 @@ export function showEmailVerificationDialog() {
|
|||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function showMigrationDialog() {
|
||||||
|
return showDialog("MigrationDialog", (Dialog, perform) => (
|
||||||
|
<Dialog onClose={() => perform(false)} />
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
type LoadingDialogProps = {
|
type LoadingDialogProps = {
|
||||||
title: string;
|
title: string;
|
||||||
message?: string;
|
message?: string;
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ import OnboardingDialog from "./onboarding-dialog";
|
|||||||
import AttachmentsDialog from "./attachmentsdialog";
|
import AttachmentsDialog from "./attachmentsdialog";
|
||||||
import { Prompt } from "./prompt";
|
import { Prompt } from "./prompt";
|
||||||
import { ToolbarConfigDialog } from "./toolbarconfigdialog";
|
import { ToolbarConfigDialog } from "./toolbarconfigdialog";
|
||||||
|
import { MigrationDialog } from "./migrationdialog";
|
||||||
|
|
||||||
const Dialogs = {
|
const Dialogs = {
|
||||||
AddNotebookDialog,
|
AddNotebookDialog,
|
||||||
@@ -43,5 +44,6 @@ const Dialogs = {
|
|||||||
RecoveryCodesDialog,
|
RecoveryCodesDialog,
|
||||||
OnboardingDialog,
|
OnboardingDialog,
|
||||||
AttachmentsDialog,
|
AttachmentsDialog,
|
||||||
|
MigrationDialog,
|
||||||
};
|
};
|
||||||
export default Dialogs;
|
export default Dialogs;
|
||||||
|
|||||||
66
apps/web/src/components/dialogs/migration-dialog.tsx
Normal file
66
apps/web/src/components/dialogs/migration-dialog.tsx
Normal file
@@ -0,0 +1,66 @@
|
|||||||
|
import { Text } from "rebass";
|
||||||
|
import { createBackup } from "../../common";
|
||||||
|
import { db } from "../../common/db";
|
||||||
|
import { Perform } from "../../common/dialog-controller";
|
||||||
|
import { TaskManager } from "../../common/task-manager";
|
||||||
|
import Dialog from "./dialog";
|
||||||
|
|
||||||
|
export type MigrationDialogProps = {
|
||||||
|
onClose: Perform;
|
||||||
|
};
|
||||||
|
|
||||||
|
export 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. This is NOT a destructive operation."
|
||||||
|
}
|
||||||
|
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();
|
||||||
|
},
|
||||||
|
});
|
||||||
|
props.onClose(true);
|
||||||
|
},
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<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 & save a backup</b> of
|
||||||
|
your data.
|
||||||
|
</Text>
|
||||||
|
<Text as="li" variant={"body"}>
|
||||||
|
Some <b>merge conflicts</b> in your notes after a migration are
|
||||||
|
expected. It is <b>recommended</b> that you go through them &
|
||||||
|
resolve them carefully.
|
||||||
|
<Text as="ol" sx={{ paddingInlineStart: 20 }}>
|
||||||
|
<Text as="li" variant={"body"}>
|
||||||
|
<b>But if you are feeling reckless</b> and want to risk losing
|
||||||
|
some data, you can logout & log back in.
|
||||||
|
</Text>
|
||||||
|
</Text>
|
||||||
|
</Text>
|
||||||
|
<Text as="li" variant={"body"}>
|
||||||
|
If you face any other issues or are unsure about what to do, feel free
|
||||||
|
to reach out to us via{" "}
|
||||||
|
<a href="https://discord.com/invite/zQBK97EE22">Discord</a> or email
|
||||||
|
us at{" "}
|
||||||
|
<a href="mailto:support@streetwriters.co">support@streetwriters.co</a>
|
||||||
|
</Text>
|
||||||
|
</Text>
|
||||||
|
</Dialog>
|
||||||
|
);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user