diff --git a/docs/db.rst b/docs/db.rst --- a/docs/db.rst +++ b/docs/db.rst @@ -114,8 +114,8 @@ returning an instance of the datastore object. Normally, this datastore object uses ``swh.core.db.BaseDb`` to interact with the actual database. -- The datastore object should provide a ``current_version`` attribute returning the - database version expected by the code. +- The datastore object should provide a ``get_current_version()`` method + returning the database version expected by the code. See existing ``swh`` packages like ``swh.storage`` or ``swh.scheduler`` for usage examples. diff --git a/swh/core/cli/db.py b/swh/core/cli/db.py --- a/swh/core/cli/db.py +++ b/swh/core/cli/db.py @@ -198,14 +198,16 @@ datastore_factory = getattr(import_swhmodule(module), "get_datastore", None) if datastore_factory: datastore = datastore_factory(**cfg) - if not hasattr(datastore, "current_version"): + try: + get_current_version = datastore.get_current_version + except AttributeError: logger.warning( - "Datastore %s does not declare the " - "'current_version' attribute", + "Datastore %s does not implement the " + "'get_current_version()' method", datastore, ) else: - code_version = datastore.current_version + code_version = get_current_version() logger.info( "Initializing database version to %s from the %s datastore", code_version, @@ -301,7 +303,7 @@ datastore_factory = getattr(import_swhmodule(db_module), "get_datastore", None) if datastore_factory: datastore = datastore_factory(**cfg) - code_version = datastore.current_version + code_version = datastore.get_current_version() click.secho( f"current code version: {code_version}", fg="green" if code_version == db_version else "red", @@ -394,7 +396,7 @@ "You cannot use this command on old-style datastore backend {db_module}" ) datastore = datastore_factory(**cfg) - ds_version = datastore.current_version + ds_version = datastore.get_current_version() if to_version is None: to_version = ds_version if to_version > ds_version: diff --git a/swh/core/db/tests/conftest.py b/swh/core/db/tests/conftest.py --- a/swh/core/db/tests/conftest.py +++ b/swh/core/db/tests/conftest.py @@ -35,7 +35,7 @@ set to `` and __file__ pointing to `data//__init__.py`. The Mock object also defines a `get_datastore()` attribute on which the - `current_version` attribute is set to 42. + `get_current_version()` exists and will return 42. Typical usage:: @@ -54,7 +54,7 @@ dirname = modname.split(".", 1)[1] def get_datastore(*args, **kw): - return mock(current_version=42) + return mock(get_current_version=lambda: 42) return mock( __name__=modname, diff --git a/swh/core/db/tests/test_cli.py b/swh/core/db/tests/test_cli.py --- a/swh/core/db/tests/test_cli.py +++ b/swh/core/db/tests/test_cli.py @@ -254,7 +254,7 @@ dirname = modname.split(".", 1)[1] def get_datastore(cls, **kw): - return mocker.MagicMock(current_version=current_version) + return mocker.MagicMock(get_current_version=lambda: current_version) return mocker.MagicMock( __name__=modname,