| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- import sqlalchemy as sa
- from alembic import op
- from sqlalchemy.dialects.mysql import BIGINT, BINARY, DATETIME
- revision = "0006_conversations"
- down_revision = "0005_agent_runtime"
- branch_labels = None
- depends_on = None
- def identifier(name, primary=False, nullable=False):
- return sa.Column(name, BINARY(16), primary_key=primary, nullable=nullable)
- def upgrade():
- op.create_table(
- "conversations",
- identifier("id", True),
- identifier("tenant_id"),
- identifier("store_id"),
- identifier("subject_id"),
- sa.Column("request_key", sa.String(64, collation="utf8mb4_bin"), nullable=False),
- sa.Column("control_owner", sa.String(16), nullable=False),
- sa.Column("control_version", BIGINT(unsigned=True), nullable=False),
- identifier("assigned_staff_id", nullable=True),
- identifier("active_run_id", nullable=True),
- sa.Column("sequence", BIGINT(unsigned=True), nullable=False),
- sa.Column("created_at", DATETIME(fsp=6), nullable=False),
- sa.UniqueConstraint("tenant_id", "id", name="uq_conversation_tenant_id"),
- sa.UniqueConstraint(
- "tenant_id", "subject_id", "request_key", name="uq_conversation_request"
- ),
- sa.ForeignKeyConstraint(["tenant_id", "store_id"], ["stores.tenant_id", "stores.id"]),
- sa.ForeignKeyConstraint(
- ["tenant_id", "subject_id"], ["principals.tenant_id", "principals.id"]
- ),
- sa.CheckConstraint(
- "control_owner IN ('AGENT','HUMAN')", name=op.f("ck_conversations_control_owner")
- ),
- )
- op.create_table(
- "conversation_events",
- identifier("tenant_id", True),
- identifier("conversation_id", True),
- sa.Column("sequence", BIGINT(unsigned=True), primary_key=True),
- identifier("actor_id"),
- sa.Column("command_key", sa.String(64, collation="utf8mb4_bin"), nullable=True),
- sa.Column("request_hash", BINARY(32), nullable=True),
- sa.Column("event_type", sa.String(64), nullable=False),
- sa.Column("control_version", BIGINT(unsigned=True), nullable=False),
- sa.Column("payload", sa.JSON(), nullable=False),
- sa.Column("created_at", DATETIME(fsp=6), nullable=False),
- sa.ForeignKeyConstraint(
- ["tenant_id", "conversation_id"], ["conversations.tenant_id", "conversations.id"]
- ),
- sa.UniqueConstraint(
- "tenant_id",
- "conversation_id",
- "actor_id",
- "command_key",
- name="uq_conversation_command",
- ),
- )
- op.add_column("agent_runs", identifier("conversation_id", nullable=True))
- op.add_column("agent_runs", sa.Column("control_version", BIGINT(unsigned=True), nullable=True))
- op.create_foreign_key(
- "fk_agent_runs_tenant_id_conversations",
- "agent_runs",
- "conversations",
- ["tenant_id", "conversation_id"],
- ["tenant_id", "id"],
- )
- def downgrade():
- op.drop_constraint("fk_agent_runs_tenant_id_conversations", "agent_runs", type_="foreignkey")
- op.drop_column("agent_runs", "control_version")
- op.drop_column("agent_runs", "conversation_id")
- op.drop_table("conversation_events")
- op.drop_table("conversations")
|