Skip to content

Commit

Permalink
Fix parameters for JSONB field
Browse files Browse the repository at this point in the history
  • Loading branch information
flvndvd committed Jan 31, 2025
1 parent ee3d399 commit 2bdebb0
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type {
CoreEntitiesRelocationBlob,
RelocationBlob,
} from "@app/temporal/relocation/activities/types";
import { isArrayOfPlainObjects } from "@app/temporal/relocation/activities/types";
import {
deleteFromRelocationStorage,
readFromRelocationStorage,
Expand Down Expand Up @@ -116,7 +117,10 @@ export async function processFrontTableChunk({
for (const { sql, params } of statements) {
await frontSequelize.transaction(async (transaction) =>
frontSequelize.query(sql, {
bind: params,
// TODO(2025-01-31): Remove this once current batch of data is processed.
bind: params.map((p) =>
isArrayOfPlainObjects(p) ? JSON.stringify(p) : p
),
type: QueryTypes.INSERT,
transaction,
})
Expand Down
7 changes: 7 additions & 0 deletions front/temporal/relocation/activities/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type {
CoreAPITableBlob,
ModelId,
} from "@dust-tt/types";
import { isPlainObject } from "lodash";

import type { RegionType } from "@app/lib/api/regions/config";

Expand Down Expand Up @@ -61,3 +62,9 @@ export type CoreTableAPIRelocationBlob = APIRelocationBlob<
"tables",
CoreAPITableBlob
>;

export function isArrayOfPlainObjects(value: unknown) {
return (
Array.isArray(value) && value.every((element) => isPlainObject(element))
);
}
10 changes: 9 additions & 1 deletion front/temporal/relocation/lib/sql/insert.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { isArrayOfPlainObjects } from "@app/temporal/relocation/activities/types";

const DEFAULT_CHUNK_SIZE = 250;

export function generateParameterizedInsertStatements(
Expand Down Expand Up @@ -29,7 +31,13 @@ export function generateParameterizedInsertStatements(
const rowPlaceholders: string[] = [];
for (const col of columns) {
rowPlaceholders.push(`$${paramIndex++}`);
params.push(row[col]);

// Array of plain objects are serialized to JSON strings.
const serialized = isArrayOfPlainObjects(row[col])
? JSON.stringify(row[col])
: row[col];

params.push(serialized);
}
placeholders.push(`(${rowPlaceholders.join(",")})`);
}
Expand Down

0 comments on commit 2bdebb0

Please sign in to comment.