# Generated by Django 3.2.4 on 2021-06-23 21:55 import django.contrib.postgres.indexes import django.contrib.postgres.search from django.db import migrations class Migration(migrations.Migration): dependencies = [ ("bookwyrm", "0076_preview_images"), ] operations = [ migrations.AddField( model_name="author", name="search_vector", field=django.contrib.postgres.search.SearchVectorField(null=True), ), migrations.AddField( model_name="book", name="search_vector", field=django.contrib.postgres.search.SearchVectorField(null=True), ), migrations.AddIndex( model_name="author", index=django.contrib.postgres.indexes.GinIndex( fields=["search_vector"], name="bookwyrm_au_search__b050a8_gin" ), ), migrations.AddIndex( model_name="book", index=django.contrib.postgres.indexes.GinIndex( fields=["search_vector"], name="bookwyrm_bo_search__51beb3_gin" ), ), migrations.RunSQL( sql=""" CREATE FUNCTION book_trigger() RETURNS trigger AS $$ begin 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), ' '), '')), 'B') 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; end $$ LANGUAGE plpgsql; CREATE TRIGGER search_vector_trigger BEFORE INSERT OR UPDATE OF title, subtitle, series, search_vector ON bookwyrm_book FOR EACH ROW EXECUTE FUNCTION book_trigger(); UPDATE bookwyrm_book SET search_vector = NULL; """, reverse_sql=""" DROP TRIGGER IF EXISTS search_vector_trigger ON bookwyrm_book; DROP FUNCTION IF EXISTS book_trigger; """, ), ]