TandoorRecipes/cookbook/migrations/0122_build_full_text_index.py

54 lines
1.9 KiB
Python

# Generated by Django 3.1.7 on 2021-04-07 20:00
from django.conf import settings
from django.contrib.postgres.indexes import GinIndex
from django.contrib.postgres.search import SearchVectorField, SearchVector
from django.db import migrations
from django_scopes import scopes_disabled
from cookbook.models import Recipe, Step
def set_default_search_vector(apps, schema_editor):
if settings.DATABASES['default']['ENGINE'] not in ['django.db.backends.postgresql_psycopg2', 'django.db.backends.postgresql']:
return
with scopes_disabled():
# TODO add language
Recipe.objects.all().update(
name_search_vector=SearchVector('name__unaccent', weight='A'),
desc_search_vector=SearchVector('description__unaccent', weight='B')
)
Step.objects.all().update(search_vector=SearchVector('instruction__unaccent', weight='B'))
class Migration(migrations.Migration):
dependencies = [
('cookbook', '0121_auto_20210518_1638'),
]
operations = [
migrations.AddField(
model_name='recipe',
name='desc_search_vector',
field=SearchVectorField(null=True),
),
migrations.AddField(
model_name='recipe',
name='name_search_vector',
field=SearchVectorField(null=True),
),
migrations.AddIndex(
model_name='recipe',
index=GinIndex(fields=['name_search_vector', 'desc_search_vector'], name='cookbook_re_name_se_bdf3ca_gin'),
),
migrations.AddField(
model_name='step',
name='search_vector',
field=SearchVectorField(null=True),
),
migrations.AddIndex(
model_name='step',
index=GinIndex(fields=['search_vector'], name='cookbook_st_search__2ef7fa_gin'),
),
migrations.RunPython(
set_default_search_vector
),
]