Skip to content

Commit

Permalink
checkpoint for migration
Browse files Browse the repository at this point in the history
  • Loading branch information
SebastienGllmt committed Oct 9, 2019
1 parent 9f19715 commit 9636640
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 22 deletions.
9 changes: 4 additions & 5 deletions app/api/ada/lib/storage/adaMigration.js
Expand Up @@ -4,6 +4,7 @@

import type { lf$Database } from 'lovefield';
import {
resetLegacy,
getLegacyAddressesList,
} from './database/legacy';
import LocalStorageApi from '../../../localStorage/index';
Expand All @@ -21,7 +22,6 @@ export async function migrateToLatest(
localStorageApi: LocalStorageApi,
persistentDb: lf$Database,
): Promise<boolean> {
return; // TODO: add migration for storagev2
let lastLaunchVersion = await localStorageApi.getLastLaunchVersion();
Logger.info(`Starting migration for ${lastLaunchVersion}`);
/**
Expand Down Expand Up @@ -54,7 +54,7 @@ export async function migrateToLatest(
}
return applied;
}],
['<1.4.0', async () => await bip44Migration(persistentDb)],
['<1.4.0', async () => await bip44Migration()],
['<1.10.0', async () => await storagev2Migation(persistentDb)],
];

Expand Down Expand Up @@ -116,10 +116,9 @@ async function moveStorage(localStorageApi: LocalStorageApi): Promise<boolean> {
* or you use the same wallet on Chrome + mobile.
*/
async function bip44Migration(
persistentDb: lf$Database,
): Promise<boolean> {
Logger.info(`Starting bip44Migration`);
const addresses = await getLegacyAddressesList(persistentDb);
const addresses = await getLegacyAddressesList();

/**
* We used to consider all addresses in the DB as explicitly generated by the user
Expand All @@ -145,7 +144,7 @@ async function bip44Migration(
* We need to do this since old wallets may have incorrect history
* Due to desync issue caused by the incorrect bip44 implementation
*/
await reset(persistentDb);
await resetLegacy();

return true;
}
Expand Down
14 changes: 10 additions & 4 deletions app/api/ada/lib/storage/database/index.js
Expand Up @@ -62,7 +62,7 @@ export const loadLovefieldDB = async (
const populateAndCreate = async (
storeType: $Values<typeof schema.DataStoreType>
): Promise<lf$Database> => {
const schemaBuilder = schema.create('yoroi-schema', 2);
const schemaBuilder = schema.create('yoroi-schema', 7);

populateUncategorizedDb(schemaBuilder);
populateBip44Db(schemaBuilder);
Expand All @@ -87,6 +87,13 @@ export async function clear(
await tx.commit();
}

/**
* expose dump of previous DB version so we can use it for migration
* Note: all connection types reuse this variable unfortunately
* since there is no way to detect the database type given just the raw back store
*/
export const dumpByVersion: { [tableName: string]: Array<any> } = {};

async function onUpgrade(
rawDb: lf$raw$BackStore,
): Promise<void> {
Expand All @@ -95,10 +102,9 @@ async function onUpgrade(
// defaults to 0 when first time launching ever
return;
}
const dump = await rawDb.dump();
if (version === 1) {
// TODO: expose dump for migration
const dump = rawDb.dump();

Object.assign(dumpByVersion, dump);
rawDb.dropTable('TxAddresses');
rawDb.dropTable('Txs');
rawDb.dropTable('Addresses');
Expand Down
38 changes: 26 additions & 12 deletions app/api/ada/lib/storage/database/legacy.js
@@ -1,9 +1,6 @@
// @flow

import type {
AdaAddress,
} from '../../../adaTypes';
import type { lf$Database } from 'lovefield';
import { dumpByVersion } from './index';

/**
* This file contains methods used to extract information
Expand All @@ -12,12 +9,29 @@ import type { lf$Database } from 'lovefield';
* to migrate to a new format
*/

export const getLegacyAddressesList = (
db: lf$Database
): Promise<Array<AdaAddress>> => {
const addressesTable = db.getSchema().table('Addresses');
return db.select()
.from(addressesTable)
.exec()
.then(rows => rows.map(row => row.value));
export type LegacyAddressingInfo = {
account: number,
change: number,
index: number,
};
export type LegacyAdaAmount = {
getCCoin: string,
};
export type LegacyAdaAddress = {
cadAmount: LegacyAdaAmount,
cadId: string,
cadIsUsed: boolean,
} & LegacyAddressingInfo;

export const getLegacyAddressesList = (): Array<LegacyAdaAddress> => {
if (dumpByVersion.Addresses) {
return dumpByVersion.Addresses;
}
return [];
};

export const resetLegacy = (): void => {
for (const prop of Object.keys(dumpByVersion)) {
delete dumpByVersion[prop];
}
};
3 changes: 3 additions & 0 deletions app/api/ada/lib/storage/tests/adaMigration.test.js
@@ -0,0 +1,3 @@
// @flow

import { migrateToLatest } from '../adaMigration';
2 changes: 1 addition & 1 deletion flow-typed/npm/lovefield_v2.x.x.js
Expand Up @@ -178,7 +178,7 @@ declare module 'lovefield' {
): Promise<void>;
createRow(payload: Object): lf$Row;
getVersion(): number;
dump(): $ReadOnlyArray<$ReadOnly<Object>>;
dump(): Promise<{ [tableName: string]: Array<any> }>;
}

declare var npm$namespace$lf$schema: {
Expand Down

0 comments on commit 9636640

Please sign in to comment.