From 270ff08a94f29f1552baaafe19e8b31a272e9f8c Mon Sep 17 00:00:00 2001 From: BonfaceKilz Date: Thu, 3 Mar 2022 16:52:42 +0300 Subject: Remove check for inserted data when inserting individual data * gn3/db/sample_data.py (insert_sample_data)[__insert_data]: Move check to the main body. With this check here, you have 3 redundant checks. For a successful insert, it will insert the first value to the `PublishData` table and ignore the rest of the inserts. --- gn3/db/sample_data.py | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) (limited to 'gn3/db/sample_data.py') diff --git a/gn3/db/sample_data.py b/gn3/db/sample_data.py index 89e95fc..ccb03f2 100644 --- a/gn3/db/sample_data.py +++ b/gn3/db/sample_data.py @@ -252,19 +252,13 @@ def insert_sample_data(conn: Any, # pylint: disable=[R0913] """ def __insert_data(conn, table, value): if value and value != "x": - with conn.cursor() as cursor: - cursor.execute( - "SELECT Id FROM PublishData where Id = %s " - "AND StrainId = %s", - (data_id, strain_id)) - if not cursor.fetchone(): - columns = ", ".join(_MAP.get(table)) - cursor.execute((f"INSERT INTO {table} " - f"({columns}) " - f"VALUES (%s, %s, %s)"), - (strain_id, data_id, value)) - return cursor.rowcount + columns = ", ".join(_MAP.get(table)) + cursor.execute((f"INSERT INTO {table} " + f"({columns}) " + f"VALUES (%s, %s, %s)"), + (strain_id, data_id, value)) + return cursor.rowcount return 0 def __insert_case_attribute(conn, case_attr, value): @@ -304,6 +298,16 @@ def insert_sample_data(conn: Any, # pylint: disable=[R0913] try: count = 0 + + # Check if the data already exists: + with conn.cursor() as cursor: + cursor.execute( + "SELECT Id FROM PublishData where Id = %s " + "AND StrainId = %s", + (data_id, strain_id)) + if cursor.fetchone(): # Data already exists + return count + for header, value in zip(csv_header.split(","), data.split(",")): header = header.strip() value = value.strip() -- cgit v1.2.3