diff --git a/nomad/groups.py b/nomad/groups.py index b8d89b612f25a148f425a48f39c7925763dcb867..c964fcc560877c56d050eeee6b05d9b1b86c938a 100644 --- a/nomad/groups.py +++ b/nomad/groups.py @@ -39,7 +39,10 @@ class MongoUserGroup(Document): owner = StringField(required=True) members = ListField(StringField()) - meta = {'indexes': ['group_name', 'owner', 'members']} + meta = { + 'collection': 'user_group', + 'indexes': ['group_name', 'owner', 'members'], + } @classmethod def q_by_ids(cls, group_ids: str | Iterable[str]) -> Q: diff --git a/nomad/infrastructure.py b/nomad/infrastructure.py index 02b35346c1dbdbd49b84b29080babad8d17f5c51..e5f458d0070e5408163b76f4aa2d5ee4856180bd 100644 --- a/nomad/infrastructure.py +++ b/nomad/infrastructure.py @@ -71,6 +71,7 @@ def setup(): """ setup_files() setup_mongo() + check_mongo() setup_elastic() @@ -99,6 +100,25 @@ def setup_mongo(client=False): return mongo_client +def check_mongo(): + db = mongo_client.get_database(config.mongo.db_name) + names = db.list_collection_names() + + expected_names = {'upload', 'user_group', 'entry', 'dataset', 'archive'} + if names != expected_names: + logger.warning( + f'Expected MongoDB collections: {expected_names} but found: {names}' + ) + + # regression https://gitlab.mpcdf.mpg.de/nomad-lab/nomad-FAIR/-/issues/2281 + if 'mongo_user_group' in names: + logger.warning(""" + The collection 'mongo_user_group' was found in MongoDB. It should be merged + into 'user_group'. This is a known issue. Please ask the NOMAD team + (support@nomad-lab.eu) if you need help with this. + """) + + def setup_elastic(): """Creates connection to elastic search.""" http_auth = None diff --git a/tests/app/v1/routers/test_groups.py b/tests/app/v1/routers/test_groups.py index a6476860391a51af092fa2e5624d06e0d4d0aac1..3b24ceea50396bbfc026f2a4c22d07c3dc5a2afc 100644 --- a/tests/app/v1/routers/test_groups.py +++ b/tests/app/v1/routers/test_groups.py @@ -1,8 +1,9 @@ import pytest -from .common import assert_response, perform_get, perform_post + from nomad.app.v1.models.groups import UserGroup, UserGroupResponse -from nomad.groups import get_user_group, user_group_exists +from nomad.groups import MongoUserGroup, get_user_group, user_group_exists +from .common import assert_response, perform_get, perform_post base_url = 'groups' @@ -33,6 +34,10 @@ def assert_group(group, ref_group, keys=None): assert_unordered_lists(val, ref_val) +def test_group_collection_name(groups_module): + MongoUserGroup._get_collection_name() == 'user_group' + + @pytest.mark.parametrize( 'user_label, expected_status_code', [