Skip to content

Commit 13b5672

Browse files
committed
Fix automatic hstore extension creation not working on Django 4.2 or newer
The following change broke the auto setup: django/django@d3e746a This breaks because the call to `pscygop2.extras.register_hstore` is now conditional. Before, it would be called multiple times with empty OIDS, when eventually our auto registration would kick in and psycopg2 would fetch the OIDs itself.
1 parent 43a6f22 commit 13b5672

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

psqlextra/backend/base.py

+23
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
from typing import TYPE_CHECKING
44

55
from django.conf import settings
6+
from django.contrib.postgres.signals import (
7+
get_hstore_oids,
8+
register_type_handlers,
9+
)
610
from django.db import ProgrammingError
711

812
from . import base_impl
@@ -94,3 +98,22 @@ def prepare_database(self):
9498
"or add the extension manually.",
9599
exc_info=True,
96100
)
101+
return
102+
103+
# Clear old (non-existent), stale oids.
104+
get_hstore_oids.cache_clear()
105+
106+
# Verify that we (and Django) can find the OIDs
107+
# for hstore.
108+
oids, _ = get_hstore_oids(self.alias)
109+
if not oids:
110+
logger.warning(
111+
'"hstore" extension was created, but we cannot find the oids'
112+
"in the database. Something went wrong.",
113+
)
114+
return
115+
116+
# We must trigger Django into registering the type handlers now
117+
# so that any subsequent code can properly use the newly
118+
# registered types.
119+
register_type_handlers(self)

0 commit comments

Comments
 (0)