Skip to content

Commit

Permalink
[offchainState] fix NextEntityId migration script
Browse files Browse the repository at this point in the history
  • Loading branch information
zeeshanakram3 committed Mar 14, 2024
1 parent 3ea30ed commit 5a6d1b9
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ processor.run(new TypeormDatabase({ isolationLevel: 'READ COMMITTED' }), async (
// there is no need to recalc video relevance before orion is synced
await overlay.updateDatabase()
const em = overlay.getEm()
await offchainState.import(em)
await offchainState.import(overlay)
await commentCountersManager.updateVideoCommentsCounters(em, true)
await commentCountersManager.updateParentRepliesCounters(em, true)
await videoRelevanceManager.updateVideoRelevanceValue(em, true)
Expand Down
40 changes: 28 additions & 12 deletions src/utils/offchainState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
} from '../model'
import { uniqueId } from './crypto'
import { defaultNotificationPreferences, notificationPrefAllTrue } from './notification/helpers'
import { EntityManagerOverlay } from './overlay'

const DEFAULT_EXPORT_PATH = path.resolve(__dirname, '../../db/export/export.json')

Expand Down Expand Up @@ -246,7 +247,32 @@ export class OffchainState {
return data
}

public async import(em: EntityManager, exportFilePath = DEFAULT_EXPORT_PATH): Promise<void> {
private async importNextEntityIdCounters(
overlay: EntityManagerOverlay,
entityName: string,
data: Record<string, unknown>[]
) {
const em = overlay.getEm()
assert(entityName === 'NextEntityId')
for (const record of data) {
if (em.connection.hasMetadata(record.entityName as string)) {
// reason: during migration the overlay would write to the database the
// old nextId, to avoid that directly set the 'nextId' in the Overlay
overlay
.getRepository(model[record.entityName as keyof typeof model] as any)
.setNextEntityId(record.nextId as number)
} else {
await em.getRepository(entityName).upsert(record, ['entityName'])
}
}
}

public async import(
overlay: EntityManagerOverlay,
exportFilePath = DEFAULT_EXPORT_PATH
): Promise<void> {
const em = overlay.getEm()

if (!fs.existsSync(exportFilePath)) {
throw new Error(
`Cannot perform offchain data import! Export file ${exportFilePath} does not exist!`
Expand Down Expand Up @@ -318,17 +344,7 @@ export class OffchainState {

// UPSERT operation specifically for NextEntityId
if (entityName === 'NextEntityId') {
for (const entity of batch) {
await em.query(
`
INSERT INTO "next_entity_id" ("entity_name", "next_id")
VALUES ($1, $2)
ON CONFLICT (entity_name)
DO UPDATE SET next_id = EXCLUDED.next_id;
`,
[entity.entityName, entity.nextId]
)
}
await this.importNextEntityIdCounters(overlay, entityName, batch)
} else {
await em.getRepository(entityName).insert(batch)
}
Expand Down
4 changes: 4 additions & 0 deletions src/utils/overlay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ export class RepositoryOverlay<E extends AnyEntity = AnyEntity> {
return this.nextId
}

setNextEntityId(nextId: number) {
this.nextId = nextId
}

// Prevents inserting strings that contain null character into the postgresql table
// (as this would cause an error)
private normalizeString(s: string) {
Expand Down

0 comments on commit 5a6d1b9

Please sign in to comment.