Skip to content

Commit

Permalink
models: adds index on version_id and bucket_id
Browse files Browse the repository at this point in the history
* adds alembic recipe
* closes zenodo/rdm-project#640
  • Loading branch information
jrcastro2 committed Jan 18, 2024
1 parent d7aca27 commit 8e6fbd9
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
64 changes: 64 additions & 0 deletions invenio_files_rest/alembic/e172c837b036_add_indexes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#
# 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.

"""Add indexes."""

import sqlalchemy as sa
from alembic import op

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


def upgrade():
"""Upgrade database."""
metadata = sa.MetaData(bind=op.get_bind())
table = sa.Table(
"files_object", metadata, autoload=True, autoload_with=op.get_bind()
)

# We need to do this checks as this index was being wrongly dropped by another alembic recipe in another module
if "ix_uq_partial_files_object_is_head" in [index.name for index in table.indexes]:
op.drop_index("ix_uq_partial_files_object_is_head", table_name="files_object")

op.create_index(
"ix_files_multipartobject_bucket_id",
"files_multipartobject",
["bucket_id"],
unique=False,
)
op.create_index(
"ix_files_objecttags_version_id",
"files_objecttags",
["version_id"],
unique=False,
)
op.create_index(
"ix_files_object_bucket_id", "files_object", ["bucket_id"], unique=False
)
op.create_index(
"ix_files_buckettags_bucket_id", "files_buckettags", ["bucket_id"], unique=False
)


def downgrade():
"""Downgrade database."""
op.create_index(
"ix_uq_partial_files_object_is_head",
"files_object",
["bucket_id", "key"],
unique=False,
)
op.drop_index(
"ix_files_multipartobject_bucket_id", table_name="files_multipartobject"
)
op.drop_index("ix_files_objecttags_version_id", table_name="files_objecttags")
op.drop_index("ix_files_object_bucket_id", table_name="files_object")
op.drop_index("ix_files_buckettags_bucket_id", table_name="files_buckettags")
4 changes: 4 additions & 0 deletions invenio_files_rest/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,7 @@ class BucketTag(db.Model):
db.ForeignKey(Bucket.id, ondelete="CASCADE"),
default=uuid.uuid4,
primary_key=True,
index=True,
)

key = db.Column(db.String(255), primary_key=True)
Expand Down Expand Up @@ -972,6 +973,7 @@ class ObjectVersion(db.Model, Timestamp):
db.ForeignKey(Bucket.id, ondelete="RESTRICT"),
default=uuid.uuid4,
nullable=False,
index=True,
)
"""Bucket identifier."""

Expand Down Expand Up @@ -1397,6 +1399,7 @@ class ObjectVersionTag(db.Model):
db.ForeignKey(ObjectVersion.version_id, ondelete="CASCADE"),
default=uuid.uuid4,
primary_key=True,
index=True,
)
"""Object version id."""

Expand Down Expand Up @@ -1508,6 +1511,7 @@ class MultipartObject(db.Model, Timestamp):
bucket_id = db.Column(
UUIDType,
db.ForeignKey(Bucket.id, ondelete="RESTRICT"),
index=True,
)
"""Bucket identifier."""

Expand Down

0 comments on commit 8e6fbd9

Please # to comment.