Skip to content

Conversation

@pyramation
Copy link
Contributor

feat(csv-to-pg): add conflictDoNothing option for ON CONFLICT DO NOTHING

Summary

Adds a new conflictDoNothing option to the csv-to-pg package that generates ON CONFLICT DO NOTHING without specifying conflict columns. This is needed when the unique constraint uses a functional index with complex expressions that cannot be specified as simple column names.

The immediate use case is the metaschema_public.field table, which has a unique constraint (databases_field_uniq_names_idx) that normalizes UUID field names by stripping suffixes like _id, _uuid, etc. This causes collisions when tables have both foo (text) and foo_id (uuid) columns - they normalize to the same hash value and violate the unique constraint during deployment.

Changes:

  • packages/csv-to-pg/src/utils.ts: Add conflictDoNothing parameter to makeConflictClause, InsertOne, and InsertMany
  • packages/csv-to-pg/src/parser.ts: Add conflictDoNothing to Parser config
  • pgpm/core/src/export/export-meta.ts: Enable conflictDoNothing: true for the field table config
  • Added unit tests for the new option

Review & Testing Checklist for Human

  • Verify silent skip behavior is acceptable: Using ON CONFLICT DO NOTHING means duplicate field records will be silently skipped. Confirm this is the desired behavior and no important data will be lost.
  • Test actual deployment flow: Run generate:constructive in constructive-db and deploy to a test database to verify the fix resolves the unique constraint violation error.
  • Review if other tables need similar treatment: Check if any other metaschema tables have similar functional index constraints that could cause collisions.

Recommended test plan:

# In constructive-db repo after updating to use new csv-to-pg
pnpm run generate:constructive
pgpm docker start
eval "$(pgpm env)"
createdb db1
pgpm deploy --yes --package constructive --database db1
pgpm deploy --yes --package constructive-services --database db1
# Should complete without unique constraint errors

Notes

This PR is part of fixing the deployment issue reported in constructive-db where generate:constructive was exporting duplicate field entries that violated the databases_field_uniq_names_idx constraint.

Link to Devin run: https://app.devin.ai/sessions/7ea5e5e99b73424db6856e5d07482b64
Requested by: Dan Lynch (@pyramation)

Add a new conflictDoNothing option to the Parser, InsertOne, and InsertMany
functions that generates ON CONFLICT DO NOTHING without specifying conflict
columns. This is useful when the unique constraint uses a functional index
with complex expressions that cannot be specified as simple column names.

The metaschema_public.field table uses a unique constraint (databases_field_uniq_names_idx)
that normalizes UUID field names by stripping suffixes like _id, _uuid, etc.
This causes collisions when tables have both 'foo' (text) and 'foo_id' (uuid)
columns. Using ON CONFLICT DO NOTHING allows the export to gracefully skip
duplicate field inserts during deployment.

Changes:
- Add conflictDoNothing option to makeConflictClause in utils.ts
- Add conflictDoNothing to InsertOne and InsertMany params
- Add conflictDoNothing to Parser config and parse method
- Update export-meta.ts to use conflictDoNothing for field table
- Add tests for the new conflictDoNothing option
@devin-ai-integration
Copy link
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

@pyramation pyramation merged commit 704352c into main Jan 8, 2026
36 checks passed
@pyramation pyramation deleted the devin/1767859893-field-conflict-do-nothing branch January 8, 2026 08:43
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.

2 participants