From fa7b63f0ee7e0f897df6d5d077b84548dff0ba67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s?= <105802444+kilted-andres@users.noreply.github.com> Date: Wed, 11 Dec 2024 14:48:22 +0100 Subject: [PATCH] feat: adapt to Indexer's new block ID (#749) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: logic to migrate blocks from string to integers * fix: handle search column * feat: paranoic down transformation * fix: change block type to integer on ctype model * fix: handle the null/zero cases explicitly * fix: mocked cTypes to use numbers for blocks * feat: make Integers Big on typeScript code * fix: make properties explainers available elsewhere * feat: make Integers Big on migration * fix: down migration * fix: delete migration to try new approach * feat: new tiny migration * fix: handle search dependency on migration * Squashed commit of the following: commit e26c54b038ec726b4b0bc87f8ba2f1955a965454 Author: Andrés <105802444+kilted-andres@users.noreply.github.com> Date: Fri Dec 6 11:30:37 2024 +0100 feat: simplify sequelize usage (#747) Rely on `dotenv` to load the environment variable `DATABASE_URI` to make it available for the sequelize configuration. On this way, there is no need to export the variable value manually. commit fc68a245dbe38b6c3f193433416bb6d192786883 Author: Andrés <105802444+kilted-andres@users.noreply.github.com> Date: Fri Dec 6 11:25:33 2024 +0100 chore: upgrade to node v22 (#748) * fix: use raw SQL to cast into BigInt --- .../CTypeDetails/CTypeDetails.astro | 2 +- .../20241206171516-integrateBlockNumbers.cjs | 57 +++++++++++++++++++ src/models/ctype.ts | 4 +- src/utilities/indexer/queryCTypes.ts | 11 ++-- src/utilities/mockCTypes.ts | 10 ++-- 5 files changed, 72 insertions(+), 12 deletions(-) create mode 100644 src/migrations/20241206171516-integrateBlockNumbers.cjs diff --git a/src/components/CTypeDetails/CTypeDetails.astro b/src/components/CTypeDetails/CTypeDetails.astro index bb708cce..39b15306 100644 --- a/src/components/CTypeDetails/CTypeDetails.astro +++ b/src/components/CTypeDetails/CTypeDetails.astro @@ -53,7 +53,7 @@ linkToW3N.pathname = web3Name ?? creator; const linkToBlock = new URL(`https://polkadot.js.org/apps/`); linkToBlock.searchParams.set('rpc', blockchainEndpoint); -linkToBlock.hash = `/explorer/${block ? 'query/' + block : ''}`; +linkToBlock.hash = `/explorer/${block != null ? 'query/' + block : ''}`; ---
diff --git a/src/migrations/20241206171516-integrateBlockNumbers.cjs b/src/migrations/20241206171516-integrateBlockNumbers.cjs new file mode 100644 index 00000000..09adc2ff --- /dev/null +++ b/src/migrations/20241206171516-integrateBlockNumbers.cjs @@ -0,0 +1,57 @@ +'use strict'; + +/** @type {import('sequelize-cli').Migration} */ +module.exports = { + async up(queryInterface, Sequelize) { + // First, drop the 'search' column since it depends on 'block' + await queryInterface.removeColumn('CTypes', 'search'); + + // Transformation the type of 'block' from string to bigint + await queryInterface.changeColumn('CTypes', 'block', { + type: 'BIGINT using (block::bigint)', + allowNull: true, + }); + // recreate the 'search' column + await queryInterface.addColumn( + 'CTypes', + 'search', + `tsvector generated always as (to_tsvector('english', + coalesce("id"::text, '') || ' ' || + coalesce("schema"::text, '') || ' ' || + coalesce("title"::text, '') || ' ' || + coalesce("properties"::text, '') || ' ' || + coalesce("type"::text, '') || ' ' || + coalesce("creator"::text, '') || ' ' || + coalesce("block"::text, '') || ' ' || + coalesce("description"::text, '')) + ) stored`, + ); + }, + + async down(queryInterface, Sequelize) { + // First, drop the 'search' column since it depends on 'block' + await queryInterface.removeColumn('CTypes', 'search'); + + // Transformation the type of 'block' from bigint to string + await queryInterface.changeColumn('CTypes', 'block', { + type: Sequelize.STRING, + allowNull: true, + }); + + // recreate the 'search' column + await queryInterface.addColumn( + 'CTypes', + 'search', + `tsvector generated always as (to_tsvector('english', + coalesce("id"::text, '') || ' ' || + coalesce("schema"::text, '') || ' ' || + coalesce("title"::text, '') || ' ' || + coalesce("properties"::text, '') || ' ' || + coalesce("type"::text, '') || ' ' || + coalesce("creator"::text, '') || ' ' || + coalesce("block"::text, '') || ' ' || + coalesce("description"::text, '')) + ) stored`, + ); + }, +}; diff --git a/src/models/ctype.ts b/src/models/ctype.ts index 3c57227a..9140be62 100644 --- a/src/models/ctype.ts +++ b/src/models/ctype.ts @@ -13,7 +13,7 @@ interface CTypeDataInput extends Omit { schema: ICType['$schema']; creator: DidUri; createdAt: Date; - block: string | null; + block: bigint | null; description: string | null; attestationsCreated?: number; } @@ -55,7 +55,7 @@ export const CTypeModelDefinition: ModelAttributes = { allowNull: false, }, block: { - type: DataTypes.STRING, + type: DataTypes.BIGINT, }, description: { type: DataTypes.STRING, diff --git a/src/utilities/indexer/queryCTypes.ts b/src/utilities/indexer/queryCTypes.ts index 3aced463..c67e1dc6 100644 --- a/src/utilities/indexer/queryCTypes.ts +++ b/src/utilities/indexer/queryCTypes.ts @@ -42,11 +42,14 @@ interface QueriedCType { web3NameId: string; }; registrationBlock: { - id: string; // Block Ordinal Number, without punctuation + /** Block Ordinal Number, without punctuation */ + id: string; hash: HexString; - timeStamp: string; // ISO8601 Date String, like 2022-02-09T13:09:18.217 + /** ISO8601 Date String, like 2022-02-09T13:09:18.217 */ + timeStamp: string; }; - definition: string; // stringified JSON of cType Schema + /** Stringified JSON of cType Schema */ + definition: string; } export async function queryCTypes() { @@ -83,7 +86,7 @@ export async function queryCTypes() { schema: $schema, createdAt: new Date(registrationBlock.timeStamp + 'Z'), creator, - block: registrationBlock.id, + block: BigInt(registrationBlock.id), ...rest, attestationsCreated, }); diff --git a/src/utilities/mockCTypes.ts b/src/utilities/mockCTypes.ts index ef2c6935..efd8b63e 100644 --- a/src/utilities/mockCTypes.ts +++ b/src/utilities/mockCTypes.ts @@ -10,7 +10,7 @@ export const mockCTypes: Record = { creator: 'did:kilt:4pehddkhEanexVTTzWAtrrfo2R7xPnePpuiJLC7shQU894aY', createdAt: new Date('2023-05-01T12:00:00'), description: 'This is some example cType data', - block: '123', + block: 123n, attestationsCreated: 1, tags: [ { @@ -35,7 +35,7 @@ export const mockCTypes: Record = { creator: 'did:kilt:4pehddkhEanexVTTzWAtrrfo2R7xPnePpuiJLC7shQU894aY', createdAt: new Date('2023-05-01T12:01:00'), description: 'This is an example of a CType with a nested property', - block: '321', + block: 321n, attestationsCreated: 22, isHidden: false, }, @@ -55,7 +55,7 @@ export const mockCTypes: Record = { creator: 'did:kilt:4pehddkhEanexVTTzWAtrrfo2R7xPnePpuiJLC7shQU894aY', createdAt: new Date('2023-05-01T12:02:00'), description: 'This is an example of a CType with a nested CType', - block: '456', + block: 456n, attestationsCreated: 333, isHidden: false, }, @@ -68,7 +68,7 @@ export const mockCTypes: Record = { creator: 'did:kilt:4pehddkhEanexVTTzWAtrrfo2R7xPnePpuiJLC7shQU894aY', createdAt: new Date('2023-05-01T12:00:00'), description: 'This is some example cType data', - block: '123', + block: 123n, attestationsCreated: 1, tags: [ { @@ -121,7 +121,7 @@ export const mockCTypes: Record = { creator: 'did:kilt:4rrkiRTZgsgxjJDFkLsivqqKTqdUTuxKk3FX3mKFAeMxsR5E', attestationsCreated: 4444, createdAt: new Date('2023-05-01T12:03:00'), - block: '456', + block: 456n, isHidden: false, }, };