Skip to content

Commit 5cfe253

Browse files
Merge pull request #4 from pulkitpareek18/ci/harden-deploy-against-local-drift
fix(backup): stringify all jsonb values, not just objects
2 parents 7ecdb12 + 8e08abb commit 5cfe253

1 file changed

Lines changed: 19 additions & 3 deletions

File tree

server/src/controllers/backup.controller.ts

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,18 @@ export async function importBackup(req: Request, res: Response, next: NextFuncti
176176
const columns = Object.keys(rows[0]);
177177
const colList = columns.map((c) => `"${c}"`).join(', ');
178178

179+
// Introspect column types so we know which need JSON serialization.
180+
// jsonb/json: stringify every non-null value regardless of JS type
181+
// (a JSON string like "on" would otherwise be sent as bare `on` and fail).
182+
const typeRes = await client.query<{ column_name: string; data_type: string }>(
183+
`SELECT column_name, data_type FROM information_schema.columns
184+
WHERE table_schema = 'public' AND table_name = $1`,
185+
[table]
186+
);
187+
const jsonCols = new Set(
188+
typeRes.rows.filter((r) => r.data_type === 'jsonb' || r.data_type === 'json').map((r) => r.column_name)
189+
);
190+
179191
// Insert in batches of 500
180192
const BATCH = 500;
181193
for (let i = 0; i < rows.length; i += BATCH) {
@@ -187,11 +199,15 @@ export async function importBackup(req: Request, res: Response, next: NextFuncti
187199
const rowPlaceholders: string[] = [];
188200
for (const col of columns) {
189201
const v = row[col];
190-
// jsonb columns: pg stringifies plain objects but treats arrays as Postgres arrays,
191-
// so explicitly stringify any non-Date object/array.
192202
if (v === undefined || v === null) {
193203
values.push(null);
194-
} else if (typeof v === "object" && !(v instanceof Date) && !Buffer.isBuffer(v)) {
204+
} else if (jsonCols.has(col)) {
205+
// Always JSON.stringify for jsonb/json columns — covers strings,
206+
// numbers, booleans, arrays, and objects uniformly.
207+
values.push(JSON.stringify(v));
208+
} else if (typeof v === 'object' && !(v instanceof Date) && !Buffer.isBuffer(v)) {
209+
// Defensive: any other object/array on a non-json column gets stringified
210+
// (shouldn't happen for our schema, but better than a crash).
195211
values.push(JSON.stringify(v));
196212
} else {
197213
values.push(v);

0 commit comments

Comments
 (0)