Use Flask-Migrate to upgrade the management database.
When upgrading the system, we often encounter operations such as updating the data structure on the server. The previous method was to manually write an alter SQL script for processing, and we often find omissions, as a result, the program cannot be used normally after being published to the server.
Now we can use the Flask-Migrate plug-in to solve this problem. The Flask-Migrate plug-in is based on Alembic, and Alembic is a data migration tool developed by the famous SQLAlchemy author.
The procedure is as follows:
1. Install the Flask-Migrate plug-in.
$ Pip install Flask-Migrate
2. modify the code of the Flask App to add the 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, automatic creation of Migration Code
$ Python app. py db migrate
5. update the database
$ Python app. py db upgrade
If a Model change occurs later, you only need to repeat steps 1 and 5 in the development environment.
On the server side, you only need to perform Step 1 to migrate the database.