问题
I am developing web systems using Django and they are deployed on Heroku. After the system goes production, all database data and the migration files (i.e., the 00*_* files) have to be retained. The followings are my procedure to perform database migration and deployment:
For the first deployment, perform
manage.py makemigrations
locally and push to Heroku.Perform
manage.py migrate
on Heroku.
If models are changed later:
Perform
makemigrations
locally and push to Heroku.Perform
migrate
on Heroku.
Steps 3 and 4 are repeated if models are changed.
As the system evolves, there are more and more migration files. I am wondering: after a successful migration and deployment, can I just delete all migration files and start like a fresh one? That is:
For the first deployment, perform
makemigration
locally and push to Heroku.Perform
migrate
on Heroku.Delete all local migration files.
Perform
makemigrations
locally to create seemingly start-up migration files.
Change models:
Perform
makemigration
locally and push to Heroku.Perform
migrate
on Heroku.
Steps 3 to 6 are repeated if models are changed.
Is the above procedure correct?
回答1:
For each of your app:
1) Pretend to rollback all existing migrations:
./manage.py migrate app zero --fake
The zero
argument indicates that we rollback to the first migration. You can confirm all migrations have been rollbacked by running ./manage.py migrate app --list
.
The --fake
option signals we should not actually run the migrations, but nonetheless mark the migrations as having been run: https://docs.djangoproject.com/en/1.8/ref/django-admin/#django-admin-option---fake
2). Delete migration files
git rm app/migrations/*
3) Create a new migration file
./manage.py makemigrations app
4) Pretend to run the new migration
./manage.py migrate app --fake
As in 1), step 4) does not actually run the migrations.
EDIT: added some explanations and fixed the zero
argument.
来源:https://stackoverflow.com/questions/32074791/south-migration-delete-all-migration-files-00-and-start-from-0001-while-k