diff options
| author | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2017-02-15 12:22:34 +0100 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2017-02-17 09:44:36 -0500 |
| commit | 87775b64cdda97567a33c7bad65ad42853048525 (patch) | |
| tree | 62626775b9188eee275531b9f4a38f2cd68ff2d1 /django | |
| parent | a66f448f11c0c7a4e074dbc7a49b2a490d9101e0 (diff) | |
[1.11.x] Fixed #27843 -- Fixed truncate_name() when the name contains a username.
Backport of b9351905721771fb49ea0020c751ae4280754c43 from master
Diffstat (limited to 'django')
| -rw-r--r-- | django/db/backends/utils.py | 15 |
1 files changed, 11 insertions, 4 deletions
diff --git a/django/db/backends/utils.py b/django/db/backends/utils.py index e4ee025077..ce5e274649 100644 --- a/django/db/backends/utils.py +++ b/django/db/backends/utils.py @@ -4,6 +4,7 @@ import datetime import decimal import hashlib import logging +import re from time import time from django.conf import settings @@ -180,13 +181,19 @@ def rev_typecast_decimal(d): def truncate_name(name, length=None, hash_len=4): - """Shortens a string to a repeatable mangled version with the given length. """ - if length is None or len(name) <= length: + Shorten a string to a repeatable mangled version with the given length. + If a quote stripped name contains a username, e.g. USERNAME"."TABLE, + truncate the table portion only. + """ + match = re.match('([^"]+)"\."([^"]+)', name) + table_name = match.group(2) if match else name + + if length is None or len(table_name) <= length: return name - hsh = hashlib.md5(force_bytes(name)).hexdigest()[:hash_len] - return '%s%s' % (name[:length - hash_len], hsh) + 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) def format_number(value, max_digits, decimal_places): |
