def upgrade(pyramid_env):
    with context.begin_transaction():
        op.create_table(
            'text_fragment_identifier',
            sa.Column('id', sa.Integer, primary_key=True),
            sa.Column('extract_id', sa.Integer, sa.ForeignKey('extract.id')),
            sa.Column('xpath_start', sa.String),
            sa.Column('offset_start', sa.Integer),
            sa.Column('xpath_end', sa.String),
            sa.Column('offset_end', sa.Integer)
        )
        op.add_column('extract', sa.Column('annotation_text', sa.UnicodeText))

    # Do stuff with the app's models here.
    from assembl.models import Extract
    db = Extract.db()
    with transaction.manager:
        q =  db.execute('''
            SELECT extract.id, email.subject, email.body, post.id 
            FROM extract
            JOIN email ON (email.id = extract.source_id)
            JOIN content ON (email.id = content.id)
            JOIN post ON (post.content_id = email.id)
            WHERE content.type = 'email'
            ''')
        vals = {ex_id: (sub, body, postid) for (ex_id, sub, body, postid) in q}
        for extract in db.query(Extract).options(lazyload('*')).all():
            v = vals.get(extract.id)
            if v:
                tfi = extract._infer_text_fragment_inner(*v)
                if tfi:
                    db.add(tfi)
def upgrade(pyramid_env):
    from assembl.models import Extract, TextFragmentIdentifier, Content, Post
    db = Extract.db()
    reg = re.compile(r"^//div\[@id='message-([0-9]+)'\](.*)")
    with transaction.manager:
        db.query(TextFragmentIdentifier).filter_by(extract=None).delete()
        for tfi in db.query(TextFragmentIdentifier).join(
                Extract, Content, Post).all():
            xpo = tfi.xpath_start
            print xpo
            match = reg.match(xpo)
            if match:
                id, remainder = match.groups()
                uri = Post.uri_generic(id)
                xp = "//div[@id='message-%s']%s" % (
                    uri, remainder)
                print xp
                tfi.xpath_start = tfi.xpath_end = xp
def upgrade(pyramid_env):
    from assembl.models import Extract, Mailbox
    db = Mailbox.db()
    with transaction.manager:
        for mb in db.query(Mailbox).all():
            Mailbox.reprocess_content(mb)
    db = Extract.db()
    with transaction.manager:
        q = db.execute('''
            SELECT extract.id, email.subject, email.body, post.id
            FROM extract
            JOIN email ON (email.id = extract.source_id)
            JOIN content ON (email.id = content.id)
            JOIN post ON (post.content_id = email.id)
            WHERE content.type = 'email'
            ''')
        vals = {ex_id: (sub, body, postid) for (ex_id, sub, body, postid) in q}
        for extract in db.query(Extract).options(lazyload('*')).all():
            v = vals.get(extract.id)
            if v:
                tfi = extract._infer_text_fragment_inner(*v)
                if tfi:
                    db.add(tfi)