diff --git a/swh/core/db/db_utils.py b/swh/core/db/db_utils.py --- a/swh/core/db/db_utils.py +++ b/swh/core/db/db_utils.py @@ -548,6 +548,33 @@ return True, current_db_version, dbflavor +def initialize_database_for_module(modname: str, version: int, **kwargs): + """Helper function to initialize and populate a database for the given module + + This aims at helping the usage of pytest_postgresql for swh.core.db based datastores. + Typical usage will be (here for swh.storage): + + from pytest_postgresql import factories + + storage_postgresql_proc = factories.postgresql_proc( + load=[partial(initialize_database_for_module, modname="storage", version=42)] + ) + storage_postgresql = factories.postgresql("storage_postgresql_proc") + + """ + conninfo = psycopg2.connect(**kwargs).dsn + init_admin_extensions(modname, conninfo) + populate_database_for_package(modname, conninfo) + try: + swh_set_db_version(conninfo, version) + except psycopg2.errors.UniqueViolation: + logger.warn( + "Version already set by db init scripts. " + f"This generally means the swh.{modname} package needs to be " + "updated for swh.core>=1.2" + ) + + def get_database_info( conninfo: str, ) -> Tuple[Optional[str], Optional[int], Optional[str]]: diff --git a/swh/core/db/pytest_plugin.py b/swh/core/db/pytest_plugin.py --- a/swh/core/db/pytest_plugin.py +++ b/swh/core/db/pytest_plugin.py @@ -8,8 +8,10 @@ import logging import subprocess from typing import Callable, Iterable, Iterator, List, Optional, Sequence, Set, Union +import warnings from _pytest.fixtures import FixtureRequest +from deprecated import deprecated import psycopg2 import pytest from pytest_postgresql.compat import check_for_psycopg2, connection @@ -17,11 +19,7 @@ from pytest_postgresql.executor_noop import NoopExecutor from pytest_postgresql.janitor import DatabaseJanitor -from swh.core.db.db_utils import ( - init_admin_extensions, - populate_database_for_package, - swh_set_db_version, -) +from swh.core.db.db_utils import initialize_database_for_module from swh.core.utils import basename_sortkey # to keep mypy happy regardless pytest-postgresql version @@ -36,6 +34,16 @@ logger = logging.getLogger(__name__) +initialize_database_for_module = deprecated( + version="2.10", + reason="Use swh.core.db.db_utils.initialize_database_for_module instead.", +)(initialize_database_for_module) + +warnings.warn( + "This pytest plugin is deprecated, it should not be used any more.", + category=DeprecationWarning, +) + class SWHDatabaseJanitor(DatabaseJanitor): """SWH database janitor implementation with a a different setup/teardown policy than @@ -169,6 +177,7 @@ # the postgres_fact factory fixture below is mostly a copy of the code # from pytest-postgresql. We need a custom version here to be able to # specify our version of the DBJanitor we use. +@deprecated(version="2.10", reason="Use stock pytest_postgresql factory instead") def postgresql_fact( process_fixture_name: str, dbname: Optional[str] = None, @@ -252,20 +261,6 @@ return postgresql_factory -def initialize_database_for_module(modname, version, **kwargs): - conninfo = psycopg2.connect(**kwargs).dsn - init_admin_extensions(modname, conninfo) - populate_database_for_package(modname, conninfo) - try: - swh_set_db_version(conninfo, version) - except psycopg2.errors.UniqueViolation: - logger.warn( - "Version already set by db init scripts. " - "This generally means the swh.{modname} package needs to be " - "updated for swh.core>=1.2" - ) - - def gen_dump_files(dump_files: Union[str, Iterable[str]]) -> Iterator[str]: """Generate files potentially resolving glob patterns if any""" if isinstance(dump_files, str): diff --git a/swh/core/db/tests/test_db.py b/swh/core/db/tests/test_db.py --- a/swh/core/db/tests/test_db.py +++ b/swh/core/db/tests/test_db.py @@ -16,11 +16,11 @@ from hypothesis.extra.pytz import timezones import psycopg2 import pytest +from pytest_postgresql import factories from typing_extensions import Protocol from swh.core.db import BaseDb from swh.core.db.common import db_transaction, db_transaction_generator -from swh.core.db.pytest_plugin import postgresql_fact from swh.core.db.tests.conftest import function_scoped_fixture_check @@ -244,7 +244,7 @@ ] -test_db = postgresql_fact("postgresql_proc", dbname="test-db2") +test_db = factories.postgresql("postgresql_proc", dbname="test-db2") @pytest.fixture