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

Add an index to columns of DbLink for SqlAlchemy #2561

Merged
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
33 changes: 5 additions & 28 deletions aiida/backends/djsite/db/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,41 +224,18 @@ def __str__(self):

@python_2_unicode_compatible
class DbLink(m.Model):
"""
Direct connection between two dbnodes. The label is identifying the
link type.
"""
"""Direct connection between two dbnodes. The label is identifying thelink type."""

# If I delete an output, delete also the link; if I delete an input, stop
# NOTE: this will in most cases render a DbNode.objects.filter(...).delete()
# call unusable because some nodes will be inputs; Nodes will have to
# be deleted in the proper order (or links will need to be deleted first)
input = m.ForeignKey('DbNode', related_name='output_links',
on_delete=m.PROTECT)
output = m.ForeignKey('DbNode', related_name='input_links',
on_delete=m.CASCADE)
# label for data input for calculation
# The `input` and `output` columns do not need an explicit `db_index` as it is `True` by default for foreign keys
input = m.ForeignKey('DbNode', related_name='output_links', on_delete=m.PROTECT)
output = m.ForeignKey('DbNode', related_name='input_links', on_delete=m.CASCADE)
label = m.CharField(max_length=255, db_index=True, blank=False)
type = m.CharField(max_length=255, db_index=True, blank=True)

class Meta:
# I cannot add twice the same link
# I want unique labels among all inputs of a node
# NOTE!
# I cannot add ('input', 'label') because in general
# if the input is a 'data' and I want to add it more than
# once to different calculations, the different links must be
# allowed to have the same name. For calculations, it is the
# responsibility of the output plugin to avoid to have many
# times the same name.
#
# A calculation can have both a 'return' and a 'create' link to
# a single data output node, which would violate the unique constraint
# defined below, since the difference in link type is not considered.
# The distinction between the type of a 'create' and a 'return' link is not
# implemented at the moment, so the unique constraint is disabled.
# unique_together = ("output", "label")
pass

def __str__(self):
return "{} ({}) --> {} ({})".format(
self.input.get_simple_name(invalid_result="Unknown node"),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""Adding indices on the `input_id`, `output_id` and `type` column of the `DbLink` table

Revision ID: 5a49629f0d45
Revises: 5ddd24e52864
Create Date: 2019-03-04 16:38:42.249231

"""
# pylint: disable=invalid-name,no-member,import-error,no-name-in-module
from __future__ import absolute_import
from alembic import op

# revision identifiers, used by Alembic.
revision = '5a49629f0d45'
down_revision = '5ddd24e52864'
branch_labels = None
depends_on = None


def upgrade():
"""Migrations for the upgrade."""
op.create_index(op.f('ix_db_dblink_input_id'), 'db_dblink', ['input_id'], unique=False)
op.create_index(op.f('ix_db_dblink_output_id'), 'db_dblink', ['output_id'], unique=False)
op.create_index(op.f('ix_db_dblink_type'), 'db_dblink', ['type'], unique=False)


def downgrade():
"""Migrations for the downgrade."""
op.drop_index(op.f('ix_db_dblink_type'), table_name='db_dblink')
op.drop_index(op.f('ix_db_dblink_output_id'), table_name='db_dblink')
op.drop_index(op.f('ix_db_dblink_input_id'), table_name='db_dblink')
8 changes: 5 additions & 3 deletions aiida/backends/sqlalchemy/models/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,8 @@ class DbLink(Base):
id = Column(Integer, primary_key=True)
input_id = Column(
Integer,
ForeignKey('db_dbnode.id', deferrable=True, initially="DEFERRED")
ForeignKey('db_dbnode.id', deferrable=True, initially="DEFERRED"),
index=True
)
output_id = Column(
Integer,
Expand All @@ -253,14 +254,15 @@ class DbLink(Base):
ondelete="CASCADE",
deferrable=True,
initially="DEFERRED"
)
),
index=True
)

input = relationship("DbNode", primaryjoin="DbLink.input_id == DbNode.id")
output = relationship("DbNode", primaryjoin="DbLink.output_id == DbNode.id")

label = Column(String(255), index=True, nullable=False)
type = Column(String(255))
type = Column(String(255), index=True)

# A calculation can have both a 'return' and a 'create' link to
# a single data output node, which would violate the unique constraint
Expand Down