misc: improve get_safe_db_name (#72643)
This commit is contained in:
parent
a652576b84
commit
ce88277e90
|
@ -1,7 +1,24 @@
|
|||
# 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():
|
||||
def get_safe_db_name(max_length=53):
|
||||
"""
|
||||
PostgreSQL database name limit is 63 characters, which can become
|
||||
an issue during testing, because we need to build a unique
|
||||
|
@ -19,4 +36,14 @@ def get_safe_db_name():
|
|||
# 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'))
|
||||
return '_'.join(parts)
|
||||
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
|
||||
|
|
Loading…
Reference in New Issue