Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: cleanup migrations #2384

Merged
merged 2 commits into from
May 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

84 changes: 15 additions & 69 deletions src/extension/background-script/migrations/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import db from "../db";
import state from "../state";
// import db from "../db";
// import state from "../state";

export type Migration = keyof typeof migrations;

/*

// TS does not want unused code.
// we need this for the next migration again

const shouldMigrate = (name: Migration): boolean => {
const { migrations } = state.getState();

Expand All @@ -25,78 +30,19 @@ const setMigrated = (name: Migration): Promise<void> => {
return state.getState().saveToStorage();
};

const migrations = {
migrateisUsingGlobalNostrKey: async () => {
const { nostrPrivateKey, accounts } = state.getState();

if (nostrPrivateKey) {
Object.values(accounts).map((account) => {
if (!account.nostrPrivateKey) account.nostrPrivateKey = nostrPrivateKey;
});

state.setState({
accounts,
});
// will be persisted by setMigrated
}
},

migratePaymentsWithoutAccountId: async () => {
const { accounts } = state.getState();
if (Object.keys(accounts).length == 1) {
const accountId = Object.keys(accounts)[0];
const payments = await db.payments.toArray();

payments.forEach(async (payments) => {
payments.id && (await db.payments.update(payments.id, { accountId }));
});
}
},
*/

ensureAccountId: async () => {
const { accounts } = state.getState();
Object.keys(accounts).forEach((accountId) => {
if (!accounts[accountId].id) {
console.info(`updating ${accountId}`);
accounts[accountId].id = accountId;
}
});
state.setState({
accounts,
});
// will be persisted by setMigrated
},

migratePermissionsWithoutAccountId: async () => {
const { accounts } = state.getState();
const accountId = Object.keys(accounts)[0];
const permissions = await db.permissions.toArray();

permissions.forEach(async (permission) => {
permission.id &&
(await db.permissions.update(permission.id, { accountId }));
});
},
};
const migrations = {};

const migrate = async () => {
// going forward we can iterate through the the migrations object above and DRY this up:
// Object.keys(migrations).forEach((name: string) => {
if (shouldMigrate("migrateisUsingGlobalNostrKey")) {
console.info("Running migration for: migrateisUsingGlobalNostrKey");
await migrations["migrateisUsingGlobalNostrKey"]();
await setMigrated("migrateisUsingGlobalNostrKey");
}
if (shouldMigrate("ensureAccountId")) {
console.info("Running migration for: ensureAccountId");
await migrations["ensureAccountId"]();
await setMigrated("ensureAccountId");
}
if (shouldMigrate("migratePermissionsWithoutAccountId")) {
console.info("Running migration for: migratePermissionsWithoutAccountId");
await migrations["migratePermissionsWithoutAccountId"]();
await setMigrated("migratePermissionsWithoutAccountId");
}
// example:
//if (shouldMigrate("migratePermissionsWithoutAccountId")) {
// console.info("Running migration for: migratePermissionsWithoutAccountId");
// await migrations["migratePermissionsWithoutAccountId"]();
// await setMigrated("migratePermissionsWithoutAccountId");
//}
};

export default migrate;