"""Added MedlinePlus schema

Revision ID: 37b1465360af
Revises: cbd502abf5ab
Create Date: 2019-10-28 09:10:15.026652

"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = "37b1465360af"
down_revision = "cbd502abf5ab"
branch_labels = None
depends_on = None


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        "also_calleds",
        sa.Column("also_called_id", sa.Integer(), nullable=False),
        sa.Column("name", sa.Unicode(), nullable=False),
        sa.PrimaryKeyConstraint("also_called_id", name=op.f("pk_also_calleds")),
        schema="medline",
    )
    op.create_index(
        op.f("ix_medline_also_calleds_name"),
        "also_calleds",
        ["name"],
        unique=True,
        schema="medline",
    )
    op.create_table(
        "health_topic_group_classes",
        sa.Column("health_topic_group_class_id", sa.Integer(), nullable=False),
        sa.Column("name", sa.Unicode(), nullable=False),
        sa.PrimaryKeyConstraint(
            "health_topic_group_class_id",
            name=op.f("pk_health_topic_group_classes"),
        ),
        schema="medline",
    )
    op.create_index(
        op.f("ix_medline_health_topic_group_classes_name"),
        "health_topic_group_classes",
        ["name"],
        unique=True,
        schema="medline",
    )
    op.create_table(
        "primary_institutes",
        sa.Column("primary_institute_id", sa.Integer(), nullable=False),
        sa.Column("name", sa.Unicode(), nullable=False),
        sa.Column("url", sa.UnicodeText(), nullable=False),
        sa.PrimaryKeyConstraint(
            "primary_institute_id", name=op.f("pk_primary_institutes")
        ),
        schema="medline",
    )
    op.create_index(
        op.f("ix_medline_primary_institutes_name"),
        "primary_institutes",
        ["name"],
        unique=True,
        schema="medline",
    )
    op.create_table(
        "see_references",
        sa.Column("see_reference_id", sa.Integer(), nullable=False),
        sa.Column("name", sa.Unicode(), nullable=False),
        sa.PrimaryKeyConstraint(
            "see_reference_id", name=op.f("pk_see_references")
        ),
        schema="medline",
    )
    op.create_index(
        op.f("ix_medline_see_references_name"),
        "see_references",
        ["name"],
        unique=True,
        schema="medline",
    )
    op.create_table(
        "health_topic_groups",
        sa.Column("health_topic_group_id", sa.Integer(), nullable=False),
        sa.Column("ui", sa.Integer(), nullable=False),
        sa.Column("name", sa.Unicode(), nullable=False),
        sa.Column("url", sa.UnicodeText(), nullable=False),
        sa.Column("health_topic_group_class_id", sa.Integer(), nullable=False),
        sa.ForeignKeyConstraint(
            ["health_topic_group_class_id"],
            ["medline.health_topic_group_classes.health_topic_group_class_id"],
            name=op.f(
                "fk_health_topic_groups_health_topic_group_"
                "class_id_health_topic_group_classes"
            ),
        ),
        sa.PrimaryKeyConstraint(
            "health_topic_group_id", name=op.f("pk_health_topic_groups")
        ),
        schema="medline",
    )
    op.create_index(
        op.f("ix_medline_health_topic_groups_name"),
        "health_topic_groups",
        ["name"],
        unique=True,
        schema="medline",
    )
    op.create_index(
        op.f("ix_medline_health_topic_groups_ui"),
        "health_topic_groups",
        ["ui"],
        unique=True,
        schema="medline",
    )
    op.create_table(
        "health_topics",
        sa.Column("health_topic_id", sa.Integer(), nullable=False),
        sa.Column("ui", sa.Integer(), nullable=False),
        sa.Column("title", sa.Unicode(), nullable=False),
        sa.Column("url", sa.UnicodeText(), nullable=False),
        sa.Column("description", sa.UnicodeText(), nullable=False),
        sa.Column("summary", sa.UnicodeText(), nullable=True),
        sa.Column("date_created", sa.Date(), nullable=False),
        sa.Column("primary_institute_id", sa.Integer(), nullable=True),
        sa.ForeignKeyConstraint(
            ["primary_institute_id"],
            ["medline.primary_institutes.primary_institute_id"],
            name=op.f(
                "fk_health_topics_primary_institute_id_primary_institutes"
            ),
        ),
        sa.PrimaryKeyConstraint(
            "health_topic_id", name=op.f("pk_health_topics")
        ),
        schema="medline",
    )
    op.create_index(
        op.f("ix_medline_health_topics_title"),
        "health_topics",
        ["title"],
        unique=True,
        schema="medline",
    )
    op.create_index(
        op.f("ix_medline_health_topics_ui"),
        "health_topics",
        ["ui"],
        unique=True,
        schema="medline",
    )
    op.create_table(
        "body_parts",
        sa.Column("body_part_id", sa.Integer(), nullable=False),
        sa.Column("name", sa.Unicode(), nullable=False),
        sa.Column("health_topic_group_id", sa.Integer(), nullable=False),
        sa.ForeignKeyConstraint(
            ["health_topic_group_id"],
            ["medline.health_topic_groups.health_topic_group_id"],
            name=op.f(
                "fk_body_parts_health_topic_group_id_health_topic_groups"
            ),
        ),
        sa.PrimaryKeyConstraint("body_part_id", name=op.f("pk_body_parts")),
        schema="medline",
    )
    op.create_index(
        op.f("ix_medline_body_parts_name"),
        "body_parts",
        ["name"],
        unique=True,
        schema="medline",
    )
    op.create_table(
        "health_topic_also_calleds",
        sa.Column("health_topic_also_called_id", sa.Integer(), nullable=False),
        sa.Column("health_topic_id", sa.Integer(), nullable=False),
        sa.Column("also_called_id", sa.Integer(), nullable=False),
        sa.ForeignKeyConstraint(
            ["also_called_id"],
            ["medline.also_calleds.also_called_id"],
            name=op.f(
                "fk_health_topic_also_calleds_also_called_id_also_calleds"
            ),
        ),
        sa.ForeignKeyConstraint(
            ["health_topic_id"],
            ["medline.health_topics.health_topic_id"],
            name=op.f(
                "fk_health_topic_also_calleds_health_topic_id_health_topics"
            ),
        ),
        sa.PrimaryKeyConstraint(
            "health_topic_also_called_id",
            name=op.f("pk_health_topic_also_calleds"),
        ),
        sa.UniqueConstraint(
            "health_topic_id",
            "also_called_id",
            name=op.f("uq_health_topic_also_calleds_health_topic_id"),
        ),
        schema="medline",
    )
    op.create_table(
        "health_topic_descriptors",
        sa.Column("health_topic_descriptor_id", sa.Integer(), nullable=False),
        sa.Column("health_topic_id", sa.Integer(), nullable=False),
        sa.Column("descriptor_id", sa.BigInteger(), nullable=False),
        sa.ForeignKeyConstraint(
            ["descriptor_id"],
            ["mesh.descriptors.descriptor_id"],
            name=op.f("fk_health_topic_descriptors_descriptor_id_descriptors"),
        ),
        sa.ForeignKeyConstraint(
            ["health_topic_id"],
            ["medline.health_topics.health_topic_id"],
            name=op.f(
                "fk_health_topic_descriptors_health_topic_id_health_topics"
            ),
        ),
        sa.PrimaryKeyConstraint(
            "health_topic_descriptor_id",
            name=op.f("pk_health_topic_descriptors"),
        ),
        sa.UniqueConstraint(
            "health_topic_id",
            "descriptor_id",
            name=op.f("uq_health_topic_descriptors_health_topic_id"),
        ),
        schema="medline",
    )
    op.create_table(
        "health_topic_health_topic_groups",
        sa.Column(
            "health_topic_health_topic_group_id", sa.Integer(), nullable=False
        ),
        sa.Column("health_topic_id", sa.Integer(), nullable=False),
        sa.Column("health_topic_group_id", sa.Integer(), nullable=False),
        sa.ForeignKeyConstraint(
            ["health_topic_group_id"],
            ["medline.health_topic_groups.health_topic_group_id"],
            name=op.f(
                "fk_health_topic_health_topic_groups_health_topic_group_id_health_topic_groups"
            ),
        ),
        sa.ForeignKeyConstraint(
            ["health_topic_id"],
            ["medline.health_topics.health_topic_id"],
            name=op.f(
                "fk_health_topic_health_topic_groups_health_topic_id_health_topics"
            ),
        ),
        sa.PrimaryKeyConstraint(
            "health_topic_health_topic_group_id",
            name=op.f("pk_health_topic_health_topic_groups"),
        ),
        sa.UniqueConstraint(
            "health_topic_id",
            "health_topic_group_id",
            name=op.f("uq_health_topic_health_topic_groups_health_topic_id"),
        ),
        schema="medline",
    )
    op.create_table(
        "health_topic_related_health_topics",
        sa.Column(
            "health_topic_related_health_topic_id", sa.Integer(), nullable=False
        ),
        sa.Column("health_topic_id", sa.Integer(), nullable=False),
        sa.Column("related_health_topic_id", sa.Integer(), nullable=False),
        sa.ForeignKeyConstraint(
            ["health_topic_id"],
            ["medline.health_topics.health_topic_id"],
            name=op.f(
                "fk_health_topic_related_health_topics_health_topic_id_health_topics"
            ),
        ),
        sa.ForeignKeyConstraint(
            ["related_health_topic_id"],
            ["medline.health_topics.health_topic_id"],
            name=op.f(
                "fk_health_topic_related_health_topics_related_health_topic_id_health_topics"
            ),
        ),
        sa.PrimaryKeyConstraint(
            "health_topic_related_health_topic_id",
            name=op.f("pk_health_topic_related_health_topics"),
        ),
        sa.UniqueConstraint(
            "health_topic_id",
            "related_health_topic_id",
            name=op.f("uq_health_topic_related_health_topics_health_topic_id"),
        ),
        schema="medline",
    )
    op.create_table(
        "health_topic_see_references",
        sa.Column(
            "health_topic_see_reference_id", sa.Integer(), nullable=False
        ),
        sa.Column("health_topic_id", sa.Integer(), nullable=False),
        sa.Column("see_reference_id", sa.Integer(), nullable=False),
        sa.ForeignKeyConstraint(
            ["health_topic_id"],
            ["medline.health_topics.health_topic_id"],
            name=op.f(
                "fk_health_topic_see_references_health_topic_id_health_topics"
            ),
        ),
        sa.ForeignKeyConstraint(
            ["see_reference_id"],
            ["medline.see_references.see_reference_id"],
            name=op.f(
                "fk_health_topic_see_references_see_reference_id_see_references"
            ),
        ),
        sa.PrimaryKeyConstraint(
            "health_topic_see_reference_id",
            name=op.f("pk_health_topic_see_references"),
        ),
        sa.UniqueConstraint(
            "health_topic_id",
            "see_reference_id",
            name=op.f("uq_health_topic_see_references_health_topic_id"),
        ),
        schema="medline",
    )
    op.create_table(
        "health_topic_body_parts",
        sa.Column("health_topic_body_part_id", sa.Integer(), nullable=False),
        sa.Column("health_topic_id", sa.Integer(), nullable=False),
        sa.Column("body_part_id", sa.Integer(), nullable=False),
        sa.ForeignKeyConstraint(
            ["body_part_id"],
            ["medline.body_parts.body_part_id"],
            name=op.f("fk_health_topic_body_parts_body_part_id_body_parts"),
        ),
        sa.ForeignKeyConstraint(
            ["health_topic_id"],
            ["medline.health_topics.health_topic_id"],
            name=op.f(
                "fk_health_topic_body_parts_health_topic_id_health_topics"
            ),
        ),
        sa.PrimaryKeyConstraint(
            "health_topic_body_part_id", name=op.f("pk_health_topic_body_parts")
        ),
        sa.UniqueConstraint(
            "health_topic_id",
            "body_part_id",
            name=op.f("uq_health_topic_body_parts_health_topic_id"),
        ),
        schema="medline",
    )
    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_table("health_topic_body_parts", schema="medline")
    op.drop_table("health_topic_see_references", schema="medline")
    op.drop_table("health_topic_related_health_topics", schema="medline")
    op.drop_table("health_topic_health_topic_groups", schema="medline")
    op.drop_table("health_topic_descriptors", schema="medline")
    op.drop_table("health_topic_also_calleds", schema="medline")
    op.drop_index(
        op.f("ix_medline_body_parts_name"),
        table_name="body_parts",
        schema="medline",
    )
    op.drop_table("body_parts", schema="medline")
    op.drop_index(
        op.f("ix_medline_health_topics_ui"),
        table_name="health_topics",
        schema="medline",
    )
    op.drop_index(
        op.f("ix_medline_health_topics_title"),
        table_name="health_topics",
        schema="medline",
    )
    op.drop_table("health_topics", schema="medline")
    op.drop_index(
        op.f("ix_medline_health_topic_groups_ui"),
        table_name="health_topic_groups",
        schema="medline",
    )
    op.drop_index(
        op.f("ix_medline_health_topic_groups_name"),
        table_name="health_topic_groups",
        schema="medline",
    )
    op.drop_table("health_topic_groups", schema="medline")
    op.drop_index(
        op.f("ix_medline_see_references_name"),
        table_name="see_references",
        schema="medline",
    )
    op.drop_table("see_references", schema="medline")
    op.drop_index(
        op.f("ix_medline_primary_institutes_name"),
        table_name="primary_institutes",
        schema="medline",
    )
    op.drop_table("primary_institutes", schema="medline")
    op.drop_index(
        op.f("ix_medline_health_topic_group_classes_name"),
        table_name="health_topic_group_classes",
        schema="medline",
    )
    op.drop_table("health_topic_group_classes", schema="medline")
    op.drop_index(
        op.f("ix_medline_also_calleds_name"),
        table_name="also_calleds",
        schema="medline",
    )
    op.drop_table("also_calleds", schema="medline")
    # ### end Alembic commands ###
