pyfedi/migrations/versions/c3cc707ab5e9_unique_post_reply_ap_id.py
2024-11-24 16:00:53 +13:00

73 lines
2.6 KiB
Python

"""unique post_reply ap id
Revision ID: c3cc707ab5e9
Revises: 299e0384c8f3
Create Date: 2024-11-24 15:47:03.293286
"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy import text
# revision identifiers, used by Alembic.
revision = 'c3cc707ab5e9'
down_revision = '299e0384c8f3'
branch_labels = None
depends_on = None
def upgrade():
# Find duplicate communities by ap_profile_id
dupes_query = text('''
SELECT ap_id FROM "post_reply"
GROUP BY ap_id
HAVING COUNT(*) > 1
''')
conn = op.get_bind()
duplicate_comments = conn.execute(dupes_query).scalars()
print('Cleaning up duplicate comments, this may take a while...')
for ap_id in duplicate_comments:
if ap_id is None:
continue
# Get all communities with the same ap_profile_id, ordered by ID
comments_query = text('''
SELECT id FROM "post_reply"
WHERE ap_id = :ap_id
ORDER BY id
''')
comments = conn.execute(comments_query, {"ap_id": ap_id}).fetchall()
# Set the lowest ID as the new_id, and collect other IDs to update/delete
new_id = comments[0].id
old_ids = [comment.id for comment in comments[1:]]
print(ap_id)
if old_ids:
# Update tables with batch IN clause
conn.execute(text('DELETE FROM "post_reply_vote" WHERE post_reply_id IN :old_ids'),
{"old_ids": tuple(old_ids)})
conn.execute(text('DELETE FROM "report" WHERE suspect_post_reply_id IN :old_ids'),
{"old_ids": tuple(old_ids)})
conn.execute(text('DELETE FROM "post_reply_bookmark" WHERE post_reply_id IN :old_ids'),
{"old_ids": tuple(old_ids)})
# Delete the duplicate comments
conn.execute(text('DELETE FROM "post_reply" WHERE id IN :old_ids'), {"old_ids": tuple(old_ids)})
# ### commands auto generated by Alembic - please adjust! ###
with op.batch_alter_table('post_reply', schema=None) as batch_op:
batch_op.drop_index('ix_post_reply_ap_id')
batch_op.create_index(batch_op.f('ix_post_reply_ap_id'), ['ap_id'], unique=True)
# ### end Alembic commands ###
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
with op.batch_alter_table('post_reply', schema=None) as batch_op:
batch_op.drop_index(batch_op.f('ix_post_reply_ap_id'))
batch_op.create_index('ix_post_reply_ap_id', ['ap_id'], unique=False)
# ### end Alembic commands ###