From 38ebc629ba231ed57d760ec4793d4f94fda31d64 Mon Sep 17 00:00:00 2001 From: realaravinth Date: Thu, 14 Apr 2022 23:22:31 +0530 Subject: [PATCH 1/3] feat: conditionally load data from DB when updating issues and comments SUMMARY DBIssue and DBComment loaded data from database when performing updates via self.__update() which in some cases where unnecessary as a fetch would have been performed just before invoking __update(). This patch introduces an optical parameter to provide recently loaded data from DB --- interface/db/comments.py | 115 +++++++++++++++++++++++++++++++-------- interface/db/issues.py | 68 ++++++++++++++--------- 2 files changed, 135 insertions(+), 48 deletions(-) diff --git a/interface/db/comments.py b/interface/db/comments.py index 3d4e0b8..bfd6964 100644 --- a/interface/db/comments.py +++ b/interface/db/comments.py @@ -23,6 +23,7 @@ from .issues import DBIssue from .interfaces import DBInterfaces from .cache import RecordCount +from .activity import DBActivity, ActivityType @dataclass @@ -37,6 +38,8 @@ class DBComment: belongs_to_issue: DBIssue count = RecordCount("gitea_issue_comments") + id = None + __belongs_to_issue_id: int = None def __set_sqlite_to_bools(self): @@ -48,28 +51,44 @@ def __set_sqlite_to_bools(self): """ self.is_native = bool(self.is_native) - def __update(self): + def __update(self, from_db: "DBComment" = None): """ Update changes in database Only fields that can be mutated on the forge will be updated in the DB """ - conn = get_db() - cur = conn.cursor() - cur.execute( - """ - UPDATE gitea_issue_comments - SET - body = ?, - updated = ? - WHERE - comment_id = ?; - """, - (self.body, self.updated, self.comment_id), - ) - conn.commit() + comment = from_db + if from_db is None: + comment = self.load_from_id(self.id) + if any([comment.body != self.body, comment.updated != self.updated]): + conn = get_db() + cur = conn.cursor() + cur.execute( + """ + UPDATE gitea_issue_comments + SET + body = ?, + updated = ? + WHERE + comment_id = ?; + """, + (self.body, self.updated, self.comment_id), + ) + conn.commit() + DBActivity( + user_id=self.user.id, + activity=ActivityType.UPDATE, + created=self.updated, + comment_id=self.id, + ).save() def save(self): """Save COmment to database""" + + comment = self.load_from_comment_url(self.html_url) + if comment is not None: + self.id = comment.id + self.__update(from_db=comment) + self.user.save() self.belongs_to_issue.save() @@ -106,13 +125,18 @@ def save(self): conn.commit() data = cur.execute( """ - SELECT ID from gitea_issue_comments WHERE html_url = ? - - """, + SELECT ID from gitea_issue_comments WHERE html_url = ? + """, (self.html_url,), ).fetchone() self.id = data[0] self.__update() + DBActivity( + user_id=self.user.id, + activity=ActivityType.CREATE, + created=self.created, + comment_id=self.id, + ).save() @classmethod def load_from_comment_url(cls, comment_url: str) -> "DBComment": @@ -128,7 +152,8 @@ def load_from_comment_url(cls, comment_url: str) -> "DBComment": updated, comment_id, is_native, - user + user, + ID FROM gitea_issue_comments WHERE @@ -151,6 +176,48 @@ def load_from_comment_url(cls, comment_url: str) -> "DBComment": user=user, belongs_to_issue=belongs_to_issue, ) + comment.id = data[7] + comment.__set_sqlite_to_bools() + return comment + + @classmethod + def load_from_id(cls, db_id: int) -> "DBComment": + """Load comment based on ID assigned by database""" + conn = get_db() + cur = conn.cursor() + data = cur.execute( + """ + SELECT + body, + belongs_to_issue, + created, + updated, + comment_id, + is_native, + user, + html_url + FROM + gitea_issue_comments + WHERE + ID = ? + """, + (db_id,), + ).fetchone() + if data is None: + return None + + user = DBUser.load_with_db_id(data[6]) + belongs_to_issue = DBIssue.load_with_id(data[1]) + comment = cls( + body=data[0], + html_url=data[7], + created=data[2], + updated=data[3], + comment_id=data[4], + is_native=data[5], + user=user, + belongs_to_issue=belongs_to_issue, + ) comment.__set_sqlite_to_bools() return comment @@ -168,7 +235,8 @@ def load_issue_comments(cls, issue: DBIssue) -> "[DBComment]": updated, comment_id, is_native, - user + user, + ID FROM gitea_issue_comments WHERE @@ -182,7 +250,7 @@ def load_issue_comments(cls, issue: DBIssue) -> "[DBComment]": comments = [] for comment in data: user = DBUser.load_with_db_id(comment[6]) - comment = cls( + obj = cls( body=comment[0], html_url=comment[1], created=comment[2], @@ -192,8 +260,9 @@ def load_issue_comments(cls, issue: DBIssue) -> "[DBComment]": user=user, belongs_to_issue=issue, ) - comment.__set_sqlite_to_bools() - comments.append(comment) + obj.id = comment[7] + obj.__set_sqlite_to_bools() + comments.append(obj) if len(comments) == 0: return None diff --git a/interface/db/issues.py b/interface/db/issues.py index f0ecf98..860af97 100644 --- a/interface/db/issues.py +++ b/interface/db/issues.py @@ -125,35 +125,52 @@ def set_open(self, updated: str): self.is_merged = False self.__update() - def __update(self): + def __update(self, from_db: "DBIssue" = None): """ Update changes in database Only fields that can be mutated on the forge will be updated in the DB """ - conn = get_db() - cur = conn.cursor() - cur.execute( - """ - UPDATE gitea_forge_issues - SET - title = ?, - description = ?, - updated = ?, - is_closed = ?, - is_merged = ? - WHERE - id = ? - """, - ( - self.title, - self.description, - self.updated, - self.is_closed, - self.is_merged, - self.id, - ), - ) - conn.commit() + issue = from_db + if from_db is None: + issue = self.load_with_id(db_id=self.id) + if any( + [ + issue.title != self.title, + issue.description != self.description, + issue.is_closed != self.is_closed, + issue.is_merged != self.is_merged, + ] + ): + conn = get_db() + cur = conn.cursor() + cur.execute( + """ + UPDATE gitea_forge_issues + SET + title = ?, + description = ?, + updated = ?, + is_closed = ?, + is_merged = ? + WHERE + id = ? + """, + ( + self.title, + self.description, + self.updated, + self.is_closed, + self.is_merged, + self.id, + ), + ) + conn.commit() + DBActivity( + user_id=self.user.id, + activity=ActivityType.UPDATE, + created=self.updated, + issue_id=self.id, + ).save() def save(self): """Save Issue to database""" @@ -164,6 +181,7 @@ def save(self): self.user = issue.user self.repository = issue.repository self.id = issue.id + self.__update(from_db=issue) return self.user.save() From 46442cca5fd61d07b482707d582f8123d5189869 Mon Sep 17 00:00:00 2001 From: realaravinth Date: Thu, 14 Apr 2022 23:52:12 +0530 Subject: [PATCH 2/3] fix: sort comments based on cration time by default --- interface/db/comments.py | 1 + 1 file changed, 1 insertion(+) diff --git a/interface/db/comments.py b/interface/db/comments.py index bfd6964..27c045c 100644 --- a/interface/db/comments.py +++ b/interface/db/comments.py @@ -241,6 +241,7 @@ def load_issue_comments(cls, issue: DBIssue) -> "[DBComment]": gitea_issue_comments WHERE belongs_to_issue = ? + ORDER BY created """, (issue.id,), ).fetchall() From ab6c56b9a877b8f6a8bc03920d5c9a59070d89b7 Mon Sep 17 00:00:00 2001 From: realaravinth Date: Thu, 14 Apr 2022 23:56:24 +0530 Subject: [PATCH 3/3] fix: set user ID upon DBUser.save() --- interface/db/users.py | 1 + 1 file changed, 1 insertion(+) diff --git a/interface/db/users.py b/interface/db/users.py index 594031b..c4cf1e4 100644 --- a/interface/db/users.py +++ b/interface/db/users.py @@ -76,6 +76,7 @@ def save(self): ), ) conn.commit() + self.id = self.load(self.user_id).id break except IntegrityError as e: count += 1