Skip to content

Commit f5c52f8

Browse files
committed
Add new Mention model
1 parent e55727e commit f5c52f8

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
"""Add migration to create Mention table."""
2+
3+
import sqlalchemy as sa
4+
from alembic import op
5+
6+
revision = "39cc1025a3a2"
7+
down_revision = "78d3d6fe1d42"
8+
9+
10+
def upgrade():
11+
op.create_table(
12+
"mention",
13+
sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
14+
sa.Column("annotation_id", sa.Integer(), nullable=False),
15+
sa.Column("user_id", sa.Integer(), nullable=False),
16+
sa.Column(
17+
"created", sa.DateTime(), server_default=sa.text("now()"), nullable=False
18+
),
19+
sa.Column(
20+
"updated", sa.DateTime(), server_default=sa.text("now()"), nullable=False
21+
),
22+
sa.ForeignKeyConstraint(
23+
["annotation_id"],
24+
["annotation_slim.id"],
25+
name=op.f("fk__mention__annotation_id__annotation_slim"),
26+
ondelete="CASCADE",
27+
),
28+
sa.ForeignKeyConstraint(
29+
["user_id"],
30+
["user.id"],
31+
name=op.f("fk__mention__user_id__user"),
32+
ondelete="CASCADE",
33+
),
34+
sa.PrimaryKeyConstraint("id", name=op.f("pk__mention")),
35+
)
36+
op.create_index(op.f("ix__mention_user_id"), "mention", ["user_id"], unique=False)
37+
38+
39+
def downgrade():
40+
op.drop_index(op.f("ix__mention_user_id"), table_name="mention")
41+
op.drop_table("mention")

h/models/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
from h.models.group import Group, GroupMembership, GroupMembershipRoles
3434
from h.models.group_scope import GroupScope
3535
from h.models.job import Job
36+
from h.models.mention import Mention
3637
from h.models.organization import Organization
3738
from h.models.setting import Setting
3839
from h.models.subscriptions import Subscriptions
@@ -60,6 +61,7 @@
6061
"GroupMembership",
6162
"GroupScope",
6263
"Job",
64+
"Mention",
6365
"Organization",
6466
"Setting",
6567
"Subscriptions",

h/models/mention.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import sqlalchemy as sa
2+
from sqlalchemy.orm import Mapped, mapped_column
3+
4+
from h.db import Base
5+
from h.db.mixins import Timestamps
6+
from h.models import helpers
7+
8+
9+
class Mention(Base, Timestamps): # pragma: nocover
10+
__tablename__ = "mention"
11+
12+
id: Mapped[int] = mapped_column(sa.Integer, autoincrement=True, primary_key=True)
13+
14+
annotation_id: Mapped[int] = mapped_column(
15+
sa.Integer,
16+
sa.ForeignKey("annotation_slim.id", ondelete="CASCADE"),
17+
nullable=False,
18+
)
19+
"""FK to annotation_slim.id"""
20+
annotation = sa.orm.relationship("AnnotationSlim")
21+
22+
user_id: Mapped[int] = mapped_column(
23+
sa.Integer,
24+
sa.ForeignKey("user.id", ondelete="CASCADE"),
25+
nullable=False,
26+
index=True,
27+
)
28+
"""FK to user.id"""
29+
user = sa.orm.relationship("User")
30+
31+
def __repr__(self) -> str:
32+
return helpers.repr_(self, ["id", "annotation_id", "user_id"])

0 commit comments

Comments
 (0)