Skip to content

Commit

Permalink
fix: clean up orphan currencies
Browse files Browse the repository at this point in the history
  • Loading branch information
N3TC4T committed Nov 27, 2024
1 parent e5567e9 commit 2743f21
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 6 deletions.
9 changes: 9 additions & 0 deletions src/store/__tests__/fixture/v1.test.data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ export default {
registerAt: new Date(),
updatedAt: new Date(),
},

[schemas.CurrencySchema.schema.name]: {
id: 'ORPHAN.ORP',
issuer: 'ORPHAN',
currency: 'ORP',
name: 'Orphaned currency',
avatar: 'https://cdn.image.com',
},
[schemas.AccountSchema.schema.name]: {
address: 'rADDRESSxxxxxxxxxxxxxxxxxxxxxxxxxx',
label: 'Personal account',
Expand Down Expand Up @@ -67,6 +75,7 @@ export default {
registerAt: new Date(),
updatedAt: new Date(),
},

[schemas.ProfileSchema.schema.name]: {
username: 'my username',
slug: 'my slug',
Expand Down
21 changes: 19 additions & 2 deletions src/store/__tests__/integration/migrations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,12 +269,29 @@ describe('Storage', () => {
});

it('should run v18 migrations successfully', async () => {
const v17Instance = RealmTestUtils.getRealmInstanceWithVersion(17);
expect(v17Instance.schemaVersion).toBe(17);

const orphanCurrency = RealmTestUtils.getSecondModelItem(v17Instance, 'Currency');

expect(orphanCurrency).toBeDefined();
expect(orphanCurrency.id).toBe('ORPHAN.ORP');
expect(orphanCurrency.linkingObjectsCount()).toBe(0);

v17Instance.close();

const instance = RealmTestUtils.getRealmInstanceWithVersion(18);
expect(instance.schemaVersion).toBe(18);

const currency = RealmTestUtils.getFirstModelItem(instance, 'Currency');
// let's check if orphan objects has been removed
const newCurrencies = RealmTestUtils.getAllModelItem(instance, 'Currency');

for (const currency of newCurrencies) {
expect(currency.id).not.toBe('ORPHAN.ORP');
}

// renamed fields
const currency = RealmTestUtils.getFirstModelItem(instance, 'Currency');
// // renamed fields
expect(currency.xappIdentifier).toBeDefined();
expect(currency.avatarUrl).toBeDefined();
// force the token to update
Expand Down
8 changes: 8 additions & 0 deletions src/store/__tests__/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,18 @@ const RealmTestUtils = {
RealmPath: './.jest/realmTemp',
EncryptionKey: new Int8Array(64),

getAllModelItem: (instance: Realm, schemaName: string): any => {
return instance.objects(schemaName) as any;
},

getFirstModelItem: (instance: Realm, schemaName: string): any => {
return instance.objects(schemaName)[0] as any;
},

getSecondModelItem: (instance: Realm, schemaName: string): any => {
return instance.objects(schemaName)[1] as any;
},

getSchemaWithVersion: (version: number) => {
return find(require('../../models/schemas').default, { schemaVersion: version });
},
Expand Down
19 changes: 15 additions & 4 deletions src/store/models/schemas/v18/currency.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,22 @@ const CurrencySchema = {
const oldObjects = oldRealm.objects(CurrencySchema.schema.name) as any;
const newObjects = newRealm.objects(CurrencySchema.schema.name) as any;

const orphanObjects = [] as any;

// clear up orphan currencies
for (let i = 0; i < newObjects.length; i++) {
newObjects[i].xappIdentifier = oldObjects.find((c: any) => c.id === newObjects[i].id).xapp_identifier;
newObjects[i].avatarUrl = oldObjects.find((c: any) => c.id === newObjects[i].id).avatar;
// NOTE: this will force update the token details
newObjects[i].updatedAt = new Date(0);
if (newObjects[i].linkingObjectsCount() > 0) {
newObjects[i].xappIdentifier = oldObjects.find((c: any) => c.id === newObjects[i].id).xapp_identifier;
newObjects[i].avatarUrl = oldObjects.find((c: any) => c.id === newObjects[i].id).avatar;
// NOTE: this will force update the token details
newObjects[i].updatedAt = new Date(0);
} else {
orphanObjects.push(newObjects[i]);
}
}

if (orphanObjects.length > 0) {
newRealm.delete(orphanObjects);
}
},
};
Expand Down

0 comments on commit 2743f21

Please sign in to comment.