django-test-migrations
django-test-migrations copied to clipboard
Create a check for initial migrations
Today I found a code like this:
class Migration(migrations.Migration):
# initial = True
dependencies = []
And this was an initial migration for an app.
This suprised me:
- It works correctly
- It does not produce any warnings
- It is cryptic
What am I concerned about? I have an impression that all migrations with dependencies = [] must be initial, because otherwise they should be dependent on a previous migration. Am I correct?
So, I guess we can check this pretty easily in case initial really works this way.
That's strange, however, according to django docs about Migration.initial, such migration can be valid initial migration:
Initial migrations are marked with an initial = True class attribute on the migration class. If an initial class attribute isn’t found, a migration will be considered “initial” if it is the first migration in the app (i.e. if it has no dependencies on any other migration in the same app).
I agree that this is really confusing and IMHO it's better to be explicit and set initial = True, so adding a check for this will be really nice.
What do you think about creating a new app (something like django_test_migrations.contrib.django_checks.MigrationClasses) for this check (and potentially a few others in the future)?
@skarzi absolutely great idea! 👍
Do you have any other checks in mind?
I was also thinking about adding this feature to django-migration-linter, but after reading its short description in README.md:
Detect backward incompatible migrations for your Django project. It will save you time by making sure migrations will not break with a older codebase.
I am not sure it's a proper place for such thing
Yeah, I also consider django-migration-linter as django-incompatible-migration-linter 🙂
Hi
Is there any activity here? Is it still relevant?
And if so, what do you expect from this feature?
hello @denisSurkov :wave:
I think it's still relevant, however, we haven't got time to work on this issue, so any help is really appreciated. To be honest, I haven't checked how to implement this feature, but the general process could look like this:
- Let's add a new file with checks to this directory
a) Maybe
MigrationLoader.graph.root_nodes()can be used to implementinitial = Truecheck - Let's add a new app config class
MigrationClassesto this file so migration-related checks can be included separately
Of course, don't forget about unit tests of all newly introduced code. Let's ping me if you have any questions etc ;)