diff --git a/alembic/versions/a4bf1f58ce69_fix_journalist_association_in_replies.py b/alembic/versions/a4bf1f58ce69_fix_journalist_association_in_replies.py new file mode 100644 index 000000000..b371df040 --- /dev/null +++ b/alembic/versions/a4bf1f58ce69_fix_journalist_association_in_replies.py @@ -0,0 +1,63 @@ +"""fix journalist association in replies table + +Revision ID: a4bf1f58ce69 +Revises: 7f682532afa2 +Create Date: 2020-10-20 13:49:53.035383 + +""" +from alembic import op + + +# revision identifiers, used by Alembic. +revision = 'a4bf1f58ce69' +down_revision = '7f682532afa2' +branch_labels = None +depends_on = None + + +def upgrade(): + """ + Fix reply association with journalist by updating journalist uuid to journalist id in the + journalist_id column. + """ + conn = op.get_bind() + cursor = conn.execute(""" + SELECT journalist_id + FROM replies, users + WHERE journalist_id=users.uuid; + """) + + replies_with_incorrect_associations = cursor.fetchall() + if not replies_with_incorrect_associations: + return + + conn.execute(""" + UPDATE replies + SET journalist_id= + ( + SELECT users.id + FROM users + WHERE journalist_id=users.uuid + ) + WHERE exists + ( + SELECT users.id + FROM users + WHERE journalist_id=users.uuid + ); + """) + + cursor = conn.execute(""" + SELECT journalist_id + FROM replies, users + WHERE journalist_id=users.uuid; + """) + assert not replies_with_incorrect_associations + + +def downgrade(): + """ + We do not want to undo this bug fix, because nothing will break if we downgrade to an earlier + version of the client. + """ + pass