Skip to content

Commit 6d30da1

Browse files
authored
fix #2868 (#3028)
* fix #2868 * 2nd round * fix errors * more changes * fix errors * fix ProcessingJobTest * fix PY_PATCH * add missing TRN.add * encapsulated_query -> perform_as_transaction
1 parent 7f13ac2 commit 6d30da1

24 files changed

+267
-366
lines changed

qiita_db/analysis.py

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -355,11 +355,9 @@ def description(self, description):
355355
QiitaDBStatusError
356356
Analysis is public
357357
"""
358-
with qdb.sql_connection.TRN:
359-
sql = """UPDATE qiita.{0} SET description = %s
360-
WHERE analysis_id = %s""".format(self._table)
361-
qdb.sql_connection.TRN.add(sql, [description, self._id])
362-
qdb.sql_connection.TRN.execute()
358+
sql = """UPDATE qiita.{0} SET description = %s
359+
WHERE analysis_id = %s""".format(self._table)
360+
qdb.sql_connection.perform_as_transaction(sql, [description, self._id])
363361

364362
@property
365363
def samples(self):
@@ -513,11 +511,9 @@ def pmid(self, pmid):
513511
-----
514512
An analysis should only ever have one PMID attached to it.
515513
"""
516-
with qdb.sql_connection.TRN:
517-
sql = """UPDATE qiita.{0} SET pmid = %s
518-
WHERE analysis_id = %s""".format(self._table)
519-
qdb.sql_connection.TRN.add(sql, [pmid, self._id])
520-
qdb.sql_connection.TRN.execute()
514+
sql = """UPDATE qiita.{0} SET pmid = %s
515+
WHERE analysis_id = %s""".format(self._table)
516+
qdb.sql_connection.perform_as_transaction(sql, [pmid, self._id])
521517

522518
@property
523519
def can_be_publicized(self):
@@ -618,13 +614,11 @@ def set_error(self, error_msg):
618614
error_msg : str
619615
The error message
620616
"""
621-
with qdb.sql_connection.TRN:
622-
le = qdb.logger.LogEntry.create('Runtime', error_msg)
623-
sql = """UPDATE qiita.analysis
624-
SET logging_id = %s
625-
WHERE analysis_id = %s"""
626-
qdb.sql_connection.TRN.add(sql, [le.id, self.id])
627-
qdb.sql_connection.TRN.execute()
617+
le = qdb.logger.LogEntry.create('Runtime', error_msg)
618+
sql = """UPDATE qiita.analysis
619+
SET logging_id = %s
620+
WHERE analysis_id = %s"""
621+
qdb.sql_connection.perform_as_transaction(sql, [le.id, self.id])
628622

629623
def has_access(self, user):
630624
"""Returns whether the given user has access to the analysis
@@ -696,11 +690,9 @@ def share(self, user):
696690
if user.id == self.owner or user.id in self.shared_with:
697691
return
698692

699-
with qdb.sql_connection.TRN:
700-
sql = """INSERT INTO qiita.analysis_users (analysis_id, email)
701-
VALUES (%s, %s)"""
702-
qdb.sql_connection.TRN.add(sql, [self._id, user.id])
703-
qdb.sql_connection.TRN.execute()
693+
sql = """INSERT INTO qiita.analysis_users (analysis_id, email)
694+
VALUES (%s, %s)"""
695+
qdb.sql_connection.perform_as_transaction(sql, [self._id, user.id])
704696

705697
def unshare(self, user):
706698
"""Unshare the analysis with another user
@@ -710,11 +702,9 @@ def unshare(self, user):
710702
user: User object
711703
The user to unshare the analysis with
712704
"""
713-
with qdb.sql_connection.TRN:
714-
sql = """DELETE FROM qiita.analysis_users
715-
WHERE analysis_id = %s AND email = %s"""
716-
qdb.sql_connection.TRN.add(sql, [self._id, user.id])
717-
qdb.sql_connection.TRN.execute()
705+
sql = """DELETE FROM qiita.analysis_users
706+
WHERE analysis_id = %s AND email = %s"""
707+
qdb.sql_connection.perform_as_transaction(sql, [self._id, user.id])
718708

719709
def _lock_samples(self):
720710
"""Only dflt analyses can have samples added/removed

qiita_db/artifact.py

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -388,8 +388,7 @@ def _associate_with_analysis(instance, analysis_id):
388388
(analysis_id, artifact_id)
389389
VALUES (%s, %s)"""
390390
sql_args = [analysis_id, instance.id]
391-
qdb.sql_connection.TRN.add(sql, sql_args)
392-
qdb.sql_connection.TRN.execute()
391+
qdb.sql_connection.perform_as_transaction(sql, sql_args)
393392

394393
with qdb.sql_connection.TRN:
395394
if parents:
@@ -673,12 +672,10 @@ def name(self, value):
673672
ValueError
674673
If `value` contains more than 35 chars
675674
"""
676-
with qdb.sql_connection.TRN:
677-
sql = """UPDATE qiita.artifact
678-
SET name = %s
679-
WHERE artifact_id = %s"""
680-
qdb.sql_connection.TRN.add(sql, [value, self.id])
681-
qdb.sql_connection.TRN.execute()
675+
sql = """UPDATE qiita.artifact
676+
SET name = %s
677+
WHERE artifact_id = %s"""
678+
qdb.sql_connection.perform_as_transaction(sql, [value, self.id])
682679

683680
@property
684681
def timestamp(self):
@@ -751,8 +748,7 @@ def _set_visibility(self, value):
751748
sql = """UPDATE qiita.artifact
752749
SET visibility_id = %s
753750
WHERE artifact_id IN %s"""
754-
qdb.sql_connection.TRN.add(sql, [vis_id, tuple(ids)])
755-
qdb.sql_connection.TRN.execute()
751+
qdb.sql_connection.perform_as_transaction(sql, [vis_id, tuple(ids)])
756752

757753
@visibility.setter
758754
def visibility(self, value):
@@ -989,15 +985,13 @@ def is_submitted_to_vamps(self, value):
989985
QiitaDBOperationNotPermittedError
990986
If the artifact cannot be submitted to VAMPS
991987
"""
992-
with qdb.sql_connection.TRN:
993-
if not self.can_be_submitted_to_vamps:
994-
raise qdb.exceptions.QiitaDBOperationNotPermittedError(
995-
"Artifact %s cannot be submitted to VAMPS" % self.id)
996-
sql = """UPDATE qiita.artifact
997-
SET submitted_to_vamps = %s
998-
WHERE artifact_id = %s"""
999-
qdb.sql_connection.TRN.add(sql, [value, self.id])
1000-
qdb.sql_connection.TRN.execute()
988+
if not self.can_be_submitted_to_vamps:
989+
raise qdb.exceptions.QiitaDBOperationNotPermittedError(
990+
"Artifact %s cannot be submitted to VAMPS" % self.id)
991+
sql = """UPDATE qiita.artifact
992+
SET submitted_to_vamps = %s
993+
WHERE artifact_id = %s"""
994+
qdb.sql_connection.perform_as_transaction(sql, [value, self.id])
1001995

1002996
@property
1003997
def filepaths(self):

qiita_db/download_link.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,8 @@ def delete(cls, jti):
7272
jti : object
7373
The jwt token identifier
7474
"""
75-
with qdb.sql_connection.TRN:
76-
sql = """DELETE FROM qiita.{0} WHERE jti=%s""".format(cls._table)
77-
qdb.sql_connection.TRN.add(sql, [jti])
78-
qdb.sql_connection.TRN.execute()
75+
sql = """DELETE FROM qiita.{0} WHERE jti=%s""".format(cls._table)
76+
qdb.sql_connection.perform_as_transaction(sql, [jti])
7977

8078
@classmethod
8179
def exists(cls, jti):
@@ -98,10 +96,8 @@ def delete_expired(cls):
9896
r"""Deletes all expired download links"""
9997
now = datetime.now(timezone.utc)
10098

101-
with qdb.sql_connection.TRN:
102-
sql = """DELETE FROM qiita.{0} WHERE exp<%s""".format(cls._table)
103-
qdb.sql_connection.TRN.add(sql, [now])
104-
qdb.sql_connection.TRN.execute()
99+
sql = """DELETE FROM qiita.{0} WHERE exp<%s""".format(cls._table)
100+
qdb.sql_connection.perform_as_transaction(sql, [now])
105101

106102
@classmethod
107103
def get(cls, jti):

qiita_db/logger.py

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -181,12 +181,9 @@ def msg(self):
181181
def clear_info(self):
182182
"""Resets the list of info dicts to be an empty list
183183
"""
184-
with qdb.sql_connection.TRN:
185-
sql = """UPDATE qiita.{} SET information = %s
186-
WHERE logging_id = %s""".format(self._table)
187-
qdb.sql_connection.TRN.add(sql, [dumps([]), self.id])
188-
189-
qdb.sql_connection.TRN.execute()
184+
sql = """UPDATE qiita.{} SET information = %s
185+
WHERE logging_id = %s""".format(self._table)
186+
qdb.sql_connection.perform_as_transaction(sql, [dumps([]), self.id])
190187

191188
def add_info(self, info):
192189
"""Adds new information to the info associated with this LogEntry
@@ -201,12 +198,10 @@ def add_info(self, info):
201198
- When `info` is added, keys can be of any type, but upon retrieval,
202199
they will be of type str
203200
"""
204-
with qdb.sql_connection.TRN:
205-
current_info = self.info
206-
current_info.append(info)
207-
new_info = dumps(current_info)
201+
current_info = self.info
202+
current_info.append(info)
203+
new_info = dumps(current_info)
208204

209-
sql = """UPDATE qiita.{} SET information = %s
210-
WHERE logging_id = %s""".format(self._table)
211-
qdb.sql_connection.TRN.add(sql, [new_info, self.id])
212-
qdb.sql_connection.TRN.execute()
205+
sql = """UPDATE qiita.{} SET information = %s
206+
WHERE logging_id = %s""".format(self._table)
207+
qdb.sql_connection.perform_as_transaction(sql, [new_info, self.id])

qiita_db/meta_util.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -335,11 +335,9 @@ def sizeof_fmt(value, position):
335335

336336
# preparing vals to insert into DB
337337
vals = dumps(dict([x[:-1] for x in vals]))
338-
with qdb.sql_connection.TRN:
339-
sql = """INSERT INTO qiita.stats_daily (stats, stats_timestamp)
340-
VALUES (%s, NOW())"""
341-
qdb.sql_connection.TRN.add(sql, [vals])
342-
qdb.sql_connection.TRN.execute()
338+
sql = """INSERT INTO qiita.stats_daily (stats, stats_timestamp)
339+
VALUES (%s, NOW())"""
340+
qdb.sql_connection.perform_as_transaction(sql, [vals])
343341

344342
return missing_files
345343

qiita_db/metadata_template/base_metadata_template.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -279,19 +279,18 @@ def setitem(self, column, value):
279279
QiitaDBColumnError
280280
If the column does not exist in the table
281281
"""
282-
with qdb.sql_connection.TRN:
283-
# Check if the column exist in the table
284-
if column not in self._get_categories():
285-
raise qdb.exceptions.QiitaDBColumnError(
286-
"Column %s does not exist in %s" %
287-
(column, self._dynamic_table))
282+
# Check if the column exist in the table
283+
if column not in self._get_categories():
284+
raise qdb.exceptions.QiitaDBColumnError(
285+
"Column %s does not exist in %s" %
286+
(column, self._dynamic_table))
288287

289-
sql = """UPDATE qiita.{0}
290-
SET sample_values = sample_values || %s
291-
WHERE sample_id = %s""".format(self._dynamic_table)
288+
sql = """UPDATE qiita.{0}
289+
SET sample_values = sample_values || %s
290+
WHERE sample_id = %s""".format(self._dynamic_table)
292291

293-
qdb.sql_connection.TRN.add(sql, [dumps({column: value}), self.id])
294-
qdb.sql_connection.TRN.execute()
292+
qdb.sql_connection.perform_as_transaction(
293+
sql, [dumps({column: value}), self.id])
295294

296295
def __setitem__(self, column, value):
297296
r"""Sets the metadata value for the category `column`

qiita_db/metadata_template/prep_template.py

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -453,14 +453,13 @@ def investigation_type(self, investigation_type):
453453
QiitaDBColumnError
454454
If the investigation type is not a valid ENA ontology
455455
"""
456-
with qdb.sql_connection.TRN:
457-
if investigation_type is not None:
458-
self.validate_investigation_type(investigation_type)
456+
if investigation_type is not None:
457+
self.validate_investigation_type(investigation_type)
459458

460-
sql = """UPDATE qiita.prep_template SET investigation_type = %s
461-
WHERE {0} = %s""".format(self._id_column)
462-
qdb.sql_connection.TRN.add(sql, [investigation_type, self.id])
463-
qdb.sql_connection.TRN.execute()
459+
sql = """UPDATE qiita.prep_template SET investigation_type = %s
460+
WHERE {0} = %s""".format(self._id_column)
461+
qdb.sql_connection.perform_as_transaction(
462+
sql, [investigation_type, self.id])
464463

465464
@property
466465
def study_id(self):
@@ -494,11 +493,9 @@ def deprecated(self, deprecated):
494493
deprecated : bool
495494
If the prep info file is deprecated
496495
"""
497-
with qdb.sql_connection.TRN:
498-
sql = """UPDATE qiita.prep_template SET deprecated = %s
499-
WHERE {0} = %s""".format(self._id_column)
500-
qdb.sql_connection.TRN.add(sql, [deprecated, self.id])
501-
qdb.sql_connection.TRN.execute()
496+
sql = """UPDATE qiita.prep_template SET deprecated = %s
497+
WHERE {0} = %s""".format(self._id_column)
498+
qdb.sql_connection.perform_as_transaction(sql, [deprecated, self.id])
502499

503500
def generate_files(self, samples=None, columns=None):
504501
r"""Generates all the files that contain data from this template
@@ -761,12 +758,10 @@ def name(self):
761758
@name.setter
762759
def name(self, value):
763760
"""Changes the name of the prep template"""
764-
with qdb.sql_connection.TRN:
765-
sql = """UPDATE qiita.prep_template
766-
SET name = %s
767-
WHERE prep_template_id = %s"""
768-
qdb.sql_connection.TRN.add(sql, [value, self.id])
769-
qdb.sql_connection.TRN.execute()
761+
sql = """UPDATE qiita.prep_template
762+
SET name = %s
763+
WHERE prep_template_id = %s"""
764+
qdb.sql_connection.perform_as_transaction(sql, [value, self.id])
770765

771766
def to_dataframe(self, add_ebi_accessions=False):
772767
"""Returns the metadata template as a dataframe

qiita_db/ontology.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -77,16 +77,14 @@ def add_user_defined_term(self, term):
7777
term : str
7878
New user defined term to add into a given ontology
7979
"""
80-
with qdb.sql_connection.TRN:
81-
# we don't need to add an existing term
82-
terms = self.user_defined_terms + self.terms
83-
84-
if term not in terms:
85-
sql = """INSERT INTO qiita.term
86-
(ontology_id, term, user_defined)
87-
VALUES (%s, %s, true);"""
88-
qdb.sql_connection.TRN.add(sql, [self.id, term])
89-
qdb.sql_connection.TRN.execute()
80+
# we don't need to add an existing term
81+
terms = self.user_defined_terms + self.terms
82+
83+
if term not in terms:
84+
sql = """INSERT INTO qiita.term
85+
(ontology_id, term, user_defined)
86+
VALUES (%s, %s, true);"""
87+
qdb.sql_connection.perform_as_transaction(sql, [self.id, term])
9088

9189
def term_type(self, term):
9290
"""Get the type of a given ontology term

qiita_db/portal.py

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -72,38 +72,36 @@ def create(cls, portal, desc):
7272
QiitaDBDuplicateError
7373
Portal name already exists
7474
"""
75-
with qdb.sql_connection.TRN:
76-
if cls.exists(portal):
77-
raise qdb.exceptions.QiitaDBDuplicateError("Portal", portal)
78-
79-
# Add portal and default analyses for all users
80-
sql = """DO $do$
81-
DECLARE
82-
pid bigint;
83-
eml varchar;
84-
aid bigint;
85-
BEGIN
86-
INSERT INTO qiita.portal_type (portal, portal_description)
87-
VALUES (%s, %s)
88-
RETURNING portal_type_id INTO pid;
89-
90-
FOR eml IN
91-
SELECT email FROM qiita.qiita_user
92-
LOOP
93-
INSERT INTO qiita.analysis
94-
(email, name, description, dflt)
95-
VALUES (eml, eml || '-dflt', 'dflt', true)
96-
RETURNING analysis_id INTO aid;
97-
98-
INSERT INTO qiita.analysis_portal
99-
(analysis_id, portal_type_id)
100-
VALUES (aid, pid);
101-
END LOOP;
102-
END $do$;"""
103-
qdb.sql_connection.TRN.add(sql, [portal, desc])
104-
qdb.sql_connection.TRN.execute()
75+
if cls.exists(portal):
76+
raise qdb.exceptions.QiitaDBDuplicateError("Portal", portal)
77+
78+
# Add portal and default analyses for all users
79+
sql = """DO $do$
80+
DECLARE
81+
pid bigint;
82+
eml varchar;
83+
aid bigint;
84+
BEGIN
85+
INSERT INTO qiita.portal_type (portal, portal_description)
86+
VALUES (%s, %s)
87+
RETURNING portal_type_id INTO pid;
88+
89+
FOR eml IN
90+
SELECT email FROM qiita.qiita_user
91+
LOOP
92+
INSERT INTO qiita.analysis
93+
(email, name, description, dflt)
94+
VALUES (eml, eml || '-dflt', 'dflt', true)
95+
RETURNING analysis_id INTO aid;
96+
97+
INSERT INTO qiita.analysis_portal
98+
(analysis_id, portal_type_id)
99+
VALUES (aid, pid);
100+
END LOOP;
101+
END $do$;"""
102+
qdb.sql_connection.perform_as_transaction(sql, [portal, desc])
105103

106-
return cls(portal)
104+
return cls(portal)
107105

108106
@staticmethod
109107
def delete(portal):

0 commit comments

Comments
 (0)