"""Add stack/component env and secrets [a71cca37e42d].

Revision ID: a71cca37e42d
Revises: 0.85.0
Create Date: 2025-05-08 09:50:39.261042

"""

import sqlalchemy as sa
import sqlmodel
from alembic import op

# revision identifiers, used by Alembic.
revision = "a71cca37e42d"
down_revision = "0.85.0"
branch_labels = None
depends_on = None


def upgrade() -> None:
    """Upgrade database schema and/or data, creating a new revision."""
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        "secret_resource",
        sa.Column("id", sqlmodel.sql.sqltypes.GUID(), nullable=False),
        sa.Column("created", sa.DateTime(), nullable=False),
        sa.Column("updated", sa.DateTime(), nullable=False),
        sa.Column("secret_id", sqlmodel.sql.sqltypes.GUID(), nullable=False),
        sa.Column("resource_id", sqlmodel.sql.sqltypes.GUID(), nullable=False),
        sa.Column("resource_type", sa.VARCHAR(length=255), nullable=False),
        sa.ForeignKeyConstraint(
            ["secret_id"],
            ["secret.id"],
            name="fk_secret_resource_secret_id_secret",
            ondelete="CASCADE",
        ),
        sa.PrimaryKeyConstraint("id"),
    )
    with op.batch_alter_table("secret_resource", schema=None) as batch_op:
        batch_op.create_index(
            "ix_secret_resource_resource_id_resource_type_secret_id",
            ["resource_id", "resource_type", "secret_id"],
            unique=True,
        )

    with op.batch_alter_table("stack", schema=None) as batch_op:
        batch_op.add_column(
            sa.Column("environment", sa.LargeBinary(), nullable=True)
        )

    with op.batch_alter_table("stack_component", schema=None) as batch_op:
        batch_op.add_column(
            sa.Column("environment", sa.LargeBinary(), nullable=True)
        )

    # ### end Alembic commands ###


def downgrade() -> None:
    """Downgrade database schema and/or data back to the previous revision."""
    # ### commands auto generated by Alembic - please adjust! ###
    with op.batch_alter_table("stack_component", schema=None) as batch_op:
        batch_op.drop_column("environment")

    with op.batch_alter_table("stack", schema=None) as batch_op:
        batch_op.drop_column("environment")

    with op.batch_alter_table("secret_resource", schema=None) as batch_op:
        batch_op.drop_index(
            "ix_secret_resource_resource_id_resource_type_secret_id"
        )

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