"""Remove project shared data

Revision ID: 9a7260e6a3bf
Revises: 2ce65db29472
Create Date: 2024-11-21 14:07:50.542424

"""

import sqlalchemy as sa
from alembic import op

# revision identifiers, used by Alembic.
revision = "9a7260e6a3bf"
down_revision = "4ca4e5e44ff2"
branch_labels = None
depends_on = None


project_data = sa.Table(
    "project_data",
    sa.MetaData(),
    sa.Column(
        "data_type",
        sa.Enum("geo_data", "generator", "global_data", name="datatype"),
        nullable=True,
    ),
    sa.Column("id", sa.Integer(), nullable=False),
    sa.Column("data_id", sa.Integer(), nullable=True),
    sa.Column("last_update", sa.DateTime(), nullable=True),
    sa.Column("project_id", sa.Integer(), nullable=False),
    sa.Column("description", sa.String(), nullable=True),
    sa.Column("name", sa.String(), nullable=True),
    sa.Column("active_model_id", sa.Integer(), nullable=True),
)


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_table("project_shared_data")

    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    project_shared_data = op.create_table(
        "project_shared_data",
        sa.Column(
            "project_id", sa.INTEGER(), autoincrement=False, nullable=False
        ),
        sa.Column(
            "project_data_id",
            sa.INTEGER(),
            autoincrement=False,
            nullable=False,
        ),
        sa.ForeignKeyConstraint(
            ["project_data_id"],
            ["project_data.id"],
            name="fk_project_shared_data_project_data_id_project_data",
        ),
        sa.ForeignKeyConstraint(
            ["project_id"],
            ["project.id"],
            name="fk_project_shared_data_project_id_project",
        ),
        sa.PrimaryKeyConstraint(
            "project_id", "project_data_id", name="pk_project_shared_data"
        ),
    )

    shared_data = sa.select(project_data.c["project_id", "id"]).where(
        project_data.c.data_type != "generator"
    )
    op.execute(
        project_shared_data.insert().from_select(
            names=["project_id", "project_data_id"], select=shared_data
        )
    )
    # ### end Alembic commands ###
