diff --git a/alembic/versions/a13967424130_add_pr_tags_table.py b/alembic/versions/a13967424130_add_pr_tags_table.py new file mode 100644 index 0000000..7b30c49 --- /dev/null +++ b/alembic/versions/a13967424130_add_pr_tags_table.py @@ -0,0 +1,48 @@ +"""Add PR tags table + +Revision ID: a13967424130 +Revises: 01e58ee9eccb +Create Date: 2017-11-05 16:56:01.164976 + +""" + +import datetime + +from alembic import op +import sqlalchemy as sa + +# revision identifiers, used by Alembic. +revision = 'a13967424130' +down_revision = '01e58ee9eccb' + + +def upgrade(): + """ Create the tags_pull_requests to store the tags of pull-requests. + """ + op.create_table( + 'tags_pull_requests', + sa.Column( + 'tag_id', + sa.Integer, + sa.ForeignKey( + 'tags_colored.id', ondelete='CASCADE', onupdate='CASCADE', + ), + primary_key=True), + sa.Column( + 'request_uid', + sa.String(32), + sa.ForeignKey( + 'pull_requests.uid', ondelete='CASCADE', onupdate='CASCADE', + ), + primary_key=True), + sa.Column( + 'date_created', + sa.DateTime, + nullable=False, + default=datetime.datetime.utcnow), + ) + + +def downgrade(): + """ Delete the tags_pull_requests table. """ + op.drop_table('tags_pull_requests') diff --git a/pagure/lib/model.py b/pagure/lib/model.py index cb78982..40b1440 100644 --- a/pagure/lib/model.py +++ b/pagure/lib/model.py @@ -1724,6 +1724,14 @@ class PullRequest(BASE): closed_by = relation('User', foreign_keys=[closed_by_id], remote_side=[User.id], backref='closed_requests') + tags = relation( + "TagColored", + secondary="tags_pull_requests", + primaryjoin="pull_requests.c.uid==tags_pull_requests.c.request_uid", + secondaryjoin="tags_pull_requests.c.tag_id==tags_colored.c.id", + viewonly=True + ) + def __repr__(self): return 'PullRequest(%s, project:%s, user:%s, title:%s)' % ( self.id, self.project.name, self.user.user, self.title @@ -1742,6 +1750,11 @@ class PullRequest(BASE): return '%s-pull-request-%s' % (self.project.name, self.uid) @property + def tags_text(self): + ''' Return the list of tags in a simple text form. ''' + return [tag.tag for tag in self.tags] + + @property def discussion(self): ''' Return the list of comments related to the pull-request itself, ie: not related to a specific commit. @@ -2093,6 +2106,46 @@ class CommitFlag(BASE): return output +class TagPullRequest(BASE): + """ Stores the tag associated with an pull-request. + + Table -- tags_pull_requests + """ + + __tablename__ = 'tags_pull_requests' + + tag_id = sa.Column( + sa.Integer, + sa.ForeignKey( + 'tags_colored.id', ondelete='CASCADE', onupdate='CASCADE', + ), + primary_key=True) + request_uid = sa.Column( + sa.String(32), + sa.ForeignKey( + 'pull_requests.uid', ondelete='CASCADE', onupdate='CASCADE', + ), + primary_key=True) + date_created = sa.Column(sa.DateTime, nullable=False, + default=datetime.datetime.utcnow) + + pull_request = relation( + 'PullRequest', + foreign_keys=[request_uid], + remote_side=[PullRequest.uid], + backref=backref( + 'tags_pr_colored', cascade="delete, delete-orphan" + ) + ) + tag = relation( + 'TagColored', foreign_keys=[tag_id], remote_side=[TagColored.id], + ) + + def __repr__(self): + return 'TagPullRequest(PR:%s, tag:%s)' % ( + self.pull_request.id, self.tag) + + class PagureGroupType(BASE): """ A list of the type a group can have definition.