def add_owner(owner):    
        ouser = pmig.sql_x("SELECT user FROM user_relations WHERE user = %s", (owner,))
        if ouser:
            jassigned = pmig.sql_x("SELECT assigned FROM user_relations WHERE user = %s", (owner,))
            jflat = tflatten(jassigned)
            if any(jflat):
                assigned = json.loads(jassigned[0][0])
            else:
                assigned = []
            if bugid not in assigned:
                log("Assigning %s to %s" % (str(bugid), owner))
                assigned.append(bugid)
            vlog("owner %s" % (str(assigned),))
            pmig.sql_x("UPDATE user_relations SET assigned=%s, modified=%s WHERE user = %s", (json.dumps(assigned),
                                                                                              now(),
                                                                                              owner))
        else:
            vlog('inserting new record')
            assigned = json.dumps([bugid])
            insert_values =  (owner,
                              assigned,
                              now(),
                              now())

            pmig.sql_x("INSERT INTO user_relations (user, assigned, created, modified) VALUES (%s, %s, %s, %s)",
                       insert_values)
    def add_comment_ref(owner):    
        """ adds an issue reference to a user or later updating their comments
        """
        ouser = pmig.sql_x("SELECT user FROM user_relations_comments WHERE user = %s", (owner,))
        if ouser:
            jcommed = pmig.sql_x("SELECT issues FROM user_relations_comments WHERE user = %s", (owner,))
            if jcommed and any(tflatten(jcommed)):
                issues = json.loads(jcommed[0][0])
            else:
                issues = []

            if bugid not in issues:
                log("Comment reference %s to %s" % (str(bugid), owner))
                issues.append(bugid)
            pmig.sql_x("UPDATE user_relations_comments SET issues=%s, modified=%s WHERE user = %s", (json.dumps(issues),
                                                                                                     now(),
                                                                                                     owner))
        else:
            issues = json.dumps([bugid])
            insert_values =  (owner,
                              issues,
                              now(),
                              now())

            pmig.sql_x("INSERT INTO user_relations_comments (user, issues, created, modified) VALUES (%s, %s, %s, %s)",
                       insert_values)
 def add_cc(ccuser):
     eccuser = pmig.sql_x("SELECT user FROM user_relations WHERE user = %s", (ccuser,))
     if eccuser:
         jcc = pmig.sql_x("SELECT cc FROM user_relations WHERE user = %s", (ccuser,))
         jflat = tflatten(jcc)
         if any(jflat):
             cc = json.loads(jcc[0][0])
         else:
             cc = []
         if rtid not in cc:
             cc.append(rtid)
         vlog("cc %s" % (str(cc),))
         pmig.sql_x("UPDATE user_relations SET cc=%s, modified=%s WHERE user = %s", (json.dumps(cc), now(), ccuser))
     else:
         vlog("inserting new record")
         cc = json.dumps([rtid])
         insert_values = (ccuser, cc, now(), now())
         pmig.sql_x(
             "INSERT INTO user_relations (user, cc, created, modified) VALUES (%s, %s, %s, %s)", insert_values
         )
 def add_author(author):
     euser = pmig.sql_x("SELECT user FROM user_relations WHERE user = %s", (relations["author"],))
     if euser:
         jauthored = pmig.sql_x("SELECT author FROM user_relations WHERE user = %s", (relations["author"],))
         jflat = tflatten(jauthored)
         if any(jflat):
             authored = json.loads(jauthored[0][0])
         else:
             authored = []
         if rtid not in authored:
             authored.append(rtid)
         vlog("author %s" % (str(authored),))
         pmig.sql_x(
             "UPDATE user_relations SET author=%s, modified=%s WHERE user = %s",
             (json.dumps(authored), now(), relations["author"]),
         )
     else:
         vlog("inserting new record")
         authored = json.dumps([rtid])
         insert_values = (relations["author"], authored, now(), now())
         pmig.sql_x(
             "INSERT INTO user_relations (user, author, created, modified) VALUES (%s, %s, %s, %s)", insert_values
         )