84 lines
3.3 KiB
Python
84 lines
3.3 KiB
Python
"""Create chat_token_usage table
|
|
|
|
Revision ID: cba70ab35793
|
|
Revises: cd27562afa2e
|
|
Create Date: 2026-04-16 13:39:46.842540
|
|
|
|
"""
|
|
from typing import Sequence, Union
|
|
|
|
from alembic import op
|
|
import sqlalchemy as sa
|
|
|
|
|
|
# revision identifiers, used by Alembic.
|
|
revision: str = 'cba70ab35793'
|
|
down_revision: Union[str, None] = 'cd27562afa2e'
|
|
branch_labels: Union[str, Sequence[str], None] = None
|
|
depends_on: Union[str, Sequence[str], None] = None
|
|
|
|
|
|
# (index_name, columns, unique, use_naming_convention)
|
|
# use_naming_convention=True wraps in batch_op.f(); False uses the raw string
|
|
# (matches the original auto-generated/hand-written split).
|
|
_INDEXES = [
|
|
('ix_chat_token_usage_chat_id', ['chat_id'], False, True),
|
|
('ix_chat_token_usage_document_id', ['document_id'], False, True),
|
|
('ix_chat_token_usage_id', ['id'], False, True),
|
|
('ix_chat_token_usage_model_name', ['model_name'], False, True),
|
|
('ix_chat_token_usage_model_time', ['model_name', 'timestamp'], False, False),
|
|
('ix_chat_token_usage_phase', ['phase'], False, True),
|
|
('ix_chat_token_usage_timestamp', ['timestamp'], False, True),
|
|
('ix_chat_token_usage_user_id', ['user_id'], False, True),
|
|
('ix_chat_token_usage_user_time', ['user_id', 'timestamp'], False, False),
|
|
]
|
|
|
|
|
|
def _index_names(bind, table_name: str) -> set[str]:
|
|
inspector = sa.inspect(bind)
|
|
return {idx["name"] for idx in inspector.get_indexes(table_name)}
|
|
|
|
|
|
def upgrade() -> None:
|
|
bind = op.get_bind()
|
|
inspector = sa.inspect(bind)
|
|
|
|
if not inspector.has_table('chat_token_usage'):
|
|
op.create_table('chat_token_usage',
|
|
sa.Column('id', sa.Integer(), nullable=False),
|
|
sa.Column('user_id', sa.Integer(), nullable=False),
|
|
sa.Column('chat_id', sa.Integer(), nullable=True),
|
|
sa.Column('document_id', sa.String(length=255), nullable=True),
|
|
sa.Column('prompt_tokens', sa.BigInteger(), nullable=False),
|
|
sa.Column('completion_tokens', sa.BigInteger(), nullable=False),
|
|
sa.Column('total_tokens', sa.BigInteger(), nullable=False),
|
|
sa.Column('model_name', sa.String(length=100), nullable=False),
|
|
sa.Column('cost_usd', sa.DECIMAL(precision=12, scale=8), nullable=False),
|
|
sa.Column('timestamp', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
|
|
sa.Column('phase', sa.String(length=50), nullable=True),
|
|
sa.ForeignKeyConstraint(['user_id'], ['users.id'], ondelete='CASCADE'),
|
|
sa.PrimaryKeyConstraint('id')
|
|
)
|
|
|
|
existing = _index_names(bind, 'chat_token_usage')
|
|
with op.batch_alter_table('chat_token_usage', schema=None) as batch_op:
|
|
for name, cols, unique, use_f in _INDEXES:
|
|
if name not in existing:
|
|
ref = batch_op.f(name) if use_f else name
|
|
batch_op.create_index(ref, cols, unique=unique)
|
|
|
|
|
|
def downgrade() -> None:
|
|
bind = op.get_bind()
|
|
inspector = sa.inspect(bind)
|
|
|
|
if inspector.has_table('chat_token_usage'):
|
|
existing = _index_names(bind, 'chat_token_usage')
|
|
with op.batch_alter_table('chat_token_usage', schema=None) as batch_op:
|
|
for name, _, _, use_f in reversed(_INDEXES):
|
|
if name in existing:
|
|
ref = batch_op.f(name) if use_f else name
|
|
batch_op.drop_index(ref)
|
|
|
|
op.drop_table('chat_token_usage')
|