From 899268a45eab68c7712d6bee04fc0c577caca3d1 Mon Sep 17 00:00:00 2001 From: Antonio Gonzalez Date: Fri, 18 Aug 2017 04:48:00 -0600 Subject: [PATCH 1/2] delete-root-analysis-artifact --- qiita_db/artifact.py | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/qiita_db/artifact.py b/qiita_db/artifact.py index f9321f1f6..549f0ed4e 100644 --- a/qiita_db/artifact.py +++ b/qiita_db/artifact.py @@ -573,21 +573,25 @@ def delete(cls, artifact_id): WHERE artifact_id = %s""" qdb.sql_connection.TRN.add(sql, [artifact_id]) - # If the artifact doesn't have parents, we move the files to the - # uploads folder. We also need to nullify the column in the prep - # template table - if not instance.parents: - qdb.util.move_filepaths_to_upload_folder(study.id, filepaths) - - sql = """UPDATE qiita.prep_template - SET artifact_id = NULL - WHERE prep_template_id IN %s""" - qdb.sql_connection.TRN.add( - sql, [tuple(pt.id for pt in instance.prep_templates)]) - else: - sql = """DELETE FROM qiita.parent_artifact - WHERE artifact_id = %s""" - qdb.sql_connection.TRN.add(sql, [artifact_id]) + # This block only applies if the artifact has a study AKA not an + # analysis + if study is not None: + # If the artifact doesn't have parents, we move the files to + # the uploads folder. We also need to nullify the column in + # the prep template table + if not instance.parents: + qdb.util.move_filepaths_to_upload_folder( + study.id, filepaths) + + sql = """UPDATE qiita.prep_template + SET artifact_id = NULL + WHERE prep_template_id IN %s""" + qdb.sql_connection.TRN.add( + sql, [tuple(pt.id for pt in instance.prep_templates)]) + else: + sql = """DELETE FROM qiita.parent_artifact + WHERE artifact_id = %s""" + qdb.sql_connection.TRN.add(sql, [artifact_id]) # Detach the artifact from the study_artifact table sql = "DELETE FROM qiita.study_artifact WHERE artifact_id = %s" From 3e1787620d22b39d1824a8efa36eb8206934e347 Mon Sep 17 00:00:00 2001 From: Antonio Gonzalez Date: Fri, 18 Aug 2017 05:01:40 -0600 Subject: [PATCH 2/2] fix errors --- qiita_db/artifact.py | 35 ++++++++++++++++------------------ qiita_db/test/test_artifact.py | 3 +++ 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/qiita_db/artifact.py b/qiita_db/artifact.py index 549f0ed4e..15aa20a7f 100644 --- a/qiita_db/artifact.py +++ b/qiita_db/artifact.py @@ -573,25 +573,22 @@ def delete(cls, artifact_id): WHERE artifact_id = %s""" qdb.sql_connection.TRN.add(sql, [artifact_id]) - # This block only applies if the artifact has a study AKA not an - # analysis - if study is not None: - # If the artifact doesn't have parents, we move the files to - # the uploads folder. We also need to nullify the column in - # the prep template table - if not instance.parents: - qdb.util.move_filepaths_to_upload_folder( - study.id, filepaths) - - sql = """UPDATE qiita.prep_template - SET artifact_id = NULL - WHERE prep_template_id IN %s""" - qdb.sql_connection.TRN.add( - sql, [tuple(pt.id for pt in instance.prep_templates)]) - else: - sql = """DELETE FROM qiita.parent_artifact - WHERE artifact_id = %s""" - qdb.sql_connection.TRN.add(sql, [artifact_id]) + # If the artifact doesn't have parents and study is not None (is an + # analysis), we move the files to the uploads folder. We also need + # to nullify the column in the prep template table + if not instance.parents and study is not None: + qdb.util.move_filepaths_to_upload_folder( + study.id, filepaths) + + sql = """UPDATE qiita.prep_template + SET artifact_id = NULL + WHERE prep_template_id IN %s""" + qdb.sql_connection.TRN.add( + sql, [tuple(pt.id for pt in instance.prep_templates)]) + else: + sql = """DELETE FROM qiita.parent_artifact + WHERE artifact_id = %s""" + qdb.sql_connection.TRN.add(sql, [artifact_id]) # Detach the artifact from the study_artifact table sql = "DELETE FROM qiita.study_artifact WHERE artifact_id = %s" diff --git a/qiita_db/test/test_artifact.py b/qiita_db/test/test_artifact.py index 6441f6baf..eb573eb1f 100644 --- a/qiita_db/test/test_artifact.py +++ b/qiita_db/test/test_artifact.py @@ -721,6 +721,9 @@ def test_create_root_analysis(self): self.assertIsNone(obs.study) self.assertEqual(obs.analysis, qdb.analysis.Analysis(1)) + # testing that it can be deleted + qdb.artifact.Artifact.delete(obs.id) + def test_create_processed(self): exp_params = qdb.software.Parameters.from_default_params( qdb.software.DefaultParameters(1), {'input_data': 1})