Skip to content

Commit c80f5e7

Browse files
committed
fixed field type
1 parent 8194ec1 commit c80f5e7

1 file changed

Lines changed: 36 additions & 10 deletions

File tree

functions/process-image/handler.ts

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -287,17 +287,19 @@ async function handleFileMode(
287287
`SELECT set_config('app.database_id', $1, true)`,
288288
[String(file.database_id)]
289289
);
290-
// Use COALESCE to handle NULL domain column: if NULL, build a
291-
// minimal object with key + versions; if not NULL, merge versions in.
290+
// Write the full image object to the domain column using || merge.
291+
// Since dashboard no longer patches the img field (to avoid race conditions),
292+
// process-image is the sole writer. Includes key, mime, and versions.
292293
await sourceClient.query(
293294
`UPDATE ${file.source_table}
294-
SET ${file.source_column} = jsonb_set(
295-
COALESCE(${file.source_column}::jsonb, jsonb_build_object('key', $3::text)),
296-
'{versions}',
297-
$1::jsonb
298-
)
295+
SET ${file.source_column} = COALESCE(${file.source_column}::jsonb, '{}'::jsonb)
296+
|| jsonb_build_object(
297+
'key', $3::text,
298+
'mime', $4::text,
299+
'versions', $1::jsonb
300+
)
299301
WHERE id = $2`,
300-
[JSON.stringify(versionsArray), file.source_id, file.key]
302+
[JSON.stringify(versionsArray), file.source_id, file.key, mimeType]
301303
);
302304
await sourceClient.query('COMMIT');
303305
} catch (domainUpdateErr) {
@@ -306,10 +308,34 @@ async function handleFileMode(
306308
} finally {
307309
sourceClient.release();
308310
}
311+
} else if (file.source_table && file.source_column && file.source_id) {
312+
// No versions generated (image too small), but still write key + mime
313+
validateQualifiedName(file.source_table);
314+
validateIdentifier(file.source_column);
315+
const sourceClient = await pool.connect();
316+
try {
317+
await sourceClient.query('BEGIN');
318+
await sourceClient.query(
319+
`SELECT set_config('app.database_id', $1, true)`,
320+
[String(file.database_id)]
321+
);
322+
await sourceClient.query(
323+
`UPDATE ${file.source_table}
324+
SET ${file.source_column} = COALESCE(${file.source_column}::jsonb, '{}'::jsonb)
325+
|| jsonb_build_object('key', $2::text, 'mime', $3::text)
326+
WHERE id = $1`,
327+
[file.source_id, file.key, mimeType]
328+
);
329+
await sourceClient.query('COMMIT');
330+
} catch (err) {
331+
await sourceClient.query('ROLLBACK');
332+
log.error('[process-image] failed to write key+mime to domain table', err);
333+
} finally {
334+
sourceClient.release();
335+
}
309336
} else if (versionsJsonb.length > 0) {
310337
log.info(
311-
`[process-image] source_* not yet populated, skipping domain write-back. ` +
312-
`Versions will be written when domain trigger fires. file_id=${file.id}`
338+
`[process-image] source_* not yet populated, skipping domain write-back. file_id=${file.id}`
313339
);
314340
}
315341

0 commit comments

Comments
 (0)