diff --git a/requirements-swh.txt b/requirements-swh.txt --- a/requirements-swh.txt +++ b/requirements-swh.txt @@ -1,4 +1,4 @@ -swh.core[db,http] >= 2.9 +swh.core[db,http] >= 2.10 swh.counters >= v0.8.0 swh.model >= 6.0.0 swh.objstorage >= 0.2.2 diff --git a/swh/storage/postgresql/storage.py b/swh/storage/postgresql/storage.py --- a/swh/storage/postgresql/storage.py +++ b/swh/storage/postgresql/storage.py @@ -20,7 +20,7 @@ from swh.core.api.serializers import msgpack_dumps, msgpack_loads from swh.core.db.common import db_transaction, db_transaction_generator -from swh.core.db.db_utils import swh_db_version +from swh.core.db.db_utils import swh_db_flavor, swh_db_version from swh.model.hashutil import DEFAULT_ALGORITHMS, hash_to_bytes, hash_to_hex from swh.model.model import ( SHA1_SIZE, @@ -164,6 +164,7 @@ self.journal_writer = JournalWriter(journal_writer) self.objstorage = ObjStorage(objstorage) self.query_options = query_options + self._flavor: Optional[str] = None def get_db(self): if self._db: @@ -193,6 +194,19 @@ if db: self.put_db(db) + @db_transaction() + def get_flavor(self, *, db: Db, cur=None) -> str: + flavor = swh_db_flavor(db.conn.dsn) + assert flavor is not None + return flavor + + @property + def flavor(self) -> str: + if self._flavor is None: + self._flavor = self.get_flavor() + assert self._flavor is not None + return self._flavor + @db_transaction() def check_config(self, *, check_write: bool, db: Db, cur=None) -> bool: diff --git a/swh/storage/pytest_plugin.py b/swh/storage/pytest_plugin.py --- a/swh/storage/pytest_plugin.py +++ b/swh/storage/pytest_plugin.py @@ -9,7 +9,7 @@ import pytest from pytest_postgresql import factories -from swh.core.db.pytest_plugin import initialize_database_for_module +from swh.core.db.db_utils import initialize_database_for_module from swh.storage import get_storage from swh.storage.postgresql.storage import Storage as StorageDatastore from swh.storage.tests.storage_data import StorageData diff --git a/swh/storage/tests/test_postgresql.py b/swh/storage/tests/test_postgresql.py --- a/swh/storage/tests/test_postgresql.py +++ b/swh/storage/tests/test_postgresql.py @@ -24,6 +24,11 @@ yield db, cur +@pytest.mark.db +def test_pgstorage_flavor(swh_storage): + assert swh_storage.get_flavor() == "default" + + class TestStorage(_TestStorage): @pytest.mark.skip( "Directory pagination is not implemented in the postgresql backend yet." diff --git a/swh/storage/tests/test_postgresql_flavor_mirror.py b/swh/storage/tests/test_postgresql_flavor_mirror.py new file mode 100644 --- /dev/null +++ b/swh/storage/tests/test_postgresql_flavor_mirror.py @@ -0,0 +1,37 @@ +# Copyright (C) 2022 The Software Heritage developers +# See the AUTHORS file at the top-level directory of this distribution +# License: GNU General Public License version 3, or any later version +# See top-level LICENSE file for more information + +from functools import partial + +import pytest +from pytest_postgresql import factories + +from swh.core.db.db_utils import initialize_database_for_module +from swh.storage.postgresql.storage import Storage as StorageDatastore +from swh.storage.tests.test_postgresql import TestPgStorage # noqa: F401 +from swh.storage.tests.test_postgresql import TestStorage # noqa: F401 +from swh.storage.tests.test_postgresql import TestStorageRaceConditions # noqa: F401 + +swh_storage_postgresql_proc = factories.postgresql_proc( + load=[ + partial( + initialize_database_for_module, + modname="storage", + flavor="mirror", + version=StorageDatastore.current_version, + ) + ], +) + + +@pytest.mark.db +def test_pgstorage_flavor(swh_storage): + # get_flavor retrieve directly from the db + assert swh_storage.get_flavor() == "mirror" + + # flavor property (value is cached) + assert swh_storage._flavor is None + assert swh_storage.flavor == "mirror" + assert swh_storage._flavor == "mirror" diff --git a/swh/storage/tests/test_postgresql_flavor_readreplica.py b/swh/storage/tests/test_postgresql_flavor_readreplica.py new file mode 100644 --- /dev/null +++ b/swh/storage/tests/test_postgresql_flavor_readreplica.py @@ -0,0 +1,37 @@ +# Copyright (C) 2022 The Software Heritage developers +# See the AUTHORS file at the top-level directory of this distribution +# License: GNU General Public License version 3, or any later version +# See top-level LICENSE file for more information + +from functools import partial + +import pytest +from pytest_postgresql import factories + +from swh.core.db.db_utils import initialize_database_for_module +from swh.storage.postgresql.storage import Storage as StorageDatastore +from swh.storage.tests.test_postgresql import TestPgStorage # noqa: F401 +from swh.storage.tests.test_postgresql import TestStorage # noqa: F401 +from swh.storage.tests.test_postgresql import TestStorageRaceConditions # noqa: F401 + +swh_storage_postgresql_proc = factories.postgresql_proc( + load=[ + partial( + initialize_database_for_module, + modname="storage", + flavor="read_replica", + version=StorageDatastore.current_version, + ) + ], +) + + +@pytest.mark.db +def test_pgstorage_flavor(swh_storage): + # get_flavor retrieve directly from the db + assert swh_storage.get_flavor() == "read_replica" + + # flavor property (value is cached) + assert swh_storage._flavor is None + assert swh_storage.flavor == "read_replica" + assert swh_storage._flavor == "read_replica"