0006_conversations.py 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import sqlalchemy as sa
  2. from alembic import op
  3. from sqlalchemy.dialects.mysql import BIGINT, BINARY, DATETIME
  4. revision = "0006_conversations"
  5. down_revision = "0005_agent_runtime"
  6. branch_labels = None
  7. depends_on = None
  8. def identifier(name, primary=False, nullable=False):
  9. return sa.Column(name, BINARY(16), primary_key=primary, nullable=nullable)
  10. def upgrade():
  11. op.create_table(
  12. "conversations",
  13. identifier("id", True),
  14. identifier("tenant_id"),
  15. identifier("store_id"),
  16. identifier("subject_id"),
  17. sa.Column("request_key", sa.String(64, collation="utf8mb4_bin"), nullable=False),
  18. sa.Column("control_owner", sa.String(16), nullable=False),
  19. sa.Column("control_version", BIGINT(unsigned=True), nullable=False),
  20. identifier("assigned_staff_id", nullable=True),
  21. identifier("active_run_id", nullable=True),
  22. sa.Column("sequence", BIGINT(unsigned=True), nullable=False),
  23. sa.Column("created_at", DATETIME(fsp=6), nullable=False),
  24. sa.UniqueConstraint("tenant_id", "id", name="uq_conversation_tenant_id"),
  25. sa.UniqueConstraint(
  26. "tenant_id", "subject_id", "request_key", name="uq_conversation_request"
  27. ),
  28. sa.ForeignKeyConstraint(["tenant_id", "store_id"], ["stores.tenant_id", "stores.id"]),
  29. sa.ForeignKeyConstraint(
  30. ["tenant_id", "subject_id"], ["principals.tenant_id", "principals.id"]
  31. ),
  32. sa.CheckConstraint(
  33. "control_owner IN ('AGENT','HUMAN')", name=op.f("ck_conversations_control_owner")
  34. ),
  35. )
  36. op.create_table(
  37. "conversation_events",
  38. identifier("tenant_id", True),
  39. identifier("conversation_id", True),
  40. sa.Column("sequence", BIGINT(unsigned=True), primary_key=True),
  41. identifier("actor_id"),
  42. sa.Column("command_key", sa.String(64, collation="utf8mb4_bin"), nullable=True),
  43. sa.Column("request_hash", BINARY(32), nullable=True),
  44. sa.Column("event_type", sa.String(64), nullable=False),
  45. sa.Column("control_version", BIGINT(unsigned=True), nullable=False),
  46. sa.Column("payload", sa.JSON(), nullable=False),
  47. sa.Column("created_at", DATETIME(fsp=6), nullable=False),
  48. sa.ForeignKeyConstraint(
  49. ["tenant_id", "conversation_id"], ["conversations.tenant_id", "conversations.id"]
  50. ),
  51. sa.UniqueConstraint(
  52. "tenant_id",
  53. "conversation_id",
  54. "actor_id",
  55. "command_key",
  56. name="uq_conversation_command",
  57. ),
  58. )
  59. op.add_column("agent_runs", identifier("conversation_id", nullable=True))
  60. op.add_column("agent_runs", sa.Column("control_version", BIGINT(unsigned=True), nullable=True))
  61. op.create_foreign_key(
  62. "fk_agent_runs_tenant_id_conversations",
  63. "agent_runs",
  64. "conversations",
  65. ["tenant_id", "conversation_id"],
  66. ["tenant_id", "id"],
  67. )
  68. def downgrade():
  69. op.drop_constraint("fk_agent_runs_tenant_id_conversations", "agent_runs", type_="foreignkey")
  70. op.drop_column("agent_runs", "control_version")
  71. op.drop_column("agent_runs", "conversation_id")
  72. op.drop_table("conversation_events")
  73. op.drop_table("conversations")