Skip to content

Commit

Permalink
Define search_vector_trigger via Book.Meta.triggers
Browse files Browse the repository at this point in the history
  • Loading branch information
dato committed Nov 25, 2023
1 parent bcb3a34 commit 7472383
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,20 @@ class Migration(migrations.Migration):
]

operations = [
pgtrigger.migrations.AddTrigger(
model_name="book",
trigger=pgtrigger.compiler.Trigger(
name="update_search_vector_on_book_edit",
sql=pgtrigger.compiler.UpsertTriggerSql(
func="new.search_vector := coalesce(nullif(setweight(to_tsvector('english', coalesce(new.title, '')), 'A'), ''), setweight(to_tsvector('simple', coalesce(new.title, '')), 'A')) || setweight(to_tsvector('english', coalesce(new.subtitle, '')), 'B') || (SELECT setweight(to_tsvector('simple', coalesce(array_to_string(array_agg(bookwyrm_author.name), ' '), '')), 'C') FROM bookwyrm_book LEFT OUTER JOIN bookwyrm_book_authors ON bookwyrm_book.id = bookwyrm_book_authors.book_id LEFT OUTER JOIN bookwyrm_author ON bookwyrm_book_authors.author_id = bookwyrm_author.id WHERE bookwyrm_book.id = new.id ) || setweight(to_tsvector('english', coalesce(new.series, '')), 'D');RETURN NEW;",
hash="9c898d46dfb7492ecd18f6c692bbecfa548f0e85",
operation='INSERT OR UPDATE OF "title", "subtitle", "series", "search_vector"',
pgid="pgtrigger_update_search_vector_on_book_edit_bec58",
table="bookwyrm_book",
when="BEFORE",
),
),
),
pgtrigger.migrations.AddTrigger(
model_name="author",
trigger=pgtrigger.compiler.Trigger(
Expand All @@ -41,6 +55,12 @@ class Migration(migrations.Migration):
),
),
),
migrations.RunSQL(
sql="""DROP TRIGGER IF EXISTS search_vector_trigger ON bookwyrm_book;
DROP FUNCTION IF EXISTS book_trigger;
""",
reverse_sql=search_vector_trigger.sql,
),
migrations.RunSQL(
sql="""DROP TRIGGER IF EXISTS author_search_vector_trigger ON bookwyrm_author;
DROP FUNCTION IF EXISTS author_trigger;
Expand Down
32 changes: 31 additions & 1 deletion bookwyrm/models/book.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from model_utils import FieldTracker
from model_utils.managers import InheritanceManager
from imagekit.models import ImageSpecField
import pgtrigger

from bookwyrm import activitypub
from bookwyrm.isbn.isbn import hyphenator_singleton as hyphenator
Expand All @@ -24,6 +25,7 @@
ENABLE_PREVIEW_IMAGES,
ENABLE_THUMBNAIL_GENERATION,
)
from bookwyrm.utils.db import format_trigger

from .activitypub_mixin import OrderedCollectionPageMixin, ObjectMixin
from .base_model import BookWyrmModel
Expand Down Expand Up @@ -232,9 +234,37 @@ def __repr__(self):
)

class Meta:
"""sets up postgres GIN index field"""
"""set up indexes and triggers"""
# pylint: disable=line-too-long

indexes = (GinIndex(fields=["search_vector"]),)
triggers = [
pgtrigger.Trigger(
name="update_search_vector_on_book_edit",
when=pgtrigger.Before,
operation=pgtrigger.Insert
| pgtrigger.UpdateOf("title", "subtitle", "series", "search_vector"),
func=format_trigger(
"""new.search_vector :=
COALESCE(
NULLIF(setweight(to_tsvector('english', COALESCE(new.title, '')), 'A'), ''),
setweight(to_tsvector('simple', COALESCE(new.title, '')), 'A')
) ||
setweight(to_tsvector('english', COALESCE(new.subtitle, '')), 'B') ||
(SELECT setweight(to_tsvector('simple', COALESCE(array_to_string(ARRAY_AGG(bookwyrm_author.name), ' '), '')), 'C')
FROM bookwyrm_book
LEFT OUTER JOIN bookwyrm_book_authors
ON bookwyrm_book.id = bookwyrm_book_authors.book_id
LEFT OUTER JOIN bookwyrm_author
ON bookwyrm_book_authors.author_id = bookwyrm_author.id
WHERE bookwyrm_book.id = new.id
) ||
setweight(to_tsvector('english', COALESCE(new.series, '')), 'D');
RETURN new;
"""
),
)
]


class Work(OrderedCollectionPageMixin, Book):
Expand Down
1 change: 0 additions & 1 deletion bookwyrm/tests/test_book_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,6 @@ def test_search_after_updated_author_name(self):

self.author.name = "Identifier"
self.author.save(broadcast=False)
self.edition.refresh_from_db()

self.assertFalse(self._search("Name"))
self.assertEqual(self.edition, self._search_first("Identifier"))
Expand Down
1 change: 1 addition & 0 deletions bookwyrm/utils/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def format_trigger(sql: str) -> str:
sql,
strip_comments=True,
strip_whitespace=True,
use_space_around_operators=True,
keyword_case="upper",
identifier_case="lower",
),
Expand Down

0 comments on commit 7472383

Please sign in to comment.