From 3c74116cd011dfbd9f64700276f75762e7c3a3c5 Mon Sep 17 00:00:00 2001 From: seeleng Date: Sun, 22 Sep 2024 18:25:09 +0800 Subject: [PATCH 01/10] feat: add article_event table --- ...f5af4d7ce04_add_articleevent_join_table.py | 44 +++++++++++++++++++ backend/src/events/models.py | 23 ++++++++-- 2 files changed, 64 insertions(+), 3 deletions(-) create mode 100644 backend/alembic/versions/ff5af4d7ce04_add_articleevent_join_table.py diff --git a/backend/alembic/versions/ff5af4d7ce04_add_articleevent_join_table.py b/backend/alembic/versions/ff5af4d7ce04_add_articleevent_join_table.py new file mode 100644 index 00000000..f16b394d --- /dev/null +++ b/backend/alembic/versions/ff5af4d7ce04_add_articleevent_join_table.py @@ -0,0 +1,44 @@ +"""Add ArticleEvent join table + +Revision ID: ff5af4d7ce04 +Revises: a73902039c96 +Create Date: 2024-09-22 18:24:05.699420 + +""" + +from typing import Sequence, Union + +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision: str = "ff5af4d7ce04" +down_revision: Union[str, None] = "a73902039c96" +branch_labels: Union[str, Sequence[str], None] = None +depends_on: Union[str, Sequence[str], None] = None + + +def upgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + op.create_table( + "article_event", + sa.Column("article_id", sa.Integer(), nullable=False), + sa.Column("event_id", sa.Integer(), nullable=False), + sa.ForeignKeyConstraint( + ["article_id"], + ["article.id"], + ), + sa.ForeignKeyConstraint( + ["event_id"], + ["event.id"], + ), + sa.PrimaryKeyConstraint("article_id", "event_id"), + ) + # ### end Alembic commands ### + + +def downgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + op.drop_table("article_event") + # ### end Alembic commands ### diff --git a/backend/src/events/models.py b/backend/src/events/models.py index 07fed17b..151f8667 100644 --- a/backend/src/events/models.py +++ b/backend/src/events/models.py @@ -1,6 +1,6 @@ from enum import Enum from sqlalchemy.orm import Mapped, mapped_column, relationship -from sqlalchemy import ForeignKey +from sqlalchemy import Column, ForeignKey, Table from datetime import datetime from src.common.base import Base @@ -10,6 +10,14 @@ class ArticleSource(str, Enum): GUARDIAN = "GUARDIAN" +article_event_table = Table( + "article_event", + Base.metadata, + Column("article_id", ForeignKey("article.id"), primary_key=True), + Column("event_id", ForeignKey("event.id"), primary_key=True), +) + + class Article(Base): __tablename__ = "article" @@ -21,7 +29,13 @@ class Article(Base): source: Mapped[ArticleSource] date: Mapped[datetime] - events: Mapped[list["Event"]] = relationship(back_populates="original_article") + original_events: Mapped[list["Event"]] = relationship( + back_populates="original_article" + ) + + events: Mapped[list["Event"]] = relationship( + back_populates="articles", secondary=article_event_table + ) class Event(Base): @@ -40,7 +54,10 @@ class Event(Base): back_populates="events", secondary="event_category" ) - original_article: Mapped[Article] = relationship(back_populates="events") + original_article: Mapped[Article] = relationship(back_populates="original_events") + articles: Mapped[list[Article]] = relationship( + back_populates="events", secondary=article_event_table + ) class Category(Base): From 319a304648a381a688128826615de65d085d5ef3 Mon Sep 17 00:00:00 2001 From: seeleng Date: Sun, 22 Sep 2024 18:30:03 +0800 Subject: [PATCH 02/10] feat: add analysis table --- .../8912e0e896bf_add_analysis_table.py | 58 +++++++++++++++++++ backend/src/events/models.py | 5 +- 2 files changed, 61 insertions(+), 2 deletions(-) create mode 100644 backend/alembic/versions/8912e0e896bf_add_analysis_table.py diff --git a/backend/alembic/versions/8912e0e896bf_add_analysis_table.py b/backend/alembic/versions/8912e0e896bf_add_analysis_table.py new file mode 100644 index 00000000..60c73613 --- /dev/null +++ b/backend/alembic/versions/8912e0e896bf_add_analysis_table.py @@ -0,0 +1,58 @@ +"""Add Analysis table + +Revision ID: 8912e0e896bf +Revises: ff5af4d7ce04 +Create Date: 2024-09-22 18:29:26.096419 + +""" + +from typing import Sequence, Union + +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision: str = "8912e0e896bf" +down_revision: Union[str, None] = "ff5af4d7ce04" +branch_labels: Union[str, Sequence[str], None] = None +depends_on: Union[str, Sequence[str], None] = None + + +def upgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + op.create_table( + "analysis", + sa.Column("event_id", sa.Integer(), nullable=False), + sa.Column("category_id", sa.Integer(), nullable=False), + sa.Column("content", sa.String(), nullable=False), + sa.ForeignKeyConstraint( + ["category_id"], + ["category.id"], + ), + sa.ForeignKeyConstraint( + ["event_id"], + ["event.id"], + ), + sa.PrimaryKeyConstraint("event_id", "category_id"), + ) + op.drop_table("event_category") + # ### end Alembic commands ### + + +def downgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + op.create_table( + "event_category", + sa.Column("event_id", sa.INTEGER(), autoincrement=False, nullable=False), + sa.Column("category_id", sa.INTEGER(), autoincrement=False, nullable=False), + sa.ForeignKeyConstraint( + ["category_id"], ["category.id"], name="event_category_category_id_fkey" + ), + sa.ForeignKeyConstraint( + ["event_id"], ["event.id"], name="event_category_event_id_fkey" + ), + sa.PrimaryKeyConstraint("event_id", "category_id", name="event_category_pkey"), + ) + op.drop_table("analysis") + # ### end Alembic commands ### diff --git a/backend/src/events/models.py b/backend/src/events/models.py index 151f8667..d3db64ea 100644 --- a/backend/src/events/models.py +++ b/backend/src/events/models.py @@ -71,10 +71,11 @@ class Category(Base): ) -class EventCategory(Base): - __tablename__ = "event_category" +class Analysis(Base): + __tablename__ = "analysis" event_id: Mapped[int] = mapped_column(ForeignKey("event.id"), primary_key=True) category_id: Mapped[int] = mapped_column( ForeignKey("category.id"), primary_key=True ) + content: Mapped[str] From 29abf66a8a9462bbb20f4f0e766dd83375876e8b Mon Sep 17 00:00:00 2001 From: seeleng Date: Sun, 22 Sep 2024 18:50:42 +0800 Subject: [PATCH 03/10] feat: add gp question models --- .../8c458d0adb18_add_gp_questions_tables.py | 57 +++++++++++++++++++ backend/src/events/models.py | 27 +++++++++ 2 files changed, 84 insertions(+) create mode 100644 backend/alembic/versions/8c458d0adb18_add_gp_questions_tables.py diff --git a/backend/alembic/versions/8c458d0adb18_add_gp_questions_tables.py b/backend/alembic/versions/8c458d0adb18_add_gp_questions_tables.py new file mode 100644 index 00000000..fc0812a3 --- /dev/null +++ b/backend/alembic/versions/8c458d0adb18_add_gp_questions_tables.py @@ -0,0 +1,57 @@ +"""Add GP Questions tables + +Revision ID: 8c458d0adb18 +Revises: 8912e0e896bf +Create Date: 2024-09-22 18:49:41.011371 + +""" + +from typing import Sequence, Union + +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision: str = "8c458d0adb18" +down_revision: Union[str, None] = "8912e0e896bf" +branch_labels: Union[str, Sequence[str], None] = None +depends_on: Union[str, Sequence[str], None] = None + + +def upgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + op.create_table( + "gp_question", + sa.Column("id", sa.Integer(), nullable=False), + sa.Column("question", sa.String(), nullable=False), + sa.Column("is_llm_generated", sa.Boolean(), nullable=False), + sa.Column("event_id", sa.Integer(), nullable=True), + sa.ForeignKeyConstraint( + ["event_id"], + ["event.id"], + ), + sa.PrimaryKeyConstraint("id"), + ) + op.create_table( + "gp_question_categories", + sa.Column("gp_question_id", sa.Integer(), nullable=False), + sa.Column("category_id", sa.Integer(), nullable=False), + sa.ForeignKeyConstraint( + ["category_id"], + ["category.id"], + ), + sa.ForeignKeyConstraint( + ["gp_question_id"], + ["gp_question.id"], + ), + sa.PrimaryKeyConstraint("gp_question_id", "category_id"), + ) + # ### end Alembic commands ### + + +def downgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + op.drop_table("gp_question_categories") + op.drop_table("gp_question") + # ### end Alembic commands ### diff --git a/backend/src/events/models.py b/backend/src/events/models.py index d3db64ea..03f99c0d 100644 --- a/backend/src/events/models.py +++ b/backend/src/events/models.py @@ -58,6 +58,7 @@ class Event(Base): articles: Mapped[list[Article]] = relationship( back_populates="events", secondary=article_event_table ) + gp_questions: Mapped["GPQuestion"] = relationship(back_populates="event") class Category(Base): @@ -79,3 +80,29 @@ class Analysis(Base): ForeignKey("category.id"), primary_key=True ) content: Mapped[str] + + +class GPQuestion(Base): + __tablename__ = "gp_question" + + id: Mapped[int] = mapped_column(primary_key=True) + question: Mapped[str] + is_llm_generated: Mapped[bool] = mapped_column(default=True) + event_id = mapped_column(ForeignKey("event.id")) + + categories: Mapped[list["Category"]] = relationship( + secondary="gp_question_categories" + ) + + event: Mapped[Event] = relationship(back_populates="gp_questions") + + +class GPQuestionCategories(Base): + __tablename__ = "gp_question_categories" + + gp_question_id: Mapped[int] = mapped_column( + ForeignKey("gp_question.id"), primary_key=True + ) + category_id: Mapped[int] = mapped_column( + ForeignKey("category.id"), primary_key=True + ) From ab7547e0fd09490fe160e60cbc3b98f9850c18dc Mon Sep 17 00:00:00 2001 From: seeleng Date: Sun, 22 Sep 2024 19:01:13 +0800 Subject: [PATCH 04/10] feat: modify event and article tables event - add rating article - add image url --- .../b8f3f95c78b6_add_image_url_to_article.py | 31 +++++++++++++++++++ .../d369cd69a23b_add_rating_to_event.py | 31 +++++++++++++++++++ backend/src/events/models.py | 2 ++ 3 files changed, 64 insertions(+) create mode 100644 backend/alembic/versions/b8f3f95c78b6_add_image_url_to_article.py create mode 100644 backend/alembic/versions/d369cd69a23b_add_rating_to_event.py diff --git a/backend/alembic/versions/b8f3f95c78b6_add_image_url_to_article.py b/backend/alembic/versions/b8f3f95c78b6_add_image_url_to_article.py new file mode 100644 index 00000000..7f3e49a8 --- /dev/null +++ b/backend/alembic/versions/b8f3f95c78b6_add_image_url_to_article.py @@ -0,0 +1,31 @@ +"""Add image url to article + +Revision ID: b8f3f95c78b6 +Revises: 8c458d0adb18 +Create Date: 2024-09-22 18:56:47.024304 + +""" + +from typing import Sequence, Union + +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision: str = "b8f3f95c78b6" +down_revision: Union[str, None] = "8c458d0adb18" +branch_labels: Union[str, Sequence[str], None] = None +depends_on: Union[str, Sequence[str], None] = None + + +def upgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + op.add_column("article", sa.Column("image_url", sa.String(), nullable=False)) + # ### end Alembic commands ### + + +def downgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + op.drop_column("article", "image_url") + # ### end Alembic commands ### diff --git a/backend/alembic/versions/d369cd69a23b_add_rating_to_event.py b/backend/alembic/versions/d369cd69a23b_add_rating_to_event.py new file mode 100644 index 00000000..d359ffaa --- /dev/null +++ b/backend/alembic/versions/d369cd69a23b_add_rating_to_event.py @@ -0,0 +1,31 @@ +"""Add rating to event + +Revision ID: d369cd69a23b +Revises: b8f3f95c78b6 +Create Date: 2024-09-22 18:59:11.283743 + +""" + +from typing import Sequence, Union + +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision: str = "d369cd69a23b" +down_revision: Union[str, None] = "b8f3f95c78b6" +branch_labels: Union[str, Sequence[str], None] = None +depends_on: Union[str, Sequence[str], None] = None + + +def upgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + op.add_column("event", sa.Column("rating", sa.Integer(), nullable=False)) + # ### end Alembic commands ### + + +def downgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + op.drop_column("event", "rating") + # ### end Alembic commands ### diff --git a/backend/src/events/models.py b/backend/src/events/models.py index 03f99c0d..7a5fee72 100644 --- a/backend/src/events/models.py +++ b/backend/src/events/models.py @@ -28,6 +28,7 @@ class Article(Base): url: Mapped[str] source: Mapped[ArticleSource] date: Mapped[datetime] + image_url: Mapped[str] original_events: Mapped[list["Event"]] = relationship( back_populates="original_article" @@ -49,6 +50,7 @@ class Event(Base): date: Mapped[datetime] is_singapore: Mapped[bool] original_article_id: Mapped[int] = mapped_column(ForeignKey("article.id")) + rating: Mapped[int] categories: Mapped[list["Category"]] = relationship( back_populates="events", secondary="event_category" From 0da48aaac424a2f0e8e28d40dad20d123f61a955 Mon Sep 17 00:00:00 2001 From: seeleng Date: Sun, 22 Sep 2024 21:36:25 +0800 Subject: [PATCH 05/10] feat: add userquestion models --- .../04e02312343d_add_user_question_models.py | 66 +++++++++++++++++++ backend/src/user_questions/models.py | 34 ++++++++++ backend/src/utils/models.py | 1 + 3 files changed, 101 insertions(+) create mode 100644 backend/alembic/versions/04e02312343d_add_user_question_models.py create mode 100644 backend/src/user_questions/models.py diff --git a/backend/alembic/versions/04e02312343d_add_user_question_models.py b/backend/alembic/versions/04e02312343d_add_user_question_models.py new file mode 100644 index 00000000..ef318702 --- /dev/null +++ b/backend/alembic/versions/04e02312343d_add_user_question_models.py @@ -0,0 +1,66 @@ +"""Add user question models + + +Revision ID: 04e02312343d +Revises: d369cd69a23b +Create Date: 2024-09-22 21:35:48.778303 + +""" + +from typing import Sequence, Union + +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision: str = "04e02312343d" +down_revision: Union[str, None] = "d369cd69a23b" +branch_labels: Union[str, Sequence[str], None] = None +depends_on: Union[str, Sequence[str], None] = None + + +def upgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + op.create_table( + "user_question", + sa.Column("id", sa.Integer(), nullable=False), + sa.Column("question", sa.String(), nullable=False), + sa.Column("user_id", sa.Integer(), nullable=False), + sa.ForeignKeyConstraint( + ["user_id"], + ["user.id"], + ), + sa.PrimaryKeyConstraint("id"), + ) + op.create_table( + "answer", + sa.Column("id", sa.Integer(), nullable=False), + sa.Column("user_question_id", sa.Integer(), nullable=False), + sa.ForeignKeyConstraint( + ["user_question_id"], + ["user_question.id"], + ), + sa.PrimaryKeyConstraint("id"), + ) + op.create_table( + "point", + sa.Column("id", sa.Integer(), nullable=False), + sa.Column("title", sa.String(), nullable=False), + sa.Column("body", sa.String(), nullable=False), + sa.Column("answer_id", sa.Integer(), nullable=False), + sa.ForeignKeyConstraint( + ["answer_id"], + ["answer.id"], + ), + sa.PrimaryKeyConstraint("id"), + ) + # ### end Alembic commands ### + + +def downgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + op.drop_table("point") + op.drop_table("answer") + op.drop_table("user_question") + # ### end Alembic commands ### diff --git a/backend/src/user_questions/models.py b/backend/src/user_questions/models.py new file mode 100644 index 00000000..d77ce1e1 --- /dev/null +++ b/backend/src/user_questions/models.py @@ -0,0 +1,34 @@ +from sqlalchemy import ForeignKey +from src.common.base import Base +from sqlalchemy.orm import Mapped, mapped_column, relationship + + +class UserQuestion(Base): + __tablename__ = "user_question" + + id: Mapped[int] = mapped_column(primary_key=True) + question: Mapped[str] + + answer: Mapped["Answer"] = relationship(back_populates="user_question") + + user_id: Mapped[int] = mapped_column(ForeignKey("user.id")) + + +class Answer(Base): + __tablename__ = "answer" + id: Mapped[int] = mapped_column(primary_key=True) + user_question_id: Mapped[int] = mapped_column(ForeignKey("user_question.id")) + + user_question: Mapped[UserQuestion] = relationship(back_populates="answer") + + points: Mapped[list["Point"]] = relationship(back_populates="answer") + + +class Point(Base): + __tablename__ = "point" + id: Mapped[int] = mapped_column(primary_key=True) + title: Mapped[str] + body: Mapped[str] + answer_id: Mapped[int] = mapped_column(ForeignKey("answer.id")) + + answer: Mapped[Answer] = relationship(back_populates="points") diff --git a/backend/src/utils/models.py b/backend/src/utils/models.py index 41503e91..f0da0efc 100644 --- a/backend/src/utils/models.py +++ b/backend/src/utils/models.py @@ -2,3 +2,4 @@ from src.auth import models as auth_models # noqa: F401 from src.events import models as event_models # noqa: F401 +from src.user_questions import models as user_question_models # noqa: F401 From 0dbc891fdc6af15a59e84e1f20ad3258520c7184 Mon Sep 17 00:00:00 2001 From: seeleng Date: Sun, 22 Sep 2024 22:23:41 +0800 Subject: [PATCH 06/10] feat: add note models --- .../versions/680d9bea2168_add_note_models.py | 40 +++++++++++++++++++ backend/src/events/models.py | 21 ++++++++-- backend/src/notes/models.py | 24 +++++++++++ backend/src/user_questions/models.py | 6 +++ backend/src/utils/models.py | 1 + 5 files changed, 88 insertions(+), 4 deletions(-) create mode 100644 backend/alembic/versions/680d9bea2168_add_note_models.py create mode 100644 backend/src/notes/models.py diff --git a/backend/alembic/versions/680d9bea2168_add_note_models.py b/backend/alembic/versions/680d9bea2168_add_note_models.py new file mode 100644 index 00000000..1735c4b7 --- /dev/null +++ b/backend/alembic/versions/680d9bea2168_add_note_models.py @@ -0,0 +1,40 @@ +"""Add note models + +Revision ID: 680d9bea2168 +Revises: 04e02312343d +Create Date: 2024-09-22 22:07:11.979617 + +""" + +from typing import Sequence, Union + +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision: str = "680d9bea2168" +down_revision: Union[str, None] = "04e02312343d" +branch_labels: Union[str, Sequence[str], None] = None +depends_on: Union[str, Sequence[str], None] = None + + +def upgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + op.create_table( + "note", + sa.Column("id", sa.Integer(), nullable=False), + sa.Column("content", sa.String(), nullable=False), + sa.Column("start_index", sa.Integer(), nullable=False), + sa.Column("end_index", sa.Integer(), nullable=False), + sa.Column("parent_id", sa.Integer(), nullable=False), + sa.Column("parent_type", sa.String(), nullable=False), + sa.PrimaryKeyConstraint("id"), + ) + # ### end Alembic commands ### + + +def downgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + op.drop_table("note") + # ### end Alembic commands ### diff --git a/backend/src/events/models.py b/backend/src/events/models.py index 7a5fee72..d85ac67c 100644 --- a/backend/src/events/models.py +++ b/backend/src/events/models.py @@ -1,8 +1,9 @@ from enum import Enum -from sqlalchemy.orm import Mapped, mapped_column, relationship -from sqlalchemy import Column, ForeignKey, Table +from sqlalchemy.orm import Mapped, mapped_column, relationship, foreign +from sqlalchemy import Column, ForeignKey, Table, and_ from datetime import datetime from src.common.base import Base +from src.notes.models import Note class ArticleSource(str, Enum): @@ -38,6 +39,12 @@ class Article(Base): back_populates="articles", secondary=article_event_table ) + notes = relationship( + "Note", + primaryjoin=and_(id == foreign(Note.parent_id), Note.parent_type == "article"), + backref="article", + ) + class Event(Base): __tablename__ = "event" @@ -53,7 +60,7 @@ class Event(Base): rating: Mapped[int] categories: Mapped[list["Category"]] = relationship( - back_populates="events", secondary="event_category" + back_populates="events", secondary="analysis" ) original_article: Mapped[Article] = relationship(back_populates="original_events") @@ -62,6 +69,12 @@ class Event(Base): ) gp_questions: Mapped["GPQuestion"] = relationship(back_populates="event") + notes = relationship( + "Note", + primaryjoin=and_(id == foreign(Note.parent_id), Note.parent_type == "note"), + backref="event", + ) + class Category(Base): __tablename__ = "category" @@ -70,7 +83,7 @@ class Category(Base): name: Mapped[str] events: Mapped[list[Event]] = relationship( - secondary="event_category", back_populates="categories" + secondary="analysis", back_populates="categories" ) diff --git a/backend/src/notes/models.py b/backend/src/notes/models.py new file mode 100644 index 00000000..d1e461fb --- /dev/null +++ b/backend/src/notes/models.py @@ -0,0 +1,24 @@ +from sqlalchemy import Enum +from sqlalchemy.orm import Mapped, mapped_column +from src.common.base import Base + + +class NoteType(str, Enum): + EVENT = "event" + ARTICLE = "article" + POINT = "point" + + +class Note(Base): + __tablename__ = "note" + + id: Mapped[int] = mapped_column(primary_key=True) + content: Mapped[str] + + start_index: Mapped[int] + end_index: Mapped[int] + + parent_id: Mapped[int] + parent_type: Mapped[str] + + __mapper_args__ = {"polymorphic_on": "parent_type", "polymorphic_identity": "note"} diff --git a/backend/src/user_questions/models.py b/backend/src/user_questions/models.py index d77ce1e1..c887244c 100644 --- a/backend/src/user_questions/models.py +++ b/backend/src/user_questions/models.py @@ -32,3 +32,9 @@ class Point(Base): answer_id: Mapped[int] = mapped_column(ForeignKey("answer.id")) answer: Mapped[Answer] = relationship(back_populates="points") + + notes = relationship( + "Note", + primaryjoin="and_(Event.id == foreign(Note.parent_id), Note.parent_type=='event')", + backref="point", + ) diff --git a/backend/src/utils/models.py b/backend/src/utils/models.py index f0da0efc..42c71f0d 100644 --- a/backend/src/utils/models.py +++ b/backend/src/utils/models.py @@ -3,3 +3,4 @@ from src.auth import models as auth_models # noqa: F401 from src.events import models as event_models # noqa: F401 from src.user_questions import models as user_question_models # noqa: F401 +from src.notes import models as note_models # noqa: F401 From 398015dc4f9d243cb877d7f8f78ba4754b191cba Mon Sep 17 00:00:00 2001 From: seeleng Date: Sun, 22 Sep 2024 22:29:59 +0800 Subject: [PATCH 07/10] fix: add user id to note --- .../f624d58d3f42_add_user_id_to_note.py | 33 +++++++++++++++++++ backend/src/auth/models.py | 2 ++ backend/src/notes/models.py | 4 ++- 3 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 backend/alembic/versions/f624d58d3f42_add_user_id_to_note.py diff --git a/backend/alembic/versions/f624d58d3f42_add_user_id_to_note.py b/backend/alembic/versions/f624d58d3f42_add_user_id_to_note.py new file mode 100644 index 00000000..44142a66 --- /dev/null +++ b/backend/alembic/versions/f624d58d3f42_add_user_id_to_note.py @@ -0,0 +1,33 @@ +"""Add user id to note + +Revision ID: f624d58d3f42 +Revises: 680d9bea2168 +Create Date: 2024-09-22 22:29:21.220643 + +""" + +from typing import Sequence, Union + +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision: str = "f624d58d3f42" +down_revision: Union[str, None] = "680d9bea2168" +branch_labels: Union[str, Sequence[str], None] = None +depends_on: Union[str, Sequence[str], None] = None + + +def upgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + op.add_column("note", sa.Column("user_id", sa.Integer(), nullable=False)) + op.create_foreign_key(None, "note", "user", ["user_id"], ["id"]) + # ### end Alembic commands ### + + +def downgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + op.drop_constraint(None, "note", type_="foreignkey") + op.drop_column("note", "user_id") + # ### end Alembic commands ### diff --git a/backend/src/auth/models.py b/backend/src/auth/models.py index c0385446..d5257a6b 100644 --- a/backend/src/auth/models.py +++ b/backend/src/auth/models.py @@ -3,6 +3,7 @@ from sqlalchemy.orm import Mapped, mapped_column, relationship from src.common.base import Base from src.events.models import Category +from src.notes.models import Note class AccountType(str, Enum): @@ -27,3 +28,4 @@ class User(Base): account_type: Mapped[AccountType] categories: Mapped[list[Category]] = relationship(secondary=user_category_table) + notes: Mapped[list[Note]] = relationship("Note", backref="user") diff --git a/backend/src/notes/models.py b/backend/src/notes/models.py index d1e461fb..9b27cf94 100644 --- a/backend/src/notes/models.py +++ b/backend/src/notes/models.py @@ -1,4 +1,4 @@ -from sqlalchemy import Enum +from sqlalchemy import Enum, ForeignKey from sqlalchemy.orm import Mapped, mapped_column from src.common.base import Base @@ -21,4 +21,6 @@ class Note(Base): parent_id: Mapped[int] parent_type: Mapped[str] + user_id: Mapped[int] = mapped_column(ForeignKey("user.id")) + __mapper_args__ = {"polymorphic_on": "parent_type", "polymorphic_identity": "note"} From a2a04b69a1bba89754bd2d36da92b6e4b493fbe8 Mon Sep 17 00:00:00 2001 From: seeleng Date: Sun, 22 Sep 2024 22:33:55 +0800 Subject: [PATCH 08/10] fix: remove analysis field from event --- ...e47713_remove_analysis_field_from_event.py | 34 +++++++++++++++++++ backend/src/events/models.py | 1 - backend/src/scripts/seed.py | 1 - 3 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 backend/alembic/versions/bc5fcbe47713_remove_analysis_field_from_event.py diff --git a/backend/alembic/versions/bc5fcbe47713_remove_analysis_field_from_event.py b/backend/alembic/versions/bc5fcbe47713_remove_analysis_field_from_event.py new file mode 100644 index 00000000..28215d4a --- /dev/null +++ b/backend/alembic/versions/bc5fcbe47713_remove_analysis_field_from_event.py @@ -0,0 +1,34 @@ +"""Remove analysis field from event + +Revision ID: bc5fcbe47713 +Revises: f624d58d3f42 +Create Date: 2024-09-22 22:33:38.823000 + +""" + +from typing import Sequence, Union + +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision: str = "bc5fcbe47713" +down_revision: Union[str, None] = "f624d58d3f42" +branch_labels: Union[str, Sequence[str], None] = None +depends_on: Union[str, Sequence[str], None] = None + + +def upgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + op.drop_column("event", "analysis") + # ### end Alembic commands ### + + +def downgrade() -> None: + # ### commands auto generated by Alembic - please adjust! ### + op.add_column( + "event", + sa.Column("analysis", sa.VARCHAR(), autoincrement=False, nullable=False), + ) + # ### end Alembic commands ### diff --git a/backend/src/events/models.py b/backend/src/events/models.py index d85ac67c..6209b518 100644 --- a/backend/src/events/models.py +++ b/backend/src/events/models.py @@ -52,7 +52,6 @@ class Event(Base): id: Mapped[int] = mapped_column(primary_key=True) title: Mapped[str] description: Mapped[str] - analysis: Mapped[str] duplicate: Mapped[bool] date: Mapped[datetime] is_singapore: Mapped[bool] diff --git a/backend/src/scripts/seed.py b/backend/src/scripts/seed.py index 834e1b78..664c5320 100644 --- a/backend/src/scripts/seed.py +++ b/backend/src/scripts/seed.py @@ -44,7 +44,6 @@ def test_associations(): event = Event( title="test event 1", description="x", - analysis="x", duplicate=False, date=datetime.now(), is_singapore=False, From 6bf54c79adc00a8123230ef1ee0a6707cf247e81 Mon Sep 17 00:00:00 2001 From: seeleng Date: Sun, 22 Sep 2024 22:44:06 +0800 Subject: [PATCH 09/10] fix: notes relationship in point --- backend/src/user_questions/models.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/backend/src/user_questions/models.py b/backend/src/user_questions/models.py index c887244c..0f31ed6b 100644 --- a/backend/src/user_questions/models.py +++ b/backend/src/user_questions/models.py @@ -1,6 +1,7 @@ -from sqlalchemy import ForeignKey +from sqlalchemy import ForeignKey, and_ from src.common.base import Base -from sqlalchemy.orm import Mapped, mapped_column, relationship +from sqlalchemy.orm import Mapped, mapped_column, relationship, foreign +from src.notes.models import Note class UserQuestion(Base): @@ -35,6 +36,6 @@ class Point(Base): notes = relationship( "Note", - primaryjoin="and_(Event.id == foreign(Note.parent_id), Note.parent_type=='event')", + primaryjoin=and_(id == foreign(Note.parent_id), Note.parent_type == "note"), backref="point", ) From a441921b702c0111e03a407bf539a5da7c25b3b0 Mon Sep 17 00:00:00 2001 From: seeleng Date: Sun, 22 Sep 2024 22:48:53 +0800 Subject: [PATCH 10/10] fix: same thing --- backend/src/user_questions/models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backend/src/user_questions/models.py b/backend/src/user_questions/models.py index 0f31ed6b..70a551eb 100644 --- a/backend/src/user_questions/models.py +++ b/backend/src/user_questions/models.py @@ -36,6 +36,6 @@ class Point(Base): notes = relationship( "Note", - primaryjoin=and_(id == foreign(Note.parent_id), Note.parent_type == "note"), + primaryjoin=and_(id == foreign(Note.parent_id), Note.parent_type == "point"), backref="point", )