"""v0.11.0.

Revision ID: 1a2098221d39
Revises: bf85fbba104c
Create Date: 2023-09-06 15:46:13.761054

"""
import sqlalchemy as sa  # noqa
import sqlmodel  # noqa
from alembic import op

from laminhub_rest.schema.migrations.rls._2023_09_06_1a2098221d39_v0_11_0 import (
    sql_drop_rls_laminapp_admin_instance,
    sql_rls_instance_account_db_user,
)

# revision identifiers, used by Alembic.
revision = "1a2098221d39"
down_revision = "bf85fbba104c"
branch_labels = None
depends_on = None


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        "account_instance_db_user",
        sa.Column("account_id", sqlmodel.sql.sqltypes.GUID(), nullable=False),
        sa.Column("instance_id", sqlmodel.sql.sqltypes.GUID(), nullable=False),
        sa.Column("db_user_id", sqlmodel.sql.sqltypes.GUID(), nullable=True),
        sa.Column(
            "created_at", sa.DateTime(), server_default=sa.text("now()"), nullable=False
        ),
        sa.Column("updated_at", sa.DateTime(), nullable=True),
        sa.ForeignKeyConstraint(
            ["account_id"],
            ["account.id"],
            name=op.f("fk_account_instance_db_user_account_id_account"),
            ondelete="CASCADE",
        ),
        sa.ForeignKeyConstraint(
            ["db_user_id"],
            ["db_user.id"],
            name=op.f("fk_account_instance_db_user_db_user_id_db_user"),
            ondelete="CASCADE",
        ),
        sa.ForeignKeyConstraint(
            ["instance_id"],
            ["instance.id"],
            name=op.f("fk_account_instance_db_user_instance_id_instance"),
            ondelete="CASCADE",
        ),
        sa.PrimaryKeyConstraint(
            "account_id", "instance_id", name=op.f("pk_account_instance_db_user")
        ),
    )
    op.create_index(
        op.f("ix_account_instance_db_user_account_id"),
        "account_instance_db_user",
        ["account_id"],
        unique=False,
    )
    op.create_index(
        op.f("ix_account_instance_db_user_created_at"),
        "account_instance_db_user",
        ["created_at"],
        unique=False,
    )
    op.create_index(
        op.f("ix_account_instance_db_user_db_user_id"),
        "account_instance_db_user",
        ["db_user_id"],
        unique=False,
    )
    op.create_index(
        op.f("ix_account_instance_db_user_instance_id"),
        "account_instance_db_user",
        ["instance_id"],
        unique=False,
    )
    op.create_index(
        op.f("ix_account_instance_db_user_updated_at"),
        "account_instance_db_user",
        ["updated_at"],
        unique=False,
    )
    # ### end Alembic commands ###
    op.execute(sql_rls_instance_account_db_user)


def downgrade() -> None:
    op.execute(sql_drop_rls_laminapp_admin_instance)
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_index(
        op.f("ix_account_instance_db_user_updated_at"),
        table_name="account_instance_db_user",
    )
    op.drop_index(
        op.f("ix_account_instance_db_user_instance_id"),
        table_name="account_instance_db_user",
    )
    op.drop_index(
        op.f("ix_account_instance_db_user_db_user_id"),
        table_name="account_instance_db_user",
    )
    op.drop_index(
        op.f("ix_account_instance_db_user_created_at"),
        table_name="account_instance_db_user",
    )
    op.drop_index(
        op.f("ix_account_instance_db_user_account_id"),
        table_name="account_instance_db_user",
    )
    op.drop_table("account_instance_db_user")
    # ### end Alembic commands ###
