Fixed #21892: RunPython no longer accepts strings
This commit is contained in:
parent
38b4adc696
commit
98dd8dd02e
|
@ -109,22 +109,15 @@ class RunPython(Operation):
|
||||||
|
|
||||||
def __init__(self, code, reverse_code=None):
|
def __init__(self, code, reverse_code=None):
|
||||||
# Forwards code
|
# Forwards code
|
||||||
if isinstance(code, six.string_types):
|
if not callable(code):
|
||||||
# Trim any leading whitespace that is at the start of all code lines
|
raise ValueError("RunPython must be supplied with a callable")
|
||||||
# so users can nicely indent code in migration files
|
self.code = code
|
||||||
code = textwrap.dedent(code)
|
|
||||||
# Run the code through a parser first to make sure it's at least
|
|
||||||
# syntactically correct
|
|
||||||
self.code = compile(code, "<string>", "exec")
|
|
||||||
else:
|
|
||||||
self.code = code
|
|
||||||
# Reverse code
|
# Reverse code
|
||||||
if reverse_code is None:
|
if reverse_code is None:
|
||||||
self.reverse_code = None
|
self.reverse_code = None
|
||||||
elif isinstance(reverse_code, six.string_types):
|
|
||||||
reverse_code = textwrap.dedent(reverse_code)
|
|
||||||
self.reverse_code = compile(reverse_code, "<string>", "exec")
|
|
||||||
else:
|
else:
|
||||||
|
if not callable(reverse_code):
|
||||||
|
raise ValueError("RunPython must be supplied with callable arguments")
|
||||||
self.reverse_code = reverse_code
|
self.reverse_code = reverse_code
|
||||||
|
|
||||||
def state_forwards(self, app_label, state):
|
def state_forwards(self, app_label, state):
|
||||||
|
|
|
@ -479,13 +479,11 @@ class OperationTests(MigrationTestBase):
|
||||||
|
|
||||||
project_state = self.set_up_test_model("test_runpython")
|
project_state = self.set_up_test_model("test_runpython")
|
||||||
# Create the operation
|
# Create the operation
|
||||||
operation = migrations.RunPython(
|
def inner_method(models, schema_editor):
|
||||||
"""
|
|
||||||
Pony = models.get_model("test_runpython", "Pony")
|
Pony = models.get_model("test_runpython", "Pony")
|
||||||
Pony.objects.create(pink=2, weight=4.55)
|
Pony.objects.create(pink=1, weight=3.55)
|
||||||
Pony.objects.create(weight=1)
|
Pony.objects.create(weight=5)
|
||||||
""",
|
operation = migrations.RunPython(inner_method)
|
||||||
)
|
|
||||||
# Test the state alteration does nothing
|
# Test the state alteration does nothing
|
||||||
new_state = project_state.clone()
|
new_state = project_state.clone()
|
||||||
operation.state_forwards("test_runpython", new_state)
|
operation.state_forwards("test_runpython", new_state)
|
||||||
|
@ -498,16 +496,9 @@ class OperationTests(MigrationTestBase):
|
||||||
# And test reversal fails
|
# And test reversal fails
|
||||||
with self.assertRaises(NotImplementedError):
|
with self.assertRaises(NotImplementedError):
|
||||||
operation.database_backwards("test_runpython", None, new_state, project_state)
|
operation.database_backwards("test_runpython", None, new_state, project_state)
|
||||||
# Now test we can do it with a callable
|
# Now test we can't use a string
|
||||||
|
with self.assertRaises(ValueError):
|
||||||
def inner_method(models, schema_editor):
|
operation = migrations.RunPython("print 'ahahaha'")
|
||||||
Pony = models.get_model("test_runpython", "Pony")
|
|
||||||
Pony.objects.create(pink=1, weight=3.55)
|
|
||||||
Pony.objects.create(weight=5)
|
|
||||||
operation = migrations.RunPython(inner_method)
|
|
||||||
with connection.schema_editor() as editor:
|
|
||||||
operation.database_forwards("test_runpython", editor, project_state, new_state)
|
|
||||||
self.assertEqual(project_state.render().get_model("test_runpython", "Pony").objects.count(), 4)
|
|
||||||
|
|
||||||
|
|
||||||
class MigrateNothingRouter(object):
|
class MigrateNothingRouter(object):
|
||||||
|
|
Loading…
Reference in New Issue