From c1d8d62932b56d9dd607682e9d501371a21e3274 Mon Sep 17 00:00:00 2001 From: Ignazio Bovo Date: Wed, 2 Aug 2023 14:30:19 +0200 Subject: [PATCH] feat: :art: migrate next entity id for account --- src/tests/integration/Alice.json | 1 + src/tests/integration/Bob.json | 1 + src/utils/offchainState.ts | 31 +++++++++++++++++++++++++++++-- 3 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 src/tests/integration/Alice.json create mode 100644 src/tests/integration/Bob.json diff --git a/src/tests/integration/Alice.json b/src/tests/integration/Alice.json new file mode 100644 index 000000000..263afc30c --- /dev/null +++ b/src/tests/integration/Alice.json @@ -0,0 +1 @@ +{"USER_URI": "Alice", "USER_MEMBER_ID": "16", "LOGIN_SESSION_ID": "", "USER": "j4W7rVcUCxi2crhhjRq46fNDRbVHTjJrz6bKxZwehEMQxZeSf"} diff --git a/src/tests/integration/Bob.json b/src/tests/integration/Bob.json new file mode 100644 index 000000000..7dda6919a --- /dev/null +++ b/src/tests/integration/Bob.json @@ -0,0 +1 @@ +{"USER_URI": "Bob", "USER_MEMBER_ID": "17", "LOGIN_SESSION_ID": "", "USER": "j4UYhDYJ4pz2ihhDDzu69v2JTVeGaGmTebmBdWaX2ANVinXyE"} diff --git a/src/utils/offchainState.ts b/src/utils/offchainState.ts index 65f72bdf1..a59463647 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.1.0': ['Account'], + } + private migrations: Migrations = { '3.0.0': migrateExportDataToV300, } @@ -141,7 +147,7 @@ export class OffchainState { public prepareExportData(exportState: ExportedState, em: EntityManager): ExportedData { let { data } = exportState Object.entries(this.migrations) - .sort(([a], [b]) => this.versionToNumber(a) - this.versionToNumber(b)) + .sort(([a], [b]) => this.versionToNumber(a) - this.versionToNumber(b)) // sort in increasing order .forEach(([version, fn]) => { if (this.versionToNumber(exportState.orionVersion || '0') < this.versionToNumber(version)) { this.logger.info(`Migrating export data to version ${version}`) @@ -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,11 @@ export class OffchainState { `Done ${type === 'update' ? 'updating' : 'inserting'} ${entityName} entities` ) } + + // migrate counters for NextEntityId + const { version } = exportFile + await this.migrateCounters(version, em) + const renamedExportFilePath = `${exportFilePath}.imported` this.logger.info(`Renaming export file to ${renamedExportFilePath})...`) fs.renameSync(exportFilePath, renamedExportFilePath) @@ -243,4 +255,19 @@ export class OffchainState { this.logger.info(`Last export block number established: ${blockNumber}`) return blockNumber } + + private async migrateCounters(exportedVersion: string, em: EntityManager): Promise { + Object.entries(this.globalCountersMigration) + .sort(([a], [b]) => this.versionToNumber(a) - this.versionToNumber(b)) // sort in increasing order + .forEach(([version, counters]) => { + if (this.versionToNumber(exportedVersion) < this.versionToNumber(version)) { + this.logger.info(`Migrating global counters to version ${version}`) + counters.forEach(async (entityName) => { + // build query that gets the entityName with the highest id + const latestId = await em.query(`SELECT id FROM ${entityName} ORDER BY id DESC LIMIT 1`) + await em.save(new NextEntityId({ entityName, nextId: Number(latestId) + 1 })) + }) + } + }) + } }