"""add graph_schemas graph_tables graph_files tables

Revision ID: c60644793c3b
Revises: c52984bde339
Create Date: 2025-10-23 01:26:55.483786

"""

import sqlalchemy as sa
from alembic import op
from sqlalchemy.dialects import postgresql

# revision identifiers, used by Alembic.
revision = "c60644793c3b"
down_revision = "c52984bde339"
branch_labels = None
depends_on = None


def upgrade() -> None:
  # ### commands auto generated by Alembic - please adjust! ###
  op.create_table(
    "graph_schemas",
    sa.Column("id", sa.String(), nullable=False),
    sa.Column("graph_id", sa.String(), nullable=False),
    sa.Column("schema_type", sa.String(), nullable=False),
    sa.Column("schema_version", sa.Integer(), nullable=False),
    sa.Column("schema_ddl", sa.Text(), nullable=False),
    sa.Column("schema_json", postgresql.JSONB(astext_type=sa.Text()), nullable=True),
    sa.Column("custom_schema_name", sa.String(), nullable=True),
    sa.Column("custom_schema_version", sa.String(), nullable=True),
    sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
    sa.Column("is_active", sa.Boolean(), nullable=False),
    sa.ForeignKeyConstraint(["graph_id"], ["graphs.graph_id"], ondelete="CASCADE"),
    sa.PrimaryKeyConstraint("id"),
  )
  op.create_index(
    "idx_graph_schemas_active", "graph_schemas", ["is_active"], unique=False
  )
  op.create_index(
    "idx_graph_schemas_graph_id", "graph_schemas", ["graph_id"], unique=False
  )
  op.create_index(
    "idx_graph_schemas_type", "graph_schemas", ["schema_type"], unique=False
  )
  op.create_index(
    op.f("ix_graph_schemas_graph_id"), "graph_schemas", ["graph_id"], unique=False
  )
  op.create_table(
    "graph_tables",
    sa.Column("id", sa.String(), nullable=False),
    sa.Column("graph_id", sa.String(), nullable=False),
    sa.Column("table_name", sa.String(), nullable=False),
    sa.Column("table_type", sa.String(), nullable=False),
    sa.Column("schema_json", postgresql.JSONB(astext_type=sa.Text()), nullable=False),
    sa.Column("target_node_type", sa.String(), nullable=True),
    sa.Column("row_count", sa.Integer(), nullable=True),
    sa.Column("file_count", sa.Integer(), nullable=True),
    sa.Column("total_size_bytes", sa.Integer(), nullable=True),
    sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
    sa.Column("updated_at", sa.DateTime(timezone=True), nullable=True),
    sa.ForeignKeyConstraint(["graph_id"], ["graphs.graph_id"], ondelete="CASCADE"),
    sa.PrimaryKeyConstraint("id"),
    sa.UniqueConstraint("graph_id", "table_name", name="unique_graph_table"),
  )
  op.create_index(
    "idx_graph_tables_graph_id", "graph_tables", ["graph_id"], unique=False
  )
  op.create_index("idx_graph_tables_type", "graph_tables", ["table_type"], unique=False)
  op.create_index(
    op.f("ix_graph_tables_graph_id"), "graph_tables", ["graph_id"], unique=False
  )
  op.create_table(
    "graph_files",
    sa.Column("id", sa.String(), nullable=False),
    sa.Column("graph_id", sa.String(), nullable=False),
    sa.Column("table_id", sa.String(), nullable=False),
    sa.Column("file_name", sa.String(), nullable=False),
    sa.Column("s3_key", sa.String(), nullable=False),
    sa.Column("file_format", sa.String(), nullable=False),
    sa.Column("file_size_bytes", sa.Integer(), nullable=False),
    sa.Column("row_count", sa.Integer(), nullable=True),
    sa.Column("upload_status", sa.String(), nullable=False),
    sa.Column("upload_method", sa.String(), nullable=False),
    sa.Column("created_at", sa.DateTime(timezone=True), nullable=False),
    sa.Column("uploaded_at", sa.DateTime(timezone=True), nullable=True),
    sa.ForeignKeyConstraint(["graph_id"], ["graphs.graph_id"], ondelete="CASCADE"),
    sa.ForeignKeyConstraint(["table_id"], ["graph_tables.id"], ondelete="CASCADE"),
    sa.PrimaryKeyConstraint("id"),
  )
  op.create_index("idx_graph_files_graph_id", "graph_files", ["graph_id"], unique=False)
  op.create_index(
    "idx_graph_files_status", "graph_files", ["upload_status"], unique=False
  )
  op.create_index("idx_graph_files_table_id", "graph_files", ["table_id"], unique=False)
  op.create_index(
    op.f("ix_graph_files_graph_id"), "graph_files", ["graph_id"], unique=False
  )
  op.create_index(
    op.f("ix_graph_files_table_id"), "graph_files", ["table_id"], unique=False
  )
  # ### end Alembic commands ###


def downgrade() -> None:
  # ### commands auto generated by Alembic - please adjust! ###
  op.drop_index(op.f("ix_graph_files_table_id"), table_name="graph_files")
  op.drop_index(op.f("ix_graph_files_graph_id"), table_name="graph_files")
  op.drop_index("idx_graph_files_table_id", table_name="graph_files")
  op.drop_index("idx_graph_files_status", table_name="graph_files")
  op.drop_index("idx_graph_files_graph_id", table_name="graph_files")
  op.drop_table("graph_files")
  op.drop_index(op.f("ix_graph_tables_graph_id"), table_name="graph_tables")
  op.drop_index("idx_graph_tables_type", table_name="graph_tables")
  op.drop_index("idx_graph_tables_graph_id", table_name="graph_tables")
  op.drop_table("graph_tables")
  op.drop_index(op.f("ix_graph_schemas_graph_id"), table_name="graph_schemas")
  op.drop_index("idx_graph_schemas_type", table_name="graph_schemas")
  op.drop_index("idx_graph_schemas_graph_id", table_name="graph_schemas")
  op.drop_index("idx_graph_schemas_active", table_name="graph_schemas")
  op.drop_table("graph_schemas")
  # ### end Alembic commands ###
