Skip to content

Commit 9ef5c19

Browse files
committed
fix: add-to-fixture subjects
1 parent 05a0f83 commit 9ef5c19

File tree

3 files changed

+30
-13
lines changed

3 files changed

+30
-13
lines changed

invenio_rdm_records/fixtures/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ def add_to(self, fixture):
7373
app_data_folder=app_data_folder,
7474
pkg_data_folder=data_folder,
7575
filename="vocabularies.yaml",
76+
delay=False,
7677
).load(reload=fixture)
7778

7879

invenio_rdm_records/fixtures/tasks.py

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,10 @@
2424
from invenio_communities.generators import CommunityRoleNeed
2525
from invenio_communities.members.errors import AlreadyMemberError
2626
from invenio_communities.proxies import current_communities
27-
from invenio_pidstore.errors import PersistentIdentifierError
27+
from invenio_pidstore.models import PersistentIdentifier
2828
from invenio_records_resources.proxies import current_service_registry
2929
from invenio_requests import current_events_service, current_requests_service
3030
from invenio_requests.customizations import CommentEventType
31-
from invenio_vocabularies.records.api import Vocabulary
3231

3332
from ..proxies import current_oaipmh_server_service, current_rdm_records_service
3433
from ..requests import CommunitySubmission
@@ -49,19 +48,32 @@ def get_authenticated_identity(user_id):
4948
def create_vocabulary_record(service_str, data):
5049
"""Create or update a vocabulary record."""
5150
service = current_service_registry.get(service_str)
51+
52+
# Determine the PID based on vocabulary type
5253
if "type" in data:
53-
# We only check non-datastream vocabularies for updates
54-
try:
55-
pid = (data["type"], data["id"])
56-
# If the entry hasn't been added, this will fail
57-
record = Vocabulary.pid.resolve(pid)
58-
service.update(system_identity, pid, data=data)
59-
current_app.logger.info(f"updated existing fixture with {data}")
60-
except PersistentIdentifierError:
61-
service.create(system_identity, data)
62-
current_app.logger.info(f"added new fixture with {data}")
54+
# Generic vocabularies with type field
55+
pid_type = data["type"]
56+
pid_value = data["id"]
57+
else:
58+
# Special vocabularies like subjects (without type field)
59+
# Use the service_str as the type
60+
pid_type = service_str
61+
pid_value = data["id"]
62+
63+
# Check if PID exists in the database
64+
existing_pid = PersistentIdentifier.query.filter_by(
65+
pid_type=pid_type, pid_value=pid_value
66+
).first()
67+
68+
if existing_pid:
69+
# Update existing record
70+
pid = (pid_type, pid_value)
71+
service.update(system_identity, pid, data=data)
72+
current_app.logger.debug(f"updated existing fixture with {data}")
6373
else:
74+
# Create new record
6475
service.create(system_identity, data)
76+
current_app.logger.debug(f"added new fixture with {data}")
6577

6678

6779
@shared_task
@@ -235,4 +247,4 @@ def create_demo_invitation_requests(user_id, n_requests):
235247
# comment_with_type
236248
# )
237249

238-
print(f"Created request for user {user_id} and " f"community {community_id}")
250+
print(f"Created request for user {user_id} and community {community_id}")

invenio_rdm_records/fixtures/vocabularies.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,10 @@ def load(self, reload=None):
190190
# If it's not already loaded it means it's a new one
191191
if reload and reload in self._loaded_vocabularies:
192192
self._loaded_vocabularies.remove(reload)
193+
# Also remove all child schemes (e.g., subjects.FOS when reloading subjects)
194+
self._loaded_vocabularies = {
195+
v for v in self._loaded_vocabularies if not v.startswith(f"{reload}.")
196+
}
193197
# 1- Load from app_data_folder
194198
filepath = self._app_data_folder / self._filename
195199
# An instance doesn't necessarily have custom vocabularies

0 commit comments

Comments
 (0)