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

switch to using IDB_VERSION defined inside the package #1341

Merged
merged 2 commits into from
Jun 20, 2024
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
5 changes: 5 additions & 0 deletions .changeset/dirty-turtles-fail.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@penumbra-zone/storage': major
---

switch to using IDB_VERSION defined inside the package
7 changes: 3 additions & 4 deletions packages/storage/src/indexed-db/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ import { ChainRegistryClient } from '@penumbra-labs/registry';
import { PartialMessage } from '@bufbuild/protobuf';
import { getAmountFromRecord } from '@penumbra-zone/getters/spendable-note-record';
import { isZero } from '@penumbra-zone/types/amount';
import { IDB_VERSION } from './config';

interface IndexedDbProps {
idbVersion: number; // Incremented during schema changes
chainId: string;
walletId: WalletId;
registryClient: ChainRegistryClient;
Expand All @@ -84,15 +84,14 @@ export class IndexedDb implements IndexedDbInterface {
) {}

static async initialize({
idbVersion,
walletId,
chainId,
registryClient,
}: IndexedDbProps): Promise<IndexedDb> {
const bech32Id = bech32mWalletId(walletId);
const idbName = `viewdata/${chainId}/${bech32Id}`;

const db = await openDB<PenumbraDb>(idbName, idbVersion, {
const db = await openDB<PenumbraDb>(idbName, IDB_VERSION, {
upgrade(db: IDBPDatabase<PenumbraDb>) {
// delete existing ObjectStores before re-creating them
// all existing indexed-db data will be deleted when version is increased
Expand Down Expand Up @@ -134,7 +133,7 @@ export class IndexedDb implements IndexedDbInterface {
});
const constants = {
name: idbName,
version: idbVersion,
version: IDB_VERSION,
tables: IDB_TABLES,
} satisfies IdbConstants;

Expand Down
7 changes: 3 additions & 4 deletions packages/storage/src/indexed-db/indexed-db.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ describe('IndexedDb', () => {
const chainId = 'penumbra-testnet-deimos-6';
const generateInitialProps = () => ({
chainId,
idbVersion: 1,
walletId: new WalletId({
inner: Uint8Array.from({ length: 32 }, () => Math.floor(Math.random() * 256)),
}),
Expand Down Expand Up @@ -98,15 +97,16 @@ describe('IndexedDb', () => {
expect((await dbB.getAssetsMetadata(metadataA.penumbraAssetId!))?.name).toBe(metadataA.name);
});

it('increasing version should re-create object stores', async () => {
// TODO: Do not skip this test after vitest has been updated to v2.0.0.
// use vi.mock to override the IDB_VERSION value (vi.mock is not available in browser mode for vitest 1.6.0).
it.skip('increasing version should re-create object stores', async () => {
const version1Props = generateInitialProps();
const dbA = await IndexedDb.initialize(version1Props);
await dbA.saveAssetsMetadata(metadataA);
dbA.close();

const version2Props = {
chainId,
idbVersion: 2,
walletId: version1Props.walletId,
registryClient: new ChainRegistryClient(),
};
Expand Down Expand Up @@ -289,7 +289,6 @@ describe('IndexedDb', () => {
it('should be pre-loaded with hardcoded assets', async () => {
const propsWithAssets = {
chainId,
idbVersion: 2,
walletId: new WalletId({
inner: Uint8Array.from({ length: 32 }, () => Math.floor(Math.random() * 256)),
}),
Expand Down
Loading