From ab14059538e7d6961ed279219fdc6b68d45a7741 Mon Sep 17 00:00:00 2001 From: Jessica Tallon Date: Mon, 29 Feb 2016 19:04:44 +0000 Subject: [PATCH] Migration for #5415 - I forgot it, Doh! --- ...b8fe3_remove_tombstone_comment_wrappers.py | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 mediagoblin/db/migrations/versions/3145accb8fe3_remove_tombstone_comment_wrappers.py diff --git a/mediagoblin/db/migrations/versions/3145accb8fe3_remove_tombstone_comment_wrappers.py b/mediagoblin/db/migrations/versions/3145accb8fe3_remove_tombstone_comment_wrappers.py new file mode 100644 index 00000000..1f336048 --- /dev/null +++ b/mediagoblin/db/migrations/versions/3145accb8fe3_remove_tombstone_comment_wrappers.py @@ -0,0 +1,44 @@ +"""remove tombstone comment wrappers + +Revision ID: 3145accb8fe3 +Revises: 4066b9f8b84a +Create Date: 2016-02-29 14:38:12.096859 + +""" + +# revision identifiers, used by Alembic. +revision = '3145accb8fe3' +down_revision = '4066b9f8b84a' + +from alembic import op +from sqlalchemy import MetaData, and_ +from mediagoblin.db.migration_tools import inspect_table + +def upgrade(): + """ + Removes comments which have been deleted and exist as a tombstone but still + have their Comment wrapper. + """ + db = op.get_bind() + metadata = MetaData(bind=db) + comment_table = inspect_table(metadata, "core__comment_links") + gmr_table = inspect_table(metadata, "core__generic_model_reference") + + # Get the Comment wrappers + comment_wrappers = list(db.execute(comment_table.select())) + + for wrapper in comment_wrappers: + # Query for the graveyard GMR comment + gmr = db.execute(gmr_table.select().where(and_( + gmr_table.c.id == wrapper.comment_id, + gmr_table.c.model_type == "core__graveyard" + ))).first() + + if gmr is not None: + # Okay delete this wrapper as it's to a deleted comment + db.execute(comment_table.delete().where( + comment_table.c.id == wrapper.id + )) + +def downgrade(): + pass -- 2.25.1