Changeset View
Changeset View
Standalone View
Standalone View
swh/core/db/db_utils.py
Show First 20 Lines • Show All 542 Lines • ▼ Show 20 Lines | ) -> Tuple[bool, Optional[int], Optional[str]]: | ||||
# populate the dbmodule table | # populate the dbmodule table | ||||
swh_set_db_module(conninfo, modname) | swh_set_db_module(conninfo, modname) | ||||
current_db_version = swh_db_version(conninfo) | current_db_version = swh_db_version(conninfo) | ||||
dbflavor = swh_db_flavor(conninfo) | dbflavor = swh_db_flavor(conninfo) | ||||
return True, current_db_version, dbflavor | 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( | def get_database_info( | ||||
conninfo: str, | conninfo: str, | ||||
) -> Tuple[Optional[str], Optional[int], Optional[str]]: | ) -> Tuple[Optional[str], Optional[int], Optional[str]]: | ||||
"""Get version, flavor and module of the db""" | """Get version, flavor and module of the db""" | ||||
dbmodule = swh_db_module(conninfo) | dbmodule = swh_db_module(conninfo) | ||||
dbversion = swh_db_version(conninfo) | dbversion = swh_db_version(conninfo) | ||||
dbflavor = None | dbflavor = None | ||||
if dbversion is not None: | if dbversion is not None: | ||||
▲ Show 20 Lines • Show All 106 Lines • Show Last 20 Lines |