Skip to content
Snippets Groups Projects
Commit ab2ff743 authored by David Douard's avatar David Douard
Browse files

move initialize_database_for_module in db_utils

to prepare the deprecation of db/pytest_plugin.py; it's the only
function from db/pytest_plugin.py still in use.
parent b2635632
No related branches found
No related tags found
No related merge requests found
......@@ -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]]:
......
......@@ -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):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment