Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/datajoint/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/datajoint/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
43 changes: 43 additions & 0 deletions tests/integration/test_database_prefix.py
Original file line number Diff line number Diff line change
@@ -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)]
36 changes: 36 additions & 0 deletions tests/unit/test_database_prefix_warning.py
Original file line number Diff line number Diff line change
@@ -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]