When we upgrade the system, we often encounter the need to update the server-side data structure, and so on, the way is by hand-written alter SQL script processing, often found missing, resulting in the program published to the server will not work properly.
Now we can use the Flask-migrate plugin to solve it, flask-migrate plug-in is based on alembic,alembic is a data migration tool developed by the famous SQLAlchemy author.
Here's how:
1. Installing the Flask-migrate Plugin
Install Flask-migrate
2. Modify the code in the Flask app section to add migrate-related command
db = SQLAlchemy (APP) migrate = migrate (app, db) Manager = Manager (APP) Manager.add_command ('db', Migratecommand)
3. Initialization
$ Python app.py db init
4. Data migration, automatically create migration code
$ Python app.py db migrate
5. Updating the database
$ Python app.py db upgrade
If there is a model change in the back, you only need to repeat the 4th and 5 steps in the development environment.
In the server side only need to perform a 5th step to achieve the database migration.
Manage database upgrades with Flask-migrate