Skip to content

Commit

Permalink
fix: file-based backup during migrations cause compatibility issues, …
Browse files Browse the repository at this point in the history
…so using memory-based instead

fix #397
  • Loading branch information
chhoumann committed Feb 21, 2023
1 parent 1752c9b commit f8f408b
Showing 1 changed file with 1 addition and 37 deletions.
38 changes: 1 addition & 37 deletions src/migrations/migrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { Migrations } from "./Migrations";
import migrateToMacroIDFromEmbeddedMacro from "./migrateToMacroIDFromEmbeddedMacro";
import useQuickAddTemplateFolder from "./useQuickAddTemplateFolder";
import incrementFileNameSettingMoveToDefaultBehavior from "./incrementFileNameSettingMoveToDefaultBehavior";
import { moment } from "obsidian";
import mutualExclusionInsertAfterAndWriteToBottomOfFile from "./mutualExclusionInsertAfterAndWriteToBottomOfFile";

const migrations: Migrations = {
Expand All @@ -14,24 +13,6 @@ const migrations: Migrations = {
mutualExclusionInsertAfterAndWriteToBottomOfFile,
};

const backupFolderPath = ".obsidian/plugins/quickadd/backup";

const getBackupPath = (backupName: string): string =>
`${backupFolderPath}/${moment().format(
"DD-MM-YY_HH-mm-ss"
)}_${backupName}.json`;

// Unfortunately, we cannot use 'app.vault.getAbstractFileByPath' here, because it doesn't seem to index files in the .obsidian folder.
async function makeBackupFolderIfNotExists() {
try {
await app.vault.createFolder(backupFolderPath);
} catch (error) {
if (!error.message?.includes("Folder already exists")) {
throw error;
}
}
}

async function migrate(plugin: QuickAdd): Promise<void> {
const migrationsToRun = Object.keys(migrations).filter(
(migration: keyof Migrations) => !plugin.settings.migrations[migration]
Expand All @@ -43,23 +24,6 @@ async function migrate(plugin: QuickAdd): Promise<void> {
return;
}

try {
await makeBackupFolderIfNotExists();

const backup = structuredClone(plugin.settings);

await app.vault.create(
getBackupPath("preMigrationBackup"),
JSON.stringify(backup)
);
} catch (error) {
log.logError(
`Unable to create backup before migrating to new version. Please create an issue with the following error message: \n\n${error}\n\nYour data is still safe! QuickAdd won't proceed without backup.`
);

return;
}

// Could batch-run with Promise.all, but we want to log each migration as it runs.
for (const migration of migrationsToRun as (keyof Migrations)[]) {
log.logMessage(
Expand All @@ -76,7 +40,7 @@ async function migrate(plugin: QuickAdd): Promise<void> {
log.logMessage(`Migration ${migration} successful.`);
} catch (error) {
log.logError(
`Migration '${migration}' was unsuccessful. Please create an issue with the following error message: \n\n${error}\n\nQuickAdd will now revert to backup. You can also find a backup in the QuickAdd backup folder: "${backupFolderPath}"`
`Migration '${migration}' was unsuccessful. Please create an issue with the following error message: \n\n${error}\n\nQuickAdd will now revert to backup.`
);

plugin.settings = backup;
Expand Down

1 comment on commit f8f408b

@vercel
Copy link

@vercel vercel bot commented on f8f408b Feb 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

quickadd – ./

quickadd.obsidian.guide
quickadd-chrisbbh.vercel.app
quickadd-git-master-chrisbbh.vercel.app

Please sign in to comment.