Ticket #5437: 0002-Migration-5437-Blog-mediatype-naming.patch

File 0002-Migration-5437-Blog-mediatype-naming.patch, 2.5 KB (added by ayleph, 9 years ago)
  • new file mediagoblin/db/migrations/versions/5351b4fb14d3_5437_correct_mediatype_of_blog_media_.py

    From 9ce1bf83a4bd59e1df647fce8a5bee67154b4988 Mon Sep 17 00:00:00 2001
    From: Andrew Browning <ayleph@thisshitistemp.com>
    Date: Wed, 2 Mar 2016 22:32:28 -0500
    Subject: [PATCH 2/2] Migration - 5437 Blog mediatype naming
    
    This migration sets the correct mediatype value for existing blog
    media entries.
    ---
     ...fb14d3_5437_correct_mediatype_of_blog_media_.py | 57 ++++++++++++++++++++++
     1 file changed, 57 insertions(+)
     create mode 100644 mediagoblin/db/migrations/versions/5351b4fb14d3_5437_correct_mediatype_of_blog_media_.py
    
    diff --git a/mediagoblin/db/migrations/versions/5351b4fb14d3_5437_correct_mediatype_of_blog_media_.py b/mediagoblin/db/migrations/versions/5351b4fb14d3_5437_correct_mediatype_of_blog_media_.py
    new file mode 100644
    index 0000000..ae0c346
    - +  
     1"""#5437 Correct mediatype of blog media entries
     2
     3Revision ID: 5351b4fb14d3
     4Revises: 228916769bd2
     5Create Date: 2016-03-02 21:15:31.939453
     6
     7"""
     8
     9# revision identifiers, used by Alembic.
     10revision = '5351b4fb14d3'
     11down_revision = '228916769bd2'
     12
     13from alembic import op
     14from sqlalchemy import MetaData
     15from mediagoblin.db.migration_tools import inspect_table
     16
     17def upgrade():
     18    """
     19    Set the correct mediatype on blog media entries.
     20    """
     21    db = op.get_bind()
     22    metadata = MetaData(bind=db)
     23    media_entries_table = inspect_table(metadata, "core__media_entries")
     24
     25    # Get the media entries.
     26    media_entries = list(db.execute(media_entries_table.select()))
     27
     28    # Iterate through all the media entries.
     29    for media_entry in media_entries:
     30
     31        db.execute(media_entries_table.update().values(
     32            media_type='mediagoblin.media_types.blog'
     33        ).where(
     34            media_entries_table.c.media_type ==
     35                'mediagoblin.media_types.blogpost'
     36        ))
     37
     38def downgrade():
     39    """
     40    Revert the mediatype on blog media entries.
     41    """
     42    db = op.get_bind()
     43    metadata = MetaData(bind=db)
     44    media_entries_table = inspect_table(metadata, "core__media_entries")
     45
     46    # Get the media entries.
     47    media_entries = list(db.execute(media_entries_table.select()))
     48
     49    # Iterate through all the media entries.
     50    for media_entry in media_entries:
     51
     52        db.execute(media_entries_table.update().values(
     53            media_type='mediagoblin.media_types.blogpost'
     54        ).where(
     55            media_entries_table.c.media_type ==
     56                'mediagoblin.media_types.blog'
     57        ))