Sunday, 6 February 2022

Using the SQLAlchemy ORM inside an Alembic migration: how do I?

I currently have a column that contains HTML markup. Inside that markup, there is a timestamp that I want to store in a new column (so I can query against it). My idea was to do the following in a single migration:

  1. Create a new, nullable column for the data
  2. Use the ORM to pull back the HTML I need to parse
  3. For each row
    1. parse the HTML to pull out the timestamp
    2. update the ORM object

But when I try to run my migration, it appears to be stuck in an infinite loop. Here's what I've got so far:

def _extract_publication_date(html):
    root = html5lib.parse(html, treebuilder='lxml', namespaceHTMLElements=False)
    publication_date_string = root.xpath("//a/@data-datetime")[0]
    return parse_date(publication_date)


def _update_tip(tip):
    tip.publication_date = _extract_publication_date(tip.rendered_html)
    tip.save()


def upgrade():
    op.add_column('tip', sa.Column('publication_date', sa.DateTime(timezone=True)))
    tips = Tip.query.all()
    map(tips, _update_tip)


def downgrade():
    op.drop_column('tip', 'publication_date')


from Using the SQLAlchemy ORM inside an Alembic migration: how do I?

No comments:

Post a Comment