Migration for #5415 - I forgot it, Doh!
authorJessica Tallon <tsyesika@tsyesika.se>
Mon, 29 Feb 2016 19:04:44 +0000 (19:04 +0000)
committerJessica Tallon <tsyesika@tsyesika.se>
Mon, 29 Feb 2016 19:04:44 +0000 (19:04 +0000)
mediagoblin/db/migrations/versions/3145accb8fe3_remove_tombstone_comment_wrappers.py [new file with mode: 0644]

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 (file)
index 0000000..1f33604
--- /dev/null
@@ -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