|
32 | 32 | from .base import QiitaStatusObject |
33 | 33 | from .data import ProcessedData |
34 | 34 | from .study import Study |
35 | | -from .exceptions import QiitaDBStatusError, QiitaDBError |
| 35 | +from .exceptions import QiitaDBStatusError, QiitaDBError, QiitaDBUnknownIDError |
36 | 36 | from .util import (convert_to_id, get_work_base_dir, |
37 | 37 | get_mountpoint, insert_filepaths) |
38 | 38 |
|
@@ -66,9 +66,13 @@ class Analysis(QiitaStatusObject): |
66 | 66 | unshare |
67 | 67 | build_files |
68 | 68 | summary_data |
| 69 | + exists |
| 70 | + create |
| 71 | + delete |
69 | 72 | """ |
70 | 73 |
|
71 | 74 | _table = "analysis" |
| 75 | + _analysis_id_column = 'analysis_id' |
72 | 76 |
|
73 | 77 | def _lock_check(self, conn_handler): |
74 | 78 | """Raises QiitaDBStatusError if analysis is not in_progress""" |
@@ -165,6 +169,73 @@ def create(cls, owner, name, description, parent=None, from_default=False): |
165 | 169 | a_id = conn_handler.execute_queue(queue)[0] |
166 | 170 | return cls(a_id) |
167 | 171 |
|
| 172 | + @classmethod |
| 173 | + def delete(cls, _id): |
| 174 | + """Deletes an analysis |
| 175 | +
|
| 176 | + Parameters |
| 177 | + ---------- |
| 178 | + _id : int |
| 179 | + The analysis id |
| 180 | +
|
| 181 | + Raises |
| 182 | + ------ |
| 183 | + QiitaDBUnknownIDError |
| 184 | + If the analysis id doesn't exist |
| 185 | + """ |
| 186 | + # check if the analysis exist |
| 187 | + if not cls.exists(_id): |
| 188 | + raise QiitaDBUnknownIDError(_id, "analysis") |
| 189 | + |
| 190 | + queue = "delete_analysis_%d" % _id |
| 191 | + conn_handler = SQLConnectionHandler() |
| 192 | + conn_handler.create_queue(queue) |
| 193 | + |
| 194 | + sql = ("DELETE FROM qiita.analysis_filepath WHERE " |
| 195 | + "{0} = {1}".format(cls._analysis_id_column, _id)) |
| 196 | + conn_handler.add_to_queue(queue, sql) |
| 197 | + |
| 198 | + sql = ("DELETE FROM qiita.analysis_workflow WHERE " |
| 199 | + "{0} = {1}".format(cls._analysis_id_column, _id)) |
| 200 | + conn_handler.add_to_queue(queue, sql) |
| 201 | + |
| 202 | + sql = ("DELETE FROM qiita.analysis_sample WHERE " |
| 203 | + "{0} = {1}".format(cls._analysis_id_column, _id)) |
| 204 | + conn_handler.add_to_queue(queue, sql) |
| 205 | + |
| 206 | + sql = ("DELETE FROM qiita.collection_analysis WHERE " |
| 207 | + "{0} = {1}".format(cls._analysis_id_column, _id)) |
| 208 | + conn_handler.add_to_queue(queue, sql) |
| 209 | + |
| 210 | + # TODO: issue #1176 |
| 211 | + |
| 212 | + sql = ("DELETE FROM qiita.{0} WHERE " |
| 213 | + "{1} = {2}".format(cls._table, cls._analysis_id_column, _id)) |
| 214 | + conn_handler.add_to_queue(queue, sql) |
| 215 | + |
| 216 | + conn_handler.execute_queue(queue) |
| 217 | + |
| 218 | + @classmethod |
| 219 | + def exists(cls, analysis_id): |
| 220 | + r"""Checks if the given analysis _id exists |
| 221 | +
|
| 222 | + Parameters |
| 223 | + ---------- |
| 224 | + analysis_id : int |
| 225 | + The id of the analysis we are searching for |
| 226 | +
|
| 227 | + Returns |
| 228 | + ------- |
| 229 | + bool |
| 230 | + True if exists, false otherwise. |
| 231 | + """ |
| 232 | + conn_handler = SQLConnectionHandler() |
| 233 | + |
| 234 | + return conn_handler.execute_fetchone( |
| 235 | + "SELECT EXISTS(SELECT * FROM qiita.{0} WHERE " |
| 236 | + "{1}=%s)".format(cls._table, cls._analysis_id_column), |
| 237 | + (analysis_id, ))[0] |
| 238 | + |
168 | 239 | # ---- Properties ---- |
169 | 240 | @property |
170 | 241 | def owner(self): |
|
0 commit comments