Use ON CONFLICT upsert for genres
Some checks failed
Build docker image / Build-Docker-Image (push) Has been cancelled
rust-clippy analyze / Run rust-clippy analyzing (push) Has been cancelled

Replace the previous IF EXISTS/UPDATE/INSERT logic in the update_genre
PL/pgSQL function with a single INSERT ... ON CONFLICT DO UPDATE to
perform an atomic upsert and avoid races. Reformat the client.execute
call for readability.
This commit is contained in:
2025-10-25 21:37:52 +02:00
parent 35dc3cbf6f
commit 7d99897b0e

View File

@@ -828,25 +828,28 @@ impl FromVecExpression<Genre> for Genre {
#[async_trait]
impl Update for Genre {
async fn before_update(client: &Client) -> Result<(), Box<tokio_postgres::Error>> {
match client.execute(
"
match client
.execute(
"
CREATE OR REPLACE FUNCTION update_genre(
source_ smallint, remote_id_ int, code_ varchar, description_ varchar, meta_ varchar
) RETURNS void AS $$
BEGIN
IF EXISTS (SELECT * FROM genres WHERE source = source_ AND remote_id = remote_id_) THEN
UPDATE genres SET code = code_, description = description_, meta = meta_
WHERE source = source_ AND remote_id = remote_id_;
RETURN;
END IF;
INSERT INTO genres (source, remote_id, code, description, meta)
VALUES (source_, remote_id_, code_, description_, meta_);
VALUES (source_, remote_id_, code_, description_, meta_)
ON CONFLICT (source, remote_id) DO UPDATE SET
code = EXCLUDED.code,
description = EXCLUDED.description,
meta = EXCLUDED.meta;
END;
$$ LANGUAGE plpgsql;
"
, &[]).await {
Ok(_) => Ok(()),
Err(err) => Err(Box::new(err)),
",
&[],
)
.await
{
Ok(_) => Ok(()),
Err(err) => Err(Box::new(err)),
}
}