Alembic中的种子数据问题

时间:2016-05-09 14:19:05

标签: sqlalchemy seeding alembic

我一年来一直在使用SQLAlchemy。我使用Alembic来迁移更改。

我也在使用alembic来播种数据。举一个authorization种子的例子。

def upgrade():
    op.bulk_insert(Role.__table__, ROLE_LIST)
    op.bulk_insert(Permission.__table__, PERMISSION_LIST)
    op.bulk_insert(RolePermission.__table__, ROLE_PERMISSION_LIST)

def downgrade():
    [op.execute(a_table.__table__.delete()) for a_table in [RolePermission, Permission, Role]]

如果我们假设我的数据集中没有任何变化,这就完美了。 (在我的情况下权限,角色等)

ROLE_LIST = [
    dict(id=generate_business_id(), name='customer', default=1),
    dict(id=generate_business_id(), name='admin', default=0), 
]
PERMISSION_LIST = [
    dict(id=generate_business_id(), name='profile'),
    dict(id=generate_business_id(), name='delete_user'),
    dict(id=generate_business_id(), name='make_payment'),
]

但事实并非如此。我将拥有新的角色和权限以及role_permission映射。并且alembic upgrade head不会迁移新数据,因为我的head已远远超过种子迁移。

我可以只在相应的修订版中包含数据,但我也在我的业务逻辑中使用ROLE_LIST和PERMISSION_LIST。

我有什么方法可以在迁移中replace查询?还有更好的选择吗?

0 个答案:

没有答案