0007_knowledge_memory.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import sqlalchemy as sa
  2. from alembic import op
  3. from sqlalchemy.dialects.mysql import BIGINT, BINARY, DATETIME
  4. revision = "0007_knowledge_memory"
  5. down_revision = "0006_conversations"
  6. branch_labels = None
  7. depends_on = None
  8. def identifier(name, primary=False):
  9. return sa.Column(name, BINARY(16), primary_key=primary, nullable=False)
  10. def upgrade():
  11. op.create_table(
  12. "knowledge_documents",
  13. identifier("tenant_id", True),
  14. identifier("id", True),
  15. identifier("store_id"),
  16. sa.Column("version", BIGINT(unsigned=True), nullable=False),
  17. sa.Column("status", sa.String(16), nullable=False),
  18. sa.Column("title", sa.String(200), nullable=False),
  19. sa.Column("chunks", sa.JSON(), nullable=False),
  20. sa.Column("content_hash", BINARY(32), nullable=False),
  21. sa.Column("updated_at", DATETIME(fsp=6), nullable=False),
  22. sa.ForeignKeyConstraint(["tenant_id", "store_id"], ["stores.tenant_id", "stores.id"]),
  23. )
  24. op.create_index("ix_knowledge_documents_store_id", "knowledge_documents", ["store_id"])
  25. op.create_table(
  26. "memory_profiles",
  27. identifier("tenant_id", True),
  28. identifier("subject_id", True),
  29. sa.Column("consent", sa.Boolean(), nullable=False),
  30. sa.Column("epoch", BIGINT(unsigned=True), nullable=False),
  31. sa.Column("revision", BIGINT(unsigned=True), nullable=False),
  32. sa.Column("value_key", sa.String(32), nullable=True),
  33. sa.ForeignKeyConstraint(
  34. ["tenant_id", "subject_id"], ["principals.tenant_id", "principals.id"]
  35. ),
  36. )
  37. op.create_table(
  38. "memory_jobs",
  39. identifier("id", True),
  40. identifier("tenant_id"),
  41. identifier("subject_id"),
  42. identifier("conversation_id"),
  43. sa.Column("sequence", BIGINT(unsigned=True), nullable=False),
  44. sa.Column("epoch", BIGINT(unsigned=True), nullable=False),
  45. sa.Column("token_hash", BINARY(32), nullable=False),
  46. sa.Column("status", sa.String(16), nullable=False),
  47. sa.Column("lease_token", BIGINT(unsigned=True), nullable=False),
  48. sa.Column("lease_until", DATETIME(fsp=6), nullable=True),
  49. sa.Column("created_at", DATETIME(fsp=6), nullable=False),
  50. sa.ForeignKeyConstraint(
  51. ["tenant_id", "subject_id"], ["memory_profiles.tenant_id", "memory_profiles.subject_id"]
  52. ),
  53. )
  54. op.create_index("ix_memory_jobs_queue", "memory_jobs", ["status", "created_at", "id"])
  55. def downgrade():
  56. op.drop_table("memory_jobs")
  57. op.drop_table("memory_profiles")
  58. op.drop_table("knowledge_documents")