diff options
| author | Simon Charette <charette.s@gmail.com> | 2017-05-03 01:25:30 -0400 |
|---|---|---|
| committer | Simon Charette <charette.s@gmail.com> | 2017-05-04 00:23:49 -0400 |
| commit | 246166cfe4d5d054fcef452c5af2d0b0e5f37151 (patch) | |
| tree | d69f5e20e3191fbb881363051e68aa8830093ee6 /django | |
| parent | f3217ab59696ea095a42c7fb4d98f21bb000ca8e (diff) | |
[1.11.x] Fixed #28161 -- Fixed return type of ArrayField(CITextField()).
Thanks Tim for the review.
Backport of b91868507af08234a30e9a8e7c90b37c561ba315 from master.
Diffstat (limited to 'django')
| -rw-r--r-- | django/contrib/postgres/apps.py | 6 | ||||
| -rw-r--r-- | django/contrib/postgres/operations.py | 13 | ||||
| -rw-r--r-- | django/contrib/postgres/signals.py | 17 |
3 files changed, 24 insertions, 12 deletions
diff --git a/django/contrib/postgres/apps.py b/django/contrib/postgres/apps.py index 0eeee6332b..9de733c261 100644 --- a/django/contrib/postgres/apps.py +++ b/django/contrib/postgres/apps.py @@ -5,7 +5,7 @@ from django.db.models import CharField, TextField from django.utils.translation import ugettext_lazy as _ from .lookups import SearchLookup, TrigramSimilar, Unaccent -from .signals import register_hstore_handler +from .signals import register_type_handlers class PostgresConfig(AppConfig): @@ -16,8 +16,8 @@ class PostgresConfig(AppConfig): # Connections may already exist before we are called. for conn in connections.all(): if conn.connection is not None: - register_hstore_handler(conn) - connection_created.connect(register_hstore_handler) + register_type_handlers(conn) + connection_created.connect(register_type_handlers) CharField.register_lookup(Unaccent) TextField.register_lookup(Unaccent) CharField.register_lookup(SearchLookup) diff --git a/django/contrib/postgres/operations.py b/django/contrib/postgres/operations.py index cb45d4f705..cf790209b1 100644 --- a/django/contrib/postgres/operations.py +++ b/django/contrib/postgres/operations.py @@ -1,4 +1,4 @@ -from django.contrib.postgres.signals import register_hstore_handler +from django.contrib.postgres.signals import register_type_handlers from django.db.migrations.operations.base import Operation @@ -15,6 +15,10 @@ class CreateExtension(Operation): if schema_editor.connection.vendor != 'postgresql': return schema_editor.execute("CREATE EXTENSION IF NOT EXISTS %s" % schema_editor.quote_name(self.name)) + # Registering new type handlers cannot be done before the extension is + # installed, otherwise a subsequent data migration would use the same + # connection. + register_type_handlers(schema_editor.connection) def database_backwards(self, app_label, schema_editor, from_state, to_state): schema_editor.execute("DROP EXTENSION %s" % schema_editor.quote_name(self.name)) @@ -40,13 +44,6 @@ class HStoreExtension(CreateExtension): def __init__(self): self.name = 'hstore' - def database_forwards(self, app_label, schema_editor, from_state, to_state): - super(HStoreExtension, self).database_forwards(app_label, schema_editor, from_state, to_state) - # Register hstore straight away as it cannot be done before the - # extension is installed, a subsequent data migration would use the - # same connection - register_hstore_handler(schema_editor.connection) - class TrigramExtension(CreateExtension): diff --git a/django/contrib/postgres/signals.py b/django/contrib/postgres/signals.py index 183ba1d983..af7f0b2d30 100644 --- a/django/contrib/postgres/signals.py +++ b/django/contrib/postgres/signals.py @@ -1,10 +1,11 @@ +import psycopg2 from psycopg2 import ProgrammingError from psycopg2.extras import register_hstore from django.utils import six -def register_hstore_handler(connection, **kwargs): +def register_type_handlers(connection, **kwargs): if connection.vendor != 'postgresql': return @@ -23,3 +24,17 @@ def register_hstore_handler(connection, **kwargs): # This is also needed in order to create the connection in order to # install the hstore extension. pass + + try: + with connection.cursor() as cursor: + # Retrieve oids of citext arrays. + cursor.execute("SELECT typarray FROM pg_type WHERE typname = 'citext'") + oids = tuple(row[0] for row in cursor) + array_type = psycopg2.extensions.new_array_type(oids, 'citext[]', psycopg2.STRING) + psycopg2.extensions.register_type(array_type, None) + except ProgrammingError: + # citext is not available on the database. + # + # The same comments in the except block of the above call to + # register_hstore() also apply here. + pass |
