Revert "misc: improve get_safe_db_name (#72643)"

This reverts commit 56c611d77b.
This commit is contained in:
Benjamin Dauvergne 2023-01-12 10:27:18 +01:00
parent be635f1001
commit 760a8b1a7c
1 changed files with 2 additions and 29 deletions

View File

@ -1,24 +1,7 @@
# hobo - portal to configure and deploy applications
# Copyright (C) 2022 Entr'ouvert
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import hashlib
import os
def get_safe_db_name(max_length=53):
def get_safe_db_name():
"""
PostgreSQL database name limit is 63 characters, which can become
an issue during testing, because we need to build a unique
@ -36,14 +19,4 @@ def get_safe_db_name(max_length=53):
# when we're in parallel mode, pytest-django will do this
# for us at a later point
parts.append(os.environ.get('TOX_ENV_NAME'))
full_db_name = '_'.join(parts)
if len(full_db_name) < max_length:
return full_db_name
hashcode_length = 8
hashcode = hashlib.md5(full_db_name.encode()).hexdigest()[: hashcode_length - 2]
prefix_length = (max_length - hashcode_length) - (max_length - hashcode_length) // 2
suffix_length = (max_length - hashcode_length) // 2
assert hashcode_length + prefix_length + suffix_length == max_length
truncated_db_name = full_db_name[:prefix_length] + '_' + hashcode + '_' + full_db_name[-suffix_length:]
assert len(truncated_db_name) == max_length
return truncated_db_name
return '_'.join(parts)