This is the 19th day of my participation in the Gwen Challenge in November. Check out the details: The last Gwen Challenge in 2021.

Flask-migrate is used for database migration

Flask-SQLAlchemy! Db.create_all () does not create a new table or update it. Instead, drop all tables in the database using db.drop_all() and then call db.create_all() to create a new table. This is where the concept of database migration comes in.

Flask-migrate is an extension that updates the structure of a database table without destroying the data in the table. Flask-migrate is an extension that changes the structure of a database table without destroying the data. And complete data migration from the old table to the new table.

The use of Flask to Migrate

You can install this using PIP install flask-Migrate. In the program, we instantiate the Migrate class provided by Flask_Migrate for initialization.

from flask_migrate import Migrate
from flask_sqlalchemy importFlask = Flask(stack) = Flask(stack) = Flask(stack) = Flask(stack)Copy the code

To instantiate Migrate, you need to pass in Flask instance APP and SQLAlchemy instance DB.

Database migration process

Start by defining the User model class.

class User(db.Model) :
    __tablename__ = 'user'
    id = db.Column(db.Integer, primary_key=True)
    user_name = db.Column(db.String)
    password = db.Column(db.String)
Copy the code

Create a migration environment

Before you start migrating data, you need to create a migration environment using the following command:

flask db init
Copy the code

The migration environment needs to be created only once, and once created, a Migrations directory is generated in the project root directory containing the automatically generated configuration files and the migration version directory.

Generate migration script

Use the following command to automatically generate the migration script:

flask db migrate -m "create_table"
Copy the code

The -m option adds remarks. Migration scripts are generated after the version directory is migrated.

The migration script has two functions:

  • Upgrade () : Apply the changes in the migration to the database
  • Downgrade () : will undo the changes

The automatically generated migration script will generate upgrade() and downgrade() functions based on the differences between the model definition and the current state of the database. It may not be completely correct, so it is necessary to check again.

Updating the database

After the migration script is generated, you can run the flask db upgrade command to update the database. After execution, the database and tables are generated.

If we need to change a field in the User table later, such as adding a mobile field, we simply add this property to the User model class and then execute the flask db migrate -m ‘comment’ and the flask DB upgrade command.

class User(db.Model) :
    __tablename__ = 'user'
    id = db.Column(db.Integer, primary_key=True)
    user_name = db.Column(db.String)
    password = db.Column(db.String)
    mobile = db.Column(db.String)
Copy the code

If you want to roll back the migration, you can run the flask db downgrade command.

conclusion

How to Migrate databases in Flask is just described here. Whether or not you need to use migration tools in a production environment is not discussed here. As for me, I have not used flask-Migrate in a production environment. Instead choose to write SQL scripts to handle database and table updates or changes, I think this is less prone to error, in fact, each has its own benefits, see your choice.

Original is not easy, if small partners feel helpful, please click a “like” and then go ~

Finally, thank my girlfriend for her tolerance, understanding and support in work and life!