Skip to content

Commit

Permalink
Merge pull request #2071 from antgonza/fix-969-db
Browse files Browse the repository at this point in the history
database changes to fix 969
  • Loading branch information
josenavas committed Feb 6, 2017
2 parents 6360675 + 18d77e1 commit 01c656c
Show file tree
Hide file tree
Showing 5 changed files with 2,002 additions and 1,704 deletions.
83 changes: 82 additions & 1 deletion qiita_db/study.py
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,42 @@ def delete(cls, id_):

qdb.sql_connection.TRN.execute()

@classmethod
def get_tags(cls):
"""Returns the available study tags
Returns
-------
list of DictCursor
Table-like structure of metadata, one tag per row. Can be
accessed as a list of dictionaries, keyed on column name.
"""
with qdb.sql_connection.TRN:
sql = """SELECT study_tag_id, study_tag
FROM qiita.study_tags"""

qdb.sql_connection.TRN.add(sql)
return qdb.sql_connection.TRN.execute_fetchindex()

@classmethod
def insert_tags(cls, user, tags):
"""Insert available study tags
Parameters
----------
user : qiita_db.user.User
The user adding the tags
tags : list of str
The list of tags to add
"""
with qdb.sql_connection.TRN:
sql = """INSERT INTO qiita.study_tags (email, study_tag)
VALUES (%s, %s)"""
sql_args = [[user.email, tag] for tag in tags]

qdb.sql_connection.TRN.add(sql, sql_args, many=True)
qdb.sql_connection.TRN.execute()


# --- Attributes ---
@property
Expand Down Expand Up @@ -921,7 +957,52 @@ def ebi_submission_status(self, value):

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

# --- methods ---
@property
def tags(self):
"""Returns the tags of the study
Returns
-------
list of str
The study tags
"""
with qdb.sql_connection.TRN:
sql = """SELECT study_tag_id, study_tag
FROM qiita.study_tags
LEFT JOIN qiita.per_study_tags USING (study_tag_id)
WHERE study_id = {0}""".format(self._id)
qdb.sql_connection.TRN.add(sql)
return qdb.sql_connection.TRN.execute_fetchindex()

@tags.setter
def tags(self, tag_ids):
"""Sets the tags of the study
Parameters
----------
tag_ids : list of int
The tag ids of the study
"""
with qdb.sql_connection.TRN:
sql = """DELETE FROM qiita.per_study_tags WHERE study_id = %s"""
qdb.sql_connection.TRN.add(sql, [self._id])

if tag_ids:
sql = """INSERT INTO qiita.per_study_tags
(study_tag_id, study_id)
SELECT %s, %s
WHERE
NOT EXISTS (
SELECT study_tag_id, study_id
FROM qiita.per_study_tags
WHERE study_tag_id = %s AND study_id = %s
)"""
sql_args = [[tid, self._id, tid, self._id] for tid in tag_ids]
qdb.sql_connection.TRN.add(sql, sql_args, many=True)

qdb.sql_connection.TRN.execute()

# --- methods ---
def artifacts(self, dtype=None, artifact_type=None):
"""Returns the list of artifacts associated with the study
Expand Down
19 changes: 19 additions & 0 deletions qiita_db/support_files/patches/50.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
-- Feb 3, 2017
-- adding study tagging system

CREATE TABLE qiita.study_tags (
study_tag_id bigserial NOT NULL,
email varchar NOT NULL,
study_tag varchar NOT NULL,
CONSTRAINT pk_study_tag UNIQUE ( study_tag ),
CONSTRAINT pk_study_tag_id PRIMARY KEY ( study_tag_id )
) ;

CREATE INDEX idx_study_tag_id ON qiita.study_tags ( study_tag_id ) ;
ALTER TABLE qiita.study_tags ADD CONSTRAINT fk_study_tags FOREIGN KEY ( email ) REFERENCES qiita.qiita_user( email );

CREATE TABLE qiita.per_study_tags (
study_tag_id bigint NOT NULL,
study_id bigint NOT NULL,
CONSTRAINT pk_per_study_tags PRIMARY KEY ( study_tag_id, study_id )
) ;

0 comments on commit 01c656c

Please sign in to comment.