Use Flask-Migrate to upgrade the management database.

Source: Internet
Author: User

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.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.