Skip to content

Commit

Permalink
🗃️ Update non-string variable values in Database
Browse files Browse the repository at this point in the history
  • Loading branch information
baptisteArno committed Dec 5, 2022
1 parent d75eceb commit 461d2e2
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 7 deletions.
11 changes: 5 additions & 6 deletions packages/bot-engine/src/features/variables/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) =>
Expand Down
73 changes: 73 additions & 0 deletions packages/scripts/bulkUpdate.ts
Original file line number Diff line number Diff line change
@@ -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()
3 changes: 2 additions & 1 deletion packages/scripts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 461d2e2

Please sign in to comment.