From ec6d41bf25b634b841fea0bbf160546010fd348c Mon Sep 17 00:00:00 2001 From: Ignazio Bovo Date: Tue, 12 Sep 2023 14:41:38 +0200 Subject: [PATCH] fix: :ambulance: fix accountId global counter not being migrated (#188) --- CHANGELOG.md | 4 ++++ src/utils/offchainState.ts | 33 ++++++++++++++++++++++++++++++++- 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b0a99a4a8..96456c85f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,8 @@ # 3.0.1 +### Misc +- add migration for the `Account` id field +- exposes the grahql api to any unauthentcate user +- adds an index to the `videoRelevance` field for the `Video` entity, used when loading the home page by gateways ### Bug Fixes: Fixed: Added the locking mechanism to prevent multiple asynchronous operation from having concurrent access to Overlay (Orion processor's in-memory cache layer), which otherwise could lead to one asynchronous operation overriding the changes made (to the cache) by the other asynchronous operation. diff --git a/src/utils/offchainState.ts b/src/utils/offchainState.ts index 65f72bdf1..a3e8dc5be 100644 --- a/src/utils/offchainState.ts +++ b/src/utils/offchainState.ts @@ -4,6 +4,7 @@ import path from 'path' import { createLogger } from '@subsquid/logger' import assert from 'assert' import { uniqueId } from './crypto' +import { NextEntityId } from '../model' const DEFAULT_EXPORT_PATH = path.resolve(__dirname, '../../db/export/export.json') @@ -73,6 +74,11 @@ export class OffchainState { private logger = createLogger('offchainState') private _isImported = false + private globalCountersMigration = { + // destination version : [global counters names] + '3.0.1': ['Account'], + } + private migrations: Migrations = { '3.0.0': migrateExportDataToV300, } @@ -157,7 +163,8 @@ export class OffchainState { `Cannot perform offchain data import! Export file ${exportFilePath} does not exist!` ) } - const data = this.prepareExportData(JSON.parse(fs.readFileSync(exportFilePath, 'utf-8')), em) + const exportFile = JSON.parse(fs.readFileSync(exportFilePath, 'utf-8')) + const data = this.prepareExportData(exportFile, em) this.logger.info('Importing offchain state') for (const [entityName, { type, values }] of Object.entries(data)) { if (!values.length) { @@ -226,6 +233,10 @@ export class OffchainState { `Done ${type === 'update' ? 'updating' : 'inserting'} ${entityName} entities` ) } + // migrate counters for NextEntityId + const { orionVersion } = exportFile + await this.migrateCounters(orionVersion, em) + const renamedExportFilePath = `${exportFilePath}.imported` this.logger.info(`Renaming export file to ${renamedExportFilePath})...`) fs.renameSync(exportFilePath, renamedExportFilePath) @@ -243,4 +254,24 @@ export class OffchainState { this.logger.info(`Last export block number established: ${blockNumber}`) return blockNumber } + + private async migrateCounters(exportedVersion: string, em: EntityManager): Promise { + const migrationData = Object.entries(this.globalCountersMigration).sort( + ([a], [b]) => this.versionToNumber(a) - this.versionToNumber(b) + ) // sort in increasing order + + for (const [version, counters] of migrationData) { + if (this.versionToNumber(exportedVersion) < this.versionToNumber(version)) { + this.logger.info(`Migrating global counters to version ${version}`) + for (const entityName of counters) { + // build query that gets the entityName with the highest id + const rowNumber = await em.query(`SELECT COUNT(*) FROM ${entityName}`) + const latestId = parseInt(rowNumber[0].count) + + this.logger.info(`Setting next id for ${entityName} to ${latestId + 1}`) + await em.save(new NextEntityId({ entityName, nextId: latestId + 1 })) + } + } + } + } }