Skip to content

fix(csv-to-pg): emit empty array '{}' instead of NULL for array types#887

Merged
pyramation merged 3 commits intomainfrom
devin/1774334480-csv-to-pg-empty-array-defaults
Mar 24, 2026
Merged

fix(csv-to-pg): emit empty array '{}' instead of NULL for array types#887
pyramation merged 3 commits intomainfrom
devin/1774334480-csv-to-pg-empty-array-defaults

Conversation

@pyramation
Copy link
Copy Markdown
Contributor

@pyramation pyramation commented Mar 24, 2026

Summary

When array-typed columns (jsonb[], uuid[], text[]) have null or empty values, csv-to-pg now emits '{}' (empty PostgreSQL array literal) instead of SQL NULL. This fixes NOT NULL constraint violations when exported SQL is deployed to databases where array columns have NOT NULL defaults (e.g. secure_table_provision.fields and secure_table_provision.grant_privileges).

csv-to-pg/src/parse.ts: makeNullOrThrow now returns '{}' for any array type instead of NULL when the value is missing/empty. psqlArray also returns '{}' for empty arrays instead of undefined. A new isArrayType helper detects array types by checking for trailing [].

Two new test cases verify null and empty array inputs both emit '{}' instead of NULL.

Updates since last revision

  • Removed the redundant COALESCE fix from pgpm/export/src/export-meta.ts — the csv-to-pg serialization layer now handles this, so the query-level workaround is unnecessary.

Review & Testing Checklist for Human

  • Breaking change risk on makeNullOrThrow: This changes behavior for ALL array types across ALL consumers of csv-to-pg, not just the export pipeline. Verify no existing consumer relies on getting SQL NULL for empty/missing array columns. If any table legitimately needs NULL arrays, this would silently change the semantics.
  • End-to-end test: Run a full provision → export → deploy cycle on agentic-db (or any project) to confirm the exported SQL deploys cleanly without the manual NULL → '{}' post-processing workaround.

Notes

  • The psqlArray change (empty array → '{}' instead of undefined) also affects the text[] handler path, which calls psqlArray directly before checking emptiness.
  • This was the root cause of repeated CI failures in agentic-db PR #30 where pgpm-deploy failed with null value in column "fields" violates not-null constraint.

Link to Devin session: https://app.devin.ai/sessions/f72c3360c9494043aa60d59ff302be83
Requested by: @pyramation

When array-typed columns (jsonb[], uuid[], text[]) have null or empty
values, emit '{}' (empty PostgreSQL array literal) instead of SQL NULL.
This fixes NOT NULL constraint violations when exported SQL is deployed
to databases where array columns have NOT NULL defaults.

Changes:
- parse.ts: makeNullOrThrow emits '{}' for array types instead of NULL
- parse.ts: psqlArray returns '{}' for empty arrays instead of undefined
- export-meta.ts: COALESCE fields/grant_privileges in secure_table_provision query
- Added tests for null and empty array serialization
@devin-ai-integration
Copy link
Copy Markdown
Contributor

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR. Add '(aside)' to your comment to have me ignore it.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment and CI monitoring

Resolve conflict in pgpm/export/src/export-utils.ts (take main's version).
Apply COALESCE fix to new pgpm/export/src/export-meta.ts location.
@pyramation pyramation merged commit 4a20617 into main Mar 24, 2026
43 checks passed
@pyramation pyramation deleted the devin/1774334480-csv-to-pg-empty-array-defaults branch March 24, 2026 07:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant