import sqlalchemy as sa from alembic import op from sqlalchemy.dialects.mysql import BIGINT, BINARY, DATETIME revision = "0007_knowledge_memory" down_revision = "0006_conversations" branch_labels = None depends_on = None def identifier(name, primary=False): return sa.Column(name, BINARY(16), primary_key=primary, nullable=False) def upgrade(): op.create_table( "knowledge_documents", identifier("tenant_id", True), identifier("id", True), identifier("store_id"), sa.Column("version", BIGINT(unsigned=True), nullable=False), sa.Column("status", sa.String(16), nullable=False), sa.Column("title", sa.String(200), nullable=False), sa.Column("chunks", sa.JSON(), nullable=False), sa.Column("content_hash", BINARY(32), nullable=False), sa.Column("updated_at", DATETIME(fsp=6), nullable=False), sa.ForeignKeyConstraint(["tenant_id", "store_id"], ["stores.tenant_id", "stores.id"]), ) op.create_index("ix_knowledge_documents_store_id", "knowledge_documents", ["store_id"]) op.create_table( "memory_profiles", identifier("tenant_id", True), identifier("subject_id", True), sa.Column("consent", sa.Boolean(), nullable=False), sa.Column("epoch", BIGINT(unsigned=True), nullable=False), sa.Column("revision", BIGINT(unsigned=True), nullable=False), sa.Column("value_key", sa.String(32), nullable=True), sa.ForeignKeyConstraint( ["tenant_id", "subject_id"], ["principals.tenant_id", "principals.id"] ), ) op.create_table( "memory_jobs", identifier("id", True), identifier("tenant_id"), identifier("subject_id"), identifier("conversation_id"), sa.Column("sequence", BIGINT(unsigned=True), nullable=False), sa.Column("epoch", BIGINT(unsigned=True), nullable=False), sa.Column("token_hash", BINARY(32), nullable=False), sa.Column("status", sa.String(16), nullable=False), sa.Column("lease_token", BIGINT(unsigned=True), nullable=False), sa.Column("lease_until", DATETIME(fsp=6), nullable=True), sa.Column("created_at", DATETIME(fsp=6), nullable=False), sa.ForeignKeyConstraint( ["tenant_id", "subject_id"], ["memory_profiles.tenant_id", "memory_profiles.subject_id"] ), ) op.create_index("ix_memory_jobs_queue", "memory_jobs", ["status", "created_at", "id"]) def downgrade(): op.drop_table("memory_jobs") op.drop_table("memory_profiles") op.drop_table("knowledge_documents")