🗃️ Update non-string variable values in Database
This commit is contained in:
73
packages/scripts/bulkUpdate.ts
Normal file
73
packages/scripts/bulkUpdate.ts
Normal file
@ -0,0 +1,73 @@
|
||||
import { PrismaClient } from 'db'
|
||||
import { promptAndSetEnvironment } from './utils'
|
||||
import { Result } from 'models'
|
||||
import { isDefined, isNotDefined } from 'utils'
|
||||
|
||||
let progress = 0
|
||||
|
||||
const bulkUpdate = async () => {
|
||||
await promptAndSetEnvironment()
|
||||
const prisma = new PrismaClient({
|
||||
log: [
|
||||
{
|
||||
emit: 'event',
|
||||
level: 'query',
|
||||
},
|
||||
'info',
|
||||
'warn',
|
||||
'error',
|
||||
],
|
||||
})
|
||||
|
||||
const results = (await prisma.result.findMany({
|
||||
where: {
|
||||
variables: { isEmpty: false },
|
||||
},
|
||||
select: { variables: true, id: true },
|
||||
})) as Pick<Result, 'variables' | 'id'>[]
|
||||
|
||||
const queries = results
|
||||
.map((result) => {
|
||||
if (
|
||||
result.variables.some((variable) => typeof variable.value !== 'string')
|
||||
) {
|
||||
return prisma.result.updateMany({
|
||||
where: { id: result.id },
|
||||
data: {
|
||||
variables: result.variables
|
||||
.map((variable) => ({
|
||||
...variable,
|
||||
value:
|
||||
typeof variable.value !== 'string'
|
||||
? safeStringify(variable.value)
|
||||
: variable.value,
|
||||
}))
|
||||
.filter(isDefined),
|
||||
},
|
||||
})
|
||||
}
|
||||
})
|
||||
.filter(isDefined)
|
||||
|
||||
const total = queries.length
|
||||
|
||||
prisma.$on('query', () => {
|
||||
progress += 1
|
||||
console.log(`Progress: ${progress}/${total}`)
|
||||
})
|
||||
|
||||
await prisma.$transaction(queries)
|
||||
}
|
||||
|
||||
export const safeStringify = (val: unknown): string | null => {
|
||||
if (isNotDefined(val)) return null
|
||||
if (typeof val === 'string') return val
|
||||
try {
|
||||
return JSON.stringify(val)
|
||||
} catch {
|
||||
console.warn('Failed to safely stringify variable value', val)
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
bulkUpdate()
|
Reference in New Issue
Block a user