aboutsummaryrefslogtreecommitdiffstats
path: root/mediagoblin/db/migrations/versions/3145accb8fe3_remove_tombstone_comment_wrappers.py
blob: 1f3360481fb11998f1cc643725df814fb00adc5c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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