From adaf7a36b143538da61086b2a86a5bcc99e646c6 Mon Sep 17 00:00:00 2001 From: Bart Schuurmans Date: Mon, 11 Mar 2024 11:11:40 +0100 Subject: [PATCH] Fix author_trigger() The join that was used to match books to a changed author used the id column instead of the author_id column to join on authors. This migration re-calculates all book search vectors to fix any missed author name changes. --- .../migrations/0196_fix_author_trigger.py | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 bookwyrm/migrations/0196_fix_author_trigger.py diff --git a/bookwyrm/migrations/0196_fix_author_trigger.py b/bookwyrm/migrations/0196_fix_author_trigger.py new file mode 100644 index 0000000000..2ad54ba6f1 --- /dev/null +++ b/bookwyrm/migrations/0196_fix_author_trigger.py @@ -0,0 +1,52 @@ +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ("bookwyrm", "0195_alter_user_preferred_language"), + ] + + operations = [ + # join on bookwyrm_book_authors.author_id instead of .id + migrations.RunSQL( + sql=""" + CREATE OR REPLACE FUNCTION author_trigger() RETURNS trigger AS $$ + begin + WITH book AS ( + SELECT bookwyrm_book.id as row_id + FROM bookwyrm_author + LEFT OUTER JOIN bookwyrm_book_authors + ON bookwyrm_book_authors.author_id = new.id + LEFT OUTER JOIN bookwyrm_book + ON bookwyrm_book.id = bookwyrm_book_authors.book_id + ) + UPDATE bookwyrm_book SET search_vector = '' + FROM book + WHERE id = book.row_id; + return new; + end + $$ LANGUAGE plpgsql; + + UPDATE bookwyrm_book SET search_vector = NULL; + """, + reverse_sql=""" + CREATE OR REPLACE FUNCTION author_trigger() RETURNS trigger AS $$ + begin + WITH book AS ( + SELECT bookwyrm_book.id as row_id + FROM bookwyrm_author + LEFT OUTER JOIN bookwyrm_book_authors + ON bookwyrm_book_authors.id = new.id + LEFT OUTER JOIN bookwyrm_book + ON bookwyrm_book.id = bookwyrm_book_authors.book_id + ) + UPDATE bookwyrm_book SET search_vector = '' + FROM book + WHERE id = book.row_id; + return new; + end + $$ LANGUAGE plpgsql; + """, + ), + ]