Find a cleaner solution for the return_skipped_upsert? logic introduced in #626

Open
#628 1 comment 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
40/100
Issue type
Refactor
Clarity
Needs clarification
Activity status
Quiet
Tech stack
elixir, postgresql
Domain
databases

Research direction

Start by reviewing PR #626 and the current return_skipped_upsert? logic, then trace how bulk_upserts builds its PostgreSQL upsert statement and how Ecto handles the :returning option. Compare the extra-query and data-wrangling approach with the attempted upsert-condition marker; done means a supported, maintainable way to identify skipped records without the current workaround.

Written by the indexing model from the issue text.

Description

enhancement
Code of Conduct
  • I agree to follow this project's Code of Conduct
AI Policy
  • I agree to follow this project's AI Policy, or I agree that AI was not used while creating this issue.
Is your feature request related to a problem? Please describe.

https://github.com/ash-project/ash_postgres/pull/626 adds support for returning skipped records from bulk_upserts. but we need to do an extra query and some data wrangling to achieve it.

Describe the solution you'd like

I tried doing it as part of the upsert statement by adding the upsert_condition logic to the update statement and add a marker to the return values to check if one of the inserts was actually skipped because of the condition logic.

  1. CASE WHEN in DO UPDATE SET was successfully implemented - the conditional logic works, SQL generates correctly
  2. The goal was to add the condition to RETURNING like:
    RETURNING id, price, (price != EXCLUDED.price) AS met_upsert_condition
  3. PostgreSQL supports this - expressions in RETURNING are valid SQL
  4. But Ecto doesn't expose a way to do it:
    - The :returning option flows through quote_names() which calls quote_name/1 on each item
    - quote_name/1 only has function clauses for atoms and strings - it quotes them as identifiers
    - If {:unsafe_fragment, "..."} is passed (like :conflict_target supports), it crashes with FunctionClauseError because there's no matching clause
    - If a plain string like "(condition) AS met" is passed, it gets quoted as "(condition) AS met" which is an invalid identifier
Describe alternatives you've considered

No response

Additional context

No response

Dominant language
Elixir
Stars
191
Forks
186
Avg merge
16h 4m
Merged PRs (30d)
17

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from ash-project/ash_postgres

All issues in ash-project/ash_postgres

Similar issues

More Elixir issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.