Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

models: adds index on version_id and bucket_id #304

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
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
Loading