"""Add self-referential image reference

Revision ID: 8f5b2066cbac
Revises: 698cc06661d6
Create Date: 2016-03-20 19:35:31.321144

"""

# revision identifiers, used by Alembic.
revision = '8f5b2066cbac'
down_revision = '698cc06661d6'
branch_labels = None
depends_on = None

from alembic import op
import sqlalchemy as sa


def upgrade():
    ### commands auto generated by Alembic - please adjust! ###
    with op.batch_alter_table('images', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_images_gallery_id'), ['gallery_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_images_site_id'), ['site_id'], unique=False)
        batch_op.create_foreign_key('ix_images_parent_image_id', 'images', ['parent_image_id'], ['id'])
        batch_op.create_foreign_key('ix_sites_site_id', 'sites', ['site_id'], ['id'])
        batch_op.create_foreign_key('ix_galleries_gallery_id', 'galleries', ['gallery_id'], ['id'])

    with op.batch_alter_table('pages_sections_layout_settings', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_pages_sections_layout_settings_page_section_id'), ['page_section_id'], unique=False)

    with op.batch_alter_table('sites_settings', schema=None) as batch_op:
        batch_op.drop_index('ix_sites_settings_site_id')

    ### end Alembic commands ###


def downgrade():
    ### commands auto generated by Alembic - please adjust! ###
    with op.batch_alter_table('sites_settings', schema=None) as batch_op:
        batch_op.create_index('ix_sites_settings_site_id', ['site_id'], unique=False)

    with op.batch_alter_table('pages_sections_layout_settings', schema=None) as batch_op:
        batch_op.drop_index(batch_op.f('ix_pages_sections_layout_settings_page_section_id'))

    with op.batch_alter_table('images', schema=None) as batch_op:
        batch_op.drop_constraint(None, type_='foreignkey')
        batch_op.drop_constraint(None, type_='foreignkey')
        batch_op.drop_constraint(None, type_='foreignkey')
        batch_op.drop_index(batch_op.f('ix_images_site_id'))
        batch_op.drop_index(batch_op.f('ix_images_gallery_id'))

    ### end Alembic commands ###

Add DB migration for self-referential image id .. that works on sqlalchemy
Add DB migration for self-referential image id .. that works on sqlalchemy
