In Django, the migration concept was developed with an idea to optmize large number of migrations during the product development. On general basis, its ok to have a large amount of information about migrations in the code base. Though occasionally it may cause some unexpected effects, such as to consume too much of time when tests are run. However, in such cases Django is flexible enough to allow disabling of migrations.
In this blog, you will get to know how to perform a clean-up of migrations, The blog covers two scenarios for the cleanup.
If, the project in in the development phase, it is sometimes feasible to do a fill clean up. This will lead to resetting the complete database.
Remove each and every file in all the migration folders present in the prijeexcept the __init__.pyfile.
In unix like OS, this can be done by using following commands inside the project directory:
find . -path "*/migrations/*.py" -not -name "__init__.py" -delete find . -path "*/migrations/*.pyc" -delete
python manage.py makemigrations python manage.py migrate
And that's it. All done.
If we want to clean up the history of migration without deleting the existing database.
python manage.py makemigrations
Run showmigrations command to keep track of existing migrations:
$ python manage.py showmigrations
Result:
admin [X] 0001_initial [X] 0002_logentry_remove_auto_add auth [X] 0001_initial [X] 0002_alter_permission_name_max_length [X] 0003_alter_user_email_max_length [X] 0004_alter_user_username_opts [X] 0005_alter_user_last_login_null [X] 0006_require_contenttypes_0002 [X] 0007_alter_validators_add_error_messages contenttypes [X] 0001_initial [X] 0002_remove_content_type_name app_name [X] 0001_initial [X] 0002_remove_mymodel_i [X] 0003_mymodel_bio sessions [X] 0001_initial
Clear migration history:
$ python manage.py migrate --fake app_name zero
The result should be:
Operations to perform: Unapply all migrations: app_name Running migrations: Rendering model states... DONE Unapplying app_name.0003_mymodel_bio... FAKED Unapplying app_name.0002_remove_mymodel_i... FAKED Unapplying app_name.0001_initial... FAKED
Run the show migration command again:
$ python manage.py showmigrations
Result:
admin [X] 0001_initial [X] 0002_logentry_remove_auto_add auth [X] 0001_initial [X] 0002_alter_permission_name_max_length [X] 0003_alter_user_email_max_length [X] 0004_alter_user_username_opts [X] 0005_alter_user_last_login_null [X] 0006_require_contenttypes_0002 [X] 0007_alter_validators_add_error_messages contenttypes [X] 0001_initial [X] 0002_remove_content_type_name app_name [ ] 0001_initial [ ] 0002_remove_mymodel_i [ ] 0003_mymodel_bio sessions [X] 0001_initial
You must do that for all the apps you want to reset the migration history.
find . -path "*/migrations/*.py" -not -name "__init__.py" -delete
find . -path "*/migrations/*.pyc" -delete
Runing the showmigrations again:
$ python manage.py showmigrations
Result should be:
admin [X] 0001_initial [X] 0002_logentry_remove_auto_add auth [X] 0001_initial [X] 0002_alter_permission_name_max_length [X] 0003_alter_user_email_max_length [X] 0004_alter_user_username_opts [X] 0005_alter_user_last_login_null [X] 0006_require_contenttypes_0002 [X] 0007_alter_validators_add_error_messages contenttypes [X] 0001_initial [X] 0002_remove_content_type_name app_name (no migrations) sessions [X] 0001_initial
$ python manage.py makemigrations
Result:
Migrations for 'app_name': 0001_initial.py: - Create model MyModel
In this particular case, since the tables are already present in the DB, one should fake the migrations using --fake-initial flag:
$ python manage.py migrate --fake-initial
Results:
Operations to perform: Apply all migrations: admin, app_name, contenttypes, auth, sessions Running migrations: Rendering model states... DONE Applying app_name.0001_initial... FAKED
Running show migrations again to see the output:
admin [X] 0001_initial [X] 0002_logentry_remove_auto_add auth [X] 0001_initial [X] 0002_alter_permission_name_max_length [X] 0003_alter_user_email_max_length [X] 0004_alter_user_username_opts [X] 0005_alter_user_last_login_null [X] 0006_require_contenttypes_0002 [X] 0007_alter_validators_add_error_messages contenttypes [X] 0001_initial [X] 0002_remove_content_type_name app_name [X] 0001_initial sessions [X] 0001_initial
All Done.
Thanks