20

我目前有一列包含 HTML 标记。在该标记中,有一个我想存储在新列中的时间戳(以便我可以查询它)。我的想法是在一次迁移中执行以下操作:

  1. 为数据创建一个可以为空的新列
  2. 使用 ORM 拉回我需要解析的 HTML
  3. 对于每一行
    1. 解析 HTML 以提取时间戳
    2. 更新 ORM 对象

但是当我尝试运行迁移时,它似乎陷入了无限循环。这是我到目前为止所得到的:

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')
4

4 回答 4

31

在使用@velochy 的答案进行了一些实验后,我确定了类似以下在 Alembic 中使用 SqlAlchemy 的模式。这对我很有用,可能可以作为 OP 问题的一般解决方案:

from sqlalchemy.orm.session import Session
from alembic import op

# Copy the model definitions into the migration script if
# you want the migration script to be robust against later
# changes to the models. Also, if your migration includes
# deleting an existing column that you want to access as 
# part of the migration, then you'll want to leave that 
# column defined in the model copies here.
class Model1(Base): ...
class Model2(Base): ...

def upgrade():
    # Attach a sqlalchemy Session to the env connection
    session = Session(bind=op.get_bind())

    # Perform arbitrarily-complex ORM logic
    instance1 = Model1(foo='bar')
    instance2 = Model2(monkey='banana')

    # Add models to Session so they're tracked
    session.add(instance1)
    session.add(instance2)

    # Apply a transform to existing data
    m1s = session.query(Model1).all()
    for m1 in m1s:
        m1.foo = transform(m1.foo)
    session.commit()

def downgrade():
    # Attach a sqlalchemy Session to the env connection
    session = Session(bind=op.get_bind())

    # Perform ORM logic in downgrade (e.g. clear tables)
    session.query(Model2).delete()
    session.query(Model1).delete()

    # Revert transform of existing data
    m1s = session.query(Model1).all()
    for m1 in m1s:
        m1.foo = un_transform(m1.foo)
    session.commit()

这种方法似乎可以正确处理事务。在处理这个问题时,我经常会生成数据库异常,它们会按预期回滚。

于 2016-03-18T14:36:11.277 回答
7

对我有用的是通过执行以下操作来获得会话:

connection = op.get_bind()
Session = sa.orm.sessionmaker()
session = Session(bind=connection)
于 2015-03-12T12:43:37.967 回答
3

您可以使用automap 扩展在迁移期间自动创建数据库的 ORM 模型,而无需将它们复制到代码中:

import sqlalchemy as sa
from alembic import op
from sqlalchemy.ext.automap import automap_base

Base = automap_base()

def upgrade():
    # Add the new column
    op.add_column('tip', sa.Column('publication_date', sa.DateTime(timezone=True)))

    # Reflect ORM models from the database
    # Note that this needs to be done *after* all needed schema migrations.
    bind = op.get_bind()
    Base.prepare(autoload_with=bind)  # SQLAlchemy 1.4 and later
    # Base.prepare(bind, reflect=True)  # SQLAlchemy before version 1.4
    Tip = Base.classes.tip  # "tip" is the table name

    # Query/modify the data as it exists during the time of the migration
    session = Session(bind=bind)
    tips = session.query(Tip).all()
    for tip in tips:
        # arbitrary update logic
        ...


def downgrade():
    op.drop_column('tip', 'publication_date')
于 2022-02-04T11:12:50.640 回答
1

从评论继续,你可以尝试这样的事情:

import sqlalchemy as sa


tip = sa.sql.table(
    'tip',
    sa.sql.column('id', sa.Integer),
    sa.sql.column('publication_date', sa.DateTime(timezone=True)),
)


def upgrade():
    mappings = [
        (x.id, _extract_publication_date(x.rendered_html))
        for x in Tip.query
    ]

    op.add_column('tip', sa.Column('publication_date', sa.DateTime(timezone=True)))

    exp = sa.sql.case(value=tip.c.id, whens=(
        (op.inline_literal(id), op.inline_literal(publication_date))
        for id, publication_date in mappings.iteritems()
    ))

    op.execute(tip.update().values({'publication_date': exp}))


def downgrade():
    op.drop_column('tip', 'publication_date')
于 2012-12-03T14:04:06.477 回答