mirror of
https://github.com/jhd3197/ServerKit.git
synced 2026-12-05 06:31:46 +09:00
On an existing install the startup schema sync runs before alembic and added users.auth_version as a bare nullable TEXT, so migration 097 saw the column and skipped its '0' default. Every pre-existing user then carried a NULL auth_version that no JWT claim could match: 2FA verification and plain logins both failed with "Invalid or expired token" (reproduced on the builditdesign test box after `update.sh --branch dev`). - _fix_missing_columns now renders a literal server_default as a SQLite DEFAULT, so pre-alembic column adds backfill existing rows. - migration 097 always backfills NULL/empty auth_version to '0'. - tests pin both paths plus the default rendering. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01LVRnRXqNMZagqQ3RTqG5pz
39 lines
1.7 KiB
Python
39 lines
1.7 KiB
Python
"""Persist the revocation epoch for all browser authentication tokens.
|
|
|
|
Existing JWTs lack this claim and intentionally require a fresh login.
|
|
"""
|
|
from alembic import op
|
|
import sqlalchemy as sa
|
|
|
|
revision = '097_user_auth_version'
|
|
down_revision = '096_index_all_fk_columns'
|
|
branch_labels = None
|
|
depends_on = None
|
|
|
|
|
|
def upgrade():
|
|
# The initial migration bootstraps from current model metadata, so fresh
|
|
# installs may already have the new schema by the time they reach here.
|
|
inspector = sa.inspect(op.get_bind())
|
|
columns = {column['name'] for column in inspector.get_columns('users')}
|
|
if 'auth_version' not in columns:
|
|
op.add_column('users', sa.Column('auth_version', sa.String(32),
|
|
nullable=False, server_default='0'))
|
|
# The startup schema sync may have added the column ahead of us as a bare
|
|
# nullable TEXT (no default). A NULL auth_version can never match a token
|
|
# claim, which locks every existing user out — always backfill.
|
|
op.execute("UPDATE users SET auth_version = '0' "
|
|
"WHERE auth_version IS NULL OR auth_version = ''")
|
|
if 'revoked_sessions' not in inspector.get_table_names():
|
|
op.create_table('revoked_sessions',
|
|
sa.Column('session_id', sa.String(32), primary_key=True),
|
|
sa.Column('user_id', sa.Integer(), sa.ForeignKey('users.id', ondelete='CASCADE'), nullable=False),
|
|
sa.Column('revoked_at', sa.DateTime(), nullable=False))
|
|
op.create_index('ix_revoked_sessions_user_id', 'revoked_sessions', ['user_id'])
|
|
|
|
|
|
def downgrade():
|
|
op.drop_table('revoked_sessions')
|
|
with op.batch_alter_table('users') as batch:
|
|
batch.drop_column('auth_version')
|