luckyframework / luckyframework/avram

Upserting using insert on conflict

Open
#963 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Crystal
Stars
183
Forks
67
PR merge metrics
No merged PRs in 30d

Description

I was looking into upsert for something I am working on and noticed that it is actually a find followed by create or update rather than a "true" upsert. This means it is prone to race conditions as we may have inserted a conflicting record between the find and the subsequent action.

For my use-case ideally I want to use INSERT ... ON CONFLICT UPDATE SET foo=$1 to do the upsert in a single query. The downside is that we wouldn't run any of the after callbacks as we will no longer know whether it was an update or a save. This matches the behaviour provided by other frameworks like Rails (https://apidock.com/rails/v6.0.0/ActiveRecord/Persistence/ClassMethods/upsert).

Looking for feedback whether this change would be accepted into Avram and along those lines should it replace the existing upsert method (although this would be a breaking change due to callbacks) or be a new API entirely?

Contributor guide

Open the contributing guide

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.

Research direction

Start by reading Avram’s existing upsert method and its callback behavior, then compare that flow with the proposed PostgreSQL INSERT ... ON CONFLICT UPDATE query. Done requires an agreed API direction, including whether this replaces upsert or becomes a new method, and defined callback behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
crystal, postgresql
Domain
databases
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.