diff --git a/qiita_db/analysis.py b/qiita_db/analysis.py index c7e44855f..0cb388564 100644 --- a/qiita_db/analysis.py +++ b/qiita_db/analysis.py @@ -215,6 +215,22 @@ def create(cls, owner, name, description, from_default=False, job.submit() return instance + @classmethod + def delete_analysis_artifacts(cls, _id): + """Deletes the artifacts linked to an artifact and then the analysis + + Parameters + ---------- + _id : int + The analysis id + """ + analysis = cls(_id) + aids = [a.id for a in analysis.artifacts if not a.parents] + aids.sort(reverse=True) + for aid in aids: + cls.delete(aid) + cls.delete(analysis.id) + @classmethod def delete(cls, _id): """Deletes an analysis diff --git a/qiita_db/metadata_template/prep_template.py b/qiita_db/metadata_template/prep_template.py index 3ae26d17d..e3d4e8439 100644 --- a/qiita_db/metadata_template/prep_template.py +++ b/qiita_db/metadata_template/prep_template.py @@ -11,7 +11,6 @@ from iteration_utilities import duplicates from qiita_core.exceptions import IncompetentQiitaDeveloperError -from qiita_ware.private_plugin import _delete_analysis_artifacts import qiita_db as qdb from .constants import (PREP_TEMPLATE_COLUMNS, TARGET_GENE_DATA_TYPES, PREP_TEMPLATE_COLUMNS_TARGET_GENE) @@ -281,6 +280,7 @@ def delete(cls, id_): qdb.sql_connection.TRN.add(sql, args) archived_artifacts = set( qdb.sql_connection.TRN.execute_fetchflatten()) + ANALYSIS = qdb.analysis.Analysis if archived_artifacts: for aid in archived_artifacts: # before we can delete the archived artifact, we need @@ -294,9 +294,11 @@ def delete(cls, id_): qdb.sql_connection.TRN.add(sql, [aid]) analyses = set( qdb.sql_connection.TRN.execute_fetchflatten()) - for _id in analyses: - _delete_analysis_artifacts(qdb.analysis.Analysis(_id)) - qdb.artifact.Artifact.delete(aid) + if analyses: + for _id in analyses: + ANALYSIS.delete_analysis_artifacts(_id) + else: + ANALYSIS.delete(aid) # Delete the prep template filepaths sql = """DELETE FROM qiita.prep_template_filepath diff --git a/qiita_db/test/test_artifact.py b/qiita_db/test/test_artifact.py index bed71c45a..2319acfaa 100644 --- a/qiita_db/test/test_artifact.py +++ b/qiita_db/test/test_artifact.py @@ -23,7 +23,6 @@ from qiita_core.util import qiita_test_checker from qiita_core.testing import wait_for_processing_job import qiita_db as qdb -from qiita_ware.private_plugin import _delete_analysis_artifacts class ArtifactTestsReadOnly(TestCase): @@ -1559,9 +1558,9 @@ def test_archive(self): qdb.sql_connection.perform_as_transaction(sql) sql = "UPDATE qiita.artifact SET visibility_id = 1" qdb.sql_connection.perform_as_transaction(sql) - _delete_analysis_artifacts(qdb.analysis.Analysis(1)) - _delete_analysis_artifacts(qdb.analysis.Analysis(2)) - _delete_analysis_artifacts(qdb.analysis.Analysis(3)) + qdb.analysis.Analysis.delete_analysis_artifacts(1) + qdb.analysis.Analysis.delete_analysis_artifacts(2) + qdb.analysis.Analysis.delete_analysis_artifacts(3) for aid in [3, 2, 1]: A.delete(aid) diff --git a/qiita_ware/private_plugin.py b/qiita_ware/private_plugin.py index f050499a1..c07a12fed 100644 --- a/qiita_ware/private_plugin.py +++ b/qiita_ware/private_plugin.py @@ -292,14 +292,6 @@ def delete_sample_or_column(job): job._set_status('success') -def _delete_analysis_artifacts(analysis): - aids = [a.id for a in analysis.artifacts if not a.parents] - aids.sort(reverse=True) - for aid in aids: - qdb.artifact.Artifact.delete(aid) - qdb.analysis.Analysis.delete(analysis.id) - - def delete_study(job): """Deletes a full study @@ -315,7 +307,7 @@ def delete_study(job): # deleting analyses for analysis in study.analyses(): - _delete_analysis_artifacts(analysis) + qdb.analysis.Analysis.delete_analysis_artifacts(analysis) for pt in study.prep_templates(): if pt.artifact is not None: @@ -375,9 +367,7 @@ def delete_analysis(job): """ with qdb.sql_connection.TRN: analysis_id = job.parameters.values['analysis_id'] - analysis = qdb.analysis.Analysis(analysis_id) - - _delete_analysis_artifacts(analysis) + qdb.analysis.Analysis.delete_analysis_artifacts(analysis_id) r_client.delete('analysis_delete_%d' % analysis_id)