Skip to content

Commit

Permalink
fix journalist association bug in replies table
Browse files Browse the repository at this point in the history
  • Loading branch information
Allie Crevier committed Oct 20, 2020
1 parent 3fdd8c0 commit 269845b
Showing 1 changed file with 63 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 269845b

Please sign in to comment.