Skip to content

Commit

Permalink
Improved offchain import/export script
Browse files Browse the repository at this point in the history
  • Loading branch information
zeeshanakram3 committed Jan 8, 2024
1 parent 4c48c45 commit e6d2d2f
Showing 1 changed file with 25 additions and 28 deletions.
53 changes: 25 additions & 28 deletions src/utils/offchainState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,33 @@ import assert from 'assert'
import fs from 'fs'
import path from 'path'
import { EntityManager } from 'typeorm'
import { NextEntityId } from '../model'
import * as model from '../model'
import { uniqueId } from './crypto'
import { defaultNotificationPreferences } from './notification/helpers'

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

const exportedStateMap = {
type CamelToSnakeCase<S> = S extends `${infer T}${infer U}`
? T extends Capitalize<T>
? `_${Lowercase<T>}${CamelToSnakeCase<U>}`
: `${T}${CamelToSnakeCase<U>}`
: S

type SnakeCaseKeys<T> = {
[K in keyof T as CamelToSnakeCase<K>]: T[K]
}

type ClassConstructors<T> = {
[K in keyof T]: T[K] extends Function ? T[K] : never
}

type ExportedStateMap = {
[K in keyof ClassConstructors<typeof model>]?:
| true
| (keyof SnakeCaseKeys<ClassConstructors<typeof model>[K]['prototype']>)[]
}

const exportedStateMap: ExportedStateMap = {
VideoViewEvent: true,
ChannelFollow: true,
Report: true,
Expand All @@ -27,8 +47,9 @@ const exportedStateMap = {
Account: true,
Notification: true,
NotificationEmailDelivery: true,
NotificationEmailDeliveryAttempt: true,
EmailDeliveryAttempt: true,
Token: true,
NextEntityId: true,
Channel: ['is_excluded', 'video_views_num', 'follows_num', 'ypp_status', 'channel_weight'],
Video: ['is_excluded', 'views_num'],
Comment: ['is_excluded'],
Expand Down Expand Up @@ -137,7 +158,7 @@ export class OffchainState {
? await em
.getRepository(entityName)
.createQueryBuilder()
.select(['id', ...fields])
.select(['id', ...(fields as unknown as string)])
.getRawMany()
: await em.getRepository(entityName).find({})
if (!values.length) {
Expand Down Expand Up @@ -261,10 +282,6 @@ export class OffchainState {
)
}

// migrate counter values
const { exportedVersion } = exportFile
await this.migrateCounters(exportedVersion, em)

const renamedExportFilePath = `${exportFilePath}.imported`
this.logger.info(`Renaming export file to ${renamedExportFilePath})...`)
fs.renameSync(exportFilePath, renamedExportFilePath)
Expand All @@ -282,24 +299,4 @@ 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 e6d2d2f

Please sign in to comment.