简体   繁体   中英

flask-migrate make migrations in alphabetical order

Using flask-migrate and flask-script , I set my project up, such that I only have to do

python manage.py db migrate

Inside the migrations folder, I get files such as

0f46602752b7_.py
8fdf8259859b_.py

There is no guarantee that the first migration precedes the second one. Django fixes this issue by prefixing all migrations with an auto-incrementing number. Can we tell flask-migrate / alembic to do the same?

Ideally, the two files in the example above would be

001_8fdf8259859b_.py
002_0f46602752b7_.py

If you check every migration file you'll discover lines such as:

revision = '09364330399c'
down_revision = None

down_revision stands for the preceding migration. If you really want to change naming convention you can do it by adding file_template field to your alembic.ini

Following docs:

file_template - this is the naming scheme used to generate new migration > files. The value present is the default, so is commented out. Tokens available include:

  %%(rev)s - revision id %%(slug)s - a truncated string derived from the revision message %%(year)d, %%(month).2d, %%(day).2d, %%(hour).2d, %%(minute).2d, %%(second).2d - components of the create date, by default datetime.datetime.now() unless the timezone configuration option is also used. 

For your particular example, add the following line inside alembic.ini

file_template = %%(year)d%%(month).2d%%(day).2d_%%(hour).2d%%(minute).2d%%(second).2d_%%(rev)s_%%(slug)s

It will generate a filename such as

20190527_122029_de2c595ec169_hello_world.py

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM