From 6bb51023032281f28736a4c761fa50e3072327e1 Mon Sep 17 00:00:00 2001 From: BonfaceKilz Date: Wed, 22 Dec 2021 14:03:27 +0300 Subject: traits.py Delete a record only if it exists In the case when the user tries to delete the same data twice, prior to this commit, an error was being generated. This commit remedies this by checking if a record exists prior to deleting it. --- gn3/db/traits.py | 39 ++++++++++++++++++++++----------------- 1 file changed, 22 insertions(+), 17 deletions(-) (limited to 'gn3/db') diff --git a/gn3/db/traits.py b/gn3/db/traits.py index 68b6059..9d5ddd1 100644 --- a/gn3/db/traits.py +++ b/gn3/db/traits.py @@ -203,25 +203,30 @@ def delete_sample_data(conn: Any, "AND Strain.Name = \"%s\"") % (trait_name, phenotype_id, str(strain_name))) - strain_id, data_id = cursor.fetchone() - cursor.execute(("DELETE FROM PublishData " + # Check if it exists if the data was already deleted: + if _result := cursor.fetchone(): + strain_id, data_id = _result + + # Only run if the strain_id and data_id exist + if strain_id and data_id: + cursor.execute(("DELETE FROM PublishData " "WHERE StrainId = %s AND Id = %s") - % (strain_id, data_id)) - deleted_published_data = cursor.rowcount - - # Delete the PublishSE table - cursor.execute(("DELETE FROM PublishSE " - "WHERE StrainId = %s AND DataId = %s") % - (strain_id, data_id)) - deleted_se_data = cursor.rowcount - - # Delete the NStrain table - cursor.execute(("DELETE FROM NStrain " - "WHERE StrainId = %s AND DataId = %s" % - (strain_id, data_id))) - deleted_n_strains = cursor.rowcount - except Exception as e: #pylint: disable=[C0103, W0612] + % (strain_id, data_id)) + deleted_published_data = cursor.rowcount + + # Delete the PublishSE table + cursor.execute(("DELETE FROM PublishSE " + "WHERE StrainId = %s AND DataId = %s") % + (strain_id, data_id)) + deleted_se_data = cursor.rowcount + + # Delete the NStrain table + cursor.execute(("DELETE FROM NStrain " + "WHERE StrainId = %s AND DataId = %s" % + (strain_id, data_id))) + deleted_n_strains = cursor.rowcount + except Exception as e: #pylint: disable=[C0103, W0612] conn.rollback() raise MySQLdb.Error conn.commit() -- cgit v1.2.3 From 1acd3873f44e0edff055c07fbb6d11066920f379 Mon Sep 17 00:00:00 2001 From: BonfaceKilz Date: Tue, 4 Jan 2022 13:12:36 +0300 Subject: traits.py: Return early during an insert if the give record exists Sometimes, a user will try to insert data twice, on in some instances, 2 different users will attempt the same inserts of the same records separately. In such cases, ignore the insert, and return early. --- gn3/db/traits.py | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'gn3/db') diff --git a/gn3/db/traits.py b/gn3/db/traits.py index 9d5ddd1..2cab94e 100644 --- a/gn3/db/traits.py +++ b/gn3/db/traits.py @@ -259,6 +259,13 @@ def insert_sample_data(conn: Any, #pylint: disable=[R0913] (strain_name,)) strain_id = cursor.fetchone() + # Return early if an insert already exists! + cursor.execute("SELECT Id FROM PublishData where Id = %s " + "AND StrainId = %s AND value = %s ", + (data_id, strain_id, value)) + if cursor.fetchone(): # This strain already exists + return (0, 0, 0) + # Insert the PublishData table cursor.execute(("INSERT INTO PublishData (Id, StrainId, value)" "VALUES (%s, %s, %s)"), -- cgit v1.2.3 From 2d6a6f3cffc93718d405ab9c88c305fb44fd1752 Mon Sep 17 00:00:00 2001 From: BonfaceKilz Date: Tue, 4 Jan 2022 17:02:05 +0300 Subject: traits.py: Don't use "value" to check if a record exists The problem with using the "value" record is that it's a floating point number. See on why comparing floating point numbers can be an issue. --- gn3/db/traits.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'gn3/db') diff --git a/gn3/db/traits.py b/gn3/db/traits.py index 2cab94e..338b320 100644 --- a/gn3/db/traits.py +++ b/gn3/db/traits.py @@ -110,7 +110,6 @@ def get_trait_csv_sample_data(conn: Any, def update_sample_data(conn: Any, #pylint: disable=[R0913] - trait_name: str, strain_name: str, phenotype_id: int, @@ -261,8 +260,8 @@ def insert_sample_data(conn: Any, #pylint: disable=[R0913] # Return early if an insert already exists! cursor.execute("SELECT Id FROM PublishData where Id = %s " - "AND StrainId = %s AND value = %s ", - (data_id, strain_id, value)) + "AND StrainId = %s", + (data_id, strain_id)) if cursor.fetchone(): # This strain already exists return (0, 0, 0) -- cgit v1.2.3