diff --git a/h/migrations/versions/39cc1025a3a2_create_mention_table.py b/h/migrations/versions/39cc1025a3a2_create_mention_table.py new file mode 100644 index 00000000000..4a92f714590 --- /dev/null +++ b/h/migrations/versions/39cc1025a3a2_create_mention_table.py @@ -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") diff --git a/h/models/__init__.py b/h/models/__init__.py index 77cf47d35f3..a436a922f71 100644 --- a/h/models/__init__.py +++ b/h/models/__init__.py @@ -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 @@ -60,6 +61,7 @@ "GroupMembership", "GroupScope", "Job", + "Mention", "Organization", "Setting", "Subscriptions", diff --git a/h/models/mention.py b/h/models/mention.py new file mode 100644 index 00000000000..04afb6bfab6 --- /dev/null +++ b/h/models/mention.py @@ -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"])