# type: ignore
"""Enable versioning for optimization items

Revision ID: e189ffe6a65e
Revises: fbda0eb90f37
Create Date: 2025-09-05 19:06:29.177463

"""

import sqlalchemy as sa
from alembic import op
from sqlalchemy.dialects import postgresql

# Revision identifiers, used by Alembic.
revision = "e189ffe6a65e"
down_revision = "fbda0eb90f37"
branch_labels = None
depends_on = None


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        "opt_idx",
        sa.Column(
            "_data_type",
            sa.Enum("float", "int", "str", native_enum=False),
            nullable=True,
        ),
        sa.Column("name", sa.String(length=255), nullable=False),
        sa.Column("run__id", sa.Integer(), nullable=False),
        sa.Column(
            "id",
            sa.Integer(),
            sa.Identity(always=False, on_null=True, start=1, increment=1),
            nullable=False,
        ),
        sa.Column("created_at", sa.DateTime(), nullable=True),
        sa.Column("created_by", sa.String(length=255), nullable=True),
        sa.ForeignKeyConstraint(
            ["run__id"], ["run.id"], name=op.f("fk_opt_idx_run__id_run")
        ),
        sa.PrimaryKeyConstraint("id", name=op.f("pk_opt_idx")),
        sa.UniqueConstraint("name", "run__id", name=op.f("uq_opt_idx_name_run__id")),
    )
    with op.batch_alter_table("opt_idx", schema=None) as batch_op:
        batch_op.create_index(
            batch_op.f("ix_opt_idx_run__id"), ["run__id"], unique=False
        )

    op.create_table(
        "opt_equ",
        sa.Column(
            "data",
            sa.JSON().with_variant(
                postgresql.JSONB(astext_type=sa.Text()), "postgresql"
            ),
            nullable=False,
        ),
        sa.Column("name", sa.String(length=255), nullable=False),
        sa.Column("run__id", sa.Integer(), nullable=False),
        sa.Column(
            "id",
            sa.Integer(),
            sa.Identity(always=False, on_null=True, start=1, increment=1),
            nullable=False,
        ),
        sa.Column("created_at", sa.DateTime(), nullable=True),
        sa.Column("created_by", sa.String(length=255), nullable=True),
        sa.ForeignKeyConstraint(
            ["run__id"], ["run.id"], name=op.f("fk_opt_equ_run__id_run")
        ),
        sa.PrimaryKeyConstraint("id", name=op.f("pk_opt_equ")),
        sa.UniqueConstraint("name", "run__id", name=op.f("uq_opt_equ_name_run__id")),
    )
    with op.batch_alter_table("opt_equ", schema=None) as batch_op:
        batch_op.create_index(
            batch_op.f("ix_opt_equ_run__id"), ["run__id"], unique=False
        )

    op.create_table(
        "opt_par",
        sa.Column(
            "data",
            sa.JSON().with_variant(
                postgresql.JSONB(astext_type=sa.Text()), "postgresql"
            ),
            nullable=False,
        ),
        sa.Column("name", sa.String(length=255), nullable=False),
        sa.Column("run__id", sa.Integer(), nullable=False),
        sa.Column(
            "id",
            sa.Integer(),
            sa.Identity(always=False, on_null=True, start=1, increment=1),
            nullable=False,
        ),
        sa.Column("created_at", sa.DateTime(), nullable=True),
        sa.Column("created_by", sa.String(length=255), nullable=True),
        sa.ForeignKeyConstraint(
            ["run__id"], ["run.id"], name=op.f("fk_opt_par_run__id_run")
        ),
        sa.PrimaryKeyConstraint("id", name=op.f("pk_opt_par")),
        sa.UniqueConstraint("name", "run__id", name=op.f("uq_opt_par_name_run__id")),
    )
    with op.batch_alter_table("opt_par", schema=None) as batch_op:
        batch_op.create_index(
            batch_op.f("ix_opt_par_run__id"), ["run__id"], unique=False
        )

    op.create_table(
        "opt_sca",
        sa.Column("run__id", sa.Integer(), nullable=False),
        sa.Column("value", sa.Float(), nullable=True),
        sa.Column("unit__id", sa.Integer(), nullable=False),
        sa.Column("name", sa.String(length=255), nullable=False),
        sa.Column(
            "id",
            sa.Integer(),
            sa.Identity(always=False, on_null=True, start=1, increment=1),
            nullable=False,
        ),
        sa.Column("created_at", sa.DateTime(), nullable=True),
        sa.Column("created_by", sa.String(length=255), nullable=True),
        sa.ForeignKeyConstraint(
            ["run__id"], ["run.id"], name=op.f("fk_opt_sca_run__id_run")
        ),
        sa.ForeignKeyConstraint(
            ["unit__id"], ["unit.id"], name=op.f("fk_opt_sca_unit__id_unit")
        ),
        sa.PrimaryKeyConstraint("id", name=op.f("pk_opt_sca")),
        sa.UniqueConstraint("name", "run__id", name=op.f("uq_opt_sca_name_run__id")),
    )
    with op.batch_alter_table("opt_sca", schema=None) as batch_op:
        batch_op.create_index(
            batch_op.f("ix_opt_sca_run__id"), ["run__id"], unique=False
        )
        batch_op.create_index(
            batch_op.f("ix_opt_sca_unit__id"), ["unit__id"], unique=False
        )

    op.create_table(
        "opt_tab",
        sa.Column(
            "data",
            sa.JSON().with_variant(
                postgresql.JSONB(astext_type=sa.Text()), "postgresql"
            ),
            nullable=False,
        ),
        sa.Column("name", sa.String(length=255), nullable=False),
        sa.Column("run__id", sa.Integer(), nullable=False),
        sa.Column(
            "id",
            sa.Integer(),
            sa.Identity(always=False, on_null=True, start=1, increment=1),
            nullable=False,
        ),
        sa.Column("created_at", sa.DateTime(), nullable=True),
        sa.Column("created_by", sa.String(length=255), nullable=True),
        sa.ForeignKeyConstraint(
            ["run__id"], ["run.id"], name=op.f("fk_opt_tab_run__id_run")
        ),
        sa.PrimaryKeyConstraint("id", name=op.f("pk_opt_tab")),
        sa.UniqueConstraint("name", "run__id", name=op.f("uq_opt_tab_name_run__id")),
    )
    with op.batch_alter_table("opt_tab", schema=None) as batch_op:
        batch_op.create_index(
            batch_op.f("ix_opt_tab_run__id"), ["run__id"], unique=False
        )

    op.create_table(
        "opt_var",
        sa.Column(
            "data",
            sa.JSON().with_variant(
                postgresql.JSONB(astext_type=sa.Text()), "postgresql"
            ),
            nullable=False,
        ),
        sa.Column("name", sa.String(length=255), nullable=False),
        sa.Column("run__id", sa.Integer(), nullable=False),
        sa.Column(
            "id",
            sa.Integer(),
            sa.Identity(always=False, on_null=True, start=1, increment=1),
            nullable=False,
        ),
        sa.Column("created_at", sa.DateTime(), nullable=True),
        sa.Column("created_by", sa.String(length=255), nullable=True),
        sa.ForeignKeyConstraint(
            ["run__id"], ["run.id"], name=op.f("fk_opt_var_run__id_run")
        ),
        sa.PrimaryKeyConstraint("id", name=op.f("pk_opt_var")),
        sa.UniqueConstraint("name", "run__id", name=op.f("uq_opt_var_name_run__id")),
    )
    with op.batch_alter_table("opt_var", schema=None) as batch_op:
        batch_op.create_index(
            batch_op.f("ix_opt_var_run__id"), ["run__id"], unique=False
        )

    op.create_table(
        "opt_equ_idx_association_version",
        sa.Column("equation__id", sa.Integer(), nullable=False),
        sa.Column("indexset__id", sa.Integer(), nullable=False),
        sa.Column("column_name", sa.String(length=255), nullable=True),
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("transaction_id", sa.BigInteger(), nullable=False),
        sa.Column("operation_type", sa.SmallInteger(), nullable=False),
        sa.Column("end_transaction_id", sa.BigInteger(), nullable=True),
        sa.PrimaryKeyConstraint(
            "id", "transaction_id", name=op.f("pk_opt_equ_idx_association_version")
        ),
    )
    with op.batch_alter_table(
        "opt_equ_idx_association_version", schema=None
    ) as batch_op:
        batch_op.create_index(
            batch_op.f("ix_opt_equ_idx_association_version_end_transaction_id"),
            ["end_transaction_id"],
            unique=False,
        )
        batch_op.create_index(
            batch_op.f("ix_opt_equ_idx_association_version_equation__id"),
            ["equation__id"],
            unique=False,
        )
        batch_op.create_index(
            batch_op.f("ix_opt_equ_idx_association_version_indexset__id"),
            ["indexset__id"],
            unique=False,
        )
        batch_op.create_index(
            batch_op.f("ix_opt_equ_idx_association_version_operation_type"),
            ["operation_type"],
            unique=False,
        )
        batch_op.create_index(
            batch_op.f("ix_opt_equ_idx_association_version_transaction_id"),
            ["transaction_id"],
            unique=False,
        )

    op.create_table(
        "opt_equ_version",
        sa.Column("name", sa.String(length=255), nullable=False),
        sa.Column("run__id", sa.Integer(), nullable=False),
        sa.Column(
            "data",
            sa.JSON().with_variant(
                postgresql.JSONB(astext_type=sa.Text()), "postgresql"
            ),
            nullable=False,
        ),
        sa.Column("created_at", sa.DateTime(), nullable=True),
        sa.Column("created_by", sa.String(length=255), nullable=True),
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("transaction_id", sa.BigInteger(), nullable=False),
        sa.Column("operation_type", sa.SmallInteger(), nullable=False),
        sa.Column("end_transaction_id", sa.BigInteger(), nullable=True),
        sa.PrimaryKeyConstraint(
            "id", "transaction_id", name=op.f("pk_opt_equ_version")
        ),
    )
    with op.batch_alter_table("opt_equ_version", schema=None) as batch_op:
        batch_op.create_index(
            batch_op.f("ix_opt_equ_version_end_transaction_id"),
            ["end_transaction_id"],
            unique=False,
        )
        batch_op.create_index(
            batch_op.f("ix_opt_equ_version_operation_type"),
            ["operation_type"],
            unique=False,
        )
        batch_op.create_index(
            batch_op.f("ix_opt_equ_version_run__id"), ["run__id"], unique=False
        )
        batch_op.create_index(
            batch_op.f("ix_opt_equ_version_transaction_id"),
            ["transaction_id"],
            unique=False,
        )

    op.create_table(
        "opt_idx_data_version",
        sa.Column("indexset__id", sa.Integer(), nullable=False),
        sa.Column("value", sa.String(), nullable=False),
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("transaction_id", sa.BigInteger(), nullable=False),
        sa.Column("operation_type", sa.SmallInteger(), nullable=False),
        sa.Column("end_transaction_id", sa.BigInteger(), nullable=True),
        sa.PrimaryKeyConstraint(
            "id", "transaction_id", name=op.f("pk_opt_idx_data_version")
        ),
    )
    with op.batch_alter_table("opt_idx_data_version", schema=None) as batch_op:
        batch_op.create_index(
            batch_op.f("ix_opt_idx_data_version_end_transaction_id"),
            ["end_transaction_id"],
            unique=False,
        )
        batch_op.create_index(
            batch_op.f("ix_opt_idx_data_version_indexset__id"),
            ["indexset__id"],
            unique=False,
        )
        batch_op.create_index(
            batch_op.f("ix_opt_idx_data_version_operation_type"),
            ["operation_type"],
            unique=False,
        )
        batch_op.create_index(
            batch_op.f("ix_opt_idx_data_version_transaction_id"),
            ["transaction_id"],
            unique=False,
        )

    op.create_table(
        "opt_idx_version",
        sa.Column("name", sa.String(length=255), nullable=False),
        sa.Column("run__id", sa.Integer(), nullable=False),
        sa.Column(
            "_data_type",
            sa.Enum("float", "int", "str", native_enum=False),
            nullable=True,
        ),
        sa.Column("created_at", sa.DateTime(), nullable=True),
        sa.Column("created_by", sa.String(length=255), nullable=True),
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("transaction_id", sa.BigInteger(), nullable=False),
        sa.Column("operation_type", sa.SmallInteger(), nullable=False),
        sa.Column("end_transaction_id", sa.BigInteger(), nullable=True),
        sa.PrimaryKeyConstraint(
            "id", "transaction_id", name=op.f("pk_opt_idx_version")
        ),
    )
    with op.batch_alter_table("opt_idx_version", schema=None) as batch_op:
        batch_op.create_index(
            batch_op.f("ix_opt_idx_version_end_transaction_id"),
            ["end_transaction_id"],
            unique=False,
        )
        batch_op.create_index(
            batch_op.f("ix_opt_idx_version_operation_type"),
            ["operation_type"],
            unique=False,
        )
        batch_op.create_index(
            batch_op.f("ix_opt_idx_version_run__id"), ["run__id"], unique=False
        )
        batch_op.create_index(
            batch_op.f("ix_opt_idx_version_transaction_id"),
            ["transaction_id"],
            unique=False,
        )

    op.create_table(
        "opt_par_idx_association_version",
        sa.Column("parameter__id", sa.Integer(), nullable=False),
        sa.Column("indexset__id", sa.Integer(), nullable=False),
        sa.Column("column_name", sa.String(length=255), nullable=True),
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("transaction_id", sa.BigInteger(), nullable=False),
        sa.Column("operation_type", sa.SmallInteger(), nullable=False),
        sa.Column("end_transaction_id", sa.BigInteger(), nullable=True),
        sa.PrimaryKeyConstraint(
            "id", "transaction_id", name=op.f("pk_opt_par_idx_association_version")
        ),
    )
    with op.batch_alter_table(
        "opt_par_idx_association_version", schema=None
    ) as batch_op:
        batch_op.create_index(
            batch_op.f("ix_opt_par_idx_association_version_end_transaction_id"),
            ["end_transaction_id"],
            unique=False,
        )
        batch_op.create_index(
            batch_op.f("ix_opt_par_idx_association_version_indexset__id"),
            ["indexset__id"],
            unique=False,
        )
        batch_op.create_index(
            batch_op.f("ix_opt_par_idx_association_version_operation_type"),
            ["operation_type"],
            unique=False,
        )
        batch_op.create_index(
            batch_op.f("ix_opt_par_idx_association_version_parameter__id"),
            ["parameter__id"],
            unique=False,
        )
        batch_op.create_index(
            batch_op.f("ix_opt_par_idx_association_version_transaction_id"),
            ["transaction_id"],
            unique=False,
        )

    op.create_table(
        "opt_par_version",
        sa.Column("name", sa.String(length=255), nullable=False),
        sa.Column("run__id", sa.Integer(), nullable=False),
        sa.Column(
            "data",
            sa.JSON().with_variant(
                postgresql.JSONB(astext_type=sa.Text()), "postgresql"
            ),
            nullable=False,
        ),
        sa.Column("created_at", sa.DateTime(), nullable=True),
        sa.Column("created_by", sa.String(length=255), nullable=True),
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("transaction_id", sa.BigInteger(), nullable=False),
        sa.Column("operation_type", sa.SmallInteger(), nullable=False),
        sa.Column("end_transaction_id", sa.BigInteger(), nullable=True),
        sa.PrimaryKeyConstraint(
            "id", "transaction_id", name=op.f("pk_opt_par_version")
        ),
    )
    with op.batch_alter_table("opt_par_version", schema=None) as batch_op:
        batch_op.create_index(
            batch_op.f("ix_opt_par_version_end_transaction_id"),
            ["end_transaction_id"],
            unique=False,
        )
        batch_op.create_index(
            batch_op.f("ix_opt_par_version_operation_type"),
            ["operation_type"],
            unique=False,
        )
        batch_op.create_index(
            batch_op.f("ix_opt_par_version_run__id"), ["run__id"], unique=False
        )
        batch_op.create_index(
            batch_op.f("ix_opt_par_version_transaction_id"),
            ["transaction_id"],
            unique=False,
        )

    op.create_table(
        "opt_sca_version",
        sa.Column("name", sa.String(length=255), nullable=False),
        sa.Column("run__id", sa.Integer(), nullable=False),
        sa.Column("value", sa.Float(), nullable=True),
        sa.Column("unit__id", sa.Integer(), nullable=False),
        sa.Column("created_at", sa.DateTime(), nullable=True),
        sa.Column("created_by", sa.String(length=255), nullable=True),
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("transaction_id", sa.BigInteger(), nullable=False),
        sa.Column("operation_type", sa.SmallInteger(), nullable=False),
        sa.Column("end_transaction_id", sa.BigInteger(), nullable=True),
        sa.PrimaryKeyConstraint(
            "id", "transaction_id", name=op.f("pk_opt_sca_version")
        ),
    )
    with op.batch_alter_table("opt_sca_version", schema=None) as batch_op:
        batch_op.create_index(
            batch_op.f("ix_opt_sca_version_end_transaction_id"),
            ["end_transaction_id"],
            unique=False,
        )
        batch_op.create_index(
            batch_op.f("ix_opt_sca_version_operation_type"),
            ["operation_type"],
            unique=False,
        )
        batch_op.create_index(
            batch_op.f("ix_opt_sca_version_run__id"), ["run__id"], unique=False
        )
        batch_op.create_index(
            batch_op.f("ix_opt_sca_version_transaction_id"),
            ["transaction_id"],
            unique=False,
        )
        batch_op.create_index(
            batch_op.f("ix_opt_sca_version_unit__id"), ["unit__id"], unique=False
        )

    op.create_table(
        "opt_tab_idx_association_version",
        sa.Column("table__id", sa.Integer(), nullable=False),
        sa.Column("indexset__id", sa.Integer(), nullable=False),
        sa.Column("column_name", sa.String(length=255), nullable=True),
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("transaction_id", sa.BigInteger(), nullable=False),
        sa.Column("operation_type", sa.SmallInteger(), nullable=False),
        sa.Column("end_transaction_id", sa.BigInteger(), nullable=True),
        sa.PrimaryKeyConstraint(
            "id", "transaction_id", name=op.f("pk_opt_tab_idx_association_version")
        ),
    )
    with op.batch_alter_table(
        "opt_tab_idx_association_version", schema=None
    ) as batch_op:
        batch_op.create_index(
            batch_op.f("ix_opt_tab_idx_association_version_end_transaction_id"),
            ["end_transaction_id"],
            unique=False,
        )
        batch_op.create_index(
            batch_op.f("ix_opt_tab_idx_association_version_indexset__id"),
            ["indexset__id"],
            unique=False,
        )
        batch_op.create_index(
            batch_op.f("ix_opt_tab_idx_association_version_operation_type"),
            ["operation_type"],
            unique=False,
        )
        batch_op.create_index(
            batch_op.f("ix_opt_tab_idx_association_version_table__id"),
            ["table__id"],
            unique=False,
        )
        batch_op.create_index(
            batch_op.f("ix_opt_tab_idx_association_version_transaction_id"),
            ["transaction_id"],
            unique=False,
        )

    op.create_table(
        "opt_tab_version",
        sa.Column("name", sa.String(length=255), nullable=False),
        sa.Column("run__id", sa.Integer(), nullable=False),
        sa.Column(
            "data",
            sa.JSON().with_variant(
                postgresql.JSONB(astext_type=sa.Text()), "postgresql"
            ),
            nullable=False,
        ),
        sa.Column("created_at", sa.DateTime(), nullable=True),
        sa.Column("created_by", sa.String(length=255), nullable=True),
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("transaction_id", sa.BigInteger(), nullable=False),
        sa.Column("operation_type", sa.SmallInteger(), nullable=False),
        sa.Column("end_transaction_id", sa.BigInteger(), nullable=True),
        sa.PrimaryKeyConstraint(
            "id", "transaction_id", name=op.f("pk_opt_tab_version")
        ),
    )
    with op.batch_alter_table("opt_tab_version", schema=None) as batch_op:
        batch_op.create_index(
            batch_op.f("ix_opt_tab_version_end_transaction_id"),
            ["end_transaction_id"],
            unique=False,
        )
        batch_op.create_index(
            batch_op.f("ix_opt_tab_version_operation_type"),
            ["operation_type"],
            unique=False,
        )
        batch_op.create_index(
            batch_op.f("ix_opt_tab_version_run__id"), ["run__id"], unique=False
        )
        batch_op.create_index(
            batch_op.f("ix_opt_tab_version_transaction_id"),
            ["transaction_id"],
            unique=False,
        )

    op.create_table(
        "opt_var_idx_association_version",
        sa.Column("variable__id", sa.Integer(), nullable=False),
        sa.Column("indexset__id", sa.Integer(), nullable=False),
        sa.Column("column_name", sa.String(length=255), nullable=True),
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("transaction_id", sa.BigInteger(), nullable=False),
        sa.Column("operation_type", sa.SmallInteger(), nullable=False),
        sa.Column("end_transaction_id", sa.BigInteger(), nullable=True),
        sa.PrimaryKeyConstraint(
            "id", "transaction_id", name=op.f("pk_opt_var_idx_association_version")
        ),
    )
    with op.batch_alter_table(
        "opt_var_idx_association_version", schema=None
    ) as batch_op:
        batch_op.create_index(
            batch_op.f("ix_opt_var_idx_association_version_end_transaction_id"),
            ["end_transaction_id"],
            unique=False,
        )
        batch_op.create_index(
            batch_op.f("ix_opt_var_idx_association_version_indexset__id"),
            ["indexset__id"],
            unique=False,
        )
        batch_op.create_index(
            batch_op.f("ix_opt_var_idx_association_version_operation_type"),
            ["operation_type"],
            unique=False,
        )
        batch_op.create_index(
            batch_op.f("ix_opt_var_idx_association_version_transaction_id"),
            ["transaction_id"],
            unique=False,
        )
        batch_op.create_index(
            batch_op.f("ix_opt_var_idx_association_version_variable__id"),
            ["variable__id"],
            unique=False,
        )

    op.create_table(
        "opt_var_version",
        sa.Column("name", sa.String(length=255), nullable=False),
        sa.Column("run__id", sa.Integer(), nullable=False),
        sa.Column(
            "data",
            sa.JSON().with_variant(
                postgresql.JSONB(astext_type=sa.Text()), "postgresql"
            ),
            nullable=False,
        ),
        sa.Column("created_at", sa.DateTime(), nullable=True),
        sa.Column("created_by", sa.String(length=255), nullable=True),
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("transaction_id", sa.BigInteger(), nullable=False),
        sa.Column("operation_type", sa.SmallInteger(), nullable=False),
        sa.Column("end_transaction_id", sa.BigInteger(), nullable=True),
        sa.PrimaryKeyConstraint(
            "id", "transaction_id", name=op.f("pk_opt_var_version")
        ),
    )
    with op.batch_alter_table("opt_var_version", schema=None) as batch_op:
        batch_op.create_index(
            batch_op.f("ix_opt_var_version_end_transaction_id"),
            ["end_transaction_id"],
            unique=False,
        )
        batch_op.create_index(
            batch_op.f("ix_opt_var_version_operation_type"),
            ["operation_type"],
            unique=False,
        )
        batch_op.create_index(
            batch_op.f("ix_opt_var_version_run__id"), ["run__id"], unique=False
        )
        batch_op.create_index(
            batch_op.f("ix_opt_var_version_transaction_id"),
            ["transaction_id"],
            unique=False,
        )

    op.create_table(
        "opt_equ_docs",
        sa.Column("description", sa.Text(), nullable=False),
        sa.Column("dimension__id", sa.Integer(), nullable=True),
        sa.Column(
            "id",
            sa.Integer(),
            sa.Identity(always=False, on_null=True, start=1, increment=1),
            nullable=False,
        ),
        sa.ForeignKeyConstraint(
            ["dimension__id"],
            ["opt_equ.id"],
            name=op.f("fk_opt_equ_docs_dimension__id_opt_equ"),
            ondelete="CASCADE",
        ),
        sa.PrimaryKeyConstraint("id", name=op.f("pk_opt_equ_docs")),
        sa.UniqueConstraint(
            "dimension__id", name=op.f("uq_opt_equ_docs_dimension__id")
        ),
    )
    op.create_table(
        "opt_equ_idx_association",
        sa.Column("equation__id", sa.Integer(), nullable=False),
        sa.Column("indexset__id", sa.Integer(), nullable=False),
        sa.Column("column_name", sa.String(length=255), nullable=True),
        sa.Column(
            "id",
            sa.Integer(),
            sa.Identity(always=False, on_null=True, start=1, increment=1),
            nullable=False,
        ),
        sa.ForeignKeyConstraint(
            ["equation__id"],
            ["opt_equ.id"],
            name=op.f("fk_opt_equ_idx_association_equation__id_opt_equ"),
            ondelete="CASCADE",
        ),
        sa.ForeignKeyConstraint(
            ["indexset__id"],
            ["opt_idx.id"],
            name=op.f("fk_opt_equ_idx_association_indexset__id_opt_idx"),
        ),
        sa.PrimaryKeyConstraint("id", name=op.f("pk_opt_equ_idx_association")),
    )
    with op.batch_alter_table("opt_equ_idx_association", schema=None) as batch_op:
        batch_op.create_index(
            batch_op.f("ix_opt_equ_idx_association_equation__id"),
            ["equation__id"],
            unique=False,
        )
        batch_op.create_index(
            batch_op.f("ix_opt_equ_idx_association_indexset__id"),
            ["indexset__id"],
            unique=False,
        )

    op.create_table(
        "opt_idx_data",
        sa.Column("indexset__id", sa.Integer(), nullable=False),
        sa.Column("value", sa.String(), nullable=False),
        sa.Column(
            "id",
            sa.Integer(),
            sa.Identity(always=False, on_null=True, start=1, increment=1),
            nullable=False,
        ),
        sa.ForeignKeyConstraint(
            ["indexset__id"],
            ["opt_idx.id"],
            name=op.f("fk_opt_idx_data_indexset__id_opt_idx"),
            ondelete="CASCADE",
        ),
        sa.PrimaryKeyConstraint("id", name=op.f("pk_opt_idx_data")),
        sa.UniqueConstraint(
            "indexset__id", "value", name=op.f("uq_opt_idx_data_indexset__id_value")
        ),
    )
    with op.batch_alter_table("opt_idx_data", schema=None) as batch_op:
        batch_op.create_index(
            batch_op.f("ix_opt_idx_data_indexset__id"), ["indexset__id"], unique=False
        )

    op.create_table(
        "opt_idx_docs",
        sa.Column("description", sa.Text(), nullable=False),
        sa.Column("dimension__id", sa.Integer(), nullable=True),
        sa.Column(
            "id",
            sa.Integer(),
            sa.Identity(always=False, on_null=True, start=1, increment=1),
            nullable=False,
        ),
        sa.ForeignKeyConstraint(
            ["dimension__id"],
            ["opt_idx.id"],
            name=op.f("fk_opt_idx_docs_dimension__id_opt_idx"),
            ondelete="CASCADE",
        ),
        sa.PrimaryKeyConstraint("id", name=op.f("pk_opt_idx_docs")),
        sa.UniqueConstraint(
            "dimension__id", name=op.f("uq_opt_idx_docs_dimension__id")
        ),
    )
    op.create_table(
        "opt_par_docs",
        sa.Column("description", sa.Text(), nullable=False),
        sa.Column("dimension__id", sa.Integer(), nullable=True),
        sa.Column(
            "id",
            sa.Integer(),
            sa.Identity(always=False, on_null=True, start=1, increment=1),
            nullable=False,
        ),
        sa.ForeignKeyConstraint(
            ["dimension__id"],
            ["opt_par.id"],
            name=op.f("fk_opt_par_docs_dimension__id_opt_par"),
            ondelete="CASCADE",
        ),
        sa.PrimaryKeyConstraint("id", name=op.f("pk_opt_par_docs")),
        sa.UniqueConstraint(
            "dimension__id", name=op.f("uq_opt_par_docs_dimension__id")
        ),
    )
    op.create_table(
        "opt_par_idx_association",
        sa.Column("parameter__id", sa.Integer(), nullable=False),
        sa.Column("indexset__id", sa.Integer(), nullable=False),
        sa.Column("column_name", sa.String(length=255), nullable=True),
        sa.Column(
            "id",
            sa.Integer(),
            sa.Identity(always=False, on_null=True, start=1, increment=1),
            nullable=False,
        ),
        sa.ForeignKeyConstraint(
            ["indexset__id"],
            ["opt_idx.id"],
            name=op.f("fk_opt_par_idx_association_indexset__id_opt_idx"),
        ),
        sa.ForeignKeyConstraint(
            ["parameter__id"],
            ["opt_par.id"],
            name=op.f("fk_opt_par_idx_association_parameter__id_opt_par"),
            ondelete="CASCADE",
        ),
        sa.PrimaryKeyConstraint("id", name=op.f("pk_opt_par_idx_association")),
    )
    with op.batch_alter_table("opt_par_idx_association", schema=None) as batch_op:
        batch_op.create_index(
            batch_op.f("ix_opt_par_idx_association_indexset__id"),
            ["indexset__id"],
            unique=False,
        )
        batch_op.create_index(
            batch_op.f("ix_opt_par_idx_association_parameter__id"),
            ["parameter__id"],
            unique=False,
        )

    op.create_table(
        "opt_sca_docs",
        sa.Column("description", sa.Text(), nullable=False),
        sa.Column("dimension__id", sa.Integer(), nullable=True),
        sa.Column(
            "id",
            sa.Integer(),
            sa.Identity(always=False, on_null=True, start=1, increment=1),
            nullable=False,
        ),
        sa.ForeignKeyConstraint(
            ["dimension__id"],
            ["opt_sca.id"],
            name=op.f("fk_opt_sca_docs_dimension__id_opt_sca"),
            ondelete="CASCADE",
        ),
        sa.PrimaryKeyConstraint("id", name=op.f("pk_opt_sca_docs")),
        sa.UniqueConstraint(
            "dimension__id", name=op.f("uq_opt_sca_docs_dimension__id")
        ),
    )
    op.create_table(
        "opt_tab_docs",
        sa.Column("description", sa.Text(), nullable=False),
        sa.Column("dimension__id", sa.Integer(), nullable=True),
        sa.Column(
            "id",
            sa.Integer(),
            sa.Identity(always=False, on_null=True, start=1, increment=1),
            nullable=False,
        ),
        sa.ForeignKeyConstraint(
            ["dimension__id"],
            ["opt_tab.id"],
            name=op.f("fk_opt_tab_docs_dimension__id_opt_tab"),
            ondelete="CASCADE",
        ),
        sa.PrimaryKeyConstraint("id", name=op.f("pk_opt_tab_docs")),
        sa.UniqueConstraint(
            "dimension__id", name=op.f("uq_opt_tab_docs_dimension__id")
        ),
    )
    op.create_table(
        "opt_tab_idx_association",
        sa.Column("table__id", sa.Integer(), nullable=False),
        sa.Column("indexset__id", sa.Integer(), nullable=False),
        sa.Column("column_name", sa.String(length=255), nullable=True),
        sa.Column(
            "id",
            sa.Integer(),
            sa.Identity(always=False, on_null=True, start=1, increment=1),
            nullable=False,
        ),
        sa.ForeignKeyConstraint(
            ["indexset__id"],
            ["opt_idx.id"],
            name=op.f("fk_opt_tab_idx_association_indexset__id_opt_idx"),
        ),
        sa.ForeignKeyConstraint(
            ["table__id"],
            ["opt_tab.id"],
            name=op.f("fk_opt_tab_idx_association_table__id_opt_tab"),
            ondelete="CASCADE",
        ),
        sa.PrimaryKeyConstraint("id", name=op.f("pk_opt_tab_idx_association")),
    )
    with op.batch_alter_table("opt_tab_idx_association", schema=None) as batch_op:
        batch_op.create_index(
            batch_op.f("ix_opt_tab_idx_association_indexset__id"),
            ["indexset__id"],
            unique=False,
        )
        batch_op.create_index(
            batch_op.f("ix_opt_tab_idx_association_table__id"),
            ["table__id"],
            unique=False,
        )

    op.create_table(
        "opt_var_docs",
        sa.Column("description", sa.Text(), nullable=False),
        sa.Column("dimension__id", sa.Integer(), nullable=True),
        sa.Column(
            "id",
            sa.Integer(),
            sa.Identity(always=False, on_null=True, start=1, increment=1),
            nullable=False,
        ),
        sa.ForeignKeyConstraint(
            ["dimension__id"],
            ["opt_var.id"],
            name=op.f("fk_opt_var_docs_dimension__id_opt_var"),
            ondelete="CASCADE",
        ),
        sa.PrimaryKeyConstraint("id", name=op.f("pk_opt_var_docs")),
        sa.UniqueConstraint(
            "dimension__id", name=op.f("uq_opt_var_docs_dimension__id")
        ),
    )
    op.create_table(
        "opt_var_idx_association",
        sa.Column("variable__id", sa.Integer(), nullable=False),
        sa.Column("indexset__id", sa.Integer(), nullable=False),
        sa.Column("column_name", sa.String(length=255), nullable=True),
        sa.Column(
            "id",
            sa.Integer(),
            sa.Identity(always=False, on_null=True, start=1, increment=1),
            nullable=False,
        ),
        sa.ForeignKeyConstraint(
            ["indexset__id"],
            ["opt_idx.id"],
            name=op.f("fk_opt_var_idx_association_indexset__id_opt_idx"),
        ),
        sa.ForeignKeyConstraint(
            ["variable__id"],
            ["opt_var.id"],
            name=op.f("fk_opt_var_idx_association_variable__id_opt_var"),
            ondelete="CASCADE",
        ),
        sa.PrimaryKeyConstraint("id", name=op.f("pk_opt_var_idx_association")),
    )
    with op.batch_alter_table("opt_var_idx_association", schema=None) as batch_op:
        batch_op.create_index(
            batch_op.f("ix_opt_var_idx_association_indexset__id"),
            ["indexset__id"],
            unique=False,
        )
        batch_op.create_index(
            batch_op.f("ix_opt_var_idx_association_variable__id"),
            ["variable__id"],
            unique=False,
        )

    with op.batch_alter_table(
        "optimization_tableindexsetassociation", schema=None
    ) as batch_op:
        batch_op.drop_index(
            batch_op.f("ix_optimization_tableindexsetassociation_indexset__id")
        )
        batch_op.drop_index(
            batch_op.f("ix_optimization_tableindexsetassociation_table__id")
        )

    op.drop_table("optimization_tableindexsetassociation")
    op.drop_table("optimization_parameter_docs")
    op.drop_table("optimization_equation_docs")
    with op.batch_alter_table(
        "optimization_variableindexsetassociation", schema=None
    ) as batch_op:
        batch_op.drop_index(
            batch_op.f("ix_optimization_variableindexsetassociation_indexset__id")
        )
        batch_op.drop_index(
            batch_op.f("ix_optimization_variableindexsetassociation_variable__id")
        )

    op.drop_table("optimization_variableindexsetassociation")
    op.drop_table("optimization_table_docs")
    op.drop_table("optimization_optimizationvariable_docs")
    with op.batch_alter_table("optimization_indexsetdata", schema=None) as batch_op:
        batch_op.drop_index(batch_op.f("ix_optimization_indexsetdata_indexset__id"))

    op.drop_table("optimization_indexsetdata")
    op.drop_table("optimization_indexset_docs")
    with op.batch_alter_table(
        "optimization_equationindexsetassociation", schema=None
    ) as batch_op:
        batch_op.drop_index(
            batch_op.f("ix_optimization_equationindexsetassociation_equation__id")
        )
        batch_op.drop_index(
            batch_op.f("ix_optimization_equationindexsetassociation_indexset__id")
        )

    op.drop_table("optimization_equationindexsetassociation")
    with op.batch_alter_table(
        "optimization_parameterindexsetassociation", schema=None
    ) as batch_op:
        batch_op.drop_index(
            batch_op.f("ix_optimization_parameterindexsetassociation_indexset__id")
        )
        batch_op.drop_index(
            batch_op.f("ix_optimization_parameterindexsetassociation_parameter__id")
        )

    op.drop_table("optimization_parameterindexsetassociation")
    op.drop_table("optimization_scalar_docs")

    with op.batch_alter_table("optimization_scalar", schema=None) as batch_op:
        batch_op.drop_index(batch_op.f("ix_optimization_scalar_run__id"))
        batch_op.drop_index(batch_op.f("ix_optimization_scalar_unit__id"))

    op.drop_table("optimization_scalar")

    with op.batch_alter_table("optimization_table", schema=None) as batch_op:
        batch_op.drop_index(batch_op.f("ix_optimization_table_run__id"))

    op.drop_table("optimization_table")

    with op.batch_alter_table("optimization_parameter", schema=None) as batch_op:
        batch_op.drop_index(batch_op.f("ix_optimization_parameter_run__id"))

    op.drop_table("optimization_parameter")

    with op.batch_alter_table("optimization_equation", schema=None) as batch_op:
        batch_op.drop_index(batch_op.f("ix_optimization_equation_run__id"))

    op.drop_table("optimization_equation")

    with op.batch_alter_table(
        "optimization_optimizationvariable", schema=None
    ) as batch_op:
        batch_op.drop_index(batch_op.f("ix_optimization_optimizationvariable_run__id"))

    op.drop_table("optimization_optimizationvariable")

    with op.batch_alter_table("optimization_indexset", schema=None) as batch_op:
        batch_op.drop_index(batch_op.f("ix_optimization_indexset_run__id"))

    op.drop_table("optimization_indexset")

    with op.batch_alter_table("iamc_variable_docs", schema=None) as batch_op:
        batch_op.drop_constraint(
            batch_op.f("fk_iamc_variable_docs_dimension__id_iamc_variable"),
            type_="foreignkey",
        )
        batch_op.create_foreign_key(
            batch_op.f("fk_iamc_variable_docs_dimension__id_iamc_variable"),
            "iamc_variable",
            ["dimension__id"],
            ["id"],
            ondelete="CASCADE",
        )

    with op.batch_alter_table("model_docs", schema=None) as batch_op:
        batch_op.drop_constraint(
            batch_op.f("fk_model_docs_dimension__id_model"), type_="foreignkey"
        )
        batch_op.create_foreign_key(
            batch_op.f("fk_model_docs_dimension__id_model"),
            "model",
            ["dimension__id"],
            ["id"],
            ondelete="CASCADE",
        )

    with op.batch_alter_table("region_docs", schema=None) as batch_op:
        batch_op.drop_constraint(
            batch_op.f("fk_region_docs_dimension__id_region"), type_="foreignkey"
        )
        batch_op.create_foreign_key(
            batch_op.f("fk_region_docs_dimension__id_region"),
            "region",
            ["dimension__id"],
            ["id"],
            ondelete="CASCADE",
        )

    with op.batch_alter_table("scenario_docs", schema=None) as batch_op:
        batch_op.drop_constraint(
            batch_op.f("fk_scenario_docs_dimension__id_scenario"), type_="foreignkey"
        )
        batch_op.create_foreign_key(
            batch_op.f("fk_scenario_docs_dimension__id_scenario"),
            "scenario",
            ["dimension__id"],
            ["id"],
            ondelete="CASCADE",
        )

    with op.batch_alter_table("unit_docs", schema=None) as batch_op:
        batch_op.drop_constraint(
            batch_op.f("fk_unit_docs_dimension__id_unit"), type_="foreignkey"
        )
        batch_op.create_foreign_key(
            batch_op.f("fk_unit_docs_dimension__id_unit"),
            "unit",
            ["dimension__id"],
            ["id"],
            ondelete="CASCADE",
        )

    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    with op.batch_alter_table("unit_docs", schema=None) as batch_op:
        batch_op.drop_constraint(
            batch_op.f("fk_unit_docs_dimension__id_unit"), type_="foreignkey"
        )
        batch_op.create_foreign_key(
            batch_op.f("fk_unit_docs_dimension__id_unit"),
            "unit",
            ["dimension__id"],
            ["id"],
        )

    with op.batch_alter_table("scenario_docs", schema=None) as batch_op:
        batch_op.drop_constraint(
            batch_op.f("fk_scenario_docs_dimension__id_scenario"), type_="foreignkey"
        )
        batch_op.create_foreign_key(
            batch_op.f("fk_scenario_docs_dimension__id_scenario"),
            "scenario",
            ["dimension__id"],
            ["id"],
        )

    with op.batch_alter_table("region_docs", schema=None) as batch_op:
        batch_op.drop_constraint(
            batch_op.f("fk_region_docs_dimension__id_region"), type_="foreignkey"
        )
        batch_op.create_foreign_key(
            batch_op.f("fk_region_docs_dimension__id_region"),
            "region",
            ["dimension__id"],
            ["id"],
        )

    with op.batch_alter_table("model_docs", schema=None) as batch_op:
        batch_op.drop_constraint(
            batch_op.f("fk_model_docs_dimension__id_model"), type_="foreignkey"
        )
        batch_op.create_foreign_key(
            batch_op.f("fk_model_docs_dimension__id_model"),
            "model",
            ["dimension__id"],
            ["id"],
        )

    with op.batch_alter_table("iamc_variable_docs", schema=None) as batch_op:
        batch_op.drop_constraint(
            batch_op.f("fk_iamc_variable_docs_dimension__id_iamc_variable"),
            type_="foreignkey",
        )
        batch_op.create_foreign_key(
            batch_op.f("fk_iamc_variable_docs_dimension__id_iamc_variable"),
            "iamc_variable",
            ["dimension__id"],
            ["id"],
        )

    op.create_table(
        "optimization_indexset",
        sa.Column("name", sa.String(length=255), nullable=False),
        sa.Column("created_at", sa.DateTime(), nullable=True),
        sa.Column("created_by", sa.String(length=255), nullable=True),
        sa.Column("run__id", sa.Integer(), nullable=False),
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("_data_type", sa.VARCHAR(length=5), nullable=True),
        sa.ForeignKeyConstraint(
            ["run__id"], ["run.id"], name="fk_optimization_indexset_run__id_run"
        ),
        sa.PrimaryKeyConstraint("id", name="pk_optimization_indexset"),
        sa.UniqueConstraint(
            "name", "run__id", name="uq_optimization_indexset_name_run__id"
        ),
    )
    with op.batch_alter_table("optimization_indexset", schema=None) as batch_op:
        batch_op.create_index(
            batch_op.f("ix_optimization_indexset_run__id"), ["run__id"], unique=False
        )

    op.create_table(
        "optimization_equation",
        sa.Column("run__id", sa.Integer(), nullable=False),
        sa.Column(
            "data",
            sa.JSON().with_variant(
                postgresql.JSONB(astext_type=sa.Text()), "postgresql"
            ),
            nullable=False,
        ),
        sa.Column("name", sa.String(length=255), nullable=False),
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("created_at", sa.DateTime(), nullable=True),
        sa.Column("created_by", sa.String(length=255), nullable=True),
        sa.ForeignKeyConstraint(
            ["run__id"], ["run.id"], name=op.f("fk_optimization_equation_run__id_run")
        ),
        sa.PrimaryKeyConstraint("id", name=op.f("pk_optimization_equation")),
        sa.UniqueConstraint(
            "name", "run__id", name=op.f("uq_optimization_equation_name_run__id")
        ),
    )
    with op.batch_alter_table("optimization_equation", schema=None) as batch_op:
        batch_op.create_index(
            batch_op.f("ix_optimization_equation_run__id"), ["run__id"], unique=False
        )

    op.create_table(
        "optimization_table",
        sa.Column("name", sa.String(length=255), nullable=False),
        sa.Column(
            "data",
            sa.JSON().with_variant(
                postgresql.JSONB(astext_type=sa.Text()), "postgresql"
            ),
            nullable=False,
        ),
        sa.Column("run__id", sa.Integer(), nullable=False),
        sa.Column("created_at", sa.DateTime(), nullable=True),
        sa.Column("created_by", sa.String(length=255), nullable=True),
        sa.Column("id", sa.Integer(), nullable=False),
        sa.ForeignKeyConstraint(
            ["run__id"], ["run.id"], name="fk_optimization_table_run__id_run"
        ),
        sa.PrimaryKeyConstraint("id", name="pk_optimization_table"),
        sa.UniqueConstraint(
            "name", "run__id", name="uq_optimization_table_name_run__id"
        ),
    )
    with op.batch_alter_table("optimization_table", schema=None) as batch_op:
        batch_op.create_index(
            batch_op.f("ix_optimization_table_run__id"), ["run__id"], unique=False
        )

    op.create_table(
        "optimization_scalar",
        sa.Column("name", sa.String(length=255), nullable=False),
        sa.Column("value", sa.Float(), nullable=True),
        sa.Column("unit__id", sa.Integer(), nullable=False),
        sa.Column("run__id", sa.Integer(), nullable=False),
        sa.Column("created_at", sa.DateTime(), nullable=True),
        sa.Column("created_by", sa.String(length=255), nullable=True),
        sa.Column("id", sa.Integer(), nullable=False),
        sa.ForeignKeyConstraint(
            ["run__id"], ["run.id"], name=op.f("fk_optimization_scalar_run__id_run")
        ),
        sa.ForeignKeyConstraint(
            ["unit__id"], ["unit.id"], name=op.f("fk_optimization_scalar_unit__id_unit")
        ),
        sa.PrimaryKeyConstraint("id", name=op.f("pk_optimization_scalar")),
        sa.UniqueConstraint(
            "name", "run__id", name=op.f("uq_optimization_scalar_name_run__id")
        ),
    )
    with op.batch_alter_table("optimization_scalar", schema=None) as batch_op:
        batch_op.create_index(
            batch_op.f("ix_optimization_scalar_unit__id"), ["unit__id"], unique=False
        )
        batch_op.create_index(
            batch_op.f("ix_optimization_scalar_run__id"), ["run__id"], unique=False
        )

    op.create_table(
        "optimization_optimizationvariable",
        sa.Column("run__id", sa.Integer(), nullable=False),
        sa.Column(
            "data",
            sa.JSON().with_variant(
                postgresql.JSONB(astext_type=sa.Text()), "postgresql"
            ),
            nullable=False,
        ),
        sa.Column("name", sa.String(length=255), nullable=False),
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("created_at", sa.DateTime(), nullable=True),
        sa.Column("created_by", sa.String(length=255), nullable=True),
        sa.ForeignKeyConstraint(
            ["run__id"],
            ["run.id"],
            name="fk_optimization_optimizationvariable_run__id_run",
        ),
        sa.PrimaryKeyConstraint("id", name="pk_optimization_optimizationvariable"),
        sa.UniqueConstraint(
            "name", "run__id", name="uq_optimization_optimizationvariable_name_run__id"
        ),
    )
    with op.batch_alter_table(
        "optimization_optimizationvariable", schema=None
    ) as batch_op:
        batch_op.create_index(
            batch_op.f("ix_optimization_optimizationvariable_run__id"),
            ["run__id"],
            unique=False,
        )

    op.create_table(
        "optimization_parameter",
        sa.Column("run__id", sa.Integer(), nullable=False),
        sa.Column(
            "data",
            sa.JSON().with_variant(
                postgresql.JSONB(astext_type=sa.Text()), "postgresql"
            ),
            nullable=False,
        ),
        sa.Column("name", sa.String(length=255), nullable=False),
        sa.Column("id", sa.Integer(), nullable=False),
        sa.Column("created_at", sa.DateTime(), nullable=True),
        sa.Column("created_by", sa.String(length=255), nullable=True),
        sa.ForeignKeyConstraint(
            ["run__id"], ["run.id"], name="fk_optimization_parameter_run__id_run"
        ),
        sa.PrimaryKeyConstraint("id", name="pk_optimization_parameter"),
        sa.UniqueConstraint(
            "name", "run__id", name="uq_optimization_parameter_name_run__id"
        ),
    )
    with op.batch_alter_table("optimization_parameter", schema=None) as batch_op:
        batch_op.create_index(
            batch_op.f("ix_optimization_parameter_run__id"), ["run__id"], unique=False
        )

    op.create_table(
        "optimization_scalar_docs",
        sa.Column("description", sa.TEXT(), nullable=False),
        sa.Column("dimension__id", sa.INTEGER(), nullable=True),
        sa.Column("id", sa.INTEGER(), nullable=False),
        sa.ForeignKeyConstraint(
            ["dimension__id"],
            ["optimization_scalar.id"],
            name=op.f("fk_optimization_scalar_docs_dimension__id_optimization_scalar"),
        ),
        sa.PrimaryKeyConstraint("id", name=op.f("pk_optimization_scalar_docs")),
        sa.UniqueConstraint(
            "dimension__id", name=op.f("uq_optimization_scalar_docs_dimension__id")
        ),
    )
    op.create_table(
        "optimization_parameterindexsetassociation",
        sa.Column("parameter__id", sa.INTEGER(), nullable=False),
        sa.Column("indexset__id", sa.INTEGER(), nullable=False),
        sa.Column("column_name", sa.VARCHAR(length=255), nullable=True),
        sa.Column("id", sa.INTEGER(), nullable=False),
        sa.ForeignKeyConstraint(
            ["indexset__id"],
            ["optimization_indexset.id"],
            name=op.f("fk_optimization_parameterindexsetassociation_indexset___7045"),
        ),
        sa.ForeignKeyConstraint(
            ["parameter__id"],
            ["optimization_parameter.id"],
            name=op.f("fk_optimization_parameterindexsetassociation_parameter__8a6d"),
            ondelete="CASCADE",
        ),
        sa.PrimaryKeyConstraint(
            "id", name=op.f("pk_optimization_parameterindexsetassociation")
        ),
    )
    with op.batch_alter_table(
        "optimization_parameterindexsetassociation", schema=None
    ) as batch_op:
        batch_op.create_index(
            batch_op.f("ix_optimization_parameterindexsetassociation_parameter__id"),
            ["parameter__id"],
            unique=False,
        )
        batch_op.create_index(
            batch_op.f("ix_optimization_parameterindexsetassociation_indexset__id"),
            ["indexset__id"],
            unique=False,
        )

    op.create_table(
        "optimization_equationindexsetassociation",
        sa.Column("equation__id", sa.INTEGER(), nullable=False),
        sa.Column("indexset__id", sa.INTEGER(), nullable=False),
        sa.Column("column_name", sa.VARCHAR(length=255), nullable=True),
        sa.Column("id", sa.INTEGER(), nullable=False),
        sa.ForeignKeyConstraint(
            ["equation__id"],
            ["optimization_equation.id"],
            name=op.f("fk_optimization_equationindexsetassociation_equation__i_be8b"),
            ondelete="CASCADE",
        ),
        sa.ForeignKeyConstraint(
            ["indexset__id"],
            ["optimization_indexset.id"],
            name=op.f("fk_optimization_equationindexsetassociation_indexset__i_82d0"),
        ),
        sa.PrimaryKeyConstraint(
            "id", name=op.f("pk_optimization_equationindexsetassociation")
        ),
    )
    with op.batch_alter_table(
        "optimization_equationindexsetassociation", schema=None
    ) as batch_op:
        batch_op.create_index(
            batch_op.f("ix_optimization_equationindexsetassociation_indexset__id"),
            ["indexset__id"],
            unique=False,
        )
        batch_op.create_index(
            batch_op.f("ix_optimization_equationindexsetassociation_equation__id"),
            ["equation__id"],
            unique=False,
        )

    op.create_table(
        "optimization_indexset_docs",
        sa.Column("description", sa.TEXT(), nullable=False),
        sa.Column("dimension__id", sa.INTEGER(), nullable=True),
        sa.Column("id", sa.INTEGER(), nullable=False),
        sa.ForeignKeyConstraint(
            ["dimension__id"],
            ["optimization_indexset.id"],
            name=op.f("fk_optimization_indexset_docs_dimension__id_optimizatio_72ee"),
        ),
        sa.PrimaryKeyConstraint("id", name=op.f("pk_optimization_indexset_docs")),
        sa.UniqueConstraint(
            "dimension__id", name=op.f("uq_optimization_indexset_docs_dimension__id")
        ),
    )

    op.create_table(
        "optimization_indexsetdata",
        sa.Column("indexset__id", sa.INTEGER(), nullable=False),
        sa.Column("value", sa.VARCHAR(), nullable=False),
        sa.Column("id", sa.INTEGER(), nullable=False),
        sa.ForeignKeyConstraint(
            ["indexset__id"],
            ["optimization_indexset.id"],
            name=op.f(
                "fk_optimization_indexsetdata_indexset__id_optimization_indexset"
            ),
            ondelete="CASCADE",
        ),
        sa.PrimaryKeyConstraint("id", name=op.f("pk_optimization_indexsetdata")),
        sa.UniqueConstraint(
            "indexset__id",
            "value",
            name=op.f("uq_optimization_indexsetdata_indexset__id_value"),
        ),
    )
    with op.batch_alter_table("optimization_indexsetdata", schema=None) as batch_op:
        batch_op.create_index(
            batch_op.f("ix_optimization_indexsetdata_indexset__id"),
            ["indexset__id"],
            unique=False,
        )

    op.create_table(
        "optimization_optimizationvariable_docs",
        sa.Column("description", sa.TEXT(), nullable=False),
        sa.Column("dimension__id", sa.INTEGER(), nullable=True),
        sa.Column("id", sa.INTEGER(), nullable=False),
        sa.ForeignKeyConstraint(
            ["dimension__id"],
            ["optimization_optimizationvariable.id"],
            name=op.f("fk_optimization_optimizationvariable_docs_dimension__id_ba30"),
        ),
        sa.PrimaryKeyConstraint(
            "id", name=op.f("pk_optimization_optimizationvariable_docs")
        ),
        sa.UniqueConstraint(
            "dimension__id",
            name=op.f("uq_optimization_optimizationvariable_docs_dimension__id"),
        ),
    )
    op.create_table(
        "optimization_table_docs",
        sa.Column("description", sa.TEXT(), nullable=False),
        sa.Column("dimension__id", sa.INTEGER(), nullable=True),
        sa.Column("id", sa.INTEGER(), nullable=False),
        sa.ForeignKeyConstraint(
            ["dimension__id"],
            ["optimization_table.id"],
            name=op.f("fk_optimization_table_docs_dimension__id_optimization_table"),
        ),
        sa.PrimaryKeyConstraint("id", name=op.f("pk_optimization_table_docs")),
        sa.UniqueConstraint(
            "dimension__id", name=op.f("uq_optimization_table_docs_dimension__id")
        ),
    )
    op.create_table(
        "optimization_variableindexsetassociation",
        sa.Column("variable__id", sa.INTEGER(), nullable=False),
        sa.Column("indexset__id", sa.INTEGER(), nullable=False),
        sa.Column("column_name", sa.VARCHAR(length=255), nullable=True),
        sa.Column("id", sa.INTEGER(), nullable=False),
        sa.ForeignKeyConstraint(
            ["indexset__id"],
            ["optimization_indexset.id"],
            name=op.f("fk_optimization_variableindexsetassociation_indexset__i_044c"),
        ),
        sa.ForeignKeyConstraint(
            ["variable__id"],
            ["optimization_optimizationvariable.id"],
            name=op.f("fk_optimization_variableindexsetassociation_variable__i_02a8"),
            ondelete="CASCADE",
        ),
        sa.PrimaryKeyConstraint(
            "id", name=op.f("pk_optimization_variableindexsetassociation")
        ),
    )
    with op.batch_alter_table(
        "optimization_variableindexsetassociation", schema=None
    ) as batch_op:
        batch_op.create_index(
            batch_op.f("ix_optimization_variableindexsetassociation_variable__id"),
            ["variable__id"],
            unique=False,
        )
        batch_op.create_index(
            batch_op.f("ix_optimization_variableindexsetassociation_indexset__id"),
            ["indexset__id"],
            unique=False,
        )

    op.create_table(
        "optimization_equation_docs",
        sa.Column("description", sa.TEXT(), nullable=False),
        sa.Column("dimension__id", sa.INTEGER(), nullable=True),
        sa.Column("id", sa.INTEGER(), nullable=False),
        sa.ForeignKeyConstraint(
            ["dimension__id"],
            ["optimization_equation.id"],
            name=op.f("fk_optimization_equation_docs_dimension__id_optimizatio_bb11"),
        ),
        sa.PrimaryKeyConstraint("id", name=op.f("pk_optimization_equation_docs")),
        sa.UniqueConstraint(
            "dimension__id", name=op.f("uq_optimization_equation_docs_dimension__id")
        ),
    )

    op.create_table(
        "optimization_parameter_docs",
        sa.Column("description", sa.TEXT(), nullable=False),
        sa.Column("dimension__id", sa.INTEGER(), nullable=True),
        sa.Column("id", sa.INTEGER(), nullable=False),
        sa.ForeignKeyConstraint(
            ["dimension__id"],
            ["optimization_parameter.id"],
            name=op.f("fk_optimization_parameter_docs_dimension__id_optimizati_898e"),
        ),
        sa.PrimaryKeyConstraint("id", name=op.f("pk_optimization_parameter_docs")),
        sa.UniqueConstraint(
            "dimension__id", name=op.f("uq_optimization_parameter_docs_dimension__id")
        ),
    )
    op.create_table(
        "optimization_tableindexsetassociation",
        sa.Column("table__id", sa.INTEGER(), nullable=False),
        sa.Column("indexset__id", sa.INTEGER(), nullable=False),
        sa.Column("column_name", sa.VARCHAR(length=255), nullable=True),
        sa.Column("id", sa.INTEGER(), nullable=False),
        sa.ForeignKeyConstraint(
            ["indexset__id"],
            ["optimization_indexset.id"],
            name=op.f("fk_optimization_tableindexsetassociation_indexset__id_o_868a"),
        ),
        sa.ForeignKeyConstraint(
            ["table__id"],
            ["optimization_table.id"],
            name=op.f("fk_optimization_tableindexsetassociation_table__id_opti_5cfe"),
            ondelete="CASCADE",
        ),
        sa.PrimaryKeyConstraint(
            "id", name=op.f("pk_optimization_tableindexsetassociation")
        ),
    )
    with op.batch_alter_table(
        "optimization_tableindexsetassociation", schema=None
    ) as batch_op:
        batch_op.create_index(
            batch_op.f("ix_optimization_tableindexsetassociation_table__id"),
            ["table__id"],
            unique=False,
        )
        batch_op.create_index(
            batch_op.f("ix_optimization_tableindexsetassociation_indexset__id"),
            ["indexset__id"],
            unique=False,
        )

    with op.batch_alter_table("opt_var_idx_association", schema=None) as batch_op:
        batch_op.drop_index(batch_op.f("ix_opt_var_idx_association_variable__id"))
        batch_op.drop_index(batch_op.f("ix_opt_var_idx_association_indexset__id"))

    op.drop_table("opt_var_idx_association")
    op.drop_table("opt_var_docs")
    with op.batch_alter_table("opt_tab_idx_association", schema=None) as batch_op:
        batch_op.drop_index(batch_op.f("ix_opt_tab_idx_association_table__id"))
        batch_op.drop_index(batch_op.f("ix_opt_tab_idx_association_indexset__id"))

    op.drop_table("opt_tab_idx_association")
    op.drop_table("opt_tab_docs")
    op.drop_table("opt_sca_docs")
    with op.batch_alter_table("opt_par_idx_association", schema=None) as batch_op:
        batch_op.drop_index(batch_op.f("ix_opt_par_idx_association_parameter__id"))
        batch_op.drop_index(batch_op.f("ix_opt_par_idx_association_indexset__id"))

    op.drop_table("opt_par_idx_association")
    op.drop_table("opt_par_docs")
    op.drop_table("opt_idx_docs")
    with op.batch_alter_table("opt_idx_data", schema=None) as batch_op:
        batch_op.drop_index(batch_op.f("ix_opt_idx_data_indexset__id"))

    op.drop_table("opt_idx_data")
    with op.batch_alter_table("opt_equ_idx_association", schema=None) as batch_op:
        batch_op.drop_index(batch_op.f("ix_opt_equ_idx_association_indexset__id"))
        batch_op.drop_index(batch_op.f("ix_opt_equ_idx_association_equation__id"))

    op.drop_table("opt_equ_idx_association")
    op.drop_table("opt_equ_docs")
    with op.batch_alter_table("opt_var_version", schema=None) as batch_op:
        batch_op.drop_index(batch_op.f("ix_opt_var_version_transaction_id"))
        batch_op.drop_index(batch_op.f("ix_opt_var_version_run__id"))
        batch_op.drop_index(batch_op.f("ix_opt_var_version_operation_type"))
        batch_op.drop_index(batch_op.f("ix_opt_var_version_end_transaction_id"))

    op.drop_table("opt_var_version")
    with op.batch_alter_table(
        "opt_var_idx_association_version", schema=None
    ) as batch_op:
        batch_op.drop_index(
            batch_op.f("ix_opt_var_idx_association_version_variable__id")
        )
        batch_op.drop_index(
            batch_op.f("ix_opt_var_idx_association_version_transaction_id")
        )
        batch_op.drop_index(
            batch_op.f("ix_opt_var_idx_association_version_operation_type")
        )
        batch_op.drop_index(
            batch_op.f("ix_opt_var_idx_association_version_indexset__id")
        )
        batch_op.drop_index(
            batch_op.f("ix_opt_var_idx_association_version_end_transaction_id")
        )

    op.drop_table("opt_var_idx_association_version")
    with op.batch_alter_table("opt_tab_version", schema=None) as batch_op:
        batch_op.drop_index(batch_op.f("ix_opt_tab_version_transaction_id"))
        batch_op.drop_index(batch_op.f("ix_opt_tab_version_run__id"))
        batch_op.drop_index(batch_op.f("ix_opt_tab_version_operation_type"))
        batch_op.drop_index(batch_op.f("ix_opt_tab_version_end_transaction_id"))

    op.drop_table("opt_tab_version")
    with op.batch_alter_table(
        "opt_tab_idx_association_version", schema=None
    ) as batch_op:
        batch_op.drop_index(
            batch_op.f("ix_opt_tab_idx_association_version_transaction_id")
        )
        batch_op.drop_index(batch_op.f("ix_opt_tab_idx_association_version_table__id"))
        batch_op.drop_index(
            batch_op.f("ix_opt_tab_idx_association_version_operation_type")
        )
        batch_op.drop_index(
            batch_op.f("ix_opt_tab_idx_association_version_indexset__id")
        )
        batch_op.drop_index(
            batch_op.f("ix_opt_tab_idx_association_version_end_transaction_id")
        )

    op.drop_table("opt_tab_idx_association_version")
    with op.batch_alter_table("opt_sca_version", schema=None) as batch_op:
        batch_op.drop_index(batch_op.f("ix_opt_sca_version_unit__id"))
        batch_op.drop_index(batch_op.f("ix_opt_sca_version_transaction_id"))
        batch_op.drop_index(batch_op.f("ix_opt_sca_version_run__id"))
        batch_op.drop_index(batch_op.f("ix_opt_sca_version_operation_type"))
        batch_op.drop_index(batch_op.f("ix_opt_sca_version_end_transaction_id"))

    op.drop_table("opt_sca_version")
    with op.batch_alter_table("opt_par_version", schema=None) as batch_op:
        batch_op.drop_index(batch_op.f("ix_opt_par_version_transaction_id"))
        batch_op.drop_index(batch_op.f("ix_opt_par_version_run__id"))
        batch_op.drop_index(batch_op.f("ix_opt_par_version_operation_type"))
        batch_op.drop_index(batch_op.f("ix_opt_par_version_end_transaction_id"))

    op.drop_table("opt_par_version")
    with op.batch_alter_table(
        "opt_par_idx_association_version", schema=None
    ) as batch_op:
        batch_op.drop_index(
            batch_op.f("ix_opt_par_idx_association_version_transaction_id")
        )
        batch_op.drop_index(
            batch_op.f("ix_opt_par_idx_association_version_parameter__id")
        )
        batch_op.drop_index(
            batch_op.f("ix_opt_par_idx_association_version_operation_type")
        )
        batch_op.drop_index(
            batch_op.f("ix_opt_par_idx_association_version_indexset__id")
        )
        batch_op.drop_index(
            batch_op.f("ix_opt_par_idx_association_version_end_transaction_id")
        )

    op.drop_table("opt_par_idx_association_version")
    with op.batch_alter_table("opt_idx_version", schema=None) as batch_op:
        batch_op.drop_index(batch_op.f("ix_opt_idx_version_transaction_id"))
        batch_op.drop_index(batch_op.f("ix_opt_idx_version_run__id"))
        batch_op.drop_index(batch_op.f("ix_opt_idx_version_operation_type"))
        batch_op.drop_index(batch_op.f("ix_opt_idx_version_end_transaction_id"))

    op.drop_table("opt_idx_version")
    with op.batch_alter_table("opt_idx_data_version", schema=None) as batch_op:
        batch_op.drop_index(batch_op.f("ix_opt_idx_data_version_transaction_id"))
        batch_op.drop_index(batch_op.f("ix_opt_idx_data_version_operation_type"))
        batch_op.drop_index(batch_op.f("ix_opt_idx_data_version_indexset__id"))
        batch_op.drop_index(batch_op.f("ix_opt_idx_data_version_end_transaction_id"))

    op.drop_table("opt_idx_data_version")
    with op.batch_alter_table("opt_equ_version", schema=None) as batch_op:
        batch_op.drop_index(batch_op.f("ix_opt_equ_version_transaction_id"))
        batch_op.drop_index(batch_op.f("ix_opt_equ_version_run__id"))
        batch_op.drop_index(batch_op.f("ix_opt_equ_version_operation_type"))
        batch_op.drop_index(batch_op.f("ix_opt_equ_version_end_transaction_id"))

    op.drop_table("opt_equ_version")
    with op.batch_alter_table(
        "opt_equ_idx_association_version", schema=None
    ) as batch_op:
        batch_op.drop_index(
            batch_op.f("ix_opt_equ_idx_association_version_transaction_id")
        )
        batch_op.drop_index(
            batch_op.f("ix_opt_equ_idx_association_version_operation_type")
        )
        batch_op.drop_index(
            batch_op.f("ix_opt_equ_idx_association_version_indexset__id")
        )
        batch_op.drop_index(
            batch_op.f("ix_opt_equ_idx_association_version_equation__id")
        )
        batch_op.drop_index(
            batch_op.f("ix_opt_equ_idx_association_version_end_transaction_id")
        )

    op.drop_table("opt_equ_idx_association_version")
    with op.batch_alter_table("opt_var", schema=None) as batch_op:
        batch_op.drop_index(batch_op.f("ix_opt_var_run__id"))

    op.drop_table("opt_var")
    with op.batch_alter_table("opt_tab", schema=None) as batch_op:
        batch_op.drop_index(batch_op.f("ix_opt_tab_run__id"))

    op.drop_table("opt_tab")
    with op.batch_alter_table("opt_sca", schema=None) as batch_op:
        batch_op.drop_index(batch_op.f("ix_opt_sca_unit__id"))
        batch_op.drop_index(batch_op.f("ix_opt_sca_run__id"))

    op.drop_table("opt_sca")
    with op.batch_alter_table("opt_par", schema=None) as batch_op:
        batch_op.drop_index(batch_op.f("ix_opt_par_run__id"))

    op.drop_table("opt_par")
    with op.batch_alter_table("opt_equ", schema=None) as batch_op:
        batch_op.drop_index(batch_op.f("ix_opt_equ_run__id"))

    op.drop_table("opt_equ")
    with op.batch_alter_table("opt_idx", schema=None) as batch_op:
        batch_op.drop_index(batch_op.f("ix_opt_idx_run__id"))

    op.drop_table("opt_idx")
    # ### end Alembic commands ###
