diff --git a/foundations/server/packages/postgres/src/__tests__/utils.spec.ts b/foundations/server/packages/postgres/src/__tests__/utils.spec.ts index 1940cc8d7ab..11aea7d4dad 100644 --- a/foundations/server/packages/postgres/src/__tests__/utils.spec.ts +++ b/foundations/server/packages/postgres/src/__tests__/utils.spec.ts @@ -39,20 +39,23 @@ describe('utils - inferType', () => { expect(inferType([1, 2, 3])).toBe('::numeric[]') }) - it('should handle empty arrays', () => { - // BUG: Empty arrays are treated as objects and return '::jsonb' - // Expected behavior would be to return '' or handle specially - expect(inferType([])).toBe('::jsonb') + it('should handle empty arrays as text arrays', () => { + // Empty arrays fall back to '::text[]' so `= ANY` stays valid on CockroachDB. + expect(inferType([])).toBe('::text[]') }) it('should handle arrays with null first element', () => { expect(inferType([null, 'text'])).toBe('::text[]') }) - it('should handle arrays with all null elements', () => { - // BUG: Arrays with only null elements return '::jsonb[]' - // Expected: Should probably return '' or handle as empty array - expect(inferType([null, null])).toBe('::jsonb[]') + it('should handle single-null arrays as text arrays', () => { + // [null] is what the github integration submits when a workspace has the + // App installed but no repositories enabled; must not infer ::jsonb. + expect(inferType([null])).toBe('::text[]') + }) + + it('should handle arrays with all null elements as text arrays', () => { + expect(inferType([null, null])).toBe('::text[]') }) it('should infer Date type as text', () => { diff --git a/foundations/server/packages/postgres/src/utils.ts b/foundations/server/packages/postgres/src/utils.ts index 469904e3a93..ca8cb497639 100644 --- a/foundations/server/packages/postgres/src/utils.ts +++ b/foundations/server/packages/postgres/src/utils.ts @@ -255,10 +255,17 @@ export function inferType (val: any): string { return '::boolean' } if (Array.isArray(val)) { - const type = inferType(val[0] ?? val[1]) - if (type !== '') { - return type + '[]' + const sample = val.find((v) => v !== null && v !== undefined) + if (sample !== undefined) { + const inner = inferType(sample) + if (inner !== '') { + return inner + '[]' + } } + // Empty arrays and all-null arrays: caller is almost always running + // $in on a Ref/string column. ::jsonb[] would be incompatible with + // CockroachDB's `= ANY`, so fall back to a real array type. + return '::text[]' } if (typeof val === 'object') { if (val instanceof Date) {