From 51fba1a15666190ded6b076eb1b9e14ad3485c18 Mon Sep 17 00:00:00 2001 From: rimu <3310831+rimu@users.noreply.github.com> Date: Sun, 24 Nov 2024 15:27:21 +1300 Subject: [PATCH] merge duplicate posts #348 --- app/models.py | 2 +- .../299e0384c8f3_unique_post_ap_id.py | 72 +++++++++++++++++++ 2 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 migrations/versions/299e0384c8f3_unique_post_ap_id.py diff --git a/app/models.py b/app/models.py index 5742db5c..06f650e1 100644 --- a/app/models.py +++ b/app/models.py @@ -1117,7 +1117,7 @@ class Post(db.Model): cross_posts = db.Column(MutableList.as_mutable(ARRAY(db.Integer))) tags = db.relationship('Tag', lazy='dynamic', secondary=post_tag, backref=db.backref('posts', lazy='dynamic')) - ap_id = db.Column(db.String(255), index=True) + ap_id = db.Column(db.String(255), index=True, unique=True) ap_create_id = db.Column(db.String(100)) ap_announce_id = db.Column(db.String(100)) diff --git a/migrations/versions/299e0384c8f3_unique_post_ap_id.py b/migrations/versions/299e0384c8f3_unique_post_ap_id.py new file mode 100644 index 00000000..460ffeb8 --- /dev/null +++ b/migrations/versions/299e0384c8f3_unique_post_ap_id.py @@ -0,0 +1,72 @@ +"""unique post ap id + +Revision ID: 299e0384c8f3 +Revises: 7f7dfd4d4f1b +Create Date: 2024-11-24 15:18:51.979847 + +""" +from alembic import op +import sqlalchemy as sa +from sqlalchemy import text + +# revision identifiers, used by Alembic. +revision = '299e0384c8f3' +down_revision = '7f7dfd4d4f1b' +branch_labels = None +depends_on = None + + +def upgrade(): + # Find duplicate posts by ap_id + dupes_query = text(''' + SELECT ap_id FROM "post" + GROUP BY ap_id + HAVING COUNT(*) > 1 + ''') + + conn = op.get_bind() + duplicate_posts = conn.execute(dupes_query).scalars() + print('Cleaning up duplicate posts, this may take a while...') + + for ap_id in duplicate_posts: + if ap_id is None: + continue + # Get all communities with the same ap_profile_id, ordered by ID + users_query = text(''' + SELECT id FROM "post" + WHERE ap_id = :ap_id + ORDER BY id + ''') + posts = conn.execute(users_query, {"ap_id": ap_id}).fetchall() + + # Set the lowest ID as the new_id, and collect other IDs to update/delete + new_id = posts[0].id + old_ids = [post.id for post in posts[1:]] + + print(ap_id) + + if old_ids: + # Update tables with batch IN clause + conn.execute(text('UPDATE "post_reply" SET post_id = :new_id WHERE post_id IN :old_ids'), {"new_id": new_id, "old_ids": tuple(old_ids)}) + conn.execute(text('DELETE FROM "post_tag" WHERE post_id IN :old_ids'), {"old_ids": tuple(old_ids)}) + conn.execute(text('DELETE FROM "read_posts" WHERE read_post_id IN :old_ids'), {"old_ids": tuple(old_ids)}) + conn.execute(text('DELETE FROM "post_vote" WHERE post_id IN :old_ids'), {"old_ids": tuple(old_ids)}) + conn.execute(text('DELETE FROM "report" WHERE suspect_post_id IN :old_ids'), {"old_ids": tuple(old_ids)}) + + # Delete the duplicate posts + conn.execute(text('DELETE FROM "post" WHERE id IN :old_ids'), {"old_ids": tuple(old_ids)}) + # ### commands auto generated by Alembic - please adjust! ### + with op.batch_alter_table('post', schema=None) as batch_op: + batch_op.drop_index('ix_post_ap_id') + batch_op.create_index(batch_op.f('ix_post_ap_id'), ['ap_id'], unique=True) + + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + with op.batch_alter_table('post', schema=None) as batch_op: + batch_op.drop_index(batch_op.f('ix_post_ap_id')) + batch_op.create_index('ix_post_ap_id', ['ap_id'], unique=False) + + # ### end Alembic commands ###