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

refactoring: unique constraint for contract.address and codeBundle.codeHash #437

Closed
wants to merge 2 commits into from

Conversation

peetzweg
Copy link
Collaborator

Adds unique constraint(&) to contract.address and codeBundle.codeHash to prevent duplicate inside these tables by the actual db schema.

We could potentially change this schema still to use the address/codeHash as the primary key instead of an autoincremented number.

Happy to discuss if we want to go with a change like this or leave the database as is.

@netlify
Copy link

netlify bot commented Mar 17, 2023

Deploy Preview for contracts-ui ready!

Name Link
🔨 Latest commit 7bb1cca
🔍 Latest deploy log https://app.netlify.com/sites/contracts-ui/deploys/641892fbaff6e700071a5f79
😎 Deploy Preview https://deploy-preview-437--contracts-ui.netlify.app/
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@peetzweg peetzweg requested a review from statictype March 17, 2023 12:18
@statictype statictype changed the title change: unique constraint for contract.address and codeBundle.codeHash refactoring: unique constraint for contract.address and codeBundle.codeHash Mar 20, 2023
@peetzweg
Copy link
Collaborator Author

@statictype

In order to test this a bit I've added this function to the db/index.ts. It uses some fixtures I've added to the branch as well to add contracts and code bundles in the style of v1 into a fresh indexeddb. This populate will only run if there is no database available.

https://github.com/paritytech/contracts-ui/blob/7bb1cca03ea7c73bf366f0f03046e3002621baf8/src/db/index.ts#L78-L92

So to test you should comment out version(2) and version(3) and add the populate line directly under version(1) in the constructor. So it looks like this:

constructor(genesisHash: string) {
    super(`contracts-ui__${genesisHash}`);

    this.version(1).stores({
      codeBundles: '++id, codeHash, name, date',
      contracts: '++id, address, codeHash, name, date',
    });
    this._populateWithMockDataV1()
    // version 2 & 3 not executing.
}

Than drop all your indexedDB databases and refresh. It should restore the fixtures as a fresh database needs to be created. Check the contents of the database it contains duplicates of contracts and codeBundles now.

After that you can remove the this._populateWithMockDataV1() line again and comment back in the version 2 & 3 to execute the migration on your next visit.

A version 3 is needed to just add the unique constraint on the cleaned up data from the version 2 migration. I wasn't able to achieve this in a single migration, dexie crashed on this.

@statictype
Copy link
Contributor

will close this for now due to limited availability

@statictype statictype closed this Jun 15, 2023
@peetzweg peetzweg deleted the upgrade-db-schema branch November 23, 2023 15:32
@peetzweg peetzweg restored the upgrade-db-schema branch November 23, 2023 15:32
@peetzweg peetzweg deleted the upgrade-db-schema branch April 15, 2024 10:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants