In the Python Flask framework, use Flask-Migrate to expand the database migration tutorial, flaskflask-migrate
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
Use Flask-Migrate to Migrate a database
As the development progresses, you will find that your database model needs to be changed, and when this happens, you need to update the database.
Flask-SQLAlchemy creates them from the model only when the database tables do not exist. Therefore, the only way to update the tables is to destroy the old tables. Of course, this will lead to data loss in all databases.
A better solution is to use the database migration framework. Like the source code version control tool that tracks changes to source code files, the database migration framework tracks changes to the database model and then applies incremental changes to the database.
Major SQLAlchemy developers have written an Alembic migration framework, but we do not directly use Alembic. Flask applications can use the Flask-Migrate extension, A lightweight Alembic package that integrates Flask-Script to provide all operation commands.
4. Create a migration warehouse
First, Flask-Migrate must have been installed in the virtual environment:
(venv) $ pip install flask-migrate
The following shows how to initialize the extension:
from flask.ext.migrate import Migrate, MigrateCommand # ...migrate = Migrate(app, db)manager.add_command('db', MigrateCommand)
To use the database migration command, Flask-Migrate provides the MigrateCommand class to connect to the manager object of Flask-Script. In this example, use db to connect to the command.
Before database migration can be maintained, you must use the init sub-command to create a migration database:
(venv) $ python hello.py db init
Creating directory /home/flask/flasky/migrations...done Creating directory /home/flask/flasky/migrations/versions...done Generating /home/flask/flasky/migrations/alembic.ini...done Generating /home/flask/flasky/migrations/env.py...done Generating /home/flask/flasky/migrations/env.pyc...done Generating /home/flask/flasky/migrations/README...done Generating /home/flask/flasky/migrations/script.py.mako...done Please edit configuration/connection/logging settings in '/home/flask/flasky/migrations/alembic.ini' before proceeding.
This command creates a migrations folder, which stores all the migration scripts.
Suggestion: if you have an application cloned on GitHub, you can run git checkout 5c to switch to this version of the application.
5. Create a migration script
In Alembic, the database migration is completed by the Migration script. This script has two functions: upgrade () and downgrade (). The upgrade () function implements database changes and is part of the migration. The downgrade () function deletes them. By adding and deleting database changes, Alembic can reconfigure the database from any time point in the history.
Alembic migration can be manually or automatically created using the revision and migrate commands. Manual migration creates a migration framework script through the empty upgrade () and downgrade () functions implemented by developers using Alembic Operations object commands. On the other hand, automatic migration generates code by looking for the differences between the model definition and the current database status: upgrade () and downgrade.
Warning automatic migration is not always accurate. You can ignore some details. Therefore, the automatically generated migration script should be reviewed frequently.
The migrate sub-command creates an automatic migration script:
(venv) $ python hello.py db migrate -m "initial migration"
INFO [alembic.migration] Context impl SQLiteImpl.INFO [alembic.migration] Will assume non-transactional DDL.INFO [alembic.autogenerate] Detected added table 'roles'INFO [alembic.autogenerate] Detected added table 'users'INFO [alembic.autogenerate.compare] Detected added index'ix_users_username' on '['username']' Generating /home/flask/flasky/migrations/versions/1bc 594146bb5_initial_migration.py...done
Suggestion: if you have an application cloned on GitHub, you can run git checkout 5c to switch to this version of the application. Note that you do not need to generate migrations for this application. All migration scripts are included in the version library.
6. update the database
Once the migration script is reviewed and accepted, you can use the db upgrade command to update it to the database:
(venv) $ python hello.py db upgrade
INFO [alembic.migration] Context impl SQLiteImpl.INFO [alembic.migration] Will assume non-transactional DDL.INFO [alembic.migration] Running upgrade None -> 1bc594146bb5, initial migration
The first migration is actually equivalent to calling db. create_all (), but in subsequent migration, the upgrade command will update the table but will not affect the table content.