Skip to content Skip to sidebar Skip to footer

How to Run All Migrations Back From Again Django

Subscribe to our YouTube Channel!
[Jul 12, 2021] New Video: How to Employ Django Remainder Framework Permissions (DRF Tutorial - Part 7)


How to Reset Migrations

The Django migration system was adult and optmized to work with large number of migrations. Mostly you shouldn't mind to continue a large amount of models migrations in your code base. Even though sometimes information technology causes some undesired effects, like consuming much time while running the tests. But in scenarios like this yous can easily disable the migrations (although in that location is no built-in selection for that at the moment).

Anyhow, if you want to perform a clean-up, I will nowadays a few options in this tutorial.


Scenario one:

The project is nonetheless in the development environment and y'all want to perform a full clean up. You don't mind throwing the whole database away.

1. Remove the all migrations files within your project

Become through each of your projects apps migration folder and remove everything within, except the __init__.py file.

Or if you are using a unix-like OS you tin run the following script (inside your projection dir):

                find . -path                  "*/migrations/*.py"                  -not -name                  "__init__.py"                  -delete discover . -path                  "*/migrations/*.pyc"                  -delete              
2. Drib the electric current database, or delete the db.sqlite3 if information technology is your example.
three. Create the initial migrations and generate the database schema:
                python manage.py makemigrations python manage.py migrate              

And you are proficient to become.


Scenario 2:

You want to clear all the migration history but y'all desire to keep the existing database.

1. Make certain your models fits the current database schema

The easiest way to do information technology is trying to create new migrations:

                python manage.py makemigrations              

If there are whatever pending migration, apply them outset.

If you see the message:

                No changes detected              

You are good to get.

2. Clear the migration history for each app

Now you will need to clear the migration history app by app.

Outset run the showmigrations command so we tin go along track of what is going on:

                                  $                  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                  [Ten] 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                  [Ten] 0001_initial                  [X] 0002_remove_content_type_name cadre                  [X] 0001_initial                  [X] 0002_remove_mymodel_i                  [X] 0003_mymodel_bio sessions                  [X] 0001_initial              

Clear the migration history (delight note that core is the name of my app):

                                  $                  python manage.py migrate --simulated core zero              

The consequence will be something similar this:

                Operations to perform:   Unapply all migrations: core Running migrations:   Rendering model states... Washed   Unapplying core.0003_mymodel_bio... FAKED   Unapplying cadre.0002_remove_mymodel_i... FAKED   Unapplying core.0001_initial... FAKED              

Now run the control showmigrations once more:

                                  $                  python manage.py showmigrations              

Result:

                admin                  [X] 0001_initial                  [10] 0002_logentry_remove_auto_add auth                  [Ten] 0001_initial                  [10] 0002_alter_permission_name_max_length                  [X] 0003_alter_user_email_max_length                  [X] 0004_alter_user_username_opts                  [Ten] 0005_alter_user_last_login_null                  [X] 0006_require_contenttypes_0002                  [X] 0007_alter_validators_add_error_messages contenttypes                  [10] 0001_initial                  [X] 0002_remove_content_type_name core                  [                  ]                  0001_initial                  [                  ]                  0002_remove_mymodel_i                  [                  ]                  0003_mymodel_bio sessions                  [10] 0001_initial              

You must do that for all the apps you want to reset the migration history.

3. Remove the actual migration files.

Go through each of your projects apps migration folder and remove everything inside, except for the __init__.py file.

Or if you are using a unix-similar OS you can run the following script (within your projection dir):

                find . -path                  "*/migrations/*.py"                  -not -name                  "__init__.py"                  -delete find . -path                  "*/migrations/*.pyc"                  -delete              

PS: The example above will remove all the migrations file inside your project.

Run the showmigrations again:

                                  $                  python manage.py showmigrations              

Issue:

                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                  [Ten] 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 cadre                  (no migrations)                  sessions                  [10] 0001_initial              
4. Create the initial migrations
                                  $                  python manage.py makemigrations              

Result:

                Migrations                  for                  'core':   0001_initial.py:     - Create model MyModel              
v. Fake the initial migration

In this case you lot won't be able to apply the initial migration because the database table already exists. What we want to practice is to fake this migration instead:

                                  $                  python manage.py migrate --fake-initial              

Result:

                Operations to perform:   Utilize all migrations: admin, core, contenttypes, auth, sessions Running migrations:   Rendering model states... DONE   Applying core.0001_initial... FAKED              

Run showmigrations once more:

                admin                  [X] 0001_initial                  [Ten] 0002_logentry_remove_auto_add auth                  [X] 0001_initial                  [X] 0002_alter_permission_name_max_length                  [X] 0003_alter_user_email_max_length                  [Ten] 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                  [Ten] 0002_remove_content_type_name core                  [X] 0001_initial sessions                  [X] 0001_initial              

And we are all set upwards :-)



pickeringhavoccon.blogspot.com

Source: https://simpleisbetterthancomplex.com/tutorial/2016/07/26/how-to-reset-migrations.html

Post a Comment for "How to Run All Migrations Back From Again Django"