feat: datenmodell für benutzer, sessions und login-versuche
- Tabellen users, sessions, login_attempts (Migration 0002) - UTCDateTime speichert UTC in SQLite und liefert aware datetimes - Tests legen das Schema über die echten Migrationen an - Test prüft, dass Migrationen und Modelle übereinstimmen Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
d9b7baf788
commit
d4cf8bdddd
12 files changed
+283
-11
No files matched your search
+13
-2
@@ -3,7 +3,7 @@ from logging.config import fileConfig
|
||||
from alembic import context
|
||||
|
||||
from app.db import get_engine
|
||||
from app.models import Base
|
||||
from app.models import Base, UTCDateTime
|
||||
|
||||
config = context.config
|
||||
|
||||
@@ -13,12 +13,20 @@ if config.config_file_name is not None:
|
||||
target_metadata = Base.metadata
|
||||
|
||||
|
||||
def render_item(type_, obj, autogen_context):
|
||||
"""Keep migrations independent of app code: render custom types as plain SQLAlchemy."""
|
||||
if type_ == "type" and isinstance(obj, UTCDateTime):
|
||||
return "sa.DateTime()"
|
||||
return False
|
||||
|
||||
|
||||
def run_migrations_offline() -> None:
|
||||
context.configure(
|
||||
url=str(get_engine().url),
|
||||
target_metadata=target_metadata,
|
||||
literal_binds=True,
|
||||
render_as_batch=True,
|
||||
render_item=render_item,
|
||||
)
|
||||
with context.begin_transaction():
|
||||
context.run_migrations()
|
||||
@@ -28,7 +36,10 @@ def run_migrations_online() -> None:
|
||||
with get_engine().connect() as connection:
|
||||
# render_as_batch: SQLite needs table rebuilds for most ALTER operations
|
||||
context.configure(
|
||||
connection=connection, target_metadata=target_metadata, render_as_batch=True
|
||||
connection=connection,
|
||||
target_metadata=target_metadata,
|
||||
render_as_batch=True,
|
||||
render_item=render_item,
|
||||
)
|
||||
with context.begin_transaction():
|
||||
context.run_migrations()
|
||||
|
||||
@@ -0,0 +1,82 @@
|
||||
"""users sessions login attempts
|
||||
|
||||
Revision ID: 0002
|
||||
Revises: 0001
|
||||
Create Date: 2026-10-25 09:52:16.653472
|
||||
|
||||
"""
|
||||
|
||||
from collections.abc import Sequence
|
||||
|
||||
import sqlalchemy as sa
|
||||
from alembic import op
|
||||
|
||||
revision: str = "0002"
|
||||
down_revision: str | None = "0001"
|
||||
branch_labels: str | Sequence[str] | None = None
|
||||
depends_on: str | Sequence[str] | None = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
op.create_table(
|
||||
"login_attempts",
|
||||
sa.Column("id", sa.Integer(), nullable=False),
|
||||
sa.Column("username", sa.String(length=255), nullable=False),
|
||||
sa.Column("ip", sa.String(length=45), nullable=True),
|
||||
sa.Column("attempted_at", sa.DateTime(), nullable=False),
|
||||
sa.Column("success", sa.Boolean(), nullable=False),
|
||||
sa.PrimaryKeyConstraint("id"),
|
||||
)
|
||||
with op.batch_alter_table("login_attempts", schema=None) as batch_op:
|
||||
batch_op.create_index("ix_login_attempts_ip_time", ["ip", "attempted_at"], unique=False)
|
||||
batch_op.create_index(
|
||||
"ix_login_attempts_username_time", ["username", "attempted_at"], unique=False
|
||||
)
|
||||
|
||||
op.create_table(
|
||||
"users",
|
||||
sa.Column("id", sa.Integer(), nullable=False),
|
||||
sa.Column("username", sa.String(length=32), nullable=False),
|
||||
sa.Column("password_hash", sa.String(length=255), nullable=False),
|
||||
sa.Column(
|
||||
"role",
|
||||
sa.Enum("admin", "user", name="role", native_enum=False, length=16),
|
||||
nullable=False,
|
||||
),
|
||||
sa.Column("is_active", sa.Boolean(), nullable=False),
|
||||
sa.Column("created_at", sa.DateTime(), nullable=False),
|
||||
sa.Column("updated_at", sa.DateTime(), nullable=False),
|
||||
sa.Column("password_changed_at", sa.DateTime(), nullable=False),
|
||||
sa.PrimaryKeyConstraint("id"),
|
||||
sa.UniqueConstraint("username"),
|
||||
)
|
||||
op.create_table(
|
||||
"sessions",
|
||||
sa.Column("id", sa.Integer(), nullable=False),
|
||||
sa.Column("token_hash", sa.String(length=64), nullable=False),
|
||||
sa.Column("csrf_token", sa.String(length=64), nullable=False),
|
||||
sa.Column("user_id", sa.Integer(), nullable=False),
|
||||
sa.Column("created_at", sa.DateTime(), nullable=False),
|
||||
sa.Column("last_seen_at", sa.DateTime(), nullable=False),
|
||||
sa.Column("expires_at", sa.DateTime(), nullable=False),
|
||||
sa.Column("ip", sa.String(length=45), nullable=True),
|
||||
sa.Column("user_agent", sa.String(length=255), nullable=True),
|
||||
sa.ForeignKeyConstraint(["user_id"], ["users.id"], ondelete="CASCADE"),
|
||||
sa.PrimaryKeyConstraint("id"),
|
||||
sa.UniqueConstraint("token_hash"),
|
||||
)
|
||||
with op.batch_alter_table("sessions", schema=None) as batch_op:
|
||||
batch_op.create_index(batch_op.f("ix_sessions_user_id"), ["user_id"], unique=False)
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
with op.batch_alter_table("sessions", schema=None) as batch_op:
|
||||
batch_op.drop_index(batch_op.f("ix_sessions_user_id"))
|
||||
|
||||
op.drop_table("sessions")
|
||||
op.drop_table("users")
|
||||
with op.batch_alter_table("login_attempts", schema=None) as batch_op:
|
||||
batch_op.drop_index("ix_login_attempts_username_time")
|
||||
batch_op.drop_index("ix_login_attempts_ip_time")
|
||||
|
||||
op.drop_table("login_attempts")
|
||||
Reference in new issue
Block a user