Skip to content

Commit

Permalink
Add new Mention model
Browse files Browse the repository at this point in the history
  • Loading branch information
mtomilov committed Jan 20, 2025
1 parent e55727e commit f5c52f8
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 0 deletions.
41 changes: 41 additions & 0 deletions h/migrations/versions/39cc1025a3a2_create_mention_table.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
"""Add migration to create Mention table."""

import sqlalchemy as sa
from alembic import op

revision = "39cc1025a3a2"
down_revision = "78d3d6fe1d42"


def upgrade():
op.create_table(
"mention",
sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
sa.Column("annotation_id", sa.Integer(), nullable=False),
sa.Column("user_id", sa.Integer(), nullable=False),
sa.Column(
"created", sa.DateTime(), server_default=sa.text("now()"), nullable=False
),
sa.Column(
"updated", sa.DateTime(), server_default=sa.text("now()"), nullable=False
),
sa.ForeignKeyConstraint(
["annotation_id"],
["annotation_slim.id"],
name=op.f("fk__mention__annotation_id__annotation_slim"),
ondelete="CASCADE",
),
sa.ForeignKeyConstraint(
["user_id"],
["user.id"],
name=op.f("fk__mention__user_id__user"),
ondelete="CASCADE",
),
sa.PrimaryKeyConstraint("id", name=op.f("pk__mention")),
)
op.create_index(op.f("ix__mention_user_id"), "mention", ["user_id"], unique=False)


def downgrade():
op.drop_index(op.f("ix__mention_user_id"), table_name="mention")
op.drop_table("mention")
2 changes: 2 additions & 0 deletions h/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
from h.models.group import Group, GroupMembership, GroupMembershipRoles
from h.models.group_scope import GroupScope
from h.models.job import Job
from h.models.mention import Mention
from h.models.organization import Organization
from h.models.setting import Setting
from h.models.subscriptions import Subscriptions
Expand Down Expand Up @@ -60,6 +61,7 @@
"GroupMembership",
"GroupScope",
"Job",
"Mention",
"Organization",
"Setting",
"Subscriptions",
Expand Down
32 changes: 32 additions & 0 deletions h/models/mention.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import sqlalchemy as sa
from sqlalchemy.orm import Mapped, mapped_column

from h.db import Base
from h.db.mixins import Timestamps
from h.models import helpers


class Mention(Base, Timestamps): # pragma: nocover
__tablename__ = "mention"

id: Mapped[int] = mapped_column(sa.Integer, autoincrement=True, primary_key=True)

annotation_id: Mapped[int] = mapped_column(
sa.Integer,
sa.ForeignKey("annotation_slim.id", ondelete="CASCADE"),
nullable=False,
)
"""FK to annotation_slim.id"""
annotation = sa.orm.relationship("AnnotationSlim")

user_id: Mapped[int] = mapped_column(
sa.Integer,
sa.ForeignKey("user.id", ondelete="CASCADE"),
nullable=False,
index=True,
)
"""FK to user.id"""
user = sa.orm.relationship("User")

def __repr__(self) -> str:
return helpers.repr_(self, ["id", "annotation_id", "user_id"])

0 comments on commit f5c52f8

Please sign in to comment.