diff --git a/src/datajoint/schemas.py b/src/datajoint/schemas.py index 9f2cc1497..53d4565e3 100644 --- a/src/datajoint/schemas.py +++ b/src/datajoint/schemas.py @@ -174,7 +174,8 @@ def activate( self.connection = _get_singleton_connection() if self.connection._config.get("database.database_prefix"): warnings.warn( - "database_prefix is deprecated and will be removed in DataJoint 2.3. " + "database.database_prefix is deprecated and will be removed in a future release. " + "It is not applied to schema names. " "Use database.name to select a PostgreSQL database instead.", DeprecationWarning, stacklevel=2, diff --git a/src/datajoint/settings.py b/src/datajoint/settings.py index 981bbc314..7025475ed 100644 --- a/src/datajoint/settings.py +++ b/src/datajoint/settings.py @@ -391,7 +391,7 @@ class DatabaseSettings(BaseSettings): database_prefix: str = Field( default="", validation_alias="DJ_DATABASE_PREFIX", - description="Deprecated. Use database.name instead.", + description="Deprecated; will be removed in a future release. Not applied to schema names. Use database.name instead.", ) create_tables: bool = Field( default=True, diff --git a/tests/integration/test_database_prefix.py b/tests/integration/test_database_prefix.py new file mode 100644 index 000000000..162a0ff94 --- /dev/null +++ b/tests/integration/test_database_prefix.py @@ -0,0 +1,43 @@ +""" +The database_prefix deprecation warning names no release that has already shipped (#1675). + +``database.database_prefix`` is deprecated. ``Schema.activate()`` warns when it is set, and the +prefix is not applied to schema names. +""" + +import time +import warnings + +import pytest + +import datajoint as dj + + +@pytest.fixture +def schema_name(connection_by_backend, db_creds_by_backend): + """A fresh schema name, dropped after the test.""" + name = f"djtest_prefix_{db_creds_by_backend['backend']}_{str(int(time.time() * 1000))[-8:]}" + yield name + drop = f"DROP DATABASE IF EXISTS {connection_by_backend.adapter.quote_identifier(name)}" + if connection_by_backend.is_connected: + connection_by_backend.query(drop) + + +def test_prefix_warning_names_no_shipped_release(connection_by_backend, schema_name): + with dj.config.override(database__database_prefix="myproject_"): + with pytest.warns(DeprecationWarning, match="database_prefix") as record: + schema = dj.Schema(schema_name, connection=connection_by_backend) + messages = [str(w.message) for w in record if "database_prefix" in str(w.message)] + assert messages + for message in messages: + assert "removed in a future release" in message + assert "2.3" not in message + assert schema.database == schema_name # the prefix is not applied + + +def test_no_warning_without_prefix(connection_by_backend, schema_name): + with dj.config.override(database__database_prefix=""): + with warnings.catch_warnings(record=True) as record: + warnings.simplefilter("always") + dj.Schema(schema_name, connection=connection_by_backend) + assert not [w for w in record if "database_prefix" in str(w.message)] diff --git a/tests/unit/test_database_prefix_warning.py b/tests/unit/test_database_prefix_warning.py new file mode 100644 index 000000000..1406bf282 --- /dev/null +++ b/tests/unit/test_database_prefix_warning.py @@ -0,0 +1,36 @@ +"""The database_prefix deprecation warning names no release that has already shipped (#1675).""" + +import warnings +from unittest import mock + +import pytest + +import datajoint as dj + + +class _Stop(Exception): + """Raised by the stub connection once activate() is past the deprecation check.""" + + +def _activate(prefix): + """Run Schema.activate() up to its first query, with ``database_prefix`` set to ``prefix``.""" + connection = mock.MagicMock() + connection._config = {"database.database_prefix": prefix} + connection.query.side_effect = _Stop + connection.adapter.schema_exists_sql.side_effect = _Stop + with warnings.catch_warnings(record=True) as record: + warnings.simplefilter("always") + with pytest.raises(_Stop): + dj.Schema("prefix_check", connection=connection) + return [str(w.message) for w in record if issubclass(w.category, DeprecationWarning)] + + +def test_warning_says_future_release(): + messages = [m for m in _activate("myproject_") if "database_prefix" in m] + assert len(messages) == 1 + assert "will be removed in a future release" in messages[0] + assert "2.3" not in messages[0] + + +def test_no_warning_when_unset(): + assert not [m for m in _activate("") if "database_prefix" in m]