diff --git a/src/apps/exchange/services.py b/src/apps/exchange/services.py index 33a36db..b95ec5f 100644 --- a/src/apps/exchange/services.py +++ b/src/apps/exchange/services.py @@ -3,6 +3,7 @@ from __future__ import annotations import json +import uuid from contextlib import suppress from typing import Any @@ -301,7 +302,10 @@ class ExchangeConnectionService: @classmethod def _configure_alias(cls, connection: ExchangeConnection) -> str: - alias = f"exchange_target_{connection.id}" + connection_key = ( + connection.pk if connection.pk is not None else uuid.uuid4().hex + ) + alias = f"exchange_target_{connection_key}" config = { "ENGINE": "django.db.backends.postgresql", diff --git a/src/apps/exchange/views.py b/src/apps/exchange/views.py index fbda1d9..1a2d197 100644 --- a/src/apps/exchange/views.py +++ b/src/apps/exchange/views.py @@ -90,8 +90,8 @@ class ExchangeConnectionTestView(APIView): tags=[EXCHANGE_TAG], operation_summary="Проверить подключение", operation_description=( - "Проверяет подключение и структуру целевой БД без сохранения " - "настроек подключения." + "Проверяет доступность PostgreSQL и учетные данные без сохранения " + "настроек подключения. Проверка структуры выполняется при сохранении." ), request_body=ExchangeConnectionCreateSerializer, responses={ diff --git a/tests/apps/exchange/test_service_units.py b/tests/apps/exchange/test_service_units.py index a4fe819..2e7d985 100644 --- a/tests/apps/exchange/test_service_units.py +++ b/tests/apps/exchange/test_service_units.py @@ -505,6 +505,27 @@ class ExchangeConnectionServiceUnitTest(TestCase): self.assertEqual(connections_mock.databases[alias]["PASSWORD"], "secret") self.assertNotIn(alias, storage.__dict__) + def test_configure_alias_uses_unique_alias_for_unsaved_connection(self): + connection = ExchangeConnection( + server="127.0.0.1", + port=5432, + username="postgres", + password="secret", # noqa: S106 + database_name="target_db", + schema_name="public", + ) + + first_alias = ExchangeConnectionService._configure_alias(connection) + second_alias = ExchangeConnectionService._configure_alias(connection) + + try: + self.assertNotEqual(first_alias, second_alias) + self.assertTrue(first_alias.startswith("exchange_target_")) + self.assertTrue(second_alias.startswith("exchange_target_")) + finally: + ExchangeConnectionService._cleanup_alias(first_alias) + ExchangeConnectionService._cleanup_alias(second_alias) + def test_validate_schema_exists_raises_when_schema_missing(self): cursor = MagicMock() cursor.fetchone.return_value = None