Flask from getting started to mastering using Flask-migrate for database migrations

Source: Internet
Author: User
Tags virtual environment

As you develop your program, you will find that you sometimes need to modify the database model, and you need to update the database after you modify it. Flask-sqlalchemy is created based on the model only if the database table does not exist. Therefore, the only way to update a table is to delete the old table first, but doing so will lose all the data in the database. A better way to update tables is to use the database migration framework. The source version control tool can track changes in the source file, similarly, the database migration framework can track changes in the database schema and then incrementally apply the changes to the database.

SQLAlchemy's main developer wrote a migration framework called Alembic (https://alembic.readthedocs.org/en/latest/index.html). In addition to using alembic directly, the Flask program can also use the flask-migrate (http://flask-migrate.readthedocs.org/en/latest/) extension. This extension does a lightweight wrapper on the Alembic and integrates into the flask-script, all of which are done through the Flask-script command.

I. Create a migration warehouse

First, we want to install flask-migrate in a virtual environment:

Pip Install Flask-migrate

The initialization method for this extension is as follows:

from = Migrate (app, db) Manager.add_command ('db', Migratecommand)

To export the database Migration command, Flask-migrate provides a Migratecommand class that can be attached to the Flask-script Manager object. In this example, the Migratecommand class is appended with the DB command.

To create a migration warehouse using the INIT subcommand before maintaining the database migration:

python hello.py db initcreating directory/home/flask/flask1/migrations. Done Creating Directory/home/flask/flask1/migrations/versions . Done Generating/home/flask/flask1/migrations/Env.pyc . Done Generating/home/flask/flask1/migrations/Alembic.ini . Done Generating/home/flask/flask1/migrations/README ... done generating/home/flask/flask1/migrations/Script.py.mako . Done Generating/home/flask/flask1/migrations/env.py. Do please edit configuration/connection/logging settingsinch  '/home/flask/flask1/migrations/alembic.ini'Before proceeding.

This command creates the Migrations folder, where all migration scripts are stored. Files in the database migration warehouse are included in version control along with other files of the program.

Two. Create a migration script

In Alembic, the database migration is represented by a migration script. There are two functions in the script, namely upgrade () and downgrade (). The upgrade () function applies the changes in the migration to the database, and the downgrade () function deletes the changes. Alembic has the ability to add and remove changes, so the database can be reset to any point in the revision history.

We can manually create Alembic migrations using the revision command, or they can be created automatically using the Migrate command. The manually created migration is just a skeleton, and the upgrade () and downgrade () functions are empty, and the developer uses the operations object instructions provided by Alembic to implement the specific operation. Automatically created migrations generate the contents of the upgrade () and downgrade () functions based on the differences between the model definition and the current state of the database. Automatically created migrations are not always correct, and some of the details may be missed. Be sure to check after the migration scripts are automatically generated.

The MIGRATE subcommand is used to automatically create migration scripts:

Python hello.py db migrate-m"Initial Migration"INFO [alembic.runtime.migration] Context impl mysqlimpl.info [alembic.runtime.migration] would assume non-transactional Ddl.info [Alembic.autogenerate.compare] detected removed table u'Sys_user'INFO [Alembic.autogenerate.compare] detected removed table u'Sys_role_privilege'INFO [Alembic.autogenerate.compare] detected removed table u'Sys_role'INFO [Alembic.autogenerate.compare] detected removed table u'Sys_privilege'INFO [Alembic.autogenerate.compare] detected removed table u'sys_dict'INFO [Alembic.autogenerate.compare] detected removed table u'User Info'INFO [Alembic.autogenerate.compare] detected removed table u'Country'INFO [Alembic.autogenerate.compare] detected removed table u'Sys_user_role'Generating/home/flask/flask1/migrations/versions/f52784fdd592_initial_migration.py ... done

Three. Updating the database

After checking and correcting the migration script, we can use the DB Upgrade command to apply the migration to the database:

python hello.py db upgradeinfo  [Alembic.runtime.migration] Context impl mysqlimpl.info  [alembic.runtime.migration] would assume non-  Transactional Ddl.info  [alembic.runtime.migration] Running upgrade  , f52784fdd592, initial Migration

For the first migration, the effect is the same as calling the Db.create_all () method. However, in subsequent migrations, the Upgrade command can apply changes to the database without affecting the saved data.

  

  

Flask from getting started to mastering using Flask-migrate for database migrations

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.