From a55f7a98173c3d033133ab97c8d08a3b735f5259 Mon Sep 17 00:00:00 2001 From: Javier Romero Castro Date: Wed, 17 Jan 2024 11:27:28 +0100 Subject: [PATCH] models: add index on bucket_id * adds alembic recipe * closes https://github.com/zenodo/rdm-project/issues/640 --- ...b37bb4119c_create_indeces_for_bucket_id.py | 42 +++++++++++++++++++ .../communities/records/models.py | 2 +- 2 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 invenio_communities/alembic/72b37bb4119c_create_indeces_for_bucket_id.py diff --git a/invenio_communities/alembic/72b37bb4119c_create_indeces_for_bucket_id.py b/invenio_communities/alembic/72b37bb4119c_create_indeces_for_bucket_id.py new file mode 100644 index 000000000..9f802cd40 --- /dev/null +++ b/invenio_communities/alembic/72b37bb4119c_create_indeces_for_bucket_id.py @@ -0,0 +1,42 @@ +# +# This file is part of Invenio. +# Copyright (C) 2023 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 indeces for bucket_id.""" +import sqlalchemy as sa +from alembic import op + +# revision identifiers, used by Alembic. +revision = "72b37bb4119c" +down_revision = "5c68d45c80f0" +branch_labels = () +depends_on = None + + +def upgrade(): + """Upgrade database.""" + op.create_index( + op.f("ix_communities_metadata_bucket_id"), + "communities_metadata", + ["bucket_id"], + unique=False, + ) + op.create_index( + op.f("ix_communities_files_object_version_id"), + "communities_files", + ["object_version_id"], + unique=False, + ) + + +def downgrade(): + """Downgrade database.""" + op.drop_index( + op.f("ix_communities_metadata_bucket_id"), table_name="communities_metadata" + ) + op.drop_index( + op.f("ix_communities_files_object_version_id"), table_name="communities_files" + ) diff --git a/invenio_communities/communities/records/models.py b/invenio_communities/communities/records/models.py index fde8605a7..f4ad14d99 100644 --- a/invenio_communities/communities/records/models.py +++ b/invenio_communities/communities/records/models.py @@ -29,7 +29,7 @@ class CommunityMetadata(db.Model, RecordMetadataBase): slug = db.Column(String(255), unique=True, nullable=True) - bucket_id = db.Column(UUIDType, db.ForeignKey(Bucket.id)) + bucket_id = db.Column(UUIDType, db.ForeignKey(Bucket.id), index=True) bucket = db.relationship(Bucket) # The deletion status is stored in the model so that we can use it in SQL queries