"""Add Tables table

Revision ID: 4aade4315ef9
Revises: 4e07917dc4e8
Create Date: 2025-02-19 15:44:24.509578

"""

from collections.abc import Sequence

import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

from alembic import op

# revision identifiers, used by Alembic.
revision: str = "4aade4315ef9"
down_revision: str | None = "4e07917dc4e8"
branch_labels: str | Sequence[str] | None = None
depends_on: str | Sequence[str] | None = None


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        "tables",
        sa.Column(
            "created_at",
            sa.TIMESTAMP(timezone=True),
            server_default=sa.text("now()"),
            nullable=False,
        ),
        sa.Column(
            "updated_at",
            sa.TIMESTAMP(timezone=True),
            server_default=sa.text("now()"),
            nullable=False,
        ),
        sa.Column("surrogate_id", sa.Integer(), nullable=False),
        sa.Column("owner_id", postgresql.UUID(as_uuid=True), nullable=False),
        sa.Column("id", postgresql.UUID(as_uuid=True), nullable=False),
        sa.Column("name", sa.String(), nullable=False),
        sa.PrimaryKeyConstraint("surrogate_id", name="tables_pkey"),
        sa.UniqueConstraint("name", "owner_id", name="tables_name_owner_id_key"),
    )
    op.create_index(op.f("ix_tables_id"), "tables", ["id"], unique=True)
    op.create_index(op.f("ix_tables_name"), "tables", ["name"], unique=False)
    op.create_table(
        "table_columns",
        sa.Column(
            "created_at",
            sa.TIMESTAMP(timezone=True),
            server_default=sa.text("now()"),
            nullable=False,
        ),
        sa.Column(
            "updated_at",
            sa.TIMESTAMP(timezone=True),
            server_default=sa.text("now()"),
            nullable=False,
        ),
        sa.Column("id", postgresql.UUID(as_uuid=True), nullable=False),
        sa.Column("table_id", sa.UUID(), nullable=True),
        sa.Column("name", sa.String(), nullable=False),
        sa.Column("type", sa.String(), nullable=False),
        sa.Column("nullable", sa.Boolean(), nullable=False),
        sa.Column("default", postgresql.JSONB(astext_type=sa.Text()), nullable=True),
        sa.ForeignKeyConstraint(
            ["table_id"],
            ["tables.id"],
            ondelete="CASCADE",
            name="table_columns_table_id_fkey",
        ),
        sa.PrimaryKeyConstraint("id", name="table_columns_pkey"),
        sa.UniqueConstraint("table_id", "name", name="table_columns_table_id_name_key"),
    )
    op.create_index(op.f("ix_table_columns_id"), "table_columns", ["id"], unique=True)
    op.create_index(
        op.f("ix_table_columns_name"), "table_columns", ["name"], unique=False
    )
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_index(op.f("ix_table_columns_name"), table_name="table_columns")
    op.drop_index(op.f("ix_table_columns_id"), table_name="table_columns")
    op.drop_table("table_columns")
    op.drop_index(op.f("ix_tables_name"), table_name="tables")
    op.drop_index(op.f("ix_tables_id"), table_name="tables")
    op.drop_table("tables")
    # ### end Alembic commands ###
