how to remove deprecated code from the old django migration files - django

I am removing dead code in my django apps, and realized that one of the functions is being used in an old migration file. What is the correct way to remove this code from the old migration file without causing database issues?

You might try the django squashmigrations command, and see if it recognises and removes the redundancy. More about squashing migrations in the docs.

Related

Best way to update Doctrine migrations tables

Overview
I have an application that uses Doctrine migrations. The application is a software code base used to manage multiple businesses who all use the same code instance in their own unique environments. i.e. Each code base is identical other than configurations.
Mistakes we made
One of the mistakes the dev team made was to include core migrations in the customer folders. i.e. With each new application we have to drop in the migration files or run a migration:diff to get going which I feel is not efficient and can lead to a mess.
What I would prefer is to have core migrations as part of the core code since it rarely changes and custom migrations on the client app side.
What I want to do
I want to move all core structure migrations to our code files
I want to have a single custom migration to drop in customised data in the client migration folder.
The problem
The problem I face is pretty much how to reorganize the migrations without breaking databases on existing client applications.
I have thought of two solutions:
Solution 1:
Add blank migrations as a placeholder for the new migrations I want.
Commit these to the repo and deploy to our environments.
They will be run, nothing will be changed, the migraitons table will store them as having been executed.
Next, Update the blank migrations to the actual code I want, and empty all other migration files. Commit this to the environments.
Finally - remove the unwanted migration files, remove the unwanted database migration records.
Solution 2
Change the migration location in the db to a new location
Remove all migration files and add blank migrations for the new ones I want
Commit this to the repo, allow to run and record the migrations as being run in the new table.
Add migration code.
Now all new applications will have the updated migration files and the old apps will have the new migration files...
Question:
Am I re-inventing the wheel? Is there a standard on how to do this as I am certain I am not the first to bump into this problem?
So for anyone who finds themselves in a similar position where they need to tidy up a mess of doctrine migrations, this should serve as a simple pattern to follow.
In our development environment we use continuous integration/git/kubernetes etc. and the following process works well with our environment.
The steps:
Update the migrations table name, this you can do in the configs quite easily.
'table_storage' => [
'table_name' => 'migration_version',
'version_column_name' => 'version_timestamp',
],
Next, delete your old migrations (delete the files) and run migrations:diff to generate a new one which will be a combination of all your changes.
Now comment out the code in the new file so that it's essentially an empty migration file.
On local, delete the old migrations table and run your build process which will add the new migration to the new table.
Commit to develop/staging/live etc. and repeat the process.
Now that the db in all your environments has the updated migrations file in it. You can now uncomments the code which will not be executed when you commit the file since it exists in your migrations table.
Hope this helps someone!

Django: Broken migration => remove old migration files [duplicate]

I'm using Django 1.8 and have an app with over 100 files in the migration folder. Is there a way in Django without deleting the files to "compress" or "optimise" these migrations so I don't have so many of them?
Have you read this part from the django docs?
Squashing migrations¶
You are encouraged to make migrations freely and not worry about how
many you have; the migration code is optimized to deal with hundreds
at a time without much slowdown. However, eventually you will want to
move back from having several hundred migrations to just a few, and
that’s where squashing comes in.
Source: https://docs.djangoproject.com/en/1.8/topics/migrations/

Creating migrations for a Django project that does not have any migrations already

I am working for a client with a 13 year old Django project. When they built the project migrations were not implemented so the project currently lacks migrations. I have tried creating migrations and then doing a datadump and loaddata into a new database to test them out. I am running into all sorts of errors and would like to start fresh.
So my question is this. What steps should I take to implement migrations in the project so that we can move forward using migrations? The django version has been updated to 3.0.5 and there are a number of GenericForeignKeys used, which might make this extra tricky. When creating the migrations originally I was told to use the fake tag but don't completely understand what this means.
Any help in the steps that I should take would be appreciated. For the sake of privacy/security for the client I don't want to just share a ton of their code but can share parts of it if it helps someone determine the steps that I should take.
For reference. After creating migration files originally and then trying the dumdata/loaddata commands I typically get an error saying that there are duplicate entries relating to contenttypes or duplicate entries for django_site domains. The client has domains set up depending on where in the world they are logging in from so the site "name" is unique but the "domain" is the same.
(1062, "Duplicate entry 'www.example.com' for key 'django_site.django_site_domain_a2e37b91_uniq'")
If you are talking about working on local, simply, delete all the migrations (except migrations/__init__.py.) and makemigrations again.
If you are on production, you are safe to use --fake once.
You are OK also to change the old migrations if you know how to do deal with migrations, and if it is few files.
I have answered a similar question here.

How to optimize migrations in Django 1.8

I'm using Django 1.8 and have an app with over 100 files in the migration folder. Is there a way in Django without deleting the files to "compress" or "optimise" these migrations so I don't have so many of them?
Have you read this part from the django docs?
Squashing migrations¶
You are encouraged to make migrations freely and not worry about how
many you have; the migration code is optimized to deal with hundreds
at a time without much slowdown. However, eventually you will want to
move back from having several hundred migrations to just a few, and
that’s where squashing comes in.
Source: https://docs.djangoproject.com/en/1.8/topics/migrations/

Django -south - Checking for existing table within migration

Some of my migrations should be run only when certain conditions are met - mostly because of the buggy nature of using south to migrate a django.contrib. needed when converting to our own user model. But since those migrations should run automatically I can't count on "--fake" - Sometime I need to run them, and sometime not - depending if the relation auth_permissions, for example, exists.
can I use the south/django orm in a migration forward section to check for an existing relation and run the migration in aif clause?
I tried using try/except in the migration but it seems to cause an error (currently can't reproduce that, I don't have this code any more)
How can I achieve that?
Thanks for the help!
Using Django 1.6.4 and south 0.8.4