mirror of
https://github.com/bookwyrm-social/bookwyrm.git
synced 2024-11-27 03:51:08 +00:00
Trigger search vector update when author is edited
This commit is contained in:
parent
2fa5fabb7b
commit
68fc5f2b5a
3 changed files with 121 additions and 1 deletions
|
@ -70,4 +70,57 @@ class Migration(migrations.Migration):
|
|||
DROP FUNCTION IF EXISTS book_trigger;
|
||||
""",
|
||||
),
|
||||
# when an author is edited
|
||||
migrations.RunSQL(
|
||||
sql="""
|
||||
CREATE 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;
|
||||
|
||||
CREATE TRIGGER author_search_vector_trigger
|
||||
AFTER UPDATE OF name
|
||||
ON bookwyrm_author
|
||||
FOR EACH ROW EXECUTE FUNCTION author_trigger();
|
||||
""",
|
||||
reverse_sql="""
|
||||
DROP TRIGGER IF EXISTS author_search_vector_trigger
|
||||
ON bookwyrm_author;
|
||||
DROP FUNCTION IF EXISTS author_trigger;
|
||||
""",
|
||||
),
|
||||
# when an author is added to or removed from a book
|
||||
migrations.RunSQL(
|
||||
sql="""
|
||||
CREATE FUNCTION book_authors_trigger() RETURNS trigger AS $$
|
||||
begin
|
||||
UPDATE bookwyrm_book SET search_vector = ''
|
||||
WHERE id = coalesce(new.book_id, old.book_id);
|
||||
return new;
|
||||
end
|
||||
$$ LANGUAGE plpgsql;
|
||||
|
||||
CREATE TRIGGER book_authors_search_vector_trigger
|
||||
AFTER INSERT OR DELETE
|
||||
ON bookwyrm_book_authors
|
||||
FOR EACH ROW EXECUTE FUNCTION book_authors_trigger();
|
||||
""",
|
||||
reverse_sql="""
|
||||
DROP TRIGGER IF EXISTS book_authors_search_vector_trigger
|
||||
ON bookwyrm_book_authors;
|
||||
DROP FUNCTION IF EXISTS book_authors_trigger;
|
||||
""",
|
||||
),
|
||||
]
|
||||
|
|
|
@ -1,8 +1,9 @@
|
|||
""" testing models """
|
||||
from unittest.mock import patch
|
||||
|
||||
from dateutil.parser import parse
|
||||
from django.test import TestCase
|
||||
from django.utils import timezone
|
||||
from unittest.mock import patch
|
||||
|
||||
from bookwyrm import models, settings
|
||||
from bookwyrm.models.book import isbn_10_to_13, isbn_13_to_10
|
||||
|
|
66
bookwyrm/tests/test_postgres.py
Normal file
66
bookwyrm/tests/test_postgres.py
Normal file
|
@ -0,0 +1,66 @@
|
|||
""" django configuration of postgres """
|
||||
from django.test import TestCase
|
||||
|
||||
from bookwyrm import models
|
||||
|
||||
|
||||
class PostgresTriggers(TestCase):
|
||||
"""special migrations, fancy stuff ya know"""
|
||||
|
||||
def test_search_vector_on_create(self):
|
||||
"""make sure that search_vector is being set correctly on create"""
|
||||
book = models.Edition.objects.create(title="The Long Goodbye")
|
||||
book.refresh_from_db()
|
||||
self.assertEqual(book.search_vector, "'goodby':3A 'long':2A")
|
||||
|
||||
def test_search_vector_on_update(self):
|
||||
"""make sure that search_vector is being set correctly on edit"""
|
||||
book = models.Edition.objects.create(title="The Long Goodbye")
|
||||
book.title = "The Even Longer Goodbye"
|
||||
book.save(broadcast=False)
|
||||
book.refresh_from_db()
|
||||
self.assertEqual(book.search_vector, "'even':2A 'goodby':4A 'longer':3A")
|
||||
|
||||
def test_search_vector_fields(self):
|
||||
"""use multiple fields to create search vector"""
|
||||
author = models.Author.objects.create(name="The Rays")
|
||||
book = models.Edition.objects.create(
|
||||
title="The Long Goodbye",
|
||||
subtitle="wow cool",
|
||||
series="series name",
|
||||
languages=["irrelevent"],
|
||||
)
|
||||
book.authors.add(author)
|
||||
book.refresh_from_db()
|
||||
self.assertEqual(
|
||||
book.search_vector,
|
||||
"'cool':5B 'goodby':3A 'long':2A 'name':9 'rays':7B 'seri':8 'the':6B 'wow':4B",
|
||||
)
|
||||
|
||||
def test_seach_vector_on_author_update(self):
|
||||
"""update search when an author name changes"""
|
||||
author = models.Author.objects.create(name="The Rays")
|
||||
book = models.Edition.objects.create(
|
||||
title="The Long Goodbye",
|
||||
)
|
||||
book.authors.add(author)
|
||||
author.name = "Jeremy"
|
||||
author.save(broadcast=False)
|
||||
book.refresh_from_db()
|
||||
|
||||
self.assertEqual(book.search_vector, "'goodby':3A 'jeremy':4B 'long':2A")
|
||||
|
||||
def test_seach_vector_on_author_delete(self):
|
||||
"""update search when an author name changes"""
|
||||
author = models.Author.objects.create(name="Jeremy")
|
||||
book = models.Edition.objects.create(
|
||||
title="The Long Goodbye",
|
||||
)
|
||||
|
||||
book.authors.add(author)
|
||||
book.refresh_from_db()
|
||||
self.assertEqual(book.search_vector, "'goodby':3A 'jeremy':4B 'long':2A")
|
||||
|
||||
book.authors.remove(author)
|
||||
book.refresh_from_db()
|
||||
self.assertEqual(book.search_vector, "'goodby':3A 'long':2A")
|
Loading…
Reference in a new issue