| 1 | """#5437 Correct mediatype of blog media entries |
| 2 | |
| 3 | Revision ID: 5351b4fb14d3 |
| 4 | Revises: 228916769bd2 |
| 5 | Create Date: 2016-03-02 21:15:31.939453 |
| 6 | |
| 7 | """ |
| 8 | |
| 9 | # revision identifiers, used by Alembic. |
| 10 | revision = '5351b4fb14d3' |
| 11 | down_revision = '228916769bd2' |
| 12 | |
| 13 | from alembic import op |
| 14 | from sqlalchemy import MetaData |
| 15 | from mediagoblin.db.migration_tools import inspect_table |
| 16 | |
| 17 | def 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 | |
| 38 | def 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 | )) |