Reputation: 119
I am currently setting up a project that is based on Python Flask (with Flask Migrate) with a PostgreSQL DB. I am working in a small team and have some questions regarding database updates.
As far as I am aware, If I update the database schema in Flask, I will have to perform a flask db migrate
and flask db upgrade
to apply the changes. And others that receives my update executes a flask db upgrade
to receive the schema changes. All is well but if I have updated rows in the database, how do I propagate the schema changes and data changes to my peers?
If I migrate and upgrade
my flask database followed by exporting a dump with psql
, when my peers receive the updates, do they simply import the data dump? Or do they have to perform a flask db upgrade
as well? If so, which one comes first? flask db upgrade
followed by dump import, or dump import followed by a flask db upgrade
?
I am not sure if simply importing the data dump with updated schema will mess up the migration commits in Flask Migrate. Not sure if this is the right place to ask but I hope I am able to find some answers here. Thanks in advance!
Edit: So far I am updating data in the database by dropping the database and recreating it with the dump, but I have yet to try it with any schema changes (project is progressing, a little slowly..)
Edit 2: Forgot to mention that all instances of PostgreSQL are hosted locally on our own machines
Upvotes: 2
Views: 2166
Reputation: 11483
I am not very familiar with Flask
and its migration mechanism. However, I guess this is not far from Entity Framework
migration feature. If so, the problem is divided into two parts:
First, you need to keep data structure up-to-date among your team. It's the simple part. Once you have changes on your entities (data classes) in your code base, you crate the related migration and commit/push the code. Everybody, including yourself, just upgrade the database based on the available migration. In this part, your actual data is not shared or sent to your colleagues.
Second, you want your data be shared among the people. You have a straightforward solution here. You can take database snapshot/backup/dump and send it to all the team. Consider that they should run the upgrade prior that import the database. Another option is to share a common database on the internet. There is also another work-around. Some technologies like Entity Framework
allow you to transfer the data via something similar to seeding. If this is not available on your platform, you can publish the SQL script to create/update the desired data.
Upvotes: 1
Reputation: 67479
The main purpose of Flask-Migrate and Alembic is to manage the database schema migration history. In general the contents of the database do not need to change and in particular a full data dump shouldn't be necessary.
That said, there are cases where you do need to make changes to the data stored in your database as part of a migration. This is often referred to as "seed data", and could include things such as updates to the list of available user roles, or other enumerated data elements. Alembic allows you to include these small changes to the database into the migration script through the bulk_insert and execute operations, so that a flask db upgrade
will also apply these data changes. But obviously these changes are application specific so you have to code them manually into your migration script after you run flask db migrate
and before you run flask db upgrade
.
Upvotes: 1