mirror of https://github.com/jackc/pgx.git
parent
53b4280456
commit
9a1ab885af
|
@ -28,19 +28,9 @@ func afterConnect(conn *pgx.Conn) (err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// There technically is a small race condition in doing an upsert with a CTE
|
|
||||||
// where one of two simultaneous requests to the shortened URL would fail
|
|
||||||
// with a unique index violation. As the point of this demo is pgx usage and
|
|
||||||
// not how to perfectly upsert in PostgreSQL it is deemed acceptable.
|
|
||||||
_, err = conn.Prepare("putUrl", `
|
_, err = conn.Prepare("putUrl", `
|
||||||
with upsert as (
|
insert into shortened_urls(id, url) values ($1, $2)
|
||||||
update shortened_urls
|
on conflict (id) do update set url=excluded.url
|
||||||
set url=$2
|
|
||||||
where id=$1
|
|
||||||
returning *
|
|
||||||
)
|
|
||||||
insert into shortened_urls(id, url)
|
|
||||||
select $1, $2 where not exists(select 1 from upsert)
|
|
||||||
`)
|
`)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue