# Path: psqlextra/indexes/unique_index.py
# class UniqueIndex(Index):
#     def create_sql(self, *args, **kwargs):
#         if django.VERSION >= (2, 0):
#             statement = super().create_sql(*args, **kwargs)
#             statement.template = self._rewrite_sql(statement.template)
#             return statement
# 
#         sql = super().create_sql(*args, **kwargs)
#         return self._rewrite_sql(sql)
# 
#     @staticmethod
#     def _rewrite_sql(sql: str) -> str:
#         return sql.replace("CREATE INDEX", "CREATE UNIQUE INDEX")
# 
# Path: tests/migrations.py
# def apply_migration(operations, state=None, backwards: bool = False):
#     """Executes the specified migration operations using the specified schema
#     editor.
# 
#     Arguments:
#         operations:
#             The migration operations to execute.
# 
#         state:
#             The state state to use during the
#             migrations.
# 
#         backwards:
#             Whether to apply the operations
#             in reverse (backwards).
#     """
# 
#     state = state or migrations.state.ProjectState.from_apps(apps)
# 
#     class Migration(migrations.Migration):
#         pass
# 
#     Migration.operations = operations
# 
#     migration = Migration("migration", "tests")
#     executor = MigrationExecutor(connection)
# 
#     if not backwards:
#         executor.apply_migration(state, migration)
#     else:
#         executor.unapply_migration(state, migration)
# 
#     return migration
# 
# @contextmanager
# def filtered_schema_editor(*filters: List[str]):
#     """Gets a schema editor, but filters executed SQL statements based on the
#     specified text filters.
# 
#     Arguments:
#         filters:
#             List of strings to filter SQL
#             statements on.
#     """
# 
#     with connection.schema_editor() as schema_editor:
#         wrapper_for = schema_editor.execute
#         with mock.patch.object(
#             PostgresSchemaEditor, "execute", wraps=wrapper_for
#         ) as execute:
#             filter_results = {}
#             yield filter_results
# 
#     for filter_text in filters:
#         filter_results[filter_text] = [
#             call for call in execute.mock_calls if filter_text in str(call)
#         ]


from django.db import models
from django.db.migrations import AddIndex, CreateModel
from psqlextra.indexes import UniqueIndex
from .migrations import apply_migration, filtered_schema_editor




def test_unique_index_migrations():
    index = UniqueIndex(fields=["name", "other_name"], name="index1")

    ops = [
        CreateModel(
            name="mymodel",
            fields=[
                ("name", models.TextField()),
                ("other_name", models.TextField()),
            ],
            options={
                # "indexes": [index],
            },
        ),
        AddIndex(model_name="mymodel", index=index),
    ]

    with filtered_schema_editor("CREATE UNIQUE INDEX") as calls:

# Path: psqlextra/indexes/unique_index.py
# class UniqueIndex(Index):
#     def create_sql(self, *args, **kwargs):
#         if django.VERSION >= (2, 0):
#             statement = super().create_sql(*args, **kwargs)
#             statement.template = self._rewrite_sql(statement.template)
#             return statement
# 
#         sql = super().create_sql(*args, **kwargs)
#         return self._rewrite_sql(sql)
# 
#     @staticmethod
#     def _rewrite_sql(sql: str) -> str:
#         return sql.replace("CREATE INDEX", "CREATE UNIQUE INDEX")
# 
# Path: tests/migrations.py
# def apply_migration(operations, state=None, backwards: bool = False):
#     """Executes the specified migration operations using the specified schema
#     editor.
# 
#     Arguments:
#         operations:
#             The migration operations to execute.
# 
#         state:
#             The state state to use during the
#             migrations.
# 
#         backwards:
#             Whether to apply the operations
#             in reverse (backwards).
#     """
# 
#     state = state or migrations.state.ProjectState.from_apps(apps)
# 
#     class Migration(migrations.Migration):
#         pass
# 
#     Migration.operations = operations
# 
#     migration = Migration("migration", "tests")
#     executor = MigrationExecutor(connection)
# 
#     if not backwards:
#         executor.apply_migration(state, migration)
#     else:
#         executor.unapply_migration(state, migration)
# 
#     return migration
# 
# @contextmanager
# def filtered_schema_editor(*filters: List[str]):
#     """Gets a schema editor, but filters executed SQL statements based on the
#     specified text filters.
# 
#     Arguments:
#         filters:
#             List of strings to filter SQL
#             statements on.
#     """
# 
#     with connection.schema_editor() as schema_editor:
#         wrapper_for = schema_editor.execute
#         with mock.patch.object(
#             PostgresSchemaEditor, "execute", wraps=wrapper_for
#         ) as execute:
#             filter_results = {}
#             yield filter_results
# 
#     for filter_text in filters:
#         filter_results[filter_text] = [
#             call for call in execute.mock_calls if filter_text in str(call)
#         ]


# Path: tests/test_unique_index.py
from django.db import models
from django.db.migrations import AddIndex, CreateModel
from psqlextra.indexes import UniqueIndex
from .migrations import apply_migration, filtered_schema_editor





def test_unique_index_migrations():
    index = UniqueIndex(fields=["name", "other_name"], name="index1")

    ops = [
        CreateModel(
            name="mymodel",
            fields=[
                ("name", models.TextField()),
                ("other_name", models.TextField()),
            ],
            options={
                # "indexes": [index],
            },
        ),
        AddIndex(model_name="mymodel", index=index),
    ]

    with filtered_schema_editor("CREATE UNIQUE INDEX") as calls:

        apply_migration(ops)