Skip to content

Commit

Permalink
added custom migration to set orionLanguage to all of the processed v…
Browse files Browse the repository at this point in the history
…ideos
  • Loading branch information
zeeshanakram3 committed Feb 14, 2024
1 parent e6f174c commit 5dae878
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions src/utils/customMigrations/setOrionLanguage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { createLogger } from '@subsquid/logger'
import { IsNull } from 'typeorm'
import { Video } from '../../model'
import { globalEm } from '../globalEm'
import { predictLanguage } from '../language'

const logger = createLogger('setOrionLanguage')

async function setOrionLanguage() {
const em = await globalEm

const batchSize = 10000
let offset = 0
let hasMore = true

while (hasMore) {
const videos = await em.find(Video, {
where: { orionLanguage: IsNull() },
order: { id: 'ASC' },
take: batchSize,
skip: offset,
})

if (videos.length === 0) {
hasMore = false
} else {
const updates = videos.map((video) => {
const languageText = [video.title ?? '', video.description ?? ''].join(' ')
video.orionLanguage = predictLanguage(languageText)
return video
})

// Save all updates in a single transaction
await em.transaction(async (transactionalEntityManager) => {
await transactionalEntityManager.save(updates)
})

logger.info(`Updated ${updates.length} videos.`)

offset += videos.length // Prepare the offset for the next batch
}
}
}

setOrionLanguage()
.then(() => logger.info('Update process completed.'))
.catch(() => {
logger.error('process failed')
process.exit(1)
})

0 comments on commit 5dae878

Please sign in to comment.