🗃️ Update non-string variable values in Database
This commit is contained in:
@ -45,17 +45,16 @@ export const parseCorrectValueType = (
|
||||
): string | boolean | number | null | undefined => {
|
||||
if (value === null) return null
|
||||
if (value === undefined) return undefined
|
||||
const isNumberStartingWithZero =
|
||||
value.startsWith('0') && !value.startsWith('0.') && value.length > 1
|
||||
if (typeof value === 'string' && isNumberStartingWithZero) return value
|
||||
if (typeof value === 'number') return value
|
||||
if (value === 'true') return true
|
||||
if (value === 'false') return false
|
||||
if (value === 'null') return null
|
||||
if (value === 'undefined') return undefined
|
||||
// isNaN works with strings
|
||||
if (isNaN(value as unknown as number)) return value
|
||||
return Number(value)
|
||||
try {
|
||||
return JSON.parse(value)
|
||||
} catch {
|
||||
return value
|
||||
}
|
||||
}
|
||||
|
||||
const jsonParse = (str: string) =>
|
||||
|
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()
|
@ -8,7 +8,8 @@
|
||||
"playground": "tsx playground.ts",
|
||||
"db:backup": "tsx backupDatabase.ts",
|
||||
"db:restore": "tsx restoreDatabase.ts",
|
||||
"db:setCustomPlan": "tsx setCustomPlan.ts"
|
||||
"db:setCustomPlan": "tsx setCustomPlan.ts",
|
||||
"db:bulkUpdate": "tsx bulkUpdate.ts"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/node": "18.11.9",
|
||||
|
Reference in New Issue
Block a user