mirror of
https://github.com/flibusta-apps/library_updater.git
synced 2025-12-06 15:45:36 +01:00
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.
This commit is contained in:
21
src/types.rs
21
src/types.rs
@@ -830,22 +830,17 @@ 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 $$
|
CREATE OR REPLACE FUNCTION update_genre(
|
||||||
DECLARE
|
source_ smallint, remote_id_ int, code_ varchar, description_ varchar, meta_ varchar
|
||||||
book_id integer := -1;
|
) RETURNS void AS $$
|
||||||
genre_id integer := -1;
|
|
||||||
BEGIN
|
BEGIN
|
||||||
SELECT id INTO book_id FROM books WHERE source = source_ AND remote_id = book_;
|
IF EXISTS (SELECT * FROM genres WHERE source = source_ AND remote_id = remote_id_) THEN
|
||||||
|
UPDATE genres SET code = code_, description = description_, meta = meta_
|
||||||
IF book_id IS NULL THEN
|
WHERE source = source_ AND remote_id = remote_id_;
|
||||||
RETURN;
|
RETURN;
|
||||||
END IF;
|
END IF;
|
||||||
|
INSERT INTO genres (source, remote_id, code, description, meta)
|
||||||
SELECT id INTO genre_id FROM genres WHERE source = source_ AND remote_id = genre_;
|
VALUES (source_, remote_id_, code_, description_, meta_);
|
||||||
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;
|
||||||
"
|
"
|
||||||
|
|||||||
Reference in New Issue
Block a user