a8n: Return existing changeset on create conflict
Created by: mrnugget
This fixes https://github.com/sourcegraph/src-cli/issues/52 by making
the createChangesets
query not fail when a changeset with the given
external number and repository ID already exists.
It does so by modifying the existing query that INSERT
s changesets to
JOIN
existing changesets
and returning their values in case the we
run into a conflict on the changesets_repo_external_id_unique
constraint.
Since ON CONFLICT [...] DO NOTHING
doesn't return a row, we need to
RIGHT JOIN
(instead of LEFT JOIN
) the batch
, so that we can access
it in case the INSERT
didn't return anything.
We use the batch to LEFT JOIN
existing changesets
rows and to be
able to use their values in the SELECT
. We use COALESCE
so that we
only "fallback" to existing values in case something doesn't exist.