diff --git a/swh/core/db/db_utils.py b/swh/core/db/db_utils.py index d78f9eca9bae3ff0fc8845b909736e9b8c227011..d28a0b8687406e3fca243c9b8aa8c31605d2b6d4 100644 --- a/swh/core/db/db_utils.py +++ b/swh/core/db/db_utils.py @@ -548,6 +548,33 @@ def populate_database_for_package( 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 index ede4d2169c1e34af677dc1ab7258d58bd4c15021..12bb1f863df769e8e0da9e8b75cf27db6e8ccb56 100644 --- a/swh/core/db/pytest_plugin.py +++ b/swh/core/db/pytest_plugin.py @@ -18,11 +18,7 @@ from pytest_postgresql.executor import PostgreSQLExecutor 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 @@ -37,6 +33,11 @@ _pytest_postgresql_get_config = getattr(_pytest_pgsql_get_config_module, "get_co 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) + class SWHDatabaseJanitor(DatabaseJanitor): """SWH database janitor implementation with a a different setup/teardown policy than @@ -254,20 +255,6 @@ def postgresql_fact( 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):