"""
Migration script to add 'ldda_id' column to the implicitly_converted_dataset_association table.
"""
from __future__ import print_function

import logging

from sqlalchemy import (
    Column,
    ForeignKey,
    Integer,
    MetaData
)

from galaxy.model.migrate.versions.util import (
    add_column,
    drop_column
)

log = logging.getLogger(__name__)
metadata = MetaData()


def upgrade(migrate_engine):
    print(__doc__)
    metadata.bind = migrate_engine
    metadata.reflect()

    # SQLAlchemy Migrate has a bug when adding a column with both a ForeignKey and a index in SQLite
    if migrate_engine.name != 'sqlite':
        c = Column("ldda_id", Integer, ForeignKey("library_dataset_dataset_association.id"), index=True, nullable=True)
    else:
        c = Column("ldda_id", Integer, index=True, nullable=True)
    add_column(c, 'implicitly_converted_dataset_association', metadata, index_name='ix_implicitly_converted_ds_assoc_ldda_id')


def downgrade(migrate_engine):
    metadata.bind = migrate_engine
    metadata.reflect()

    drop_column('ldda_id', 'implicitly_converted_dataset_association', metadata)

[((331, 358), 'logging.getLogger', 'logging.getLogger', (['__name__'], {}), '(__name__)\n', (348, 358), False, 'import logging\n'), ((370, 380), 'sqlalchemy.MetaData', 'MetaData', ([], {}), '()\n', (378, 380), False, 'from sqlalchemy import Column, ForeignKey, Integer, MetaData\n'), ((831, 957), 'galaxy.model.migrate.versions.util.add_column', 'add_column', (['c', '"""implicitly_converted_dataset_association"""', 'metadata'], {'index_name': '"""ix_implicitly_converted_ds_assoc_ldda_id"""'}), "(c, 'implicitly_converted_dataset_association', metadata,\n    index_name='ix_implicitly_converted_ds_assoc_ldda_id')\n", (841, 957), False, 'from galaxy.model.migrate.versions.util import add_column, drop_column\n'), ((1050, 1126), 'galaxy.model.migrate.versions.util.drop_column', 'drop_column', (['"""ldda_id"""', '"""implicitly_converted_dataset_association"""', 'metadata'], {}), "('ldda_id', 'implicitly_converted_dataset_association', metadata)\n", (1061, 1126), False, 'from galaxy.model.migrate.versions.util import add_column, drop_column\n'), ((773, 826), 'sqlalchemy.Column', 'Column', (['"""ldda_id"""', 'Integer'], {'index': '(True)', 'nullable': '(True)'}), "('ldda_id', Integer, index=True, nullable=True)\n", (779, 826), False, 'from sqlalchemy import Column, ForeignKey, Integer, MetaData\n'), ((670, 722), 'sqlalchemy.ForeignKey', 'ForeignKey', (['"""library_dataset_dataset_association.id"""'], {}), "('library_dataset_dataset_association.id')\n", (680, 722), False, 'from sqlalchemy import Column, ForeignKey, Integer, MetaData\n')]