# SPDX-FileCopyrightText: 2023-2024 MTS (Mobile Telesystems)
# SPDX-License-Identifier: Apache-2.0
"""Add hwm table

Revision ID: edd2e353ca38
Revises: e29b66594970
Create Date: 2023-10-16 19:10:26.499633

"""
import sqlalchemy as sa
from alembic import op

# revision identifiers, used by Alembic.
revision = "edd2e353ca38"
down_revision = "e29b66594970"
branch_labels = None
depends_on = None


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        "hwm",
        sa.Column("id", sa.BigInteger(), nullable=False),
        sa.Column("namespace_id", sa.BigInteger(), nullable=True),
        sa.Column("name", sa.String(length=256), nullable=False),
        sa.Column("description", sa.Text(), nullable=False),
        sa.Column("type", sa.String(length=64), nullable=False),
        sa.Column("value", sa.JSON(), nullable=False),
        sa.Column("entity", sa.Text(), nullable=True),
        sa.Column("expression", sa.Text(), nullable=True),
        sa.Column("changed_at", sa.DateTime(timezone=True), server_default=sa.text("now()"), nullable=False),
        sa.Column("changed_by_user_id", sa.BigInteger(), nullable=True),
        sa.Column("is_deleted", sa.Boolean(), nullable=False),
        sa.ForeignKeyConstraint(
            ["changed_by_user_id"],
            ["user.id"],
            name=op.f("fk__hwm__changed_by_user_id__user"),
            ondelete="SET NULL",
        ),
        sa.ForeignKeyConstraint(
            ["namespace_id"],
            ["namespace.id"],
            name=op.f("fk__hwm__namespace_id__namespace"),
            ondelete="CASCADE",
        ),
        sa.PrimaryKeyConstraint("id", name=op.f("pk__hwm")),
        sa.UniqueConstraint("namespace_id", "name", name="hwm_name_unique_per_namespace"),
    )
    op.create_index(op.f("ix__hwm__name"), "hwm", ["name"], unique=False)
    op.create_index(op.f("ix__hwm__namespace_id"), "hwm", ["namespace_id"], unique=False)
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_index(op.f("ix__hwm__namespace_id"), table_name="hwm")
    op.drop_index(op.f("ix__hwm__name"), table_name="hwm")
    op.drop_table("hwm")
    # ### end Alembic commands ###
