diff --git a/packages/bot-engine/src/features/variables/utils.ts b/packages/bot-engine/src/features/variables/utils.ts index e476771b43e..52d4d4f182b 100644 --- a/packages/bot-engine/src/features/variables/utils.ts +++ b/packages/bot-engine/src/features/variables/utils.ts @@ -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) => diff --git a/packages/scripts/bulkUpdate.ts b/packages/scripts/bulkUpdate.ts new file mode 100644 index 00000000000..8e848811e39 --- /dev/null +++ b/packages/scripts/bulkUpdate.ts @@ -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[] + + 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() diff --git a/packages/scripts/package.json b/packages/scripts/package.json index db943da1468..8d6cbac2ed0 100644 --- a/packages/scripts/package.json +++ b/packages/scripts/package.json @@ -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",