def downgrade(pyramid_env):
    from assembl.semantic.virtuoso_mapping import get_session
    from assembl.models import Content
    assert not ('body' in Content.__table__.c or 'subject' in Content.__table__.c), \
        "Comment out the body and subject from Content to run the back migration"
    dbsession = get_session()
    try:
        dbsession.execute("SPARQL drop quad map quadnames:col_pattern_Content_subject")
        dbsession.commit()
    except:
        dbsession.rollback()
    op.add_column('post', sa.Column('subject', sa.Unicode, server_default=""))
    op.add_column('post', sa.Column('body', sa.UnicodeText, server_default=""))
    with context.begin_transaction():
        op.execute(
            """UPDATE post SET subject = (
                    SELECT subject FROM content
                    WHERE post.id = content.id)
            """)
        op.execute(
            """UPDATE post SET body = (
                    SELECT body FROM content
                    WHERE content.id = post.id
                    AND content.body IS NOT NULL)
            """)
    from assembl.scripts.rebuild_tables import rebuild_table
    rebuild_table(Content.__table__, True)
def downgrade(pyramid_env):
    from assembl.semantic.virtuoso_mapping import get_session
    from assembl.models import Content
    assert not ('body' in Content.__table__.c or 'subject' in Content.__table__.c), \
        "Comment out the body and subject from Content to run the back migration"
    dbsession = get_session()
    try:
        dbsession.execute(
            "SPARQL drop quad map quadnames:col_pattern_Content_subject")
        dbsession.commit()
    except:
        dbsession.rollback()
    op.add_column('post', sa.Column('subject', sa.Unicode, server_default=""))
    op.add_column('post', sa.Column('body', sa.UnicodeText, server_default=""))
    with context.begin_transaction():
        op.execute("""UPDATE post SET subject = (
                    SELECT subject FROM content
                    WHERE post.id = content.id)
            """)
        op.execute("""UPDATE post SET body = (
                    SELECT body FROM content
                    WHERE content.id = post.id
                    AND content.body IS NOT NULL)
            """)
    from assembl.scripts.rebuild_tables import rebuild_table
    rebuild_table(Content.__table__, True)
def upgrade(pyramid_env):
    from assembl.semantic.virtuoso_mapping import get_session
    dbsession = get_session()
    dbsession.execute(
        "SPARQL drop quad map quadnames:col_pattern_Post_subject")
    dbsession.commit()
    with context.begin_transaction():
        op.add_column('content',
                      sa.Column('subject', sa.Unicode, server_default=""))
        op.add_column('content',
                      sa.Column('body', sa.UnicodeText, server_default=""))
    with context.begin_transaction():
        op.execute("""UPDATE content SET subject = (
                    SELECT subject FROM post
                    WHERE post.id = content.id)
            """)
        op.execute("""UPDATE content SET body = (
                    SELECT body FROM post
                    WHERE post.id = content.id)
            """)
    from assembl.scripts.rebuild_tables import rebuild_table
    from assembl.models import Post
    # Thanks to https://github.com/openlink/virtuoso-opensource/issues/378
    # The aim is to remove the columns from the table; make sure they are not in the model when you migrate
    rebuild_table(Post.__table__, True)
def upgrade(pyramid_env):
    from assembl.semantic.virtuoso_mapping import get_session
    dbsession = get_session()
    dbsession.execute("SPARQL drop quad map quadnames:col_pattern_Post_subject")
    dbsession.commit()
    with context.begin_transaction():
        op.add_column('content', sa.Column('subject', sa.Unicode, server_default=""))
        op.add_column('content', sa.Column('body', sa.UnicodeText, server_default=""))
    with context.begin_transaction():
        op.execute(
            """UPDATE content SET subject = (
                    SELECT subject FROM post
                    WHERE post.id = content.id)
            """)
        op.execute(
            """UPDATE content SET body = (
                    SELECT body FROM post
                    WHERE post.id = content.id)
            """)
    from assembl.scripts.rebuild_tables import rebuild_table
    from assembl.models import Post
    # Thanks to https://github.com/openlink/virtuoso-opensource/issues/378
    # The aim is to remove the columns from the table; make sure they are not in the model when you migrate
    rebuild_table(Post.__table__, True)