diff --git a/alembic/versions/01e58ee9eccb_adjust_constraints_on_pull_request_flags.py b/alembic/versions/01e58ee9eccb_adjust_constraints_on_pull_request_flags.py new file mode 100644 index 0000000..30d7dc9 --- /dev/null +++ b/alembic/versions/01e58ee9eccb_adjust_constraints_on_pull_request_flags.py @@ -0,0 +1,42 @@ +"""Adjust constraints on pull_request_flags + +Revision ID: 01e58ee9eccb +Revises: 6119fbbcc8e9 +Create Date: 2017-11-16 16:50:47.278252 + +""" + +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = '01e58ee9eccb' +down_revision = '6119fbbcc8e9' + + +def upgrade(): + """ Remove the unique constraints on UID in pull_request_flags and make + it a composite unique constraint on UID + pull_request_uid. + """ + # alter the constraints + op.drop_constraint('pull_request_flags_uid_key', 'pull_request_flags') + op.create_unique_constraint( + "pull_request_flags_uid_pull_request_uid_key", + 'pull_request_flags', + ["uid", "pull_request_uid"] + ) + + +def downgrade(): + """ Remove the composite unique constraints on UID + pull_request_uid + in pull_request_flags and make it an unique constraint on UID . + """ + op.drop_constraint( + 'pull_request_flags_uid_pull_request_uid_key', + 'pull_request_flags') + op.create_unique_constraint( + "pull_request_flags_uid_key", + 'pull_request_flags', + ["uid"] + ) diff --git a/alembic/versions/2b626a16542e_commit_flag.py b/alembic/versions/2b626a16542e_commit_flag.py index 20ab714..ccdbc22 100644 --- a/alembic/versions/2b626a16542e_commit_flag.py +++ b/alembic/versions/2b626a16542e_commit_flag.py @@ -48,6 +48,12 @@ def upgrade(): default=datetime.datetime.utcnow), ) + op.create_unique_constraint( + "commit_flags_uid_commit_hash_key", + 'commit_flags', + ["uid", "commit_hash"] + ) + def downgrade(): ''' Drop the commit_flags table. ''' diff --git a/doc/usage/flags.rst b/doc/usage/flags.rst index 8304706..fc0441c 100644 --- a/doc/usage/flags.rst +++ b/doc/usage/flags.rst @@ -21,8 +21,8 @@ with the titles: ``Flag a commit`` or ``Flag a pull-request``. - **uid**: the API endpoints to add flag have an optional UID argument. It - is a unique identifier (of maximum 32 characters) that is unique for the - entire pagure instance and allows to edit a flag. + is a unique identifier (of maximum 32 characters) that is unique the commit + or pull-request that is being/has been flagged. If it is not specified by the user/tool adding the flag, it will be automatically generated and in either case, will be returned in the JSON data returned by the API endpoints. Note that this is the only time you diff --git a/pagure/api/fork.py b/pagure/api/fork.py index 332f1e0..13e1aae 100644 --- a/pagure/api/fork.py +++ b/pagure/api/fork.py @@ -750,7 +750,8 @@ def api_pull_request_add_flag(repo, requestid, username=None, namespace=None): requestfolder=APP.config['REQUESTS_FOLDER'], ) SESSION.commit() - pr_flag = pagure.lib.get_pull_request_flag_by_uid(SESSION, uid) + pr_flag = pagure.lib.get_pull_request_flag_by_uid( + SESSION, request, uid) output['message'] = message output['uid'] = uid output['flag'] = pr_flag.to_json() diff --git a/pagure/api/project.py b/pagure/api/project.py index f527a75..924dbf4 100644 --- a/pagure/api/project.py +++ b/pagure/api/project.py @@ -1430,7 +1430,8 @@ def api_commit_add_flag(repo, commit_hash, username=None, namespace=None): token=flask.g.token.id, ) SESSION.commit() - c_flag = pagure.lib.get_commit_flag_by_uid(SESSION, uid) + c_flag = pagure.lib.get_commit_flag_by_uid( + SESSION, commit_hash, uid) output['message'] = message output['uid'] = uid output['flag'] = c_flag.to_json() diff --git a/pagure/lib/__init__.py b/pagure/lib/__init__.py index 703d313..3588415 100644 --- a/pagure/lib/__init__.py +++ b/pagure/lib/__init__.py @@ -1281,7 +1281,7 @@ def add_pull_request_flag(session, request, username, percent, comment, url, user_obj = get_user(session, user) action = 'added' - pr_flag = get_pull_request_flag_by_uid(session, uid) + pr_flag = get_pull_request_flag_by_uid(session, request, uid) if pr_flag: action = 'updated' pr_flag.comment = comment @@ -1328,7 +1328,7 @@ def add_commit_flag( user_obj = get_user(session, user) action = 'added' - c_flag = get_commit_flag_by_uid(session, uid) + c_flag = get_commit_flag_by_uid(session, commit_hash, uid) if c_flag: action = 'updated' c_flag.comment = comment @@ -2859,10 +2859,11 @@ def get_request_by_uid(session, request_uid): return query.first() -def get_pull_request_flag_by_uid(session, flag_uid): +def get_pull_request_flag_by_uid(session, request, flag_uid): ''' Return the flag corresponding to the specified unique identifier. :arg session: the session to use to connect to the database. + :arg request: the pull-request that was flagged :arg flag_uid: the unique identifier of a request. This identifier is unique accross all flags on this pagure instance and should be unique accross multiple pagure instances as well @@ -2875,15 +2876,18 @@ def get_pull_request_flag_by_uid(session, flag_uid): query = session.query( model.PullRequestFlag ).filter( - model.PullRequestFlag.uid == flag_uid.strip() if flag_uid else None + model.PullRequestFlag.pull_request_uid == request.uid + ).filter( + model.PullRequestFlag.uid == flag_uid.strip() ) return query.first() -def get_commit_flag_by_uid(session, flag_uid): +def get_commit_flag_by_uid(session, commit_hash, flag_uid): ''' Return the flag corresponding to the specified unique identifier. :arg session: the session to use to connect to the database. + :arg commit_hash: the hash of the commit that got flagged :arg flag_uid: the unique identifier of a request. This identifier is unique accross all flags on this pagure instance and should be unique accross multiple pagure instances as well @@ -2896,6 +2900,8 @@ def get_commit_flag_by_uid(session, flag_uid): query = session.query( model.CommitFlag ).filter( + model.CommitFlag.commit_hash == commit_hash + ).filter( model.CommitFlag.uid == flag_uid.strip() if flag_uid else None ) return query.first() diff --git a/pagure/lib/model.py b/pagure/lib/model.py index 59aafc5..807a52a 100644 --- a/pagure/lib/model.py +++ b/pagure/lib/model.py @@ -1947,7 +1947,7 @@ class PullRequestFlag(BASE): __tablename__ = 'pull_request_flags' id = sa.Column(sa.Integer, primary_key=True) - uid = sa.Column(sa.String(32), unique=True, nullable=False) + uid = sa.Column(sa.String(32), nullable=False) pull_request_uid = sa.Column( sa.String(32), sa.ForeignKey( @@ -1985,6 +1985,8 @@ class PullRequestFlag(BASE): date_created = sa.Column(sa.DateTime, nullable=False, default=datetime.datetime.utcnow) + __table_args__ = (sa.UniqueConstraint('uid', 'pull_request_uid'),) + user = relation('User', foreign_keys=[user_id], remote_side=[User.id], backref=backref( @@ -2045,7 +2047,7 @@ class CommitFlag(BASE): ), nullable=False, index=True) - uid = sa.Column(sa.String(32), unique=True, nullable=False) + uid = sa.Column(sa.String(32), nullable=False) status = sa.Column( sa.String(32), nullable=False) @@ -2065,6 +2067,8 @@ class CommitFlag(BASE): date_created = sa.Column(sa.DateTime, nullable=False, default=datetime.datetime.utcnow) + __table_args__ = (sa.UniqueConstraint('commit_hash', 'uid'),) + user = relation('User', foreign_keys=[user_id], remote_side=[User.id], backref=backref(