Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 569
Andrei Cioara Avatar asked Sep 04 '25 17:09

Andrei Cioara


1 Answers

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
like image 52
maslak Avatar answered Sep 07 '25 10:09

maslak