Skip to content

Commit

Permalink
Fix author_trigger()
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
BartSchuurmans committed Mar 11, 2024
1 parent 304c478 commit adaf7a3
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions bookwyrm/migrations/0196_fix_author_trigger.py
Original file line number Diff line number Diff line change
@@ -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;
""",
),
]

0 comments on commit adaf7a3

Please sign in to comment.