#22788 closed Bug (fixed)
Custom migration operations are rewritten incorrectly
Reported by: | Matthew Schinckel | Owned by: | nobody |
---|---|---|---|
Component: | Migrations | Version: | dev |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Accepted | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
I've written a custom migration operation, and was in the process of writing a management command to write a migration file, using the MigrationWriter class.
It's almost all perfect, except that the MigrationWriter class assumes that the migration operation will be in django.db.migrations:
https://github.com/django/django/blob/master/django/db/migrations/writer.py#L47
I suspect that the MigrationWriter will also be used for things like squashing migrations, so this problem may appear with custom migration operations then.
I suggest that it should inspect the migration operation, and if it's not in the django.db.migrations
module, add the relevant module to the imports, and use the correct path for the operation.
Attachments (1)
Change History (13)
comment:1 by , 11 years ago
comment:2 by , 11 years ago
Has patch: | set |
---|
comment:3 by , 11 years ago
Needs tests: | set |
---|---|
Triage Stage: | Unreviewed → Accepted |
Hi,
This seems reasonable but the patch needs some tests.
Thanks.
comment:4 by , 11 years ago
Better patch does contain tests, but I couldn't come up with a way to make the test fail when two classes have the same name, but a different path.
I needed to import the class using from ... import ...
syntax, as otherwise there were issues related to namespacing and the term migrations, which I may try to work out.
comment:6 by , 11 years ago
The issue I have hit with this relates only to the django test framework.
Basically, the tests for this part live in the top-level 'migrations' package (when running the tests). Defining an operation within this package conflicts with the import in _every_ migrations file:
from django.db import migrations
This means I've had to import the custom operation directly (i.e., not namespaced).
Which then in turn means you cannot have two migration operations with the same name (that are actually different classes).
That is, I can import the foo.operations.Operation
and bar.Operation
in a normal situation, but can't actually write a passing test in the normal django test system.
I could mangle the names on import "from foo.operations import Operation as foo_operations_Operation", but this seems messy.
comment:7 by , 11 years ago
Perhaps code speaks louder than words.
The replacement code for adding the migration operation name to the migration file (and ensuring it is imported):
[OperationWriter.serialize()]
# See if this operation is in django.db.migrations. If it is,
# We can just use the fact we already have that imported,
# otherwise, we need to add an import for the operation class.
if getattr(migrations, name, None) == self.operation.class:
self.feed('migrations.%s(' % name)
else:
# This version works for the django tests, but fails if
# you have two identical named operations.
imports.add('from %s import %s' % (
self.operation.class.module, name
))
self.feed('%s(' % (name))
# This version works normally, including with same-named operations,
# but fails in the django tests, because
# self.operation.class.module will start with 'migrations'
imports.add('import %s' % self.operation.class.module)
self.feed('%s.%s(' % (
self.operation.class.module, name
))
comment:8 by , 11 years ago
schinckel: I suspect you can get around this problem if you put this test in its own test app called custom_migration_operation ?
(We already have a few things in separate apps for a variety of reasons, they don't all need to be bundled into migrations)
Otherwise, the patch looks good. Sorry I overlooked this!
comment:10 by , 11 years ago
Needs tests: | unset |
---|
comment:11 by , 11 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
So I've uploaded a patch that looks at the operation class, and decides if it should add an import (and use the fully qualified path for the operation).