Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#
# This file is part of Invenio.
# Copyright (C) 2026 CERN.
#
# Invenio is free software; you can redistribute it and/or modify it
# under the terms of the MIT License; see LICENSE file for more details.

"""Create record_id index in rdm_parents_community."""

import sqlalchemy as sa
from alembic import op

# revision identifiers, used by Alembic.
revision = "1780576627"
down_revision = "1777278349"
branch_labels = ()
depends_on = None


def upgrade():
"""Upgrade database."""
# ### commands auto generated by Alembic - please adjust! ###
op.create_index(
"ix_rdm_parents_community_record_id",
"rdm_parents_community",
["record_id"],
unique=False,
)
# ### end Alembic commands ###


def downgrade():
"""Downgrade database."""
# ### commands auto generated by Alembic - please adjust! ###
op.drop_index(
"ix_rdm_parents_community_record_id",
table_name="rdm_parents_community",
)
# ### end Alembic commands ###
47 changes: 46 additions & 1 deletion invenio_rdm_records/records/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import uuid

from invenio_accounts.models import User
from invenio_communities.records.records.models import CommunityRelationMixin
from invenio_communities.communities.records.models import CommunityMetadata
from invenio_db import db
from invenio_drafts_resources.records import (
DraftMetadataBase,
Expand All @@ -21,12 +21,57 @@
from invenio_files_rest.models import Bucket
from invenio_records.models import RecordMetadataBase
from invenio_records_resources.records import FileRecordModelMixin
from invenio_requests.records.models import RequestMetadata
from sqlalchemy.ext.declarative import declared_attr
from sqlalchemy_utils.types import ChoiceType, UUIDType

from .systemfields.deletion_status import RecordDeletionStatusEnum


# Moved from invenio-communities/invenio_communities/records/records/models.py
class CommunityRelationMixin:
"""Model mixin to define a relationship between a communities and records.

Usage:

.. code-block:: python

class CommunityRecordM2M(db.Model, CommunityRelationMixin):
__record_model__ = MyParentRecord
"""

__record_model__ = None
__request_model__ = None

@declared_attr
def community_id(cls):
"""Foreign key to the related communithy."""
return db.Column(
UUIDType,
db.ForeignKey(CommunityMetadata.id, ondelete="CASCADE"),
primary_key=True,
)

@declared_attr
def record_id(cls):
"""Foreign key to the related record."""
return db.Column(
UUIDType,
db.ForeignKey(cls.__record_model__.id, ondelete="CASCADE"),
primary_key=True,
index=True, # New index, added along with the alembic migration 1780576627
)

@declared_attr
def request_id(cls):
"""Foreign key to a related request."""
return db.Column(
UUIDType,
db.ForeignKey(RequestMetadata.id, ondelete="SET NULL"),
nullable=True,
)


#
# Parent
#
Expand Down
26 changes: 26 additions & 0 deletions tests/records/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,32 @@
from invenio_rdm_records.records.api import RDMDraft


@pytest.fixture(scope="module")
def extra_entry_points():
"""Extra entry points to load the mock_module features."""
return {
"invenio_administration.views": [
"invenio_app_rdm_records_list = tests.mock_module.administration:RecordAdminListView",
"invenio_app_rdm_drafts_list = tests.mock_module.administration:DraftAdminListView",
"invenio_requests_user_moderation_list = tests.mock_module.administration:UserModerationListView",
],
"invenio_base.blueprints": [
"invenio_app_rdm_records = tests.mock_module:create_invenio_app_rdm_records_blueprint", # noqa
"invenio_app_rdm_requests = tests.mock_module:create_invenio_app_rdm_requests_blueprint", # noqa
"invenio_app_rdm_communities = tests.mock_module:create_invenio_app_rdm_communities_blueprint", # noqa
],
"invenio_db.model": [
"mock_module = tests.records.mock_module.models",
],
"invenio_jsonschemas.schemas": [
"mock_module = tests.records.mock_module.jsonschemas",
],
"invenio_search.mappings": [
"mocks = tests.records.mock_module.mappings",
],
}


@pytest.fixture(scope="module")
def create_app(instance_path, entry_points):
"""Application factory fixture."""
Expand Down
1 change: 1 addition & 0 deletions tests/records/mock_module/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Init."""
24 changes: 24 additions & 0 deletions tests/records/mock_module/api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""Example of a record draft API."""

from invenio_communities.records.records.systemfields import CommunitiesField
from invenio_records.systemfields import ConstantField
from invenio_records_resources.records import Record as RecordBase
from invenio_records_resources.records.systemfields import IndexField

from .models import MockRecordCommunity, MockRecordMetadata


class MockRecord(RecordBase):
"""Example parent record."""

# Configuration
model_cls = MockRecordMetadata

# System fields
schema = ConstantField("$schema", "local://mocks/mock-v1.0.0.json")

index = IndexField("mocks-mock-v1.0.0", search_alias="mocks")

communities = CommunitiesField(MockRecordCommunity)

# request = CommunityRequestField(model=MockRequestCommunity)
1 change: 1 addition & 0 deletions tests/records/mock_module/jsonschemas/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Init."""
21 changes: 21 additions & 0 deletions tests/records/mock_module/jsonschemas/mocks/mock-v1.0.0.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "local://mocks/mock-v1.0.0.json",
"type": "object",
"properties": {
"id": {
"type": "string"
},
"metadata": {
"type": "object",
"properties": {
"title": {
"type": "string"
}
}
},
"communities": {
"$ref": "local://communities/definitions-v2.0.0.json#/communities"
}
}
}
1 change: 1 addition & 0 deletions tests/records/mock_module/mappings/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Init."""
1 change: 1 addition & 0 deletions tests/records/mock_module/mappings/v6/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Init."""
28 changes: 28 additions & 0 deletions tests/records/mock_module/mappings/v6/mocks/mock-v1.0.0.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"mappings": {
"_doc": {
"properties": {
"id": {
"type": "keyword"
},
"metadata": {
"type": "object",
"properties": {
"title": {
"type": "text"
}
}
},
"created": {
"type": "date"
},
"updated": {
"type": "date"
},
"uuid": {
"type": "keyword"
}
}
}
}
}
1 change: 1 addition & 0 deletions tests/records/mock_module/mappings/v7/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Init."""
26 changes: 26 additions & 0 deletions tests/records/mock_module/mappings/v7/mocks/mock-v1.0.0.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"mappings": {
"properties": {
"id": {
"type": "keyword"
},
"metadata": {
"type": "object",
"properties": {
"title": {
"type": "text"
}
}
},
"created": {
"type": "date"
},
"updated": {
"type": "date"
},
"uuid": {
"type": "keyword"
}
}
}
}
19 changes: 19 additions & 0 deletions tests/records/mock_module/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
"""Example of a record/community model."""

from invenio_db import db
from invenio_records.models import RecordMetadataBase

from invenio_rdm_records.records.models import CommunityRelationMixin


class MockRecordMetadata(db.Model, RecordMetadataBase):
"""A baisc record."""

__tablename__ = "mock_metadata"


class MockRecordCommunity(db.Model, CommunityRelationMixin):
"""Relationship between record and community."""

__tablename__ = "mock_community"
__record_model__ = MockRecordMetadata
Loading
Loading