def update(bugid):

    pmig = phabdb.phdb(db=config.rtmigrate_db)

    epriority = pmig.sql_x(
        "SELECT priority \
                            from task_relations \
                            where id = %s",
        bugid,
    )

    if epriority and epriority[0] == ipriority["update_success"]:
        log("skipping %s as blockers already updated" % (bugid,))
        return True

    query = "SELECT header FROM rt_meta WHERE id = %s"
    header = pmig.sql_x(query, (bugid,))
    if not header:
        log("no header found for %s" % (bugid,))
        return "missing"

    def extref(ticket):
        refid = phabdb.reference_ticket("%s%s" % (rtlib.prepend, ticket))
        if not refid:
            return ""
        return refid[0]

    blocker_ref = extref(bugid)
    tinfo = json.loads(header[0][0])

    if "refers_to" in tinfo["links"] and tinfo["links"]["refers_to"]:
        refers_to = []
        for b in tinfo["links"]["refers_to"]:
            refersto_ref = extref(b)
            if not refersto_ref:
                continue
            refers_to.append(phabdb.get_task_id_by_phid(refersto_ref))
        if refers_to:
            refers_block = "\n\n**Refers To:**\n"
            refers_block += "\n".join(["{T%s}" % r for r in refers_to])
            log(phabdb.append_to_task_description(blocker_ref, refers_block))

    if "refers_toby" in tinfo["links"] and tinfo["links"]["refers_toby"]:
        refers_toby = []
        for b in tinfo["links"]["refers_toby"]:
            referstoby_ref = extref(b)
            if not referstoby_ref:
                continue
            refers_toby.append(phabdb.get_task_id_by_phid(referstoby_ref))
        if refers_toby:
            refer_block = "\n\n**Referred To By:**\n"
            refer_block += "\n".join(["{T%s}" % r for r in refers_toby])
            log(phabdb.append_to_task_description(blocker_ref, refer_block))
    return True
def reorder(first, start, end, placeholder=300001):
    #pmig = phabdb.phdb(db=config.bzmigrate_db,
    #                   user=config.bzmigrate_user,
    #                   passwd=config.bzmigrate_passwd)

    # range of issues to renumber
    issues = range(int(start), int(end) + 1)
    first_issue = issues[0]
    # number to start new linear renumber at
    newid = int(first)
    print 'starting renumbering at %s with %s' % (first, first_issue)
    pphid = phabdb.get_task_phid_by_id(placeholder)
    if pphid:
        print "placeholder %s not empty (%s)" % (placeholder, pphid)
        return 


    for t in issues:
        print "Reassigning reference: %s to %s" % (t, newid)
        # Find PHID of the first ticket in our lineup
        ref = bzlib.prepend + str(t)
        phid = phabdb.reference_ticket(ref)
        if len(phid) > 1 or not phid:
            newid += 1
            print 'skipping phid %s' % (ref,)
            continue
        else:
            refphid = phid[0]
        print "Reference %s is %s" % (ref, refphid)
        tid = phabdb.get_task_id_by_phid(refphid)
        print "Reference %s is starting at id %s" % (ref, tid)
        existing_task = phabdb.get_task_phid_by_id(int(newid))
        print "Existing task returns %s" % (existing_task,)
        if existing_task:
            print "Squatter task at %s is %s" % (newid, existing_task)
            print "Moving squatter %s to %s" % (existing_task, placeholder)
            phabdb.set_task_id(placeholder, existing_task)      
        phabdb.set_task_id(newid, refphid)
        if existing_task:
            print "fixup setting squatter %s to %s" % (existing_task, tid)
            phabdb.set_task_id(tid, existing_task)
        newid += 1