Skip to content

Commit

Permalink
fix: 🚑 fix accountId global counter not being migrated (Joystream#188)
Browse files Browse the repository at this point in the history
  • Loading branch information
ignazio-bovo authored Sep 12, 2023
1 parent 002b1f0 commit ec6d41b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
33 changes: 32 additions & 1 deletion src/utils/offchainState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down Expand Up @@ -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,
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
Expand All @@ -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<void> {
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 }))
}
}
}
}
}

0 comments on commit ec6d41b

Please sign in to comment.