Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[offchainState] fix NextEntityId migration script #316

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading