From f1bfa27ba334905677dab1ae8f8e72b1fa8ec6d2 Mon Sep 17 00:00:00 2001 From: dbildhauer Date: Sun, 25 Oct 2026 10:48:57 +0100 Subject: [PATCH] =?UTF-8?q?feat:=20datenmodell=20f=C3=BCr=20clips?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Tabelle clips mit Art (text/code), Titel, Inhalt, Sprache, angeheftet, version - feste Sprachliste passend zu highlight.js (Migration 0003) Co-Authored-By: Claude Opus 5.5 --- .../alembic/versions/20261025_0003_clips.py | 50 +++++++++++++++++++ backend/app/languages.py | 28 +++++++++++ backend/app/models/__init__.py | 13 ++++- backend/app/models/clip.py | 33 ++++++++++++ 4 files changed, 123 insertions(+), 1 deletion(-) create mode 100644 backend/alembic/versions/20261025_0003_clips.py create mode 100644 backend/app/languages.py create mode 100644 backend/app/models/clip.py diff --git a/backend/alembic/versions/20261025_0003_clips.py b/backend/alembic/versions/20261025_0003_clips.py new file mode 100644 index 0000000..ded503e --- /dev/null +++ b/backend/alembic/versions/20261025_0003_clips.py @@ -0,0 +1,50 @@ +"""clips + +Revision ID: 0003 +Revises: 0002 +Create Date: 2026-10-25 10:48:45.833873 + +""" + +from collections.abc import Sequence + +import sqlalchemy as sa +from alembic import op + +revision: str = "0003" +down_revision: str | None = "0002" +branch_labels: str | Sequence[str] | None = None +depends_on: str | Sequence[str] | None = None + + +def upgrade() -> None: + op.create_table( + "clips", + sa.Column("id", sa.Integer(), nullable=False), + sa.Column("user_id", sa.Integer(), nullable=False), + sa.Column( + "kind", + sa.Enum("text", "code", name="clipkind", native_enum=False, length=16), + nullable=False, + ), + sa.Column("title", sa.String(length=200), nullable=True), + sa.Column("content", sa.Text(), nullable=False), + sa.Column("language", sa.String(length=32), nullable=True), + sa.Column("pinned", sa.Boolean(), nullable=False), + sa.Column("version", sa.Integer(), nullable=False), + sa.Column("created_at", sa.DateTime(), nullable=False), + sa.Column("updated_at", sa.DateTime(), nullable=False), + sa.ForeignKeyConstraint(["user_id"], ["users.id"], ondelete="CASCADE"), + sa.PrimaryKeyConstraint("id"), + ) + with op.batch_alter_table("clips", schema=None) as batch_op: + batch_op.create_index( + "ix_clips_user_order", ["user_id", "pinned", "updated_at"], unique=False + ) + + +def downgrade() -> None: + with op.batch_alter_table("clips", schema=None) as batch_op: + batch_op.drop_index("ix_clips_user_order") + + op.drop_table("clips") diff --git a/backend/app/languages.py b/backend/app/languages.py new file mode 100644 index 0000000..4e8c0b6 --- /dev/null +++ b/backend/app/languages.py @@ -0,0 +1,28 @@ +"""Languages available for code clips; ids match highlight.js language names.""" + +LANGUAGES: tuple[str, ...] = ( + "plaintext", + "bash", + "powershell", + "python", + "javascript", + "typescript", + "json", + "yaml", + "ini", + "xml", + "css", + "sql", + "markdown", + "dockerfile", + "nginx", + "go", + "rust", + "java", + "c", + "cpp", + "csharp", + "php", + "ruby", + "diff", +) diff --git a/backend/app/models/__init__.py b/backend/app/models/__init__.py index 35eeb71..2bca2b1 100644 --- a/backend/app/models/__init__.py +++ b/backend/app/models/__init__.py @@ -1,6 +1,17 @@ from app.models.base import Base, UTCDateTime, utcnow +from app.models.clip import Clip, ClipKind from app.models.login_attempt import LoginAttempt from app.models.session import UserSession from app.models.user import Role, User -__all__ = ["Base", "LoginAttempt", "Role", "UTCDateTime", "User", "UserSession", "utcnow"] +__all__ = [ + "Base", + "Clip", + "ClipKind", + "LoginAttempt", + "Role", + "UTCDateTime", + "User", + "UserSession", + "utcnow", +] diff --git a/backend/app/models/clip.py b/backend/app/models/clip.py new file mode 100644 index 0000000..73bb0f9 --- /dev/null +++ b/backend/app/models/clip.py @@ -0,0 +1,33 @@ +from __future__ import annotations + +import enum +from datetime import datetime + +from sqlalchemy import Enum, ForeignKey, Index, String, Text +from sqlalchemy.orm import Mapped, mapped_column + +from app.models.base import Base, UTCDateTime, utcnow + + +class ClipKind(enum.StrEnum): + TEXT = "text" + CODE = "code" + + +class Clip(Base): + __tablename__ = "clips" + __table_args__ = (Index("ix_clips_user_order", "user_id", "pinned", "updated_at"),) + + id: Mapped[int] = mapped_column(primary_key=True) + user_id: Mapped[int] = mapped_column(ForeignKey("users.id", ondelete="CASCADE")) + kind: Mapped[ClipKind] = mapped_column( + Enum(ClipKind, native_enum=False, length=16, values_callable=lambda e: [m.value for m in e]) + ) + title: Mapped[str | None] = mapped_column(String(200)) + content: Mapped[str] = mapped_column(Text) + language: Mapped[str | None] = mapped_column(String(32)) + pinned: Mapped[bool] = mapped_column(default=False) + # Incremented on every change; clients send it back to detect concurrent edits + version: Mapped[int] = mapped_column(default=1) + created_at: Mapped[datetime] = mapped_column(UTCDateTime, default=utcnow) + updated_at: Mapped[datetime] = mapped_column(UTCDateTime, default=utcnow)