-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix journalist association bug in replies table
- Loading branch information
Allie Crevier
committed
Oct 20, 2020
1 parent
3fdd8c0
commit 269845b
Showing
1 changed file
with
63 additions
and
0 deletions.
There are no files selected for viewing
63 changes: 63 additions & 0 deletions
63
alembic/versions/a4bf1f58ce69_fix_journalist_association_in_replies.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |