# Generated by Django 3.2.7 on 2021-09-29 06:37
from django_scopes import scopes_disabled

from django.db import migrations, models

from cookbook.models import SearchFields


def nameSearchField():
    return [SearchFields.objects.get(name='Name').id]


def add_default_trigram(apps, schema_editor):
    with scopes_disabled():
        SearchFields = apps.get_model('cookbook', 'SearchFields')
        SearchPreference = apps.get_model('cookbook', 'SearchPreference')

        name_field = SearchFields.objects.get(name='Name')

        for p in SearchPreference.objects.all():
            if not p.trigram.all() and p.search == 'plain':
                p.trigram.add(name_field)
                p.save()


class Migration(migrations.Migration):
    dependencies = [
        ('cookbook', '0156_searchpreference_trigram_threshold'),
    ]

    operations = [
        migrations.AlterField(
            model_name='searchpreference',
            name='trigram',
            field=models.ManyToManyField(blank=True, default=nameSearchField, related_name='trigram_fields', to='cookbook.SearchFields'),
        ),
        migrations.RunPython(add_default_trigram),
    ]
