From 39748667ee1484738d203518b7d1476c1a4d6cf3 Mon Sep 17 00:00:00 2001 From: Lephe Date: Thu, 21 Apr 2022 17:54:30 +0100 Subject: [PATCH] model: add a number of missing indexes See #107. [MIGRATION] This commit contains a new version of the schema. --- app/models/attachment.py | 3 +- app/models/notification.py | 3 +- app/models/poll.py | 2 +- app/models/post.py | 5 ++- app/models/topic.py | 3 +- app/models/trophy.py | 2 +- ...271b88d_add_a_number_of_missing_indexes.py | 40 +++++++++++++++++++ 7 files changed, 51 insertions(+), 7 deletions(-) create mode 100644 migrations/versions/bcfdb271b88d_add_a_number_of_missing_indexes.py diff --git a/app/models/attachment.py b/app/models/attachment.py index 80945d7..0bc5856 100644 --- a/app/models/attachment.py +++ b/app/models/attachment.py @@ -13,7 +13,8 @@ class Attachment(db.Model): name = db.Column(db.Unicode(64)) # The comment linked with - comment_id = db.Column(db.Integer, db.ForeignKey('comment.id'), nullable=False) + comment_id = db.Column(db.Integer, db.ForeignKey('comment.id'), + nullable=False, index=True) comment = db.relationship('Comment', backref=backref('attachments')) # The size of the file diff --git a/app/models/notification.py b/app/models/notification.py index 8eccb70..315e30f 100644 --- a/app/models/notification.py +++ b/app/models/notification.py @@ -11,7 +11,8 @@ class Notification(db.Model): href = db.Column(db.UnicodeText) date = db.Column(db.DateTime, default=datetime.now()) - owner_id = db.Column(db.Integer, db.ForeignKey('member.id'),nullable=False) + owner_id = db.Column(db.Integer, db.ForeignKey('member.id'), + nullable=False, index=True) owner = db.relationship('Member', backref='notifications', foreign_keys=owner_id) diff --git a/app/models/poll.py b/app/models/poll.py index 91c3544..547b91b 100644 --- a/app/models/poll.py +++ b/app/models/poll.py @@ -103,7 +103,7 @@ class PollAnswer(db.Model): id = db.Column(db.Integer, primary_key=True) # Poll - poll_id = db.Column(db.Integer, db.ForeignKey('poll.id')) + poll_id = db.Column(db.Integer, db.ForeignKey('poll.id'), index=True) poll = db.relationship('Poll', backref=backref('answers'), foreign_keys=poll_id) diff --git a/app/models/post.py b/app/models/post.py index d5d76a5..cac722d 100644 --- a/app/models/post.py +++ b/app/models/post.py @@ -14,10 +14,11 @@ class Post(db.Model): # Creation and edition date date_created = db.Column(db.DateTime) - date_modified = db.Column(db.DateTime) + date_modified = db.Column(db.DateTime, index=True) # Post author - author_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) + author_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False, + index=True) author = db.relationship('User', backref="posts", foreign_keys=author_id) __mapper_args__ = { diff --git a/app/models/topic.py b/app/models/topic.py index dbf3bce..89383d5 100644 --- a/app/models/topic.py +++ b/app/models/topic.py @@ -23,7 +23,8 @@ class Topic(Post): title = db.Column(db.Unicode(128)) # Parent forum - forum_id = db.Column(db.Integer, db.ForeignKey('forum.id'), nullable=False) + forum_id = db.Column(db.Integer, db.ForeignKey('forum.id'), nullable=False, + index=True) forum = db.relationship('Forum', backref=backref('topics', lazy='dynamic'), foreign_keys=forum_id) diff --git a/app/models/trophy.py b/app/models/trophy.py index 0d131ad..c0336ce 100644 --- a/app/models/trophy.py +++ b/app/models/trophy.py @@ -59,4 +59,4 @@ class Title(Trophy): # Many-to-many relation for users earning trophies TrophyMember = db.Table('trophy_member', db.Model.metadata, db.Column('tid', db.Integer, db.ForeignKey('trophy.id')), - db.Column('uid', db.Integer, db.ForeignKey('member.id'))) + db.Column('uid', db.Integer, db.ForeignKey('member.id'), index=True)) diff --git a/migrations/versions/bcfdb271b88d_add_a_number_of_missing_indexes.py b/migrations/versions/bcfdb271b88d_add_a_number_of_missing_indexes.py new file mode 100644 index 0000000..108b66c --- /dev/null +++ b/migrations/versions/bcfdb271b88d_add_a_number_of_missing_indexes.py @@ -0,0 +1,40 @@ +"""Add a number of missing indexes + +Revision ID: bcfdb271b88d +Revises: adcd1577f301 +Create Date: 2022-04-21 17:45:09.787769 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = 'bcfdb271b88d' +down_revision = 'adcd1577f301' +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.create_index(op.f('ix_attachment_comment_id'), 'attachment', ['comment_id'], unique=False) + op.create_index(op.f('ix_notification_owner_id'), 'notification', ['owner_id'], unique=False) + op.create_index(op.f('ix_pollanswer_poll_id'), 'pollanswer', ['poll_id'], unique=False) + op.create_index(op.f('ix_post_author_id'), 'post', ['author_id'], unique=False) + op.create_index(op.f('ix_post_date_modified'), 'post', ['date_modified'], unique=False) + op.create_index(op.f('ix_topic_forum_id'), 'topic', ['forum_id'], unique=False) + op.create_index(op.f('ix_trophy_member_uid'), 'trophy_member', ['uid'], unique=False) + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_index(op.f('ix_trophy_member_uid'), table_name='trophy_member') + op.drop_index(op.f('ix_topic_forum_id'), table_name='topic') + op.drop_index(op.f('ix_post_date_modified'), table_name='post') + op.drop_index(op.f('ix_post_author_id'), table_name='post') + op.drop_index(op.f('ix_pollanswer_poll_id'), table_name='pollanswer') + op.drop_index(op.f('ix_notification_owner_id'), table_name='notification') + op.drop_index(op.f('ix_attachment_comment_id'), table_name='attachment') + # ### end Alembic commands ###