2006-05-17 07:27:07 +08:00
|
|
|
"""
|
|
|
|
PostgreSQL database backend for Django.
|
|
|
|
|
2020-01-29 08:58:39 +08:00
|
|
|
Requires psycopg 2: https://www.psycopg.org/
|
2006-05-17 07:27:07 +08:00
|
|
|
"""
|
2014-03-24 04:45:31 +08:00
|
|
|
|
2019-04-12 21:15:18 +08:00
|
|
|
import asyncio
|
2016-06-04 06:31:21 +08:00
|
|
|
import threading
|
2015-05-15 01:27:31 +08:00
|
|
|
import warnings
|
|
|
|
|
2013-07-08 08:39:54 +08:00
|
|
|
from django.conf import settings
|
2015-02-17 01:11:39 +08:00
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
2018-04-04 04:21:47 +08:00
|
|
|
from django.db import connections
|
2015-01-13 04:20:40 +08:00
|
|
|
from django.db.backends.base.base import BaseDatabaseWrapper
|
2019-04-26 00:09:27 +08:00
|
|
|
from django.db.backends.utils import (
|
|
|
|
CursorDebugWrapper as BaseCursorDebugWrapper,
|
|
|
|
)
|
2015-05-15 01:27:31 +08:00
|
|
|
from django.db.utils import DatabaseError as WrappedDatabaseError
|
2019-04-12 21:15:18 +08:00
|
|
|
from django.utils.asyncio import async_unsafe
|
2013-02-19 05:49:59 +08:00
|
|
|
from django.utils.functional import cached_property
|
2019-02-05 22:38:29 +08:00
|
|
|
from django.utils.safestring import SafeString
|
2017-02-16 00:14:21 +08:00
|
|
|
from django.utils.version import get_version_tuple
|
2008-08-11 20:11:25 +08:00
|
|
|
|
2006-05-27 02:58:46 +08:00
|
|
|
try:
|
|
|
|
import psycopg2 as Database
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
import psycopg2.extensions
|
2014-07-15 17:35:29 +08:00
|
|
|
import psycopg2.extras
|
2012-04-29 00:09:37 +08:00
|
|
|
except ImportError as e:
|
2007-08-20 11:32:06 +08:00
|
|
|
raise ImproperlyConfigured("Error loading psycopg2 module: %s" % e)
|
2006-05-17 07:27:07 +08:00
|
|
|
|
2015-02-17 01:11:39 +08:00
|
|
|
|
|
|
|
def psycopg2_version():
|
|
|
|
version = psycopg2.__version__.split(' ', 1)[0]
|
2017-02-16 00:14:21 +08:00
|
|
|
return get_version_tuple(version)
|
2015-02-17 01:11:39 +08:00
|
|
|
|
2016-11-13 01:11:23 +08:00
|
|
|
|
2015-02-17 01:11:39 +08:00
|
|
|
PSYCOPG2_VERSION = psycopg2_version()
|
|
|
|
|
2017-03-22 00:23:17 +08:00
|
|
|
if PSYCOPG2_VERSION < (2, 5, 4):
|
|
|
|
raise ImproperlyConfigured("psycopg2_version 2.5.4 or newer is required; you have %s" % psycopg2.__version__)
|
2015-02-17 01:11:39 +08:00
|
|
|
|
|
|
|
|
2015-01-13 04:20:40 +08:00
|
|
|
# Some of these import psycopg2, so import them after checking if it's installed.
|
2016-04-04 09:41:37 +08:00
|
|
|
from .client import DatabaseClient # NOQA isort:skip
|
|
|
|
from .creation import DatabaseCreation # NOQA isort:skip
|
|
|
|
from .features import DatabaseFeatures # NOQA isort:skip
|
|
|
|
from .introspection import DatabaseIntrospection # NOQA isort:skip
|
|
|
|
from .operations import DatabaseOperations # NOQA isort:skip
|
|
|
|
from .schema import DatabaseSchemaEditor # NOQA isort:skip
|
2015-01-13 04:20:40 +08:00
|
|
|
|
2019-02-05 22:38:29 +08:00
|
|
|
psycopg2.extensions.register_adapter(SafeString, psycopg2.extensions.QuotedString)
|
2014-07-15 17:35:29 +08:00
|
|
|
psycopg2.extras.register_uuid()
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
|
2015-01-11 02:13:28 +08:00
|
|
|
# Register support for inet[] manually so we don't have to handle the Inet()
|
|
|
|
# object on load all the time.
|
|
|
|
INETARRAY_OID = 1041
|
|
|
|
INETARRAY = psycopg2.extensions.new_array_type(
|
|
|
|
(INETARRAY_OID,),
|
|
|
|
'INETARRAY',
|
|
|
|
psycopg2.extensions.UNICODE,
|
|
|
|
)
|
|
|
|
psycopg2.extensions.register_type(INETARRAY)
|
|
|
|
|
2013-07-08 08:39:54 +08:00
|
|
|
|
2007-08-20 05:30:57 +08:00
|
|
|
class DatabaseWrapper(BaseDatabaseWrapper):
|
2010-10-11 20:55:17 +08:00
|
|
|
vendor = 'postgresql'
|
2017-05-23 21:09:35 +08:00
|
|
|
display_name = 'PostgreSQL'
|
2014-12-30 04:14:40 +08:00
|
|
|
# This dictionary maps Field objects to their associated PostgreSQL column
|
|
|
|
# types, as strings. Column-type strings can contain format strings; they'll
|
|
|
|
# be interpolated against the values of Field.__dict__ before being output.
|
|
|
|
# If a column type is set to None, it won't be included in the output.
|
|
|
|
data_types = {
|
|
|
|
'AutoField': 'serial',
|
2015-07-02 16:43:15 +08:00
|
|
|
'BigAutoField': 'bigserial',
|
2014-12-30 04:14:40 +08:00
|
|
|
'BinaryField': 'bytea',
|
|
|
|
'BooleanField': 'boolean',
|
|
|
|
'CharField': 'varchar(%(max_length)s)',
|
|
|
|
'DateField': 'date',
|
|
|
|
'DateTimeField': 'timestamp with time zone',
|
|
|
|
'DecimalField': 'numeric(%(max_digits)s, %(decimal_places)s)',
|
|
|
|
'DurationField': 'interval',
|
|
|
|
'FileField': 'varchar(%(max_length)s)',
|
|
|
|
'FilePathField': 'varchar(%(max_length)s)',
|
|
|
|
'FloatField': 'double precision',
|
|
|
|
'IntegerField': 'integer',
|
|
|
|
'BigIntegerField': 'bigint',
|
|
|
|
'IPAddressField': 'inet',
|
|
|
|
'GenericIPAddressField': 'inet',
|
|
|
|
'NullBooleanField': 'boolean',
|
|
|
|
'OneToOneField': 'integer',
|
2019-10-16 20:32:12 +08:00
|
|
|
'PositiveBigIntegerField': 'bigint',
|
2014-12-30 04:14:40 +08:00
|
|
|
'PositiveIntegerField': 'integer',
|
|
|
|
'PositiveSmallIntegerField': 'smallint',
|
|
|
|
'SlugField': 'varchar(%(max_length)s)',
|
2019-07-27 05:05:22 +08:00
|
|
|
'SmallAutoField': 'smallserial',
|
2014-12-30 04:14:40 +08:00
|
|
|
'SmallIntegerField': 'smallint',
|
|
|
|
'TextField': 'text',
|
|
|
|
'TimeField': 'time',
|
|
|
|
'UUIDField': 'uuid',
|
|
|
|
}
|
|
|
|
data_type_check_constraints = {
|
2019-10-16 20:32:12 +08:00
|
|
|
'PositiveBigIntegerField': '"%(column)s" >= 0',
|
2014-12-30 04:14:40 +08:00
|
|
|
'PositiveIntegerField': '"%(column)s" >= 0',
|
|
|
|
'PositiveSmallIntegerField': '"%(column)s" >= 0',
|
|
|
|
}
|
2007-08-20 11:26:55 +08:00
|
|
|
operators = {
|
|
|
|
'exact': '= %s',
|
2008-08-25 20:56:06 +08:00
|
|
|
'iexact': '= UPPER(%s)',
|
2007-08-20 11:26:55 +08:00
|
|
|
'contains': 'LIKE %s',
|
2008-08-25 20:56:06 +08:00
|
|
|
'icontains': 'LIKE UPPER(%s)',
|
2007-08-20 11:26:55 +08:00
|
|
|
'regex': '~ %s',
|
|
|
|
'iregex': '~* %s',
|
|
|
|
'gt': '> %s',
|
|
|
|
'gte': '>= %s',
|
|
|
|
'lt': '< %s',
|
|
|
|
'lte': '<= %s',
|
|
|
|
'startswith': 'LIKE %s',
|
|
|
|
'endswith': 'LIKE %s',
|
2008-08-25 20:56:06 +08:00
|
|
|
'istartswith': 'LIKE UPPER(%s)',
|
|
|
|
'iendswith': 'LIKE UPPER(%s)',
|
2007-08-20 11:26:55 +08:00
|
|
|
}
|
2007-08-20 06:29:57 +08:00
|
|
|
|
2014-09-27 18:41:54 +08:00
|
|
|
# The patterns below are used to generate SQL pattern lookup clauses when
|
|
|
|
# the right-hand side of the lookup isn't a raw string (it might be an expression
|
|
|
|
# or the result of a bilateral transformation).
|
|
|
|
# In those cases, special characters for LIKE operators (e.g. \, *, _) should be
|
|
|
|
# escaped on database side.
|
|
|
|
#
|
|
|
|
# Note: we use str.format() here for readability as '%' is used as a wildcard for
|
|
|
|
# the LIKE operator.
|
2018-11-01 23:14:34 +08:00
|
|
|
pattern_esc = r"REPLACE(REPLACE(REPLACE({}, E'\\', E'\\\\'), E'%%', E'\\%%'), E'_', E'\\_')"
|
2014-01-18 17:09:43 +08:00
|
|
|
pattern_ops = {
|
2014-09-27 18:41:54 +08:00
|
|
|
'contains': "LIKE '%%' || {} || '%%'",
|
|
|
|
'icontains': "LIKE '%%' || UPPER({}) || '%%'",
|
|
|
|
'startswith': "LIKE {} || '%%'",
|
|
|
|
'istartswith': "LIKE UPPER({}) || '%%'",
|
|
|
|
'endswith': "LIKE '%%' || {}",
|
|
|
|
'iendswith': "LIKE '%%' || UPPER({})",
|
2014-01-18 17:09:43 +08:00
|
|
|
}
|
|
|
|
|
Refactored database exceptions wrapping.
Squashed commit of the following:
commit 2181d833ed1a2e422494738dcef311164c4e097e
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Wed Feb 27 14:28:39 2013 +0100
Fixed #15901 -- Wrapped all PEP-249 exceptions.
commit 5476a5d93c19aa2f928c497d39ce6e33f52694e2
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 17:26:52 2013 +0100
Added PEP 3134 exception chaining.
Thanks Jacob Kaplan-Moss for the suggestion.
commit 9365fad0a650328002fb424457d675a273c95802
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 17:13:49 2013 +0100
Improved API for wrapping database errors.
Thanks Alex Gaynor for the proposal.
commit 1b463b765f2826f73a8d9266795cd5da4f8d5e9e
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 15:00:39 2013 +0100
Removed redundant exception wrapping.
This is now taken care of by the cursor wrapper.
commit 524bc7345a724bf526bdd2dd1bcf5ede67d6bb5c
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 14:55:10 2013 +0100
Wrapped database exceptions in the base backend.
This covers the most common PEP-249 APIs:
- Connection APIs: close(), commit(), rollback(), cursor()
- Cursor APIs: callproc(), close(), execute(), executemany(),
fetchone(), fetchmany(), fetchall(), nextset().
Fixed #19920.
commit a66746bb5f0839f35543222787fce3b6a0d0a3ea
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 14:53:34 2013 +0100
Added a wrap_database_exception context manager and decorator.
It re-throws backend-specific exceptions using Django's common wrappers.
2013-02-26 21:53:34 +08:00
|
|
|
Database = Database
|
2014-09-26 01:59:03 +08:00
|
|
|
SchemaEditorClass = DatabaseSchemaEditor
|
2016-09-09 04:33:36 +08:00
|
|
|
# Classes instantiated in __init__().
|
|
|
|
client_class = DatabaseClient
|
|
|
|
creation_class = DatabaseCreation
|
|
|
|
features_class = DatabaseFeatures
|
|
|
|
introspection_class = DatabaseIntrospection
|
|
|
|
ops_class = DatabaseOperations
|
2017-09-05 18:20:05 +08:00
|
|
|
# PostgreSQL backend-specific attributes.
|
|
|
|
_named_cursor_idx = 0
|
2016-06-04 06:31:21 +08:00
|
|
|
|
2012-11-27 04:42:27 +08:00
|
|
|
def get_connection_params(self):
|
|
|
|
settings_dict = self.settings_dict
|
2013-11-09 16:41:03 +08:00
|
|
|
# None may be used to connect to the default 'postgres' db
|
|
|
|
if settings_dict['NAME'] == '':
|
2012-11-27 04:42:27 +08:00
|
|
|
raise ImproperlyConfigured(
|
|
|
|
"settings.DATABASES is improperly configured. "
|
|
|
|
"Please supply the NAME value.")
|
2018-01-19 02:15:16 +08:00
|
|
|
if len(settings_dict['NAME'] or '') > self.ops.max_name_length():
|
|
|
|
raise ImproperlyConfigured(
|
2018-08-18 00:43:56 +08:00
|
|
|
"The database name '%s' (%d characters) is longer than "
|
|
|
|
"PostgreSQL's limit of %d characters. Supply a shorter NAME "
|
|
|
|
"in settings.DATABASES." % (
|
|
|
|
settings_dict['NAME'],
|
|
|
|
len(settings_dict['NAME']),
|
|
|
|
self.ops.max_name_length(),
|
|
|
|
)
|
2018-01-19 02:15:16 +08:00
|
|
|
)
|
2012-11-27 04:42:27 +08:00
|
|
|
conn_params = {
|
2013-11-09 16:41:03 +08:00
|
|
|
'database': settings_dict['NAME'] or 'postgres',
|
2017-12-11 20:08:45 +08:00
|
|
|
**settings_dict['OPTIONS'],
|
2012-11-27 04:42:27 +08:00
|
|
|
}
|
2015-02-15 00:31:53 +08:00
|
|
|
conn_params.pop('isolation_level', None)
|
2012-11-27 04:42:27 +08:00
|
|
|
if settings_dict['USER']:
|
|
|
|
conn_params['user'] = settings_dict['USER']
|
|
|
|
if settings_dict['PASSWORD']:
|
2017-01-12 06:17:25 +08:00
|
|
|
conn_params['password'] = settings_dict['PASSWORD']
|
2012-11-27 04:42:27 +08:00
|
|
|
if settings_dict['HOST']:
|
|
|
|
conn_params['host'] = settings_dict['HOST']
|
|
|
|
if settings_dict['PORT']:
|
|
|
|
conn_params['port'] = settings_dict['PORT']
|
|
|
|
return conn_params
|
|
|
|
|
2019-04-12 21:15:18 +08:00
|
|
|
@async_unsafe
|
2012-11-27 04:42:27 +08:00
|
|
|
def get_new_connection(self, conn_params):
|
2015-02-14 16:50:38 +08:00
|
|
|
connection = Database.connect(**conn_params)
|
|
|
|
|
|
|
|
# self.isolation_level must be set:
|
|
|
|
# - after connecting to the database in order to obtain the database's
|
|
|
|
# default when no value is explicitly specified in options.
|
|
|
|
# - before calling _set_autocommit() because if autocommit is on, that
|
2015-02-17 01:11:39 +08:00
|
|
|
# will set connection.isolation_level to ISOLATION_LEVEL_AUTOCOMMIT.
|
2015-02-14 16:50:38 +08:00
|
|
|
options = self.settings_dict['OPTIONS']
|
|
|
|
try:
|
|
|
|
self.isolation_level = options['isolation_level']
|
|
|
|
except KeyError:
|
|
|
|
self.isolation_level = connection.isolation_level
|
|
|
|
else:
|
2015-02-17 01:11:39 +08:00
|
|
|
# Set the isolation level to the value from OPTIONS.
|
|
|
|
if self.isolation_level != connection.isolation_level:
|
2015-02-14 16:50:38 +08:00
|
|
|
connection.set_session(isolation_level=self.isolation_level)
|
|
|
|
|
|
|
|
return connection
|
2012-11-27 04:42:27 +08:00
|
|
|
|
2016-06-06 11:46:28 +08:00
|
|
|
def ensure_timezone(self):
|
2019-02-20 17:59:40 +08:00
|
|
|
if self.connection is None:
|
2019-01-13 03:17:36 +08:00
|
|
|
return False
|
2016-06-06 11:46:28 +08:00
|
|
|
conn_timezone_name = self.connection.get_parameter_status('TimeZone')
|
|
|
|
timezone_name = self.timezone_name
|
|
|
|
if timezone_name and conn_timezone_name != timezone_name:
|
|
|
|
with self.connection.cursor() as cursor:
|
|
|
|
cursor.execute(self.ops.set_time_zone_sql(), [timezone_name])
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2012-11-27 04:42:27 +08:00
|
|
|
def init_connection_state(self):
|
|
|
|
self.connection.set_client_encoding('UTF8')
|
2015-02-14 16:53:17 +08:00
|
|
|
|
2016-06-06 11:46:28 +08:00
|
|
|
timezone_changed = self.ensure_timezone()
|
|
|
|
if timezone_changed:
|
2015-02-14 16:53:17 +08:00
|
|
|
# Commit after setting the time zone (see #17062)
|
|
|
|
if not self.get_autocommit():
|
|
|
|
self.connection.commit()
|
2012-11-27 04:42:27 +08:00
|
|
|
|
2019-04-12 21:15:18 +08:00
|
|
|
@async_unsafe
|
2016-06-04 06:31:21 +08:00
|
|
|
def create_cursor(self, name=None):
|
|
|
|
if name:
|
|
|
|
# In autocommit mode, the cursor will be used outside of a
|
|
|
|
# transaction, hence use a holdable cursor.
|
|
|
|
cursor = self.connection.cursor(name, scrollable=False, withhold=self.connection.autocommit)
|
|
|
|
else:
|
|
|
|
cursor = self.connection.cursor()
|
2019-07-20 21:38:43 +08:00
|
|
|
cursor.tzinfo_factory = self.tzinfo_factory if settings.USE_TZ else None
|
Refactored database exceptions wrapping.
Squashed commit of the following:
commit 2181d833ed1a2e422494738dcef311164c4e097e
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Wed Feb 27 14:28:39 2013 +0100
Fixed #15901 -- Wrapped all PEP-249 exceptions.
commit 5476a5d93c19aa2f928c497d39ce6e33f52694e2
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 17:26:52 2013 +0100
Added PEP 3134 exception chaining.
Thanks Jacob Kaplan-Moss for the suggestion.
commit 9365fad0a650328002fb424457d675a273c95802
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 17:13:49 2013 +0100
Improved API for wrapping database errors.
Thanks Alex Gaynor for the proposal.
commit 1b463b765f2826f73a8d9266795cd5da4f8d5e9e
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 15:00:39 2013 +0100
Removed redundant exception wrapping.
This is now taken care of by the cursor wrapper.
commit 524bc7345a724bf526bdd2dd1bcf5ede67d6bb5c
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 14:55:10 2013 +0100
Wrapped database exceptions in the base backend.
This covers the most common PEP-249 APIs:
- Connection APIs: close(), commit(), rollback(), cursor()
- Cursor APIs: callproc(), close(), execute(), executemany(),
fetchone(), fetchmany(), fetchall(), nextset().
Fixed #19920.
commit a66746bb5f0839f35543222787fce3b6a0d0a3ea
Author: Aymeric Augustin <aymeric.augustin@m4x.org>
Date: Tue Feb 26 14:53:34 2013 +0100
Added a wrap_database_exception context manager and decorator.
It re-throws backend-specific exceptions using Django's common wrappers.
2013-02-26 21:53:34 +08:00
|
|
|
return cursor
|
2009-03-11 15:06:50 +08:00
|
|
|
|
2019-07-20 21:38:43 +08:00
|
|
|
def tzinfo_factory(self, offset):
|
|
|
|
return self.timezone
|
|
|
|
|
2019-04-12 21:15:18 +08:00
|
|
|
@async_unsafe
|
2016-06-04 06:31:21 +08:00
|
|
|
def chunked_cursor(self):
|
|
|
|
self._named_cursor_idx += 1
|
2019-04-12 21:15:18 +08:00
|
|
|
# Get the current async task
|
|
|
|
# Note that right now this is behind @async_unsafe, so this is
|
|
|
|
# unreachable, but in future we'll start loosening this restriction.
|
|
|
|
# For now, it's here so that every use of "threading" is
|
|
|
|
# also async-compatible.
|
|
|
|
try:
|
|
|
|
if hasattr(asyncio, 'current_task'):
|
|
|
|
# Python 3.7 and up
|
|
|
|
current_task = asyncio.current_task()
|
|
|
|
else:
|
|
|
|
# Python 3.6
|
|
|
|
current_task = asyncio.Task.current_task()
|
|
|
|
except RuntimeError:
|
|
|
|
current_task = None
|
|
|
|
# Current task can be none even if the current_task call didn't error
|
|
|
|
if current_task:
|
|
|
|
task_ident = str(id(current_task))
|
|
|
|
else:
|
|
|
|
task_ident = 'sync'
|
|
|
|
# Use that and the thread ident to get a unique name
|
2017-01-14 14:36:25 +08:00
|
|
|
return self._cursor(
|
2019-04-12 21:15:18 +08:00
|
|
|
name='_django_curs_%d_%s_%d' % (
|
|
|
|
# Avoid reusing name in other threads / tasks
|
2016-06-04 06:31:21 +08:00
|
|
|
threading.current_thread().ident,
|
2019-04-12 21:15:18 +08:00
|
|
|
task_ident,
|
2016-06-04 06:31:21 +08:00
|
|
|
self._named_cursor_idx,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2013-03-02 20:47:46 +08:00
|
|
|
def _set_autocommit(self, autocommit):
|
2014-03-24 06:09:26 +08:00
|
|
|
with self.wrap_database_errors:
|
2015-02-17 01:11:39 +08:00
|
|
|
self.connection.autocommit = autocommit
|
2013-03-02 20:47:46 +08:00
|
|
|
|
2013-03-02 19:12:51 +08:00
|
|
|
def check_constraints(self, table_names=None):
|
2009-03-11 15:06:50 +08:00
|
|
|
"""
|
2017-01-25 07:04:12 +08:00
|
|
|
Check constraints by setting them to immediate. Return them to deferred
|
|
|
|
afterward.
|
2009-03-11 15:06:50 +08:00
|
|
|
"""
|
2013-03-02 19:12:51 +08:00
|
|
|
self.cursor().execute('SET CONSTRAINTS ALL IMMEDIATE')
|
|
|
|
self.cursor().execute('SET CONSTRAINTS ALL DEFERRED')
|
2010-10-23 08:01:22 +08:00
|
|
|
|
2013-03-02 19:12:51 +08:00
|
|
|
def is_usable(self):
|
|
|
|
try:
|
|
|
|
# Use a psycopg cursor directly, bypassing Django's utilities.
|
|
|
|
self.connection.cursor().execute("SELECT 1")
|
2014-04-10 04:41:33 +08:00
|
|
|
except Database.Error:
|
2013-03-02 19:12:51 +08:00
|
|
|
return False
|
|
|
|
else:
|
|
|
|
return True
|
2012-06-19 00:32:03 +08:00
|
|
|
|
2015-09-11 00:47:50 +08:00
|
|
|
@property
|
2015-05-15 01:27:31 +08:00
|
|
|
def _nodb_connection(self):
|
2017-01-21 21:13:44 +08:00
|
|
|
nodb_connection = super()._nodb_connection
|
2015-05-15 01:27:31 +08:00
|
|
|
try:
|
|
|
|
nodb_connection.ensure_connection()
|
2016-09-27 06:31:41 +08:00
|
|
|
except (Database.DatabaseError, WrappedDatabaseError):
|
2015-05-15 01:27:31 +08:00
|
|
|
warnings.warn(
|
|
|
|
"Normally Django will use a connection to the 'postgres' database "
|
|
|
|
"to avoid running initialization queries against the production "
|
|
|
|
"database when it's not needed (for example, when running tests). "
|
|
|
|
"Django was unable to create a connection to the 'postgres' database "
|
2018-04-04 04:21:47 +08:00
|
|
|
"and will use the first PostgreSQL database instead.",
|
2015-05-15 01:27:31 +08:00
|
|
|
RuntimeWarning
|
|
|
|
)
|
2018-04-04 04:21:47 +08:00
|
|
|
for connection in connections.all():
|
|
|
|
if connection.vendor == 'postgresql' and connection.settings_dict['NAME'] != 'postgres':
|
|
|
|
return self.__class__(
|
|
|
|
{**self.settings_dict, 'NAME': connection.settings_dict['NAME']},
|
|
|
|
alias=self.alias,
|
|
|
|
)
|
2015-05-15 01:27:31 +08:00
|
|
|
return nodb_connection
|
|
|
|
|
2013-03-02 19:12:51 +08:00
|
|
|
@cached_property
|
|
|
|
def pg_version(self):
|
|
|
|
with self.temporary_connection():
|
2017-03-02 02:17:34 +08:00
|
|
|
return self.connection.server_version
|
2019-04-26 00:09:27 +08:00
|
|
|
|
|
|
|
def make_debug_cursor(self, cursor):
|
|
|
|
return CursorDebugWrapper(cursor, self)
|
|
|
|
|
|
|
|
|
|
|
|
class CursorDebugWrapper(BaseCursorDebugWrapper):
|
|
|
|
def copy_expert(self, sql, file, *args):
|
|
|
|
with self.debug_sql(sql):
|
|
|
|
return self.cursor.copy_expert(sql, file, *args)
|
|
|
|
|
|
|
|
def copy_to(self, file, table, *args, **kwargs):
|
|
|
|
with self.debug_sql(sql='COPY %s TO STDOUT' % table):
|
|
|
|
return self.cursor.copy_to(file, table, *args, **kwargs)
|