Skip to content

Commit 01c656c

Browse files
authored
Merge pull request #2071 from antgonza/fix-969-db
database changes to fix 969
2 parents 6360675 + 18d77e1 commit 01c656c

File tree

5 files changed

+2002
-1704
lines changed

5 files changed

+2002
-1704
lines changed

qiita_db/study.py

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,42 @@ def delete(cls, id_):
464464

465465
qdb.sql_connection.TRN.execute()
466466

467+
@classmethod
468+
def get_tags(cls):
469+
"""Returns the available study tags
470+
471+
Returns
472+
-------
473+
list of DictCursor
474+
Table-like structure of metadata, one tag per row. Can be
475+
accessed as a list of dictionaries, keyed on column name.
476+
"""
477+
with qdb.sql_connection.TRN:
478+
sql = """SELECT study_tag_id, study_tag
479+
FROM qiita.study_tags"""
480+
481+
qdb.sql_connection.TRN.add(sql)
482+
return qdb.sql_connection.TRN.execute_fetchindex()
483+
484+
@classmethod
485+
def insert_tags(cls, user, tags):
486+
"""Insert available study tags
487+
488+
Parameters
489+
----------
490+
user : qiita_db.user.User
491+
The user adding the tags
492+
tags : list of str
493+
The list of tags to add
494+
"""
495+
with qdb.sql_connection.TRN:
496+
sql = """INSERT INTO qiita.study_tags (email, study_tag)
497+
VALUES (%s, %s)"""
498+
sql_args = [[user.email, tag] for tag in tags]
499+
500+
qdb.sql_connection.TRN.add(sql, sql_args, many=True)
501+
qdb.sql_connection.TRN.execute()
502+
467503

468504
# --- Attributes ---
469505
@property
@@ -921,7 +957,52 @@ def ebi_submission_status(self, value):
921957

922958
ebi_submission_status.__doc__.format(', '.join(_VALID_EBI_STATUS))
923959

924-
# --- methods ---
960+
@property
961+
def tags(self):
962+
"""Returns the tags of the study
963+
964+
Returns
965+
-------
966+
list of str
967+
The study tags
968+
"""
969+
with qdb.sql_connection.TRN:
970+
sql = """SELECT study_tag_id, study_tag
971+
FROM qiita.study_tags
972+
LEFT JOIN qiita.per_study_tags USING (study_tag_id)
973+
WHERE study_id = {0}""".format(self._id)
974+
qdb.sql_connection.TRN.add(sql)
975+
return qdb.sql_connection.TRN.execute_fetchindex()
976+
977+
@tags.setter
978+
def tags(self, tag_ids):
979+
"""Sets the tags of the study
980+
981+
Parameters
982+
----------
983+
tag_ids : list of int
984+
The tag ids of the study
985+
"""
986+
with qdb.sql_connection.TRN:
987+
sql = """DELETE FROM qiita.per_study_tags WHERE study_id = %s"""
988+
qdb.sql_connection.TRN.add(sql, [self._id])
989+
990+
if tag_ids:
991+
sql = """INSERT INTO qiita.per_study_tags
992+
(study_tag_id, study_id)
993+
SELECT %s, %s
994+
WHERE
995+
NOT EXISTS (
996+
SELECT study_tag_id, study_id
997+
FROM qiita.per_study_tags
998+
WHERE study_tag_id = %s AND study_id = %s
999+
)"""
1000+
sql_args = [[tid, self._id, tid, self._id] for tid in tag_ids]
1001+
qdb.sql_connection.TRN.add(sql, sql_args, many=True)
1002+
1003+
qdb.sql_connection.TRN.execute()
1004+
1005+
# --- methods ---
9251006
def artifacts(self, dtype=None, artifact_type=None):
9261007
"""Returns the list of artifacts associated with the study
9271008

qiita_db/support_files/patches/50.sql

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
-- Feb 3, 2017
2+
-- adding study tagging system
3+
4+
CREATE TABLE qiita.study_tags (
5+
study_tag_id bigserial NOT NULL,
6+
email varchar NOT NULL,
7+
study_tag varchar NOT NULL,
8+
CONSTRAINT pk_study_tag UNIQUE ( study_tag ),
9+
CONSTRAINT pk_study_tag_id PRIMARY KEY ( study_tag_id )
10+
) ;
11+
12+
CREATE INDEX idx_study_tag_id ON qiita.study_tags ( study_tag_id ) ;
13+
ALTER TABLE qiita.study_tags ADD CONSTRAINT fk_study_tags FOREIGN KEY ( email ) REFERENCES qiita.qiita_user( email );
14+
15+
CREATE TABLE qiita.per_study_tags (
16+
study_tag_id bigint NOT NULL,
17+
study_id bigint NOT NULL,
18+
CONSTRAINT pk_per_study_tags PRIMARY KEY ( study_tag_id, study_id )
19+
) ;

0 commit comments

Comments
 (0)