typelevel / typelevel/doobie

batch UPDATE

Open
#1,209 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Scala
Stars
2.2k
Forks
379
Avg merge
14m
Merged PRs (30d)
8

Description

I am trying to figure out how to do batch update with doobie.
I have the following code snippet:

Update[?]( // What type to use here?
  s"""
     |UPDATE mytable SET
     |commitHash = tmp.commitHash
     |config = tmp.config
     |configScript = tmp.configScript
     |containerId = tmp.containerId
     |description = tmp.description
     |killScript = tmp.killScript
     |launchScript = tmp.launchScript
     |FROM
     | (SELECT
     | unnest(?) as commitHash,
     | unnest(?) as config
     | unnest(?) as configScript
     | unnest(?) as containerId
     | unnest(?) as description
     | unnest(?) as killScript
     | unnest(?) as launchScript
     | ) AS tmp
     | WHERE mytable.id = tmp.id;
     |""".stripMargin).updateMany((
  newRows.map(_.commitHash),
  newRows.map(_.config),
  newRows.map(_.configScript),
  newRows.map(_.containerId),
  newRows.map(_.description),
  newRows.map(_.killScript),
  newRows.map(_.launchScript),
))

This would eventually become part of a larger INSERT .... ON CONFLICT ... UPDATE statement. This approach was inspired by this stackoverflow answer. The trouble is that updateMany expects an argument of type F[A] where F is Foldable. This is not possible in my case because each of the ?'s might correspond to a scala list / sql array of a different type, e.g. suppose commitHash is an Int and all the rest are Strings.

It may be that this entire approach is misguided, but this answer from https://github.com/tpolecat/doobie/issues/193#issuecomment-459971953:

@letalvoj In postgres "upsert" is just an insert query with on conflict clause . You can write this query in doobie like any other query. What exactly do you mean by "native support"?

suggests that it should be possible to simply write an on conflict clause like this using doobie. Thanks!

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 with doobie's updateMany API and the PostgreSQL batch UPDATE and INSERT ... ON CONFLICT forms shown in the issue. Determine whether the differing Scala collection element types can be represented by the API, and document or demonstrate a supported approach for the requested batch upsert.

Written by the indexing model from the issue text.

Assessment

Tech stack
postgresql, scala, sql
Domain
databases
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.