This commit is contained in:
super committed 2026-03-29 11:45:36 +08:00
1 parent 7475828c63
commit fafcdfe742
1 file changed
+57
@@ -0,0 +1,57 @@
"""align chat_logs latency and tokens_used with models
Revision ID: c4e2d9f1a6bc
Revises: b10f2e38c9b1
Create Date: 2026-03-29 01:30:00.000000
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = 'c4e2d9f1a6bc'
down_revision = 'b10f2e38c9b1'
branch_labels = None
depends_on = None
def upgrade():
bind = op.get_bind()
dialect = bind.dialect.name
# Backfill NULL values before making tokens_used non-nullable.
op.execute(sa.text("UPDATE chat_logs SET tokens_used = 0 WHERE tokens_used IS NULL"))
if dialect == 'postgresql':
# Explicit cast for PostgreSQL when changing FLOAT -> INTEGER.
op.execute(
sa.text(
"ALTER TABLE chat_logs "
"ALTER COLUMN latency TYPE INTEGER USING ROUND(latency)::integer"
)
)
op.alter_column('chat_logs', 'tokens_used', existing_type=sa.Integer(), nullable=False)
else:
# SQLite and others: use batch mode for safe table rebuild.
with op.batch_alter_table('chat_logs', schema=None) as batch_op:
batch_op.alter_column('latency', existing_type=sa.Float(), type_=sa.Integer(), nullable=False)
batch_op.alter_column('tokens_used', existing_type=sa.Integer(), nullable=False)
def downgrade():
bind = op.get_bind()
dialect = bind.dialect.name
if dialect == 'postgresql':
op.execute(
sa.text(
"ALTER TABLE chat_logs "
"ALTER COLUMN latency TYPE DOUBLE PRECISION USING latency::double precision"
)
)
op.alter_column('chat_logs', 'tokens_used', existing_type=sa.Integer(), nullable=True)
else:
with op.batch_alter_table('chat_logs', schema=None) as batch_op:
batch_op.alter_column('latency', existing_type=sa.Integer(), type_=sa.Float(), nullable=False)
batch_op.alter_column('tokens_used', existing_type=sa.Integer(), nullable=True)