4 Commits

Author SHA1 Message Date
7d99897b0e 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.
2025-10-25 21:37:52 +02:00
35dc3cbf6f Remove DROP FUNCTION IF EXISTS from Genre update 2025-10-25 21:33:29 +02:00
4625adb6a4 Update .gitignore and drop update_genre first 2025-10-25 21:01:11 +02:00
65e23787f1 Add update_genre function to upsert genres
Replace previous function that managed book_genres. New SQL function
upserts the genres table by source and remote_id, updating code,
description and meta or inserting a new row. Removed book lookup and
book_genres insertion.
2025-10-25 20:49:36 +02:00
2 changed files with 21 additions and 22 deletions

3
.gitignore vendored
View File

@@ -1,2 +1,3 @@
/target /target
.vscode /.vscode
/bruno

View File

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