Skip to content

Commit

Permalink
feat: add database versioning (#1489)
Browse files Browse the repository at this point in the history
- Closes #1490
  • Loading branch information
LuizAsFight authored Sep 18, 2024
1 parent 48d4df4 commit 9f4334c
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 37 deletions.
8 changes: 8 additions & 0 deletions .changeset/strong-falcons-worry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@fuel-wallet/connections": patch
"@fuel-wallet/types": patch
"fuels-wallet": patch
"@fuels/playwright-utils": patch
---

feat: add db versioning
39 changes: 2 additions & 37 deletions packages/app/src/systems/Core/utils/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ import type {
import type { DbEvents, PromiseExtended, Table } from 'dexie';
import Dexie from 'dexie';
import 'dexie-observable';
import { CHAIN_IDS, DEVNET_NETWORK_URL, TESTNET_NETWORK_URL } from 'fuels';
import { DATABASE_VERSION } from '~/config';
import type { Transaction } from '~/systems/Transaction/types';
import { applyDbVersioning } from './databaseVersioning';

type FailureEvents = Extract<keyof DbEvents, 'close' | 'blocked'>;

Expand All @@ -33,41 +32,7 @@ export class FuelDB extends Dexie {

constructor() {
super('FuelDB');
this.version(DATABASE_VERSION)
.stores({
vaults: 'key',
accounts: '&address, &name',
networks: '&id, chainId, &url, &name',
connections: 'origin',
transactions: '&id',
assets: '&assetId, &name, &symbol',
abis: '&contractId',
errors: '&id',
})
.upgrade(async (tx) => {
const networks = tx.table('networks');

// Clean networks
await networks.clear();

// Insert testnet network
await networks.add({
chainId: CHAIN_IDS.fuel.testnet,
name: 'Fuel Sepolia Testnet',
url: TESTNET_NETWORK_URL,
isSelected: true,
id: createUUID(),
});

// Insert devnet network
await networks.add({
chainId: CHAIN_IDS.fuel.devnet,
name: 'Fuel Ignition Sepolia Devnet',
url: DEVNET_NETWORK_URL,
isSelected: false,
id: createUUID(),
});
});
applyDbVersioning(this);
this.setupListeners();
}

Expand Down
41 changes: 41 additions & 0 deletions packages/app/src/systems/Core/utils/databaseVersioning.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { createUUID } from '@fuel-wallet/connections';
import type Dexie from 'dexie';
import { CHAIN_IDS, DEVNET_NETWORK_URL, TESTNET_NETWORK_URL } from 'fuels';

export const applyDbVersioning = (db: Dexie) => {
db.version(19)
.stores({
vaults: 'key',
accounts: '&address, &name',
networks: '&id, &url, &name',
connections: 'origin',
transactions: '&id',
assets: '&assetId, &name, &symbol',
abis: '&contractId',
errors: '&id',
})
.upgrade(async (tx) => {
const networks = tx.table('networks');

// Clean networks
await networks.clear();

// Insert testnet network
await networks.add({
chainId: CHAIN_IDS.fuel.testnet,
name: 'Fuel Sepolia Testnet',
url: TESTNET_NETWORK_URL,
isSelected: true,
id: createUUID(),
});

// Insert devnet network
await networks.add({
chainId: CHAIN_IDS.fuel.devnet,
name: 'Fuel Ignition Sepolia Devnet',
url: DEVNET_NETWORK_URL,
isSelected: false,
id: createUUID(),
});
});
};

0 comments on commit 9f4334c

Please sign in to comment.