Fixed #34344 -- Allowed PostGIS DatabaseWrapper subclasses to customize (features/introspection/ops)_class classes.

This commit is contained in:
David Buhler 2023-02-20 12:31:03 -07:00 committed by Mariusz Felisiak
parent 5afd8c6940
commit a91e59e4a0
1 changed files with 18 additions and 4 deletions

View File

@ -2,6 +2,15 @@ from functools import lru_cache
from django.db.backends.base.base import NO_DB_ALIAS from django.db.backends.base.base import NO_DB_ALIAS
from django.db.backends.postgresql.base import DatabaseWrapper as PsycopgDatabaseWrapper from django.db.backends.postgresql.base import DatabaseWrapper as PsycopgDatabaseWrapper
from django.db.backends.postgresql.features import (
DatabaseFeatures as PsycopgDatabaseFeatures,
)
from django.db.backends.postgresql.introspection import (
DatabaseIntrospection as PsycopgDatabaseIntrospection,
)
from django.db.backends.postgresql.operations import (
DatabaseOperations as PsycopgDatabaseOperations,
)
from django.db.backends.postgresql.psycopg_any import is_psycopg3 from django.db.backends.postgresql.psycopg_any import is_psycopg3
from .adapter import PostGISAdapter from .adapter import PostGISAdapter
@ -81,6 +90,9 @@ if is_psycopg3:
class DatabaseWrapper(PsycopgDatabaseWrapper): class DatabaseWrapper(PsycopgDatabaseWrapper):
SchemaEditorClass = PostGISSchemaEditor SchemaEditorClass = PostGISSchemaEditor
features_class = DatabaseFeatures
ops_class = PostGISOperations
introspection_class = PostGISIntrospection
_type_infos = { _type_infos = {
"geometry": {}, "geometry": {},
@ -89,11 +101,13 @@ class DatabaseWrapper(PsycopgDatabaseWrapper):
} }
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
if kwargs.get("alias", "") == NO_DB_ALIAS:
# Don't initialize PostGIS-specific stuff for non-db connections.
self.features_class = PsycopgDatabaseFeatures
self.ops_class = PsycopgDatabaseOperations
self.introspection_class = PsycopgDatabaseIntrospection
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
if kwargs.get("alias", "") != NO_DB_ALIAS:
self.features = DatabaseFeatures(self)
self.ops = PostGISOperations(self)
self.introspection = PostGISIntrospection(self)
def prepare_database(self): def prepare_database(self):
super().prepare_database() super().prepare_database()