summaryrefslogtreecommitdiff
path: root/django/db/backends/utils.py
diff options
context:
space:
mode:
authorSimon Charette <charette.s@gmail.com>2017-11-11 19:17:20 -0500
committerSimon Charette <charette.s@gmail.com>2017-11-14 21:36:25 -0500
commitee85ef8315db839e5723dea19d8b971420a2ebb4 (patch)
tree437643db0fb2af8951b13b24308e591dacd9fa1f /django/db/backends/utils.py
parent532a4f22ad94db320cb0fd66f4c7ee57d17ac65a (diff)
downloaddjango-ee85ef8315db839e5723dea19d8b971420a2ebb4.tar.gz
Fixed #28792 -- Fixed index name truncation of namespaced tables.
Refs #27458, #27843. Thanks Tim and Mariusz for the review.
Diffstat (limited to 'django/db/backends/utils.py')
-rw-r--r--django/db/backends/utils.py34
1 files changed, 24 insertions, 10 deletions
diff --git a/django/db/backends/utils.py b/django/db/backends/utils.py
index 816164d36a..f4641e3db8 100644
--- a/django/db/backends/utils.py
+++ b/django/db/backends/utils.py
@@ -3,7 +3,6 @@ import decimal
import functools
import hashlib
import logging
-import re
from time import time
from django.conf import settings
@@ -194,20 +193,35 @@ def rev_typecast_decimal(d):
return str(d)
-def truncate_name(name, length=None, hash_len=4):
+def split_identifier(identifier):
"""
- Shorten a string to a repeatable mangled version with the given length.
- If a quote stripped name contains a username, e.g. USERNAME"."TABLE,
+ Split a SQL identifier into a two element tuple of (namespace, name).
+
+ The identifier could be a table, column, or sequence name might be prefixed
+ by a namespace.
+ """
+ try:
+ namespace, name = identifier.split('"."')
+ except ValueError:
+ namespace, name = '', identifier
+ return namespace.strip('"'), name.strip('"')
+
+
+def truncate_name(identifier, length=None, hash_len=4):
+ """
+ Shorten a SQL identifier to a repeatable mangled version with the given
+ length.
+
+ If a quote stripped name contains a namespace, e.g. USERNAME"."TABLE,
truncate the table portion only.
"""
- match = re.match(r'([^"]+)"\."([^"]+)', name)
- table_name = match.group(2) if match else name
+ namespace, name = split_identifier(identifier)
- if length is None or len(table_name) <= length:
- return name
+ if length is None or len(name) <= length:
+ return identifier
- hsh = hashlib.md5(force_bytes(table_name)).hexdigest()[:hash_len]
- return '%s%s%s' % (match.group(1) + '"."' if match else '', table_name[:length - hash_len], hsh)
+ digest = hashlib.md5(force_bytes(name)).hexdigest()[:hash_len]
+ return '%s%s%s' % ('%s"."' % namespace if namespace else '', name[:length - hash_len], digest)
def format_number(value, max_digits, decimal_places):