# Generated by Django 3.2.4 on 2021-07-12 22:49
# Renamed and updated to remove from Wagtail on 2025-03-24

from django.db import connection, migrations, models

from modelsearch.models import IndexEntry


# This migration takes on the base model defined in 0001_initial and adds certain fields that are specific to each database system
class Migration(migrations.Migration):
    dependencies = [
        ("modelsearch", "0001_initial"),
    ]

    if connection.vendor == "postgresql":
        import django.contrib.postgres.indexes
        import django.contrib.postgres.search

        operations = [
            migrations.AddField(
                model_name="indexentry",
                name="autocomplete",
                field=django.contrib.postgres.search.SearchVectorField(),
            ),
            migrations.AddField(
                model_name="indexentry",
                name="title",
                field=django.contrib.postgres.search.SearchVectorField(),
            ),
            migrations.AddField(
                model_name="indexentry",
                name="body",
                field=django.contrib.postgres.search.SearchVectorField(),
            ),
            migrations.AddIndex(
                model_name="indexentry",
                index=django.contrib.postgres.indexes.GinIndex(
                    fields=["autocomplete"], name="modelsear_autocom_476c89_gin"
                ),
            ),
            migrations.AddIndex(
                model_name="indexentry",
                index=django.contrib.postgres.indexes.GinIndex(
                    fields=["title"], name="modelsear_title_9caae0_gin"
                ),
            ),
            migrations.AddIndex(
                model_name="indexentry",
                index=django.contrib.postgres.indexes.GinIndex(
                    fields=["body"], name="modelsear_body_90c85d_gin"
                ),
            ),
        ]

    elif connection.vendor == "sqlite":
        from modelsearch.backends.database.sqlite.utils import fts5_available

        operations = [
            migrations.AddField(
                model_name="indexentry",
                name="autocomplete",
                field=models.TextField(null=True),
            ),
            migrations.AddField(
                model_name="indexentry",
                name="body",
                field=models.TextField(null=True),
            ),
            migrations.AddField(
                model_name="indexentry",
                name="title",
                field=models.TextField(),
            ),
        ]

        if fts5_available():
            operations.append(
                migrations.SeparateDatabaseAndState(
                    state_operations=[
                        migrations.CreateModel(
                            name="sqliteftsindexentry",
                            fields=[
                                (
                                    "index_entry",
                                    models.OneToOneField(
                                        primary_key=True,
                                        serialize=False,
                                        to="modelsearch.indexentry",
                                        on_delete=models.CASCADE,
                                        db_column="rowid",
                                    ),
                                ),
                                ("title", models.TextField()),
                                ("body", models.TextField(null=True)),
                                ("autocomplete", models.TextField(null=True)),
                            ],
                            options={"db_table": "%s_fts" % IndexEntry._meta.db_table},
                        ),
                    ],
                    database_operations=[
                        migrations.RunSQL(
                            sql=(
                                "CREATE VIRTUAL TABLE %s_fts USING fts5(autocomplete, body, title)"
                                % IndexEntry._meta.db_table
                            ),
                            reverse_sql=(
                                "DROP TABLE IF EXISTS %s_fts"
                                % IndexEntry._meta.db_table
                            ),
                        ),
                        migrations.RunSQL(
                            sql=(
                                "CREATE TRIGGER insert_modelsearch_indexentry_fts AFTER INSERT ON %s BEGIN INSERT INTO %s_fts(title, body, autocomplete, rowid) VALUES (NEW.title, NEW.body, NEW.autocomplete, NEW.id); END"
                                % (IndexEntry._meta.db_table, IndexEntry._meta.db_table)
                            ),
                            reverse_sql=(
                                "DROP TRIGGER IF EXISTS insert_modelsearch_indexentry_fts"
                            ),
                        ),
                        migrations.RunSQL(
                            sql=(
                                "CREATE TRIGGER update_modelsearch_indexentry_fts AFTER UPDATE ON %s BEGIN UPDATE %s_fts SET title=NEW.title, body=NEW.body, autocomplete=NEW.autocomplete WHERE rowid=NEW.id; END"
                                % (IndexEntry._meta.db_table, IndexEntry._meta.db_table)
                            ),
                            reverse_sql=(
                                "DROP TRIGGER IF EXISTS update_modelsearch_indexentry_fts"
                            ),
                        ),
                        migrations.RunSQL(
                            sql=(
                                "CREATE TRIGGER delete_modelsearch_indexentry_fts AFTER DELETE ON %s BEGIN DELETE FROM %s_fts WHERE rowid=OLD.id; END"
                                % (IndexEntry._meta.db_table, IndexEntry._meta.db_table)
                            ),
                            reverse_sql=(
                                "DROP TRIGGER IF EXISTS delete_modelsearch_indexentry_fts"
                            ),
                        ),
                    ],
                )
            )
