moebius icon indicating copy to clipboard operation
moebius copied to clipboard

Proposal: ignore_conflict

Open bunnylushington opened this issue 9 months ago • 3 comments

Hi -- I use the following code to add an on conflict (col) do nothing clause to the SQL statement Moebius generates and wonder if (a) this would be a generally useful addition to the library and (b) how detailed an implementation would be required to be general enough. (on conflict can be arbitrarily complicated; personally I'd opt for a function for anything more involved than this simple statement but I know that might be an extreme position.)

  @spec ignore_conflict(%Moebius.QueryCommand{}, String.t) ::
      %Moebius.QueryCommand{}
  @doc "Do nothing when an insert conflict is detected on COL."
  def ignore_conflict(%Moebius.QueryCommand{} = cmd, col) do
    conflict = "on conflict (#{col}) do nothing"
    new = Regex.replace(~r/returning.+$/, cmd.sql, "#{conflict} \\0")
    %{cmd | sql: new}
  end

If there is interest, I'd be happy to supply a PR (with any guidance incorporated). Thanks!

bunnylushington avatar May 14 '24 14:05 bunnylushington