From ce5138a4f74c05fc6c7d4c5963e4c48dd2717aab Mon Sep 17 00:00:00 2001 From: Mizaki Date: Sat, 10 Aug 2024 17:49:07 +0100 Subject: [PATCH] Group issues by ID and not number as nn and series with volumes that restart back to issue 1 were lost. Log if more than one issue is found for a series ID and issue number --- gcd_talker/gcd.py | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/gcd_talker/gcd.py b/gcd_talker/gcd.py index bcc4a8d..847126a 100644 --- a/gcd_talker/gcd.py +++ b/gcd_talker/gcd.py @@ -444,8 +444,8 @@ def fetch_issues_in_series(self, series_id: str) -> list[GenericMetadata]: "ELSE NULL END, '\n') AS 'story_titles' " "FROM gcd_issue " "LEFT JOIN gcd_story ON gcd_story.issue_id = gcd_issue.id AND gcd_story.type_id = 19 " - "WHERE gcd_issue.series_id = ? " - "GROUP BY gcd_issue.number;", + "WHERE gcd_issue.series_id = ? AND gcd_issue.variant_of_id IS NULL " + "GROUP BY gcd_issue.id;", [int(series_id)], ) rows = cur.fetchall() @@ -487,14 +487,14 @@ def fetch_issues_by_series_issue_num_and_year( gcd_story.title END, '\n') AS 'story_titles' FROM gcd_issue LEFT JOIN gcd_story ON gcd_story.issue_id=gcd_issue.id AND gcd_story.type_id=19 - WHERE gcd_issue.series_id=? """ + WHERE gcd_issue.series_id=? AND gcd_issue.variant_of_id IS NULL """ sql_search_issues: str = "AND gcd_issue.number=? AND (gcd_issue.key_date LIKE ? OR gcd_issue.key_date='') " sql_search_issues_nn: str = """AND (gcd_issue.number=? OR gcd_issue.number='[nn]') AND (gcd_issue.key_date LIKE ? OR gcd_issue.key_date='') """ - sql_search_group: str = "GROUP BY gcd_issue.number;" + sql_search_group: str = "GROUP BY gcd_issue.id;" if self.nn_is_issue_one and issue_number == "1": sql_search = sql_search_main + sql_search_issues_nn + sql_search_group @@ -831,8 +831,7 @@ def _fetch_issue_data(self, series_id: int, issue_number: str) -> GenericMetadat # Find the id of the issue and pass it along sql_query: str = "" - sql_base: str = """SELECT gcd_issue.id AS 'id' - FROM gcd_issue """ + sql_base: str = "SELECT gcd_issue.id AS 'id' FROM gcd_issue " sql_where: str = "WHERE gcd_issue.series_id=? AND gcd_issue.number=?" sql_where_nn: str = "WHERE gcd_issue.series_id=? AND (gcd_issue.number=? OR gcd_issue.number='[nn]')" @@ -851,10 +850,18 @@ def _fetch_issue_data(self, series_id: int, issue_number: str) -> GenericMetadat sql_query, [series_id, issue_number], ) - row = cur.fetchone() - - if row["id"]: - return self._fetch_issue_data_by_issue_id(row["id"]) + row = cur.fetchall() + + # Expect one result however there are exception: "nn" for issue number and new volumes restarting issue + # number back to 1 are possible therefore, take the first result and log the remainder + if len(row) > 1: + logger.warning( + f"More than ONE issue found for: Series ID: {series_id}, Issue Number: " + f"{issue_number}. Using first result.\n" + f"All result IDs: {', '.join([str(i_id['id']) for i_id in row])}" + ) + if row[0]["id"]: + return self._fetch_issue_data_by_issue_id(row[0]["id"]) except sqlite3.DataError as e: logger.debug(f"DB data error: {e}")