"""sessions

迁移 ID: b54b093a9ce3
父迁移: 74381965bbe2
创建时间: 2026-01-10 19:28:56.153350

"""

from __future__ import annotations

from collections.abc import Sequence

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

revision: str = "b54b093a9ce3"
down_revision: str | Sequence[str] | None = "74381965bbe2"
branch_labels: str | Sequence[str] | None = None
depends_on: str | Sequence[str] | None = None


def upgrade(name: str = "") -> None:
    if name:
        return
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        "suggarchat_memory_sessions",
        sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
        sa.Column("ins_id", sa.BigInteger(), nullable=False),
        sa.Column("is_group", sa.Boolean(), nullable=False),
        sa.Column("created_at", sa.Float(), nullable=False),
        sa.Column("data", sa.JSON(), server_default=sa.text("'{}'"), nullable=False),
        sa.ForeignKeyConstraint(
            ["ins_id"],
            ["suggarchat_memory_data.ins_id"],
            name=op.f("fk_suggarchat_memory_sessions_ins_id_suggarchat_memory_data"),
        ),
        sa.ForeignKeyConstraint(
            ["is_group"],
            ["suggarchat_memory_data.is_group"],
            name=op.f("fk_suggarchat_memory_sessions_is_group_suggarchat_memory_data"),
        ),
        sa.PrimaryKeyConstraint("id", name=op.f("pk_suggarchat_memory_sessions")),
        info={"bind_key": "chat"},
    )
    with op.batch_alter_table("suggarchat_memory_sessions", schema=None) as batch_op:
        batch_op.create_index("idx_sessions_created_at", ["created_at"], unique=False)
        batch_op.create_index("idx_sessions_ins_id", ["ins_id"], unique=False)
        batch_op.create_index("idx_sessions_is_group", ["is_group"], unique=False)

    with op.batch_alter_table("suggarchat_memory_data", schema=None) as batch_op:
        batch_op.drop_column("sessions_json")

    # ### end Alembic commands ###


def downgrade(name: str = "") -> None:
    if name:
        return
    # ### commands auto generated by Alembic - please adjust! ###
    with op.batch_alter_table("suggarchat_memory_data", schema=None) as batch_op:
        batch_op.add_column(
            sa.Column(
                "sessions_json",
                sqlite.JSON(),
                server_default=sa.text("'[]'"),
                nullable=False,
            )
        )

    with op.batch_alter_table("suggarchat_memory_sessions", schema=None) as batch_op:
        batch_op.drop_index("idx_sessions_is_group")
        batch_op.drop_index("idx_sessions_ins_id")
        batch_op.drop_index("idx_sessions_created_at")

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