I have been using SQLAlchemy
for year now. And I use Alembic
for migrating changes.
I am also using alembic to seed data. Take an example of the authorization
seeds.
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]]
This works perfect if we assume there are going to be no changes in my data set. (permissions, roles, etc in my case)
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'),
]
But it's not the case. I am gonna have new roles and permissions and role_permission mappings. And alembic upgrade head
will not migrate new data because my head
has gone far ahead of the seed migration.
I could have the data in the respective revision only, but I am also using ROLE_LIST and PERMISSION_LIST in my business logic.
Is there any way I can do replace
query in migrations? Is there any better option?