# Path: psqlextra/backend/migrations/patched_autodetector.py
# @contextmanager
# def patched_autodetector():
#     """Patches the standard Django :seee:MigrationAutodetector for the duration
#     of the context.
# 
#     The patch intercepts the `add_operation` function to
#     customize how new operations are added.
# 
#     We have to do this because there is no way in Django
#     to extend the auto detector otherwise.
#     """
# 
#     autodetector_module_path = "django.db.migrations.autodetector"
#     autodetector_class_path = (
#         f"{autodetector_module_path}.MigrationAutodetector"
#     )
#     add_operation_path = f"{autodetector_class_path}.add_operation"
# 
#     def _patched(autodetector, app_label, operation, *args, **kwargs):
#         handler = AddOperationHandler(autodetector, app_label, args, kwargs)
# 
#         if isinstance(operation, CreateModel):
#             return handler.add_create_model(operation)
# 
#         if isinstance(operation, DeleteModel):
#             return handler.add_delete_model(operation)
# 
#         if isinstance(operation, AddField):
#             return handler.add_field(operation)
# 
#         if isinstance(operation, RemoveField):
#             return handler.remove_field(operation)
# 
#         if isinstance(operation, AlterField):
#             return handler.alter_field(operation)
# 
#         if isinstance(operation, RenameField):
#             return handler.rename_field(operation)
# 
#         return handler.add(operation)
# 
#     with mock.patch(add_operation_path, new=_patched):
#         yield
# 
# Path: psqlextra/backend/migrations/patched_project_state.py
# @contextmanager
# def patched_project_state():
#     """Patches the standard Django :see:ProjectState.from_apps for the duration
#     of the context.
# 
#     The patch intercepts the `from_apps` function to control
#     how model state is creatd. We want to use our custom
#     model state classes for certain types of models.
# 
#     We have to do this because there is no way in Django
#     to extend the project state otherwise.
#     """
# 
#     from_apps_module_path = "django.db.migrations.state"
#     from_apps_class_path = f"{from_apps_module_path}.ProjectState"
#     from_apps_path = f"{from_apps_class_path}.from_apps"
# 
#     with mock.patch(from_apps_path, new=project_state_from_apps):
#         yield


from contextlib import contextmanager
from .patched_autodetector import patched_autodetector
from .patched_project_state import patched_project_state



@contextmanager
def postgres_patched_migrations():
    """Patches migration related classes/functions to extend how Django
    generates and applies migrations.

    This adds support for automatically detecting changes in Postgres
    specific models.
    """


# Path: psqlextra/backend/migrations/patched_autodetector.py
# @contextmanager
# def patched_autodetector():
#     """Patches the standard Django :seee:MigrationAutodetector for the duration
#     of the context.
# 
#     The patch intercepts the `add_operation` function to
#     customize how new operations are added.
# 
#     We have to do this because there is no way in Django
#     to extend the auto detector otherwise.
#     """
# 
#     autodetector_module_path = "django.db.migrations.autodetector"
#     autodetector_class_path = (
#         f"{autodetector_module_path}.MigrationAutodetector"
#     )
#     add_operation_path = f"{autodetector_class_path}.add_operation"
# 
#     def _patched(autodetector, app_label, operation, *args, **kwargs):
#         handler = AddOperationHandler(autodetector, app_label, args, kwargs)
# 
#         if isinstance(operation, CreateModel):
#             return handler.add_create_model(operation)
# 
#         if isinstance(operation, DeleteModel):
#             return handler.add_delete_model(operation)
# 
#         if isinstance(operation, AddField):
#             return handler.add_field(operation)
# 
#         if isinstance(operation, RemoveField):
#             return handler.remove_field(operation)
# 
#         if isinstance(operation, AlterField):
#             return handler.alter_field(operation)
# 
#         if isinstance(operation, RenameField):
#             return handler.rename_field(operation)
# 
#         return handler.add(operation)
# 
#     with mock.patch(add_operation_path, new=_patched):
#         yield
# 
# Path: psqlextra/backend/migrations/patched_project_state.py
# @contextmanager
# def patched_project_state():
#     """Patches the standard Django :see:ProjectState.from_apps for the duration
#     of the context.
# 
#     The patch intercepts the `from_apps` function to control
#     how model state is creatd. We want to use our custom
#     model state classes for certain types of models.
# 
#     We have to do this because there is no way in Django
#     to extend the project state otherwise.
#     """
# 
#     from_apps_module_path = "django.db.migrations.state"
#     from_apps_class_path = f"{from_apps_module_path}.ProjectState"
#     from_apps_path = f"{from_apps_class_path}.from_apps"
# 
#     with mock.patch(from_apps_path, new=project_state_from_apps):
#         yield


# Path: psqlextra/backend/migrations/patched_migrations.py
from contextlib import contextmanager
from .patched_autodetector import patched_autodetector
from .patched_project_state import patched_project_state




@contextmanager
def postgres_patched_migrations():
    """Patches migration related classes/functions to extend how Django
    generates and applies migrations.

    This adds support for automatically detecting changes in Postgres
    specific models.
    """


    with patched_project_state():