mirror of
https://github.com/langbot-app/LangBot.git
synced 2026-10-23 08:55:44 +08:00
refactor(persistence): consolidate monitoring index migrations
- Merge migrations 0039 through 0041 into a single 0039 migration - Preserve existing indexes and support partial historical schemas - Update migration tests for the consolidated revision
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
"""Bound host lease maintenance to active executions within each Workspace."""
|
||||
"""Index execution leases, conversation paging, and historical inline image expiry."""
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
@@ -8,26 +8,40 @@ down_revision = '0038_llm_call_details'
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
INDEXES = (
|
||||
('agent_run', 'ix_agent_run_workspace_status_lease', ['workspace_id', 'status', 'execution_lease_expires_at']),
|
||||
(
|
||||
'monitoring_messages',
|
||||
'ix_monitoring_messages_workspace_status_lease',
|
||||
['workspace_uuid', 'status', 'execution_lease_expires_at'],
|
||||
),
|
||||
(
|
||||
'monitoring_messages',
|
||||
'ix_monitoring_messages_conversation_page',
|
||||
['workspace_uuid', 'bot_id', 'session_id', 'timestamp', 'id'],
|
||||
),
|
||||
('monitoring_messages', 'ix_monitoring_messages_media_ttl', ['workspace_uuid', 'timestamp', 'id']),
|
||||
('event_log', 'ix_event_log_media_ttl', ['workspace_id', 'created_at', 'id']),
|
||||
('agent_run', 'ix_agent_run_media_ttl', ['workspace_id', 'created_at', 'id']),
|
||||
('agent_run_event', 'ix_agent_run_event_media_ttl', ['created_at', 'id', 'run_id']),
|
||||
('transcript', 'ix_transcript_media_ttl', ['workspace_id', 'created_at', 'id']),
|
||||
)
|
||||
|
||||
|
||||
def upgrade():
|
||||
inspector = sa.inspect(op.get_bind())
|
||||
for table, workspace_column in (
|
||||
('agent_run', 'workspace_id'),
|
||||
('monitoring_messages', 'workspace_uuid'),
|
||||
):
|
||||
for table, index, columns in INDEXES:
|
||||
if not inspector.has_table(table):
|
||||
continue
|
||||
index = f'ix_{table}_workspace_status_lease'
|
||||
# Fresh installations create these indexes from ORM metadata before migrations.
|
||||
if index not in {item['name'] for item in inspector.get_indexes(table)}:
|
||||
op.create_index(index, table, [workspace_column, 'status', 'execution_lease_expires_at'])
|
||||
op.create_index(index, table, columns)
|
||||
|
||||
|
||||
def downgrade():
|
||||
inspector = sa.inspect(op.get_bind())
|
||||
for table in ('agent_run', 'monitoring_messages'):
|
||||
for table, index, _ in reversed(INDEXES):
|
||||
if not inspector.has_table(table):
|
||||
continue
|
||||
index = f'ix_{table}_workspace_status_lease'
|
||||
if index in {item['name'] for item in inspector.get_indexes(table)}:
|
||||
op.drop_index(index, table_name=table)
|
||||
|
||||
@@ -1,25 +0,0 @@
|
||||
"""Page conversation identities without reading historical message payloads."""
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
revision = '0040_conversation_page_index'
|
||||
down_revision = '0039_execution_lease_indexes'
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
TABLE = 'monitoring_messages'
|
||||
INDEX = 'ix_monitoring_messages_conversation_page'
|
||||
|
||||
|
||||
def upgrade():
|
||||
inspector = sa.inspect(op.get_bind())
|
||||
# Fresh databases already receive this index from ORM metadata.
|
||||
if inspector.has_table(TABLE) and INDEX not in {index['name'] for index in inspector.get_indexes(TABLE)}:
|
||||
op.create_index(INDEX, TABLE, ['workspace_uuid', 'bot_id', 'session_id', 'timestamp', 'id'])
|
||||
|
||||
|
||||
def downgrade():
|
||||
inspector = sa.inspect(op.get_bind())
|
||||
if inspector.has_table(TABLE) and INDEX in {index['name'] for index in inspector.get_indexes(TABLE)}:
|
||||
op.drop_index(INDEX, table_name=TABLE)
|
||||
@@ -1,37 +0,0 @@
|
||||
"""Index incremental expiry of historical inline images."""
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
|
||||
revision = '0041_inline_media_ttl_indexes'
|
||||
down_revision = '0040_conversation_page_index'
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
INDEXES = (
|
||||
('monitoring_messages', ['workspace_uuid', 'timestamp', 'id']),
|
||||
('event_log', ['workspace_id', 'created_at', 'id']),
|
||||
('agent_run', ['workspace_id', 'created_at', 'id']),
|
||||
('agent_run_event', ['created_at', 'id', 'run_id']),
|
||||
('transcript', ['workspace_id', 'created_at', 'id']),
|
||||
)
|
||||
|
||||
|
||||
def upgrade():
|
||||
inspector = sa.inspect(op.get_bind())
|
||||
for table, columns in INDEXES:
|
||||
if not inspector.has_table(table):
|
||||
continue
|
||||
name = f'ix_{table}_media_ttl'
|
||||
if name not in {index['name'] for index in inspector.get_indexes(table)}:
|
||||
op.create_index(name, table, columns)
|
||||
|
||||
|
||||
def downgrade():
|
||||
inspector = sa.inspect(op.get_bind())
|
||||
for table, _ in reversed(INDEXES):
|
||||
if not inspector.has_table(table):
|
||||
continue
|
||||
name = f'ix_{table}_media_ttl'
|
||||
if name in {index['name'] for index in inspector.get_indexes(table)}:
|
||||
op.drop_index(name, table_name=table)
|
||||
Reference in New Issue
Block a user