Reset Migrations in Django

Posted By :Ishaan Madan |31st March 2019

Introduction:

 

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.

 

Prerequisites:

  • Django
  • SQLite3 / MySQL

 

Steps:

 

Scenario 1:

 

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.

Step 1. Removing all the migrations files in the project

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
Step 2. Drop or delete the current database.
Step 3. Creating the initial migrations to generate the schema of database using:
python manage.py makemigrations
python manage.py migrate

And that's it. All done.

 

Scenario 2:

 

If we want to clean up the history of migration without deleting the existing database. 

Step 1. Run Make Migrations using and apply them if some  pending migrations exist
python manage.py makemigrations

 

Step 2. Clearing migration history for all the apps

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.

3. Removing the  migration files.

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
4. Creating initial migrations
$ python manage.py makemigrations

Result:

Migrations for 'app_name':
  0001_initial.py:
    - Create model MyModel
5. Faking initial migration

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


About Author

Ishaan Madan

Ishaan, a skilled technical project manager, excels at breaking down complex projects into manageable tasks. With a background in both technology and project management, he offers a unique perspective to every project he undertakes. His effective communication skills enable him to collaborate seamlessly with both technical and non-technical stakeholders, ensuring everyone is aligned towards shared objectives. He has hands-on experience in utilizing agile methodologies like Scrum and Kanban to drive project management and foster team collaboration. He leverages project management tools such as JIRA, Trello, and Clickup to monitor progress, manage tasks, and facilitate communication among team members and stakeholders. Moreover, his proficiency in full-stack development empowers him to comprehend the technical aspects of projects and provide guidance to developers when necessary. He demonstrates expertise in utilizing popular Python frameworks like Django and Flask, along with data analysis and manipulation libraries such as NumPy and Pandas. On the front-end, Ishaan adeptly employs JavaScript libraries like React and Angular to craft visually appealing and user-friendly interfaces. Additionally, he possesses proficiency in HTML, CSS, and JavaScript for designing responsive and mobile-friendly layouts.

Request For Proposal

[contact-form-7 404 "Not Found"]

Ready to innovate ? Let's get in touch

Chat With Us