Skip to content

Commit

Permalink
Merge pull request #2384 from getAlby/cleanup-migrations1
Browse files Browse the repository at this point in the history
chore: cleanup migrations
  • Loading branch information
bumi authored May 2, 2023
2 parents 044b395 + 4758867 commit b0fd966
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 111 deletions.

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;

0 comments on commit b0fd966

Please sign in to comment.