From e926411480c69c0c420fd3c781664120bfd8e21b Mon Sep 17 00:00:00 2001 From: Frederick Muriuki Muriithi Date: Wed, 12 Jul 2023 15:46:53 +0300 Subject: Bug: Set the `data_exists` variable within the context manager The `cursor.fetchone()` call that was used as the condition to the `if` was called outside of the context manager, and therefore would always give a non-truthy value at best and an inconsistent result at worst. This commit gets the value before the context manager has exited and stores it for later use. --- gn3/db/sample_data.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/gn3/db/sample_data.py b/gn3/db/sample_data.py index a3a6ded..140e62e 100644 --- a/gn3/db/sample_data.py +++ b/gn3/db/sample_data.py @@ -407,10 +407,9 @@ def insert_sample_data( cursor.execute( "SELECT Id FROM PublishData where Id = %s " "AND StrainId = %s", - (data_id, strain_id), - ) - conn.commit() - if cursor.fetchone(): # Data already exists + (data_id, strain_id)) + data_exists = cursor.fetchone() + if data_exists: # Data already exists return count for header, value in zip(csv_header.split(","), data.split(",")): -- cgit v1.2.3