2017-06-30 20:35:58 +08:00
|
|
|
from django.core.exceptions import FieldDoesNotExist
|
2019-08-20 15:54:41 +08:00
|
|
|
from django.db import (
|
|
|
|
IntegrityError, connection, migrations, models, transaction,
|
|
|
|
)
|
2014-04-08 13:30:25 +08:00
|
|
|
from django.db.migrations.migration import Migration
|
2017-02-03 14:32:53 +08:00
|
|
|
from django.db.migrations.operations.fields import FieldOperation
|
2016-10-04 07:23:18 +08:00
|
|
|
from django.db.migrations.state import ModelState, ProjectState
|
2013-07-02 18:51:38 +08:00
|
|
|
from django.db.transaction import atomic
|
2016-09-28 22:04:52 +08:00
|
|
|
from django.test import SimpleTestCase, override_settings, skipUnlessDBFeature
|
2014-04-08 13:30:25 +08:00
|
|
|
|
2016-04-28 00:43:56 +08:00
|
|
|
from .models import FoodManager, FoodQuerySet, UnicodeModel
|
2019-07-25 19:43:23 +08:00
|
|
|
from .test_base import OperationTestBase
|
2013-05-31 00:55:59 +08:00
|
|
|
|
|
|
|
|
2017-01-19 15:39:46 +08:00
|
|
|
class Mixin:
|
2016-04-29 06:20:48 +08:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
2014-06-18 08:45:38 +08:00
|
|
|
class OperationTests(OperationTestBase):
|
|
|
|
"""
|
|
|
|
Tests running the operations and making sure they do what they say they do.
|
|
|
|
Each test looks at their state changing, and then their database operation -
|
|
|
|
both forwards and backwards.
|
|
|
|
"""
|
|
|
|
|
2013-05-31 00:55:59 +08:00
|
|
|
def test_create_model(self):
|
|
|
|
"""
|
|
|
|
Tests the CreateModel operation.
|
2013-07-02 18:19:02 +08:00
|
|
|
Most other tests use this operation as part of setup, so check failures here first.
|
2013-05-31 00:55:59 +08:00
|
|
|
"""
|
|
|
|
operation = migrations.CreateModel(
|
|
|
|
"Pony",
|
|
|
|
[
|
|
|
|
("id", models.AutoField(primary_key=True)),
|
2013-06-20 22:12:59 +08:00
|
|
|
("pink", models.IntegerField(default=1)),
|
2013-05-31 00:55:59 +08:00
|
|
|
],
|
|
|
|
)
|
2014-06-27 22:34:48 +08:00
|
|
|
self.assertEqual(operation.describe(), "Create model Pony")
|
2013-05-31 00:55:59 +08:00
|
|
|
# Test the state alteration
|
|
|
|
project_state = ProjectState()
|
|
|
|
new_state = project_state.clone()
|
|
|
|
operation.state_forwards("test_crmo", new_state)
|
2013-07-25 23:31:34 +08:00
|
|
|
self.assertEqual(new_state.models["test_crmo", "pony"].name, "Pony")
|
2013-05-31 00:55:59 +08:00
|
|
|
self.assertEqual(len(new_state.models["test_crmo", "pony"].fields), 2)
|
|
|
|
# Test the database alteration
|
|
|
|
self.assertTableNotExists("test_crmo_pony")
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
operation.database_forwards("test_crmo", editor, project_state, new_state)
|
|
|
|
self.assertTableExists("test_crmo_pony")
|
|
|
|
# And test reversal
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
operation.database_backwards("test_crmo", editor, new_state, project_state)
|
|
|
|
self.assertTableNotExists("test_crmo_pony")
|
2013-06-07 22:28:38 +08:00
|
|
|
# And deconstruction
|
|
|
|
definition = operation.deconstruct()
|
|
|
|
self.assertEqual(definition[0], "CreateModel")
|
2014-11-23 01:57:13 +08:00
|
|
|
self.assertEqual(definition[1], [])
|
2017-05-28 07:08:46 +08:00
|
|
|
self.assertEqual(sorted(definition[2]), ["fields", "name"])
|
2014-12-13 06:19:58 +08:00
|
|
|
# And default manager not in set
|
|
|
|
operation = migrations.CreateModel("Foo", fields=[], managers=[("objects", models.Manager())])
|
|
|
|
definition = operation.deconstruct()
|
|
|
|
self.assertNotIn('managers', definition[2])
|
2013-05-31 00:55:59 +08:00
|
|
|
|
2016-04-28 00:43:56 +08:00
|
|
|
def test_create_model_with_duplicate_field_name(self):
|
2016-04-28 04:17:22 +08:00
|
|
|
with self.assertRaisesMessage(ValueError, 'Found duplicate value pink in CreateModel fields argument.'):
|
2016-04-28 00:43:56 +08:00
|
|
|
migrations.CreateModel(
|
|
|
|
"Pony",
|
|
|
|
[
|
|
|
|
("id", models.AutoField(primary_key=True)),
|
|
|
|
("pink", models.TextField()),
|
|
|
|
("pink", models.IntegerField(default=1)),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_create_model_with_duplicate_base(self):
|
2016-04-28 04:17:22 +08:00
|
|
|
message = 'Found duplicate value test_crmo.pony in CreateModel bases argument.'
|
|
|
|
with self.assertRaisesMessage(ValueError, message):
|
2016-04-28 00:43:56 +08:00
|
|
|
migrations.CreateModel(
|
|
|
|
"Pony",
|
|
|
|
fields=[],
|
|
|
|
bases=("test_crmo.Pony", "test_crmo.Pony",),
|
|
|
|
)
|
2016-04-28 04:17:22 +08:00
|
|
|
with self.assertRaisesMessage(ValueError, message):
|
2016-04-28 00:43:56 +08:00
|
|
|
migrations.CreateModel(
|
|
|
|
"Pony",
|
|
|
|
fields=[],
|
2016-04-28 04:17:22 +08:00
|
|
|
bases=("test_crmo.Pony", "test_crmo.pony",),
|
2016-04-28 00:43:56 +08:00
|
|
|
)
|
2016-04-28 04:17:22 +08:00
|
|
|
message = 'Found duplicate value migrations.unicodemodel in CreateModel bases argument.'
|
|
|
|
with self.assertRaisesMessage(ValueError, message):
|
2016-04-28 00:43:56 +08:00
|
|
|
migrations.CreateModel(
|
|
|
|
"Pony",
|
|
|
|
fields=[],
|
2016-04-28 04:17:22 +08:00
|
|
|
bases=(UnicodeModel, UnicodeModel,),
|
2016-04-28 00:43:56 +08:00
|
|
|
)
|
2016-04-28 04:17:22 +08:00
|
|
|
with self.assertRaisesMessage(ValueError, message):
|
2016-04-28 00:43:56 +08:00
|
|
|
migrations.CreateModel(
|
|
|
|
"Pony",
|
|
|
|
fields=[],
|
|
|
|
bases=(UnicodeModel, 'migrations.unicodemodel',),
|
|
|
|
)
|
2016-04-28 04:17:22 +08:00
|
|
|
with self.assertRaisesMessage(ValueError, message):
|
2016-04-28 00:43:56 +08:00
|
|
|
migrations.CreateModel(
|
|
|
|
"Pony",
|
|
|
|
fields=[],
|
|
|
|
bases=(UnicodeModel, 'migrations.UnicodeModel',),
|
|
|
|
)
|
2016-04-29 06:20:48 +08:00
|
|
|
message = "Found duplicate value <class 'django.db.models.base.Model'> in CreateModel bases argument."
|
|
|
|
with self.assertRaisesMessage(ValueError, message):
|
|
|
|
migrations.CreateModel(
|
|
|
|
"Pony",
|
|
|
|
fields=[],
|
|
|
|
bases=(models.Model, models.Model,),
|
|
|
|
)
|
|
|
|
message = "Found duplicate value <class 'migrations.test_operations.Mixin'> in CreateModel bases argument."
|
|
|
|
with self.assertRaisesMessage(ValueError, message):
|
|
|
|
migrations.CreateModel(
|
|
|
|
"Pony",
|
|
|
|
fields=[],
|
|
|
|
bases=(Mixin, Mixin,),
|
|
|
|
)
|
2016-04-28 00:43:56 +08:00
|
|
|
|
|
|
|
def test_create_model_with_duplicate_manager_name(self):
|
2016-04-28 04:17:22 +08:00
|
|
|
with self.assertRaisesMessage(ValueError, 'Found duplicate value objects in CreateModel managers argument.'):
|
2016-04-28 00:43:56 +08:00
|
|
|
migrations.CreateModel(
|
|
|
|
"Pony",
|
|
|
|
fields=[],
|
|
|
|
managers=[
|
|
|
|
("objects", models.Manager()),
|
|
|
|
("objects", models.Manager()),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
|
2014-06-16 15:27:14 +08:00
|
|
|
def test_create_model_with_unique_after(self):
|
|
|
|
"""
|
|
|
|
Tests the CreateModel operation directly followed by an
|
|
|
|
AlterUniqueTogether (bug #22844 - sqlite remake issues)
|
|
|
|
"""
|
|
|
|
operation1 = migrations.CreateModel(
|
|
|
|
"Pony",
|
|
|
|
[
|
|
|
|
("id", models.AutoField(primary_key=True)),
|
|
|
|
("pink", models.IntegerField(default=1)),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
operation2 = migrations.CreateModel(
|
|
|
|
"Rider",
|
|
|
|
[
|
|
|
|
("id", models.AutoField(primary_key=True)),
|
|
|
|
("number", models.IntegerField(default=1)),
|
2015-07-22 22:43:21 +08:00
|
|
|
("pony", models.ForeignKey("test_crmoua.Pony", models.CASCADE)),
|
2014-06-16 15:27:14 +08:00
|
|
|
],
|
|
|
|
)
|
|
|
|
operation3 = migrations.AlterUniqueTogether(
|
|
|
|
"Rider",
|
|
|
|
[
|
|
|
|
("number", "pony"),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
# Test the database alteration
|
|
|
|
project_state = ProjectState()
|
|
|
|
self.assertTableNotExists("test_crmoua_pony")
|
|
|
|
self.assertTableNotExists("test_crmoua_rider")
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
new_state = project_state.clone()
|
|
|
|
operation1.state_forwards("test_crmoua", new_state)
|
|
|
|
operation1.database_forwards("test_crmoua", editor, project_state, new_state)
|
|
|
|
project_state, new_state = new_state, new_state.clone()
|
|
|
|
operation2.state_forwards("test_crmoua", new_state)
|
|
|
|
operation2.database_forwards("test_crmoua", editor, project_state, new_state)
|
|
|
|
project_state, new_state = new_state, new_state.clone()
|
|
|
|
operation3.state_forwards("test_crmoua", new_state)
|
|
|
|
operation3.database_forwards("test_crmoua", editor, project_state, new_state)
|
|
|
|
self.assertTableExists("test_crmoua_pony")
|
|
|
|
self.assertTableExists("test_crmoua_rider")
|
|
|
|
|
2014-02-18 06:51:40 +08:00
|
|
|
def test_create_model_m2m(self):
|
|
|
|
"""
|
|
|
|
Test the creation of a model with a ManyToMany field and the
|
|
|
|
auto-created "through" model.
|
|
|
|
"""
|
|
|
|
project_state = self.set_up_test_model("test_crmomm")
|
|
|
|
operation = migrations.CreateModel(
|
|
|
|
"Stable",
|
|
|
|
[
|
|
|
|
("id", models.AutoField(primary_key=True)),
|
|
|
|
("ponies", models.ManyToManyField("Pony", related_name="stables"))
|
|
|
|
]
|
|
|
|
)
|
|
|
|
# Test the state alteration
|
|
|
|
new_state = project_state.clone()
|
|
|
|
operation.state_forwards("test_crmomm", new_state)
|
|
|
|
# Test the database alteration
|
|
|
|
self.assertTableNotExists("test_crmomm_stable_ponies")
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
operation.database_forwards("test_crmomm", editor, project_state, new_state)
|
|
|
|
self.assertTableExists("test_crmomm_stable")
|
|
|
|
self.assertTableExists("test_crmomm_stable_ponies")
|
2014-03-09 08:04:21 +08:00
|
|
|
self.assertColumnNotExists("test_crmomm_stable", "ponies")
|
2014-02-18 06:51:40 +08:00
|
|
|
# Make sure the M2M field actually works
|
|
|
|
with atomic():
|
2014-11-05 17:43:31 +08:00
|
|
|
Pony = new_state.apps.get_model("test_crmomm", "Pony")
|
|
|
|
Stable = new_state.apps.get_model("test_crmomm", "Stable")
|
2014-02-18 06:51:40 +08:00
|
|
|
stable = Stable.objects.create()
|
|
|
|
p1 = Pony.objects.create(pink=False, weight=4.55)
|
|
|
|
p2 = Pony.objects.create(pink=True, weight=5.43)
|
|
|
|
stable.ponies.add(p1, p2)
|
|
|
|
self.assertEqual(stable.ponies.count(), 2)
|
|
|
|
stable.ponies.all().delete()
|
|
|
|
# And test reversal
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
operation.database_backwards("test_crmomm", editor, new_state, project_state)
|
|
|
|
self.assertTableNotExists("test_crmomm_stable")
|
|
|
|
self.assertTableNotExists("test_crmomm_stable_ponies")
|
|
|
|
|
2014-01-20 02:35:49 +08:00
|
|
|
def test_create_model_inheritance(self):
|
|
|
|
"""
|
|
|
|
Tests the CreateModel operation on a multi-table inheritance setup.
|
|
|
|
"""
|
|
|
|
project_state = self.set_up_test_model("test_crmoih")
|
|
|
|
# Test the state alteration
|
|
|
|
operation = migrations.CreateModel(
|
|
|
|
"ShetlandPony",
|
|
|
|
[
|
|
|
|
('pony_ptr', models.OneToOneField(
|
2015-07-22 22:43:21 +08:00
|
|
|
'test_crmoih.Pony',
|
|
|
|
models.CASCADE,
|
2014-01-20 02:35:49 +08:00
|
|
|
auto_created=True,
|
|
|
|
primary_key=True,
|
2014-01-20 02:57:06 +08:00
|
|
|
to_field='id',
|
2014-01-20 02:35:49 +08:00
|
|
|
serialize=False,
|
|
|
|
)),
|
|
|
|
("cuteness", models.IntegerField(default=1)),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
new_state = project_state.clone()
|
|
|
|
operation.state_forwards("test_crmoih", new_state)
|
|
|
|
self.assertIn(("test_crmoih", "shetlandpony"), new_state.models)
|
|
|
|
# Test the database alteration
|
|
|
|
self.assertTableNotExists("test_crmoih_shetlandpony")
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
operation.database_forwards("test_crmoih", editor, project_state, new_state)
|
|
|
|
self.assertTableExists("test_crmoih_shetlandpony")
|
|
|
|
# And test reversal
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
operation.database_backwards("test_crmoih", editor, new_state, project_state)
|
|
|
|
self.assertTableNotExists("test_crmoih_shetlandpony")
|
|
|
|
|
2014-06-16 07:01:49 +08:00
|
|
|
def test_create_proxy_model(self):
|
|
|
|
"""
|
2016-10-27 15:53:39 +08:00
|
|
|
CreateModel ignores proxy models.
|
2014-06-16 07:01:49 +08:00
|
|
|
"""
|
|
|
|
project_state = self.set_up_test_model("test_crprmo")
|
|
|
|
# Test the state alteration
|
|
|
|
operation = migrations.CreateModel(
|
|
|
|
"ProxyPony",
|
|
|
|
[],
|
2014-06-16 08:45:15 +08:00
|
|
|
options={"proxy": True},
|
2017-12-29 04:07:29 +08:00
|
|
|
bases=("test_crprmo.Pony",),
|
2014-06-16 07:01:49 +08:00
|
|
|
)
|
2014-06-27 22:34:48 +08:00
|
|
|
self.assertEqual(operation.describe(), "Create proxy model ProxyPony")
|
2014-06-16 07:01:49 +08:00
|
|
|
new_state = project_state.clone()
|
|
|
|
operation.state_forwards("test_crprmo", new_state)
|
|
|
|
self.assertIn(("test_crprmo", "proxypony"), new_state.models)
|
|
|
|
# Test the database alteration
|
|
|
|
self.assertTableNotExists("test_crprmo_proxypony")
|
|
|
|
self.assertTableExists("test_crprmo_pony")
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
operation.database_forwards("test_crprmo", editor, project_state, new_state)
|
|
|
|
self.assertTableNotExists("test_crprmo_proxypony")
|
|
|
|
self.assertTableExists("test_crprmo_pony")
|
|
|
|
# And test reversal
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
operation.database_backwards("test_crprmo", editor, new_state, project_state)
|
|
|
|
self.assertTableNotExists("test_crprmo_proxypony")
|
|
|
|
self.assertTableExists("test_crprmo_pony")
|
2014-11-23 01:57:13 +08:00
|
|
|
# And deconstruction
|
|
|
|
definition = operation.deconstruct()
|
|
|
|
self.assertEqual(definition[0], "CreateModel")
|
|
|
|
self.assertEqual(definition[1], [])
|
2017-05-28 07:08:46 +08:00
|
|
|
self.assertEqual(sorted(definition[2]), ["bases", "fields", "name", "options"])
|
2014-06-16 07:01:49 +08:00
|
|
|
|
2014-08-13 03:49:20 +08:00
|
|
|
def test_create_unmanaged_model(self):
|
|
|
|
"""
|
2016-10-27 15:53:39 +08:00
|
|
|
CreateModel ignores unmanaged models.
|
2014-08-13 03:49:20 +08:00
|
|
|
"""
|
|
|
|
project_state = self.set_up_test_model("test_crummo")
|
|
|
|
# Test the state alteration
|
|
|
|
operation = migrations.CreateModel(
|
|
|
|
"UnmanagedPony",
|
|
|
|
[],
|
|
|
|
options={"proxy": True},
|
2017-12-29 04:07:29 +08:00
|
|
|
bases=("test_crummo.Pony",),
|
2014-08-13 03:49:20 +08:00
|
|
|
)
|
|
|
|
self.assertEqual(operation.describe(), "Create proxy model UnmanagedPony")
|
|
|
|
new_state = project_state.clone()
|
|
|
|
operation.state_forwards("test_crummo", new_state)
|
|
|
|
self.assertIn(("test_crummo", "unmanagedpony"), new_state.models)
|
|
|
|
# Test the database alteration
|
|
|
|
self.assertTableNotExists("test_crummo_unmanagedpony")
|
|
|
|
self.assertTableExists("test_crummo_pony")
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
operation.database_forwards("test_crummo", editor, project_state, new_state)
|
|
|
|
self.assertTableNotExists("test_crummo_unmanagedpony")
|
|
|
|
self.assertTableExists("test_crummo_pony")
|
|
|
|
# And test reversal
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
operation.database_backwards("test_crummo", editor, new_state, project_state)
|
|
|
|
self.assertTableNotExists("test_crummo_unmanagedpony")
|
|
|
|
self.assertTableExists("test_crummo_pony")
|
|
|
|
|
2016-11-05 21:12:12 +08:00
|
|
|
@skipUnlessDBFeature('supports_table_check_constraints')
|
|
|
|
def test_create_model_with_constraint(self):
|
|
|
|
where = models.Q(pink__gt=2)
|
2018-08-06 10:15:10 +08:00
|
|
|
check_constraint = models.CheckConstraint(check=where, name='test_constraint_pony_pink_gt_2')
|
2016-11-05 21:12:12 +08:00
|
|
|
operation = migrations.CreateModel(
|
|
|
|
"Pony",
|
|
|
|
[
|
|
|
|
("id", models.AutoField(primary_key=True)),
|
|
|
|
("pink", models.IntegerField(default=3)),
|
|
|
|
],
|
|
|
|
options={'constraints': [check_constraint]},
|
|
|
|
)
|
|
|
|
|
|
|
|
# Test the state alteration
|
|
|
|
project_state = ProjectState()
|
|
|
|
new_state = project_state.clone()
|
|
|
|
operation.state_forwards("test_crmo", new_state)
|
|
|
|
self.assertEqual(len(new_state.models['test_crmo', 'pony'].options['constraints']), 1)
|
|
|
|
|
|
|
|
# Test database alteration
|
|
|
|
self.assertTableNotExists("test_crmo_pony")
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
operation.database_forwards("test_crmo", editor, project_state, new_state)
|
|
|
|
self.assertTableExists("test_crmo_pony")
|
|
|
|
with connection.cursor() as cursor:
|
|
|
|
with self.assertRaises(IntegrityError):
|
|
|
|
cursor.execute("INSERT INTO test_crmo_pony (id, pink) VALUES (1, 1)")
|
|
|
|
|
|
|
|
# Test reversal
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
operation.database_backwards("test_crmo", editor, new_state, project_state)
|
|
|
|
self.assertTableNotExists("test_crmo_pony")
|
|
|
|
|
|
|
|
# Test deconstruction
|
|
|
|
definition = operation.deconstruct()
|
|
|
|
self.assertEqual(definition[0], "CreateModel")
|
|
|
|
self.assertEqual(definition[1], [])
|
|
|
|
self.assertEqual(definition[2]['options']['constraints'], [check_constraint])
|
|
|
|
|
2018-12-28 03:21:59 +08:00
|
|
|
def test_create_model_with_partial_unique_constraint(self):
|
|
|
|
partial_unique_constraint = models.UniqueConstraint(
|
|
|
|
fields=['pink'],
|
|
|
|
condition=models.Q(weight__gt=5),
|
|
|
|
name='test_constraint_pony_pink_for_weight_gt_5_uniq',
|
|
|
|
)
|
|
|
|
operation = migrations.CreateModel(
|
|
|
|
'Pony',
|
|
|
|
[
|
|
|
|
('id', models.AutoField(primary_key=True)),
|
|
|
|
('pink', models.IntegerField(default=3)),
|
|
|
|
('weight', models.FloatField()),
|
|
|
|
],
|
|
|
|
options={'constraints': [partial_unique_constraint]},
|
|
|
|
)
|
|
|
|
# Test the state alteration
|
|
|
|
project_state = ProjectState()
|
|
|
|
new_state = project_state.clone()
|
|
|
|
operation.state_forwards('test_crmo', new_state)
|
|
|
|
self.assertEqual(len(new_state.models['test_crmo', 'pony'].options['constraints']), 1)
|
|
|
|
# Test database alteration
|
|
|
|
self.assertTableNotExists('test_crmo_pony')
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
operation.database_forwards('test_crmo', editor, project_state, new_state)
|
|
|
|
self.assertTableExists('test_crmo_pony')
|
|
|
|
# Test constraint works
|
|
|
|
Pony = new_state.apps.get_model('test_crmo', 'Pony')
|
|
|
|
Pony.objects.create(pink=1, weight=4.0)
|
|
|
|
Pony.objects.create(pink=1, weight=4.0)
|
|
|
|
Pony.objects.create(pink=1, weight=6.0)
|
|
|
|
if connection.features.supports_partial_indexes:
|
|
|
|
with self.assertRaises(IntegrityError):
|
|
|
|
Pony.objects.create(pink=1, weight=7.0)
|
|
|
|
else:
|
|
|
|
Pony.objects.create(pink=1, weight=7.0)
|
|
|
|
# Test reversal
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
operation.database_backwards('test_crmo', editor, new_state, project_state)
|
|
|
|
self.assertTableNotExists('test_crmo_pony')
|
|
|
|
# Test deconstruction
|
|
|
|
definition = operation.deconstruct()
|
|
|
|
self.assertEqual(definition[0], 'CreateModel')
|
|
|
|
self.assertEqual(definition[1], [])
|
|
|
|
self.assertEqual(definition[2]['options']['constraints'], [partial_unique_constraint])
|
|
|
|
|
2014-12-13 06:19:58 +08:00
|
|
|
def test_create_model_managers(self):
|
|
|
|
"""
|
2016-10-27 15:53:39 +08:00
|
|
|
The managers on a model are set.
|
2014-12-13 06:19:58 +08:00
|
|
|
"""
|
|
|
|
project_state = self.set_up_test_model("test_cmoma")
|
|
|
|
# Test the state alteration
|
|
|
|
operation = migrations.CreateModel(
|
|
|
|
"Food",
|
|
|
|
fields=[
|
|
|
|
("id", models.AutoField(primary_key=True)),
|
|
|
|
],
|
|
|
|
managers=[
|
|
|
|
("food_qs", FoodQuerySet.as_manager()),
|
|
|
|
("food_mgr", FoodManager("a", "b")),
|
|
|
|
("food_mgr_kwargs", FoodManager("x", "y", 3, 4)),
|
|
|
|
]
|
|
|
|
)
|
|
|
|
self.assertEqual(operation.describe(), "Create model Food")
|
|
|
|
new_state = project_state.clone()
|
|
|
|
operation.state_forwards("test_cmoma", new_state)
|
|
|
|
self.assertIn(("test_cmoma", "food"), new_state.models)
|
|
|
|
managers = new_state.models["test_cmoma", "food"].managers
|
|
|
|
self.assertEqual(managers[0][0], "food_qs")
|
|
|
|
self.assertIsInstance(managers[0][1], models.Manager)
|
|
|
|
self.assertEqual(managers[1][0], "food_mgr")
|
|
|
|
self.assertIsInstance(managers[1][1], FoodManager)
|
|
|
|
self.assertEqual(managers[1][1].args, ("a", "b", 1, 2))
|
|
|
|
self.assertEqual(managers[2][0], "food_mgr_kwargs")
|
|
|
|
self.assertIsInstance(managers[2][1], FoodManager)
|
|
|
|
self.assertEqual(managers[2][1].args, ("x", "y", 3, 4))
|
|
|
|
|
2013-05-31 00:55:59 +08:00
|
|
|
def test_delete_model(self):
|
|
|
|
"""
|
|
|
|
Tests the DeleteModel operation.
|
|
|
|
"""
|
|
|
|
project_state = self.set_up_test_model("test_dlmo")
|
|
|
|
# Test the state alteration
|
|
|
|
operation = migrations.DeleteModel("Pony")
|
2014-06-27 22:34:48 +08:00
|
|
|
self.assertEqual(operation.describe(), "Delete model Pony")
|
2013-05-31 00:55:59 +08:00
|
|
|
new_state = project_state.clone()
|
|
|
|
operation.state_forwards("test_dlmo", new_state)
|
|
|
|
self.assertNotIn(("test_dlmo", "pony"), new_state.models)
|
|
|
|
# Test the database alteration
|
|
|
|
self.assertTableExists("test_dlmo_pony")
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
operation.database_forwards("test_dlmo", editor, project_state, new_state)
|
|
|
|
self.assertTableNotExists("test_dlmo_pony")
|
|
|
|
# And test reversal
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
operation.database_backwards("test_dlmo", editor, new_state, project_state)
|
|
|
|
self.assertTableExists("test_dlmo_pony")
|
2014-11-23 01:57:13 +08:00
|
|
|
# And deconstruction
|
|
|
|
definition = operation.deconstruct()
|
|
|
|
self.assertEqual(definition[0], "DeleteModel")
|
|
|
|
self.assertEqual(definition[1], [])
|
|
|
|
self.assertEqual(list(definition[2]), ["name"])
|
2013-05-31 01:08:58 +08:00
|
|
|
|
2014-06-16 07:01:49 +08:00
|
|
|
def test_delete_proxy_model(self):
|
|
|
|
"""
|
|
|
|
Tests the DeleteModel operation ignores proxy models.
|
|
|
|
"""
|
|
|
|
project_state = self.set_up_test_model("test_dlprmo", proxy_model=True)
|
|
|
|
# Test the state alteration
|
|
|
|
operation = migrations.DeleteModel("ProxyPony")
|
|
|
|
new_state = project_state.clone()
|
|
|
|
operation.state_forwards("test_dlprmo", new_state)
|
|
|
|
self.assertIn(("test_dlprmo", "proxypony"), project_state.models)
|
|
|
|
self.assertNotIn(("test_dlprmo", "proxypony"), new_state.models)
|
|
|
|
# Test the database alteration
|
|
|
|
self.assertTableExists("test_dlprmo_pony")
|
|
|
|
self.assertTableNotExists("test_dlprmo_proxypony")
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
operation.database_forwards("test_dlprmo", editor, project_state, new_state)
|
|
|
|
self.assertTableExists("test_dlprmo_pony")
|
|
|
|
self.assertTableNotExists("test_dlprmo_proxypony")
|
|
|
|
# And test reversal
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
operation.database_backwards("test_dlprmo", editor, new_state, project_state)
|
|
|
|
self.assertTableExists("test_dlprmo_pony")
|
|
|
|
self.assertTableNotExists("test_dlprmo_proxypony")
|
|
|
|
|
2018-07-26 00:07:42 +08:00
|
|
|
def test_delete_mti_model(self):
|
|
|
|
project_state = self.set_up_test_model('test_dlmtimo', mti_model=True)
|
|
|
|
# Test the state alteration
|
|
|
|
operation = migrations.DeleteModel('ShetlandPony')
|
|
|
|
new_state = project_state.clone()
|
|
|
|
operation.state_forwards('test_dlmtimo', new_state)
|
|
|
|
self.assertIn(('test_dlmtimo', 'shetlandpony'), project_state.models)
|
|
|
|
self.assertNotIn(('test_dlmtimo', 'shetlandpony'), new_state.models)
|
|
|
|
# Test the database alteration
|
|
|
|
self.assertTableExists('test_dlmtimo_pony')
|
|
|
|
self.assertTableExists('test_dlmtimo_shetlandpony')
|
|
|
|
self.assertColumnExists('test_dlmtimo_shetlandpony', 'pony_ptr_id')
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
operation.database_forwards('test_dlmtimo', editor, project_state, new_state)
|
|
|
|
self.assertTableExists('test_dlmtimo_pony')
|
|
|
|
self.assertTableNotExists('test_dlmtimo_shetlandpony')
|
|
|
|
# And test reversal
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
operation.database_backwards('test_dlmtimo', editor, new_state, project_state)
|
|
|
|
self.assertTableExists('test_dlmtimo_pony')
|
|
|
|
self.assertTableExists('test_dlmtimo_shetlandpony')
|
|
|
|
self.assertColumnExists('test_dlmtimo_shetlandpony', 'pony_ptr_id')
|
|
|
|
|
2013-11-06 21:47:58 +08:00
|
|
|
def test_rename_model(self):
|
|
|
|
"""
|
|
|
|
Tests the RenameModel operation.
|
|
|
|
"""
|
2014-06-08 09:17:31 +08:00
|
|
|
project_state = self.set_up_test_model("test_rnmo", related_model=True)
|
2013-11-06 21:47:58 +08:00
|
|
|
# Test the state alteration
|
|
|
|
operation = migrations.RenameModel("Pony", "Horse")
|
2014-06-27 22:34:48 +08:00
|
|
|
self.assertEqual(operation.describe(), "Rename model Pony to Horse")
|
2015-01-10 22:18:06 +08:00
|
|
|
# Test initial state and database
|
|
|
|
self.assertIn(("test_rnmo", "pony"), project_state.models)
|
|
|
|
self.assertNotIn(("test_rnmo", "horse"), project_state.models)
|
2013-11-06 21:47:58 +08:00
|
|
|
self.assertTableExists("test_rnmo_pony")
|
|
|
|
self.assertTableNotExists("test_rnmo_horse")
|
2014-06-08 09:17:31 +08:00
|
|
|
if connection.features.supports_foreign_keys:
|
|
|
|
self.assertFKExists("test_rnmo_rider", ["pony_id"], ("test_rnmo_pony", "id"))
|
|
|
|
self.assertFKNotExists("test_rnmo_rider", ["pony_id"], ("test_rnmo_horse", "id"))
|
2015-01-10 22:18:06 +08:00
|
|
|
# Migrate forwards
|
|
|
|
new_state = project_state.clone()
|
2017-11-27 11:39:43 +08:00
|
|
|
atomic_rename = connection.features.supports_atomic_references_rename
|
|
|
|
new_state = self.apply_operations("test_rnmo", new_state, [operation], atomic=atomic_rename)
|
2015-01-10 22:18:06 +08:00
|
|
|
# Test new state and database
|
|
|
|
self.assertNotIn(("test_rnmo", "pony"), new_state.models)
|
|
|
|
self.assertIn(("test_rnmo", "horse"), new_state.models)
|
|
|
|
# RenameModel also repoints all incoming FKs and M2Ms
|
2020-04-22 11:14:34 +08:00
|
|
|
self.assertEqual(
|
|
|
|
new_state.models['test_rnmo', 'rider'].fields['pony'].remote_field.model,
|
|
|
|
'test_rnmo.Horse',
|
|
|
|
)
|
2013-11-06 21:47:58 +08:00
|
|
|
self.assertTableNotExists("test_rnmo_pony")
|
|
|
|
self.assertTableExists("test_rnmo_horse")
|
2014-06-08 09:17:31 +08:00
|
|
|
if connection.features.supports_foreign_keys:
|
|
|
|
self.assertFKNotExists("test_rnmo_rider", ["pony_id"], ("test_rnmo_pony", "id"))
|
|
|
|
self.assertFKExists("test_rnmo_rider", ["pony_id"], ("test_rnmo_horse", "id"))
|
2015-01-10 22:18:06 +08:00
|
|
|
# Migrate backwards
|
2017-11-27 11:39:43 +08:00
|
|
|
original_state = self.unapply_operations("test_rnmo", project_state, [operation], atomic=atomic_rename)
|
2015-01-10 22:18:06 +08:00
|
|
|
# Test original state and database
|
|
|
|
self.assertIn(("test_rnmo", "pony"), original_state.models)
|
|
|
|
self.assertNotIn(("test_rnmo", "horse"), original_state.models)
|
2020-04-22 11:14:34 +08:00
|
|
|
self.assertEqual(
|
|
|
|
original_state.models['test_rnmo', 'rider'].fields['pony'].remote_field.model,
|
|
|
|
'Pony',
|
|
|
|
)
|
2014-06-08 09:17:31 +08:00
|
|
|
self.assertTableExists("test_rnmo_pony")
|
2013-11-06 21:47:58 +08:00
|
|
|
self.assertTableNotExists("test_rnmo_horse")
|
2014-06-08 09:17:31 +08:00
|
|
|
if connection.features.supports_foreign_keys:
|
|
|
|
self.assertFKExists("test_rnmo_rider", ["pony_id"], ("test_rnmo_pony", "id"))
|
|
|
|
self.assertFKNotExists("test_rnmo_rider", ["pony_id"], ("test_rnmo_horse", "id"))
|
2014-11-23 01:57:13 +08:00
|
|
|
# And deconstruction
|
|
|
|
definition = operation.deconstruct()
|
|
|
|
self.assertEqual(definition[0], "RenameModel")
|
|
|
|
self.assertEqual(definition[1], [])
|
|
|
|
self.assertEqual(definition[2], {'old_name': "Pony", 'new_name': "Horse"})
|
2014-03-11 10:52:29 +08:00
|
|
|
|
2016-10-04 07:23:18 +08:00
|
|
|
def test_rename_model_state_forwards(self):
|
|
|
|
"""
|
|
|
|
RenameModel operations shouldn't trigger the caching of rendered apps
|
|
|
|
on state without prior apps.
|
|
|
|
"""
|
|
|
|
state = ProjectState()
|
|
|
|
state.add_model(ModelState('migrations', 'Foo', []))
|
|
|
|
operation = migrations.RenameModel('Foo', 'Bar')
|
|
|
|
operation.state_forwards('migrations', state)
|
|
|
|
self.assertNotIn('apps', state.__dict__)
|
|
|
|
self.assertNotIn(('migrations', 'foo'), state.models)
|
|
|
|
self.assertIn(('migrations', 'bar'), state.models)
|
|
|
|
# Now with apps cached.
|
|
|
|
apps = state.apps
|
|
|
|
operation = migrations.RenameModel('Bar', 'Foo')
|
|
|
|
operation.state_forwards('migrations', state)
|
|
|
|
self.assertIs(state.apps, apps)
|
|
|
|
self.assertNotIn(('migrations', 'bar'), state.models)
|
|
|
|
self.assertIn(('migrations', 'foo'), state.models)
|
|
|
|
|
2014-08-04 03:02:40 +08:00
|
|
|
def test_rename_model_with_self_referential_fk(self):
|
|
|
|
"""
|
|
|
|
Tests the RenameModel operation on model with self referential FK.
|
|
|
|
"""
|
|
|
|
project_state = self.set_up_test_model("test_rmwsrf", related_model=True)
|
|
|
|
# Test the state alteration
|
|
|
|
operation = migrations.RenameModel("Rider", "HorseRider")
|
|
|
|
self.assertEqual(operation.describe(), "Rename model Rider to HorseRider")
|
|
|
|
new_state = project_state.clone()
|
|
|
|
operation.state_forwards("test_rmwsrf", new_state)
|
|
|
|
self.assertNotIn(("test_rmwsrf", "rider"), new_state.models)
|
|
|
|
self.assertIn(("test_rmwsrf", "horserider"), new_state.models)
|
|
|
|
# Remember, RenameModel also repoints all incoming FKs and M2Ms
|
2015-09-12 07:33:12 +08:00
|
|
|
self.assertEqual(
|
2016-11-04 20:58:46 +08:00
|
|
|
'self',
|
2020-04-22 11:14:34 +08:00
|
|
|
new_state.models["test_rmwsrf", "horserider"].fields['friend'].remote_field.model
|
2015-09-12 07:33:12 +08:00
|
|
|
)
|
2016-11-04 20:58:46 +08:00
|
|
|
HorseRider = new_state.apps.get_model('test_rmwsrf', 'horserider')
|
|
|
|
self.assertIs(HorseRider._meta.get_field('horserider').remote_field.model, HorseRider)
|
2014-08-04 03:02:40 +08:00
|
|
|
# Test the database alteration
|
|
|
|
self.assertTableExists("test_rmwsrf_rider")
|
|
|
|
self.assertTableNotExists("test_rmwsrf_horserider")
|
|
|
|
if connection.features.supports_foreign_keys:
|
|
|
|
self.assertFKExists("test_rmwsrf_rider", ["friend_id"], ("test_rmwsrf_rider", "id"))
|
2014-08-04 04:32:36 +08:00
|
|
|
self.assertFKNotExists("test_rmwsrf_rider", ["friend_id"], ("test_rmwsrf_horserider", "id"))
|
2017-11-27 11:39:43 +08:00
|
|
|
atomic_rename = connection.features.supports_atomic_references_rename
|
|
|
|
with connection.schema_editor(atomic=atomic_rename) as editor:
|
2014-08-04 03:02:40 +08:00
|
|
|
operation.database_forwards("test_rmwsrf", editor, project_state, new_state)
|
|
|
|
self.assertTableNotExists("test_rmwsrf_rider")
|
|
|
|
self.assertTableExists("test_rmwsrf_horserider")
|
|
|
|
if connection.features.supports_foreign_keys:
|
2014-08-04 04:32:36 +08:00
|
|
|
self.assertFKNotExists("test_rmwsrf_horserider", ["friend_id"], ("test_rmwsrf_rider", "id"))
|
2014-08-04 03:02:40 +08:00
|
|
|
self.assertFKExists("test_rmwsrf_horserider", ["friend_id"], ("test_rmwsrf_horserider", "id"))
|
|
|
|
# And test reversal
|
2017-11-27 11:39:43 +08:00
|
|
|
with connection.schema_editor(atomic=atomic_rename) as editor:
|
2014-08-04 03:02:40 +08:00
|
|
|
operation.database_backwards("test_rmwsrf", editor, new_state, project_state)
|
|
|
|
self.assertTableExists("test_rmwsrf_rider")
|
|
|
|
self.assertTableNotExists("test_rmwsrf_horserider")
|
|
|
|
if connection.features.supports_foreign_keys:
|
|
|
|
self.assertFKExists("test_rmwsrf_rider", ["friend_id"], ("test_rmwsrf_rider", "id"))
|
2014-08-04 04:32:36 +08:00
|
|
|
self.assertFKNotExists("test_rmwsrf_rider", ["friend_id"], ("test_rmwsrf_horserider", "id"))
|
2014-08-04 03:02:40 +08:00
|
|
|
|
2015-02-17 03:42:11 +08:00
|
|
|
def test_rename_model_with_superclass_fk(self):
|
|
|
|
"""
|
|
|
|
Tests the RenameModel operation on a model which has a superclass that
|
|
|
|
has a foreign key.
|
|
|
|
"""
|
|
|
|
project_state = self.set_up_test_model("test_rmwsc", related_model=True, mti_model=True)
|
|
|
|
# Test the state alteration
|
|
|
|
operation = migrations.RenameModel("ShetlandPony", "LittleHorse")
|
|
|
|
self.assertEqual(operation.describe(), "Rename model ShetlandPony to LittleHorse")
|
|
|
|
new_state = project_state.clone()
|
|
|
|
operation.state_forwards("test_rmwsc", new_state)
|
|
|
|
self.assertNotIn(("test_rmwsc", "shetlandpony"), new_state.models)
|
|
|
|
self.assertIn(("test_rmwsc", "littlehorse"), new_state.models)
|
|
|
|
# RenameModel shouldn't repoint the superclass's relations, only local ones
|
|
|
|
self.assertEqual(
|
2020-04-22 11:14:34 +08:00
|
|
|
project_state.models['test_rmwsc', 'rider'].fields['pony'].remote_field.model,
|
|
|
|
new_state.models['test_rmwsc', 'rider'].fields['pony'].remote_field.model,
|
2015-02-17 03:42:11 +08:00
|
|
|
)
|
|
|
|
# Before running the migration we have a table for Shetland Pony, not Little Horse
|
|
|
|
self.assertTableExists("test_rmwsc_shetlandpony")
|
|
|
|
self.assertTableNotExists("test_rmwsc_littlehorse")
|
|
|
|
if connection.features.supports_foreign_keys:
|
|
|
|
# and the foreign key on rider points to pony, not shetland pony
|
|
|
|
self.assertFKExists("test_rmwsc_rider", ["pony_id"], ("test_rmwsc_pony", "id"))
|
|
|
|
self.assertFKNotExists("test_rmwsc_rider", ["pony_id"], ("test_rmwsc_shetlandpony", "id"))
|
2017-11-27 11:39:43 +08:00
|
|
|
with connection.schema_editor(atomic=connection.features.supports_atomic_references_rename) as editor:
|
2015-02-17 03:42:11 +08:00
|
|
|
operation.database_forwards("test_rmwsc", editor, project_state, new_state)
|
|
|
|
# Now we have a little horse table, not shetland pony
|
|
|
|
self.assertTableNotExists("test_rmwsc_shetlandpony")
|
|
|
|
self.assertTableExists("test_rmwsc_littlehorse")
|
|
|
|
if connection.features.supports_foreign_keys:
|
|
|
|
# but the Foreign keys still point at pony, not little horse
|
|
|
|
self.assertFKExists("test_rmwsc_rider", ["pony_id"], ("test_rmwsc_pony", "id"))
|
|
|
|
self.assertFKNotExists("test_rmwsc_rider", ["pony_id"], ("test_rmwsc_littlehorse", "id"))
|
|
|
|
|
2014-09-17 13:34:10 +08:00
|
|
|
def test_rename_model_with_self_referential_m2m(self):
|
|
|
|
app_label = "test_rename_model_with_self_referential_m2m"
|
|
|
|
|
|
|
|
project_state = self.apply_operations(app_label, ProjectState(), operations=[
|
|
|
|
migrations.CreateModel("ReflexivePony", fields=[
|
2016-01-18 19:59:58 +08:00
|
|
|
("id", models.AutoField(primary_key=True)),
|
2014-09-17 13:34:10 +08:00
|
|
|
("ponies", models.ManyToManyField("self")),
|
|
|
|
]),
|
|
|
|
])
|
|
|
|
project_state = self.apply_operations(app_label, project_state, operations=[
|
|
|
|
migrations.RenameModel("ReflexivePony", "ReflexivePony2"),
|
2017-11-27 11:39:43 +08:00
|
|
|
], atomic=connection.features.supports_atomic_references_rename)
|
2014-11-05 17:43:31 +08:00
|
|
|
Pony = project_state.apps.get_model(app_label, "ReflexivePony2")
|
2014-09-17 13:34:10 +08:00
|
|
|
pony = Pony.objects.create()
|
|
|
|
pony.ponies.add(pony)
|
|
|
|
|
2015-01-15 22:56:07 +08:00
|
|
|
def test_rename_model_with_m2m(self):
|
|
|
|
app_label = "test_rename_model_with_m2m"
|
|
|
|
project_state = self.apply_operations(app_label, ProjectState(), operations=[
|
2016-01-18 19:59:58 +08:00
|
|
|
migrations.CreateModel("Rider", fields=[
|
|
|
|
("id", models.AutoField(primary_key=True)),
|
|
|
|
]),
|
2015-01-15 22:56:07 +08:00
|
|
|
migrations.CreateModel("Pony", fields=[
|
2016-01-18 19:59:58 +08:00
|
|
|
("id", models.AutoField(primary_key=True)),
|
2015-01-15 22:56:07 +08:00
|
|
|
("riders", models.ManyToManyField("Rider")),
|
|
|
|
]),
|
|
|
|
])
|
|
|
|
Pony = project_state.apps.get_model(app_label, "Pony")
|
|
|
|
Rider = project_state.apps.get_model(app_label, "Rider")
|
|
|
|
pony = Pony.objects.create()
|
|
|
|
rider = Rider.objects.create()
|
|
|
|
pony.riders.add(rider)
|
|
|
|
|
|
|
|
project_state = self.apply_operations(app_label, project_state, operations=[
|
|
|
|
migrations.RenameModel("Pony", "Pony2"),
|
2017-12-05 09:35:33 +08:00
|
|
|
], atomic=connection.features.supports_atomic_references_rename)
|
2015-01-15 22:56:07 +08:00
|
|
|
Pony = project_state.apps.get_model(app_label, "Pony2")
|
|
|
|
Rider = project_state.apps.get_model(app_label, "Rider")
|
|
|
|
pony = Pony.objects.create()
|
|
|
|
rider = Rider.objects.create()
|
2015-04-30 07:39:50 +08:00
|
|
|
pony.riders.add(rider)
|
|
|
|
self.assertEqual(Pony.objects.count(), 2)
|
|
|
|
self.assertEqual(Rider.objects.count(), 2)
|
|
|
|
self.assertEqual(Pony._meta.get_field('riders').remote_field.through.objects.count(), 2)
|
|
|
|
|
|
|
|
def test_rename_m2m_target_model(self):
|
|
|
|
app_label = "test_rename_m2m_target_model"
|
|
|
|
project_state = self.apply_operations(app_label, ProjectState(), operations=[
|
2016-01-18 19:59:58 +08:00
|
|
|
migrations.CreateModel("Rider", fields=[
|
|
|
|
("id", models.AutoField(primary_key=True)),
|
|
|
|
]),
|
2015-04-30 07:39:50 +08:00
|
|
|
migrations.CreateModel("Pony", fields=[
|
2016-01-18 19:59:58 +08:00
|
|
|
("id", models.AutoField(primary_key=True)),
|
2015-04-30 07:39:50 +08:00
|
|
|
("riders", models.ManyToManyField("Rider")),
|
|
|
|
]),
|
|
|
|
])
|
|
|
|
Pony = project_state.apps.get_model(app_label, "Pony")
|
|
|
|
Rider = project_state.apps.get_model(app_label, "Rider")
|
|
|
|
pony = Pony.objects.create()
|
|
|
|
rider = Rider.objects.create()
|
|
|
|
pony.riders.add(rider)
|
|
|
|
|
|
|
|
project_state = self.apply_operations(app_label, project_state, operations=[
|
|
|
|
migrations.RenameModel("Rider", "Rider2"),
|
2017-11-27 11:39:43 +08:00
|
|
|
], atomic=connection.features.supports_atomic_references_rename)
|
2015-04-30 07:39:50 +08:00
|
|
|
Pony = project_state.apps.get_model(app_label, "Pony")
|
|
|
|
Rider = project_state.apps.get_model(app_label, "Rider2")
|
|
|
|
pony = Pony.objects.create()
|
|
|
|
rider = Rider.objects.create()
|
2015-01-15 22:56:07 +08:00
|
|
|
pony.riders.add(rider)
|
|
|
|
self.assertEqual(Pony.objects.count(), 2)
|
|
|
|
self.assertEqual(Rider.objects.count(), 2)
|
2015-02-26 22:19:17 +08:00
|
|
|
self.assertEqual(Pony._meta.get_field('riders').remote_field.through.objects.count(), 2)
|
2015-01-15 22:56:07 +08:00
|
|
|
|
2016-05-11 04:20:27 +08:00
|
|
|
def test_rename_m2m_through_model(self):
|
|
|
|
app_label = "test_rename_through"
|
|
|
|
project_state = self.apply_operations(app_label, ProjectState(), operations=[
|
|
|
|
migrations.CreateModel("Rider", fields=[
|
|
|
|
("id", models.AutoField(primary_key=True)),
|
|
|
|
]),
|
|
|
|
migrations.CreateModel("Pony", fields=[
|
|
|
|
("id", models.AutoField(primary_key=True)),
|
|
|
|
]),
|
|
|
|
migrations.CreateModel("PonyRider", fields=[
|
|
|
|
("id", models.AutoField(primary_key=True)),
|
|
|
|
("rider", models.ForeignKey("test_rename_through.Rider", models.CASCADE)),
|
|
|
|
("pony", models.ForeignKey("test_rename_through.Pony", models.CASCADE)),
|
|
|
|
]),
|
|
|
|
migrations.AddField(
|
|
|
|
"Pony",
|
|
|
|
"riders",
|
|
|
|
models.ManyToManyField("test_rename_through.Rider", through="test_rename_through.PonyRider"),
|
|
|
|
),
|
|
|
|
])
|
|
|
|
Pony = project_state.apps.get_model(app_label, "Pony")
|
|
|
|
Rider = project_state.apps.get_model(app_label, "Rider")
|
|
|
|
PonyRider = project_state.apps.get_model(app_label, "PonyRider")
|
|
|
|
pony = Pony.objects.create()
|
|
|
|
rider = Rider.objects.create()
|
|
|
|
PonyRider.objects.create(pony=pony, rider=rider)
|
|
|
|
|
|
|
|
project_state = self.apply_operations(app_label, project_state, operations=[
|
|
|
|
migrations.RenameModel("PonyRider", "PonyRider2"),
|
|
|
|
])
|
|
|
|
Pony = project_state.apps.get_model(app_label, "Pony")
|
|
|
|
Rider = project_state.apps.get_model(app_label, "Rider")
|
|
|
|
PonyRider = project_state.apps.get_model(app_label, "PonyRider2")
|
|
|
|
pony = Pony.objects.first()
|
|
|
|
rider = Rider.objects.create()
|
|
|
|
PonyRider.objects.create(pony=pony, rider=rider)
|
|
|
|
self.assertEqual(Pony.objects.count(), 1)
|
|
|
|
self.assertEqual(Rider.objects.count(), 2)
|
|
|
|
self.assertEqual(PonyRider.objects.count(), 2)
|
|
|
|
self.assertEqual(pony.riders.count(), 2)
|
|
|
|
|
2018-05-24 03:55:27 +08:00
|
|
|
def test_rename_m2m_model_after_rename_field(self):
|
|
|
|
"""RenameModel renames a many-to-many column after a RenameField."""
|
|
|
|
app_label = 'test_rename_multiple'
|
|
|
|
project_state = self.apply_operations(app_label, ProjectState(), operations=[
|
|
|
|
migrations.CreateModel('Pony', fields=[
|
|
|
|
('id', models.AutoField(primary_key=True)),
|
|
|
|
('name', models.CharField(max_length=20)),
|
|
|
|
]),
|
|
|
|
migrations.CreateModel('Rider', fields=[
|
|
|
|
('id', models.AutoField(primary_key=True)),
|
|
|
|
('pony', models.ForeignKey('test_rename_multiple.Pony', models.CASCADE)),
|
|
|
|
]),
|
|
|
|
migrations.CreateModel('PonyRider', fields=[
|
|
|
|
('id', models.AutoField(primary_key=True)),
|
|
|
|
('riders', models.ManyToManyField('Rider')),
|
|
|
|
]),
|
|
|
|
migrations.RenameField(model_name='pony', old_name='name', new_name='fancy_name'),
|
|
|
|
migrations.RenameModel(old_name='Rider', new_name='Jockey'),
|
|
|
|
], atomic=connection.features.supports_atomic_references_rename)
|
|
|
|
Pony = project_state.apps.get_model(app_label, 'Pony')
|
|
|
|
Jockey = project_state.apps.get_model(app_label, 'Jockey')
|
|
|
|
PonyRider = project_state.apps.get_model(app_label, 'PonyRider')
|
|
|
|
# No "no such column" error means the column was renamed correctly.
|
|
|
|
pony = Pony.objects.create(fancy_name='a good name')
|
|
|
|
jockey = Jockey.objects.create(pony=pony)
|
|
|
|
ponyrider = PonyRider.objects.create()
|
|
|
|
ponyrider.riders.add(jockey)
|
|
|
|
|
2013-05-31 01:08:58 +08:00
|
|
|
def test_add_field(self):
|
|
|
|
"""
|
|
|
|
Tests the AddField operation.
|
|
|
|
"""
|
|
|
|
# Test the state alteration
|
2013-12-04 21:56:22 +08:00
|
|
|
operation = migrations.AddField(
|
|
|
|
"Pony",
|
|
|
|
"height",
|
|
|
|
models.FloatField(null=True, default=5),
|
|
|
|
)
|
2014-06-27 22:34:48 +08:00
|
|
|
self.assertEqual(operation.describe(), "Add field height to Pony")
|
2014-06-18 08:45:38 +08:00
|
|
|
project_state, new_state = self.make_test_state("test_adfl", operation)
|
2013-07-02 18:19:02 +08:00
|
|
|
self.assertEqual(len(new_state.models["test_adfl", "pony"].fields), 4)
|
2020-04-22 11:14:34 +08:00
|
|
|
field = new_state.models['test_adfl', 'pony'].fields['height']
|
2013-12-04 21:56:22 +08:00
|
|
|
self.assertEqual(field.default, 5)
|
2013-05-31 01:08:58 +08:00
|
|
|
# Test the database alteration
|
|
|
|
self.assertColumnNotExists("test_adfl_pony", "height")
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
operation.database_forwards("test_adfl", editor, project_state, new_state)
|
|
|
|
self.assertColumnExists("test_adfl_pony", "height")
|
|
|
|
# And test reversal
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
operation.database_backwards("test_adfl", editor, new_state, project_state)
|
|
|
|
self.assertColumnNotExists("test_adfl_pony", "height")
|
2014-11-23 01:57:13 +08:00
|
|
|
# And deconstruction
|
|
|
|
definition = operation.deconstruct()
|
|
|
|
self.assertEqual(definition[0], "AddField")
|
|
|
|
self.assertEqual(definition[1], [])
|
|
|
|
self.assertEqual(sorted(definition[2]), ["field", "model_name", "name"])
|
2013-06-20 21:54:11 +08:00
|
|
|
|
2014-05-18 12:58:16 +08:00
|
|
|
def test_add_charfield(self):
|
|
|
|
"""
|
|
|
|
Tests the AddField operation on TextField.
|
|
|
|
"""
|
|
|
|
project_state = self.set_up_test_model("test_adchfl")
|
|
|
|
|
2014-11-05 17:43:31 +08:00
|
|
|
Pony = project_state.apps.get_model("test_adchfl", "Pony")
|
2014-05-18 12:58:16 +08:00
|
|
|
pony = Pony.objects.create(weight=42)
|
|
|
|
|
|
|
|
new_state = self.apply_operations("test_adchfl", project_state, [
|
|
|
|
migrations.AddField(
|
|
|
|
"Pony",
|
|
|
|
"text",
|
|
|
|
models.CharField(max_length=10, default="some text"),
|
|
|
|
),
|
|
|
|
migrations.AddField(
|
|
|
|
"Pony",
|
|
|
|
"empty",
|
|
|
|
models.CharField(max_length=10, default=""),
|
|
|
|
),
|
|
|
|
# If not properly quoted digits would be interpreted as an int.
|
|
|
|
migrations.AddField(
|
|
|
|
"Pony",
|
|
|
|
"digits",
|
|
|
|
models.CharField(max_length=10, default="42"),
|
|
|
|
),
|
2014-05-18 13:46:12 +08:00
|
|
|
# Manual quoting is fragile and could trip on quotes. Refs #xyz.
|
|
|
|
migrations.AddField(
|
|
|
|
"Pony",
|
|
|
|
"quotes",
|
|
|
|
models.CharField(max_length=10, default='"\'"'),
|
|
|
|
),
|
2014-05-18 12:58:16 +08:00
|
|
|
])
|
|
|
|
|
2014-11-05 17:43:31 +08:00
|
|
|
Pony = new_state.apps.get_model("test_adchfl", "Pony")
|
2014-05-18 12:58:16 +08:00
|
|
|
pony = Pony.objects.get(pk=pony.pk)
|
|
|
|
self.assertEqual(pony.text, "some text")
|
|
|
|
self.assertEqual(pony.empty, "")
|
|
|
|
self.assertEqual(pony.digits, "42")
|
2014-05-18 13:46:12 +08:00
|
|
|
self.assertEqual(pony.quotes, '"\'"')
|
2014-05-18 12:58:16 +08:00
|
|
|
|
|
|
|
def test_add_textfield(self):
|
|
|
|
"""
|
|
|
|
Tests the AddField operation on TextField.
|
|
|
|
"""
|
|
|
|
project_state = self.set_up_test_model("test_adtxtfl")
|
|
|
|
|
2014-11-05 17:43:31 +08:00
|
|
|
Pony = project_state.apps.get_model("test_adtxtfl", "Pony")
|
2014-05-18 12:58:16 +08:00
|
|
|
pony = Pony.objects.create(weight=42)
|
|
|
|
|
|
|
|
new_state = self.apply_operations("test_adtxtfl", project_state, [
|
|
|
|
migrations.AddField(
|
|
|
|
"Pony",
|
|
|
|
"text",
|
|
|
|
models.TextField(default="some text"),
|
|
|
|
),
|
|
|
|
migrations.AddField(
|
|
|
|
"Pony",
|
|
|
|
"empty",
|
|
|
|
models.TextField(default=""),
|
|
|
|
),
|
|
|
|
# If not properly quoted digits would be interpreted as an int.
|
|
|
|
migrations.AddField(
|
|
|
|
"Pony",
|
|
|
|
"digits",
|
|
|
|
models.TextField(default="42"),
|
|
|
|
),
|
2014-05-18 13:46:12 +08:00
|
|
|
# Manual quoting is fragile and could trip on quotes. Refs #xyz.
|
|
|
|
migrations.AddField(
|
|
|
|
"Pony",
|
|
|
|
"quotes",
|
|
|
|
models.TextField(default='"\'"'),
|
|
|
|
),
|
2014-05-18 12:58:16 +08:00
|
|
|
])
|
|
|
|
|
2014-11-05 17:43:31 +08:00
|
|
|
Pony = new_state.apps.get_model("test_adtxtfl", "Pony")
|
2014-05-18 12:58:16 +08:00
|
|
|
pony = Pony.objects.get(pk=pony.pk)
|
|
|
|
self.assertEqual(pony.text, "some text")
|
|
|
|
self.assertEqual(pony.empty, "")
|
|
|
|
self.assertEqual(pony.digits, "42")
|
2014-05-18 13:46:12 +08:00
|
|
|
self.assertEqual(pony.quotes, '"\'"')
|
2014-05-18 12:58:16 +08:00
|
|
|
|
|
|
|
def test_add_binaryfield(self):
|
|
|
|
"""
|
|
|
|
Tests the AddField operation on TextField/BinaryField.
|
|
|
|
"""
|
|
|
|
project_state = self.set_up_test_model("test_adbinfl")
|
|
|
|
|
2014-11-05 17:43:31 +08:00
|
|
|
Pony = project_state.apps.get_model("test_adbinfl", "Pony")
|
2014-05-18 12:58:16 +08:00
|
|
|
pony = Pony.objects.create(weight=42)
|
|
|
|
|
|
|
|
new_state = self.apply_operations("test_adbinfl", project_state, [
|
|
|
|
migrations.AddField(
|
|
|
|
"Pony",
|
|
|
|
"blob",
|
|
|
|
models.BinaryField(default=b"some text"),
|
|
|
|
),
|
|
|
|
migrations.AddField(
|
|
|
|
"Pony",
|
|
|
|
"empty",
|
|
|
|
models.BinaryField(default=b""),
|
|
|
|
),
|
|
|
|
# If not properly quoted digits would be interpreted as an int.
|
|
|
|
migrations.AddField(
|
|
|
|
"Pony",
|
|
|
|
"digits",
|
|
|
|
models.BinaryField(default=b"42"),
|
|
|
|
),
|
2014-05-18 13:46:12 +08:00
|
|
|
# Manual quoting is fragile and could trip on quotes. Refs #xyz.
|
|
|
|
migrations.AddField(
|
|
|
|
"Pony",
|
|
|
|
"quotes",
|
|
|
|
models.BinaryField(default=b'"\'"'),
|
|
|
|
),
|
2014-05-18 12:58:16 +08:00
|
|
|
])
|
|
|
|
|
2014-11-05 17:43:31 +08:00
|
|
|
Pony = new_state.apps.get_model("test_adbinfl", "Pony")
|
2014-05-18 12:58:16 +08:00
|
|
|
pony = Pony.objects.get(pk=pony.pk)
|
|
|
|
# SQLite returns buffer/memoryview, cast to bytes for checking.
|
|
|
|
self.assertEqual(bytes(pony.blob), b"some text")
|
|
|
|
self.assertEqual(bytes(pony.empty), b"")
|
|
|
|
self.assertEqual(bytes(pony.digits), b"42")
|
2014-05-18 13:46:12 +08:00
|
|
|
self.assertEqual(bytes(pony.quotes), b'"\'"')
|
2014-05-18 12:58:16 +08:00
|
|
|
|
2014-02-28 20:14:09 +08:00
|
|
|
def test_column_name_quoting(self):
|
|
|
|
"""
|
|
|
|
Column names that are SQL keywords shouldn't cause problems when used
|
|
|
|
in migrations (#22168).
|
|
|
|
"""
|
|
|
|
project_state = self.set_up_test_model("test_regr22168")
|
|
|
|
operation = migrations.AddField(
|
|
|
|
"Pony",
|
|
|
|
"order",
|
|
|
|
models.IntegerField(default=0),
|
|
|
|
)
|
|
|
|
new_state = project_state.clone()
|
|
|
|
operation.state_forwards("test_regr22168", new_state)
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
operation.database_forwards("test_regr22168", editor, project_state, new_state)
|
|
|
|
self.assertColumnExists("test_regr22168_pony", "order")
|
|
|
|
|
2013-12-04 21:56:22 +08:00
|
|
|
def test_add_field_preserve_default(self):
|
|
|
|
"""
|
|
|
|
Tests the AddField operation's state alteration
|
|
|
|
when preserve_default = False.
|
|
|
|
"""
|
|
|
|
project_state = self.set_up_test_model("test_adflpd")
|
|
|
|
# Test the state alteration
|
|
|
|
operation = migrations.AddField(
|
|
|
|
"Pony",
|
|
|
|
"height",
|
|
|
|
models.FloatField(null=True, default=4),
|
2013-12-05 22:08:34 +08:00
|
|
|
preserve_default=False,
|
2013-12-04 21:56:22 +08:00
|
|
|
)
|
|
|
|
new_state = project_state.clone()
|
|
|
|
operation.state_forwards("test_adflpd", new_state)
|
|
|
|
self.assertEqual(len(new_state.models["test_adflpd", "pony"].fields), 4)
|
2020-04-22 11:14:34 +08:00
|
|
|
field = new_state.models['test_adflpd', 'pony'].fields['height']
|
2019-08-20 15:54:41 +08:00
|
|
|
self.assertEqual(field.default, models.NOT_PROVIDED)
|
2014-01-20 01:42:29 +08:00
|
|
|
# Test the database alteration
|
2014-11-05 17:43:31 +08:00
|
|
|
project_state.apps.get_model("test_adflpd", "pony").objects.create(
|
2014-01-20 11:15:31 +08:00
|
|
|
weight=4,
|
2014-01-20 01:42:29 +08:00
|
|
|
)
|
|
|
|
self.assertColumnNotExists("test_adflpd_pony", "height")
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
operation.database_forwards("test_adflpd", editor, project_state, new_state)
|
|
|
|
self.assertColumnExists("test_adflpd_pony", "height")
|
2014-11-23 01:57:13 +08:00
|
|
|
# And deconstruction
|
|
|
|
definition = operation.deconstruct()
|
|
|
|
self.assertEqual(definition[0], "AddField")
|
|
|
|
self.assertEqual(definition[1], [])
|
|
|
|
self.assertEqual(sorted(definition[2]), ["field", "model_name", "name", "preserve_default"])
|
2013-12-04 21:56:22 +08:00
|
|
|
|
2013-07-25 23:31:34 +08:00
|
|
|
def test_add_field_m2m(self):
|
|
|
|
"""
|
|
|
|
Tests the AddField operation with a ManyToManyField.
|
|
|
|
"""
|
|
|
|
project_state = self.set_up_test_model("test_adflmm", second_model=True)
|
|
|
|
# Test the state alteration
|
2013-08-19 20:50:26 +08:00
|
|
|
operation = migrations.AddField("Pony", "stables", models.ManyToManyField("Stable", related_name="ponies"))
|
2013-07-25 23:31:34 +08:00
|
|
|
new_state = project_state.clone()
|
|
|
|
operation.state_forwards("test_adflmm", new_state)
|
|
|
|
self.assertEqual(len(new_state.models["test_adflmm", "pony"].fields), 4)
|
|
|
|
# Test the database alteration
|
|
|
|
self.assertTableNotExists("test_adflmm_pony_stables")
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
operation.database_forwards("test_adflmm", editor, project_state, new_state)
|
|
|
|
self.assertTableExists("test_adflmm_pony_stables")
|
|
|
|
self.assertColumnNotExists("test_adflmm_pony", "stables")
|
2013-08-19 20:50:26 +08:00
|
|
|
# Make sure the M2M field actually works
|
2013-08-23 19:07:43 +08:00
|
|
|
with atomic():
|
2014-11-05 17:43:31 +08:00
|
|
|
Pony = new_state.apps.get_model("test_adflmm", "Pony")
|
2013-08-23 19:07:43 +08:00
|
|
|
p = Pony.objects.create(pink=False, weight=4.55)
|
|
|
|
p.stables.create()
|
|
|
|
self.assertEqual(p.stables.count(), 1)
|
|
|
|
p.stables.all().delete()
|
2013-07-25 23:31:34 +08:00
|
|
|
# And test reversal
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
operation.database_backwards("test_adflmm", editor, new_state, project_state)
|
|
|
|
self.assertTableNotExists("test_adflmm_pony_stables")
|
|
|
|
|
2014-03-31 14:09:12 +08:00
|
|
|
def test_alter_field_m2m(self):
|
|
|
|
project_state = self.set_up_test_model("test_alflmm", second_model=True)
|
|
|
|
|
|
|
|
project_state = self.apply_operations("test_alflmm", project_state, operations=[
|
|
|
|
migrations.AddField("Pony", "stables", models.ManyToManyField("Stable", related_name="ponies"))
|
|
|
|
])
|
2014-11-05 17:43:31 +08:00
|
|
|
Pony = project_state.apps.get_model("test_alflmm", "Pony")
|
2014-03-31 14:09:12 +08:00
|
|
|
self.assertFalse(Pony._meta.get_field('stables').blank)
|
|
|
|
|
|
|
|
project_state = self.apply_operations("test_alflmm", project_state, operations=[
|
2015-09-12 07:33:12 +08:00
|
|
|
migrations.AlterField(
|
|
|
|
"Pony", "stables", models.ManyToManyField(to="Stable", related_name="ponies", blank=True)
|
|
|
|
)
|
2014-03-31 14:09:12 +08:00
|
|
|
])
|
2014-11-05 17:43:31 +08:00
|
|
|
Pony = project_state.apps.get_model("test_alflmm", "Pony")
|
2014-03-31 14:09:12 +08:00
|
|
|
self.assertTrue(Pony._meta.get_field('stables').blank)
|
|
|
|
|
2014-05-20 23:25:59 +08:00
|
|
|
def test_repoint_field_m2m(self):
|
|
|
|
project_state = self.set_up_test_model("test_alflmm", second_model=True, third_model=True)
|
|
|
|
|
|
|
|
project_state = self.apply_operations("test_alflmm", project_state, operations=[
|
|
|
|
migrations.AddField("Pony", "places", models.ManyToManyField("Stable", related_name="ponies"))
|
|
|
|
])
|
2014-11-05 17:43:31 +08:00
|
|
|
Pony = project_state.apps.get_model("test_alflmm", "Pony")
|
2014-05-20 23:25:59 +08:00
|
|
|
|
|
|
|
project_state = self.apply_operations("test_alflmm", project_state, operations=[
|
|
|
|
migrations.AlterField("Pony", "places", models.ManyToManyField(to="Van", related_name="ponies"))
|
|
|
|
])
|
|
|
|
|
|
|
|
# Ensure the new field actually works
|
2014-11-05 17:43:31 +08:00
|
|
|
Pony = project_state.apps.get_model("test_alflmm", "Pony")
|
2014-05-20 23:25:59 +08:00
|
|
|
p = Pony.objects.create(pink=False, weight=4.55)
|
|
|
|
p.places.create()
|
|
|
|
self.assertEqual(p.places.count(), 1)
|
|
|
|
p.places.all().delete()
|
|
|
|
|
2014-04-08 13:30:25 +08:00
|
|
|
def test_remove_field_m2m(self):
|
|
|
|
project_state = self.set_up_test_model("test_rmflmm", second_model=True)
|
|
|
|
|
|
|
|
project_state = self.apply_operations("test_rmflmm", project_state, operations=[
|
|
|
|
migrations.AddField("Pony", "stables", models.ManyToManyField("Stable", related_name="ponies"))
|
|
|
|
])
|
|
|
|
self.assertTableExists("test_rmflmm_pony_stables")
|
|
|
|
|
2014-11-06 03:53:39 +08:00
|
|
|
with_field_state = project_state.clone()
|
2014-04-08 13:30:25 +08:00
|
|
|
operations = [migrations.RemoveField("Pony", "stables")]
|
2014-11-06 03:53:39 +08:00
|
|
|
project_state = self.apply_operations("test_rmflmm", project_state, operations=operations)
|
2014-04-08 13:30:25 +08:00
|
|
|
self.assertTableNotExists("test_rmflmm_pony_stables")
|
|
|
|
|
|
|
|
# And test reversal
|
2014-11-06 03:53:39 +08:00
|
|
|
self.unapply_operations("test_rmflmm", with_field_state, operations=operations)
|
2014-04-08 13:30:25 +08:00
|
|
|
self.assertTableExists("test_rmflmm_pony_stables")
|
|
|
|
|
|
|
|
def test_remove_field_m2m_with_through(self):
|
|
|
|
project_state = self.set_up_test_model("test_rmflmmwt", second_model=True)
|
|
|
|
|
|
|
|
self.assertTableNotExists("test_rmflmmwt_ponystables")
|
|
|
|
project_state = self.apply_operations("test_rmflmmwt", project_state, operations=[
|
|
|
|
migrations.CreateModel("PonyStables", fields=[
|
2015-07-22 22:43:21 +08:00
|
|
|
("pony", models.ForeignKey('test_rmflmmwt.Pony', models.CASCADE)),
|
|
|
|
("stable", models.ForeignKey('test_rmflmmwt.Stable', models.CASCADE)),
|
2014-04-08 13:30:25 +08:00
|
|
|
]),
|
2015-09-12 07:33:12 +08:00
|
|
|
migrations.AddField(
|
|
|
|
"Pony", "stables",
|
|
|
|
models.ManyToManyField("Stable", related_name="ponies", through='test_rmflmmwt.PonyStables')
|
|
|
|
)
|
2014-04-08 13:30:25 +08:00
|
|
|
])
|
|
|
|
self.assertTableExists("test_rmflmmwt_ponystables")
|
|
|
|
|
2017-05-09 06:55:54 +08:00
|
|
|
operations = [migrations.RemoveField("Pony", "stables"), migrations.DeleteModel("PonyStables")]
|
2014-04-08 13:30:25 +08:00
|
|
|
self.apply_operations("test_rmflmmwt", project_state, operations=operations)
|
|
|
|
|
2013-06-20 21:54:11 +08:00
|
|
|
def test_remove_field(self):
|
|
|
|
"""
|
|
|
|
Tests the RemoveField operation.
|
|
|
|
"""
|
|
|
|
project_state = self.set_up_test_model("test_rmfl")
|
|
|
|
# Test the state alteration
|
|
|
|
operation = migrations.RemoveField("Pony", "pink")
|
2014-06-27 22:34:48 +08:00
|
|
|
self.assertEqual(operation.describe(), "Remove field pink from Pony")
|
2013-06-20 21:54:11 +08:00
|
|
|
new_state = project_state.clone()
|
|
|
|
operation.state_forwards("test_rmfl", new_state)
|
2013-07-02 18:19:02 +08:00
|
|
|
self.assertEqual(len(new_state.models["test_rmfl", "pony"].fields), 2)
|
2013-06-20 21:54:11 +08:00
|
|
|
# Test the database alteration
|
|
|
|
self.assertColumnExists("test_rmfl_pony", "pink")
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
operation.database_forwards("test_rmfl", editor, project_state, new_state)
|
|
|
|
self.assertColumnNotExists("test_rmfl_pony", "pink")
|
|
|
|
# And test reversal
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
operation.database_backwards("test_rmfl", editor, new_state, project_state)
|
|
|
|
self.assertColumnExists("test_rmfl_pony", "pink")
|
2014-11-23 01:57:13 +08:00
|
|
|
# And deconstruction
|
|
|
|
definition = operation.deconstruct()
|
|
|
|
self.assertEqual(definition[0], "RemoveField")
|
|
|
|
self.assertEqual(definition[1], [])
|
|
|
|
self.assertEqual(definition[2], {'model_name': "Pony", 'name': 'pink'})
|
2013-06-20 21:54:11 +08:00
|
|
|
|
2014-04-16 22:55:34 +08:00
|
|
|
def test_remove_fk(self):
|
|
|
|
"""
|
|
|
|
Tests the RemoveField operation on a foreign key.
|
|
|
|
"""
|
|
|
|
project_state = self.set_up_test_model("test_rfk", related_model=True)
|
|
|
|
self.assertColumnExists("test_rfk_rider", "pony_id")
|
|
|
|
operation = migrations.RemoveField("Rider", "pony")
|
2014-06-27 22:34:48 +08:00
|
|
|
|
2014-04-16 22:55:34 +08:00
|
|
|
new_state = project_state.clone()
|
|
|
|
operation.state_forwards("test_rfk", new_state)
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
operation.database_forwards("test_rfk", editor, project_state, new_state)
|
|
|
|
self.assertColumnNotExists("test_rfk_rider", "pony_id")
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
operation.database_backwards("test_rfk", editor, new_state, project_state)
|
|
|
|
self.assertColumnExists("test_rfk_rider", "pony_id")
|
|
|
|
|
2013-06-20 21:54:11 +08:00
|
|
|
def test_alter_model_table(self):
|
|
|
|
"""
|
|
|
|
Tests the AlterModelTable operation.
|
|
|
|
"""
|
|
|
|
project_state = self.set_up_test_model("test_almota")
|
|
|
|
# Test the state alteration
|
|
|
|
operation = migrations.AlterModelTable("Pony", "test_almota_pony_2")
|
2014-06-27 22:34:48 +08:00
|
|
|
self.assertEqual(operation.describe(), "Rename table for Pony to test_almota_pony_2")
|
2013-06-20 21:54:11 +08:00
|
|
|
new_state = project_state.clone()
|
|
|
|
operation.state_forwards("test_almota", new_state)
|
|
|
|
self.assertEqual(new_state.models["test_almota", "pony"].options["db_table"], "test_almota_pony_2")
|
|
|
|
# Test the database alteration
|
|
|
|
self.assertTableExists("test_almota_pony")
|
|
|
|
self.assertTableNotExists("test_almota_pony_2")
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
operation.database_forwards("test_almota", editor, project_state, new_state)
|
|
|
|
self.assertTableNotExists("test_almota_pony")
|
|
|
|
self.assertTableExists("test_almota_pony_2")
|
|
|
|
# And test reversal
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
operation.database_backwards("test_almota", editor, new_state, project_state)
|
|
|
|
self.assertTableExists("test_almota_pony")
|
|
|
|
self.assertTableNotExists("test_almota_pony_2")
|
2014-11-23 01:57:13 +08:00
|
|
|
# And deconstruction
|
|
|
|
definition = operation.deconstruct()
|
|
|
|
self.assertEqual(definition[0], "AlterModelTable")
|
|
|
|
self.assertEqual(definition[1], [])
|
|
|
|
self.assertEqual(definition[2], {'name': "Pony", 'table': "test_almota_pony_2"})
|
2013-06-20 22:12:59 +08:00
|
|
|
|
2016-07-18 05:56:12 +08:00
|
|
|
def test_alter_model_table_none(self):
|
|
|
|
"""
|
|
|
|
Tests the AlterModelTable operation if the table name is set to None.
|
|
|
|
"""
|
|
|
|
operation = migrations.AlterModelTable("Pony", None)
|
|
|
|
self.assertEqual(operation.describe(), "Rename table for Pony to (default)")
|
|
|
|
|
2014-07-21 17:14:00 +08:00
|
|
|
def test_alter_model_table_noop(self):
|
|
|
|
"""
|
|
|
|
Tests the AlterModelTable operation if the table name is not changed.
|
|
|
|
"""
|
|
|
|
project_state = self.set_up_test_model("test_almota")
|
|
|
|
# Test the state alteration
|
|
|
|
operation = migrations.AlterModelTable("Pony", "test_almota_pony")
|
|
|
|
new_state = project_state.clone()
|
|
|
|
operation.state_forwards("test_almota", new_state)
|
|
|
|
self.assertEqual(new_state.models["test_almota", "pony"].options["db_table"], "test_almota_pony")
|
|
|
|
# Test the database alteration
|
|
|
|
self.assertTableExists("test_almota_pony")
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
operation.database_forwards("test_almota", editor, project_state, new_state)
|
|
|
|
self.assertTableExists("test_almota_pony")
|
|
|
|
# And test reversal
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
operation.database_backwards("test_almota", editor, new_state, project_state)
|
|
|
|
self.assertTableExists("test_almota_pony")
|
|
|
|
|
2014-10-22 21:59:00 +08:00
|
|
|
def test_alter_model_table_m2m(self):
|
|
|
|
"""
|
|
|
|
AlterModelTable should rename auto-generated M2M tables.
|
|
|
|
"""
|
|
|
|
app_label = "test_talflmltlm2m"
|
|
|
|
pony_db_table = 'pony_foo'
|
|
|
|
project_state = self.set_up_test_model(app_label, second_model=True, db_table=pony_db_table)
|
|
|
|
# Add the M2M field
|
|
|
|
first_state = project_state.clone()
|
|
|
|
operation = migrations.AddField("Pony", "stables", models.ManyToManyField("Stable"))
|
|
|
|
operation.state_forwards(app_label, first_state)
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
operation.database_forwards(app_label, editor, project_state, first_state)
|
|
|
|
original_m2m_table = "%s_%s" % (pony_db_table, "stables")
|
|
|
|
new_m2m_table = "%s_%s" % (app_label, "pony_stables")
|
|
|
|
self.assertTableExists(original_m2m_table)
|
|
|
|
self.assertTableNotExists(new_m2m_table)
|
|
|
|
# Rename the Pony db_table which should also rename the m2m table.
|
|
|
|
second_state = first_state.clone()
|
|
|
|
operation = migrations.AlterModelTable(name='pony', table=None)
|
|
|
|
operation.state_forwards(app_label, second_state)
|
2017-12-05 09:35:33 +08:00
|
|
|
atomic_rename = connection.features.supports_atomic_references_rename
|
|
|
|
with connection.schema_editor(atomic=atomic_rename) as editor:
|
2014-10-22 21:59:00 +08:00
|
|
|
operation.database_forwards(app_label, editor, first_state, second_state)
|
|
|
|
self.assertTableExists(new_m2m_table)
|
|
|
|
self.assertTableNotExists(original_m2m_table)
|
|
|
|
# And test reversal
|
2017-12-05 09:35:33 +08:00
|
|
|
with connection.schema_editor(atomic=atomic_rename) as editor:
|
2014-10-22 21:59:00 +08:00
|
|
|
operation.database_backwards(app_label, editor, second_state, first_state)
|
|
|
|
self.assertTableExists(original_m2m_table)
|
|
|
|
self.assertTableNotExists(new_m2m_table)
|
|
|
|
|
2013-06-20 22:12:59 +08:00
|
|
|
def test_alter_field(self):
|
|
|
|
"""
|
|
|
|
Tests the AlterField operation.
|
|
|
|
"""
|
|
|
|
project_state = self.set_up_test_model("test_alfl")
|
|
|
|
# Test the state alteration
|
|
|
|
operation = migrations.AlterField("Pony", "pink", models.IntegerField(null=True))
|
2014-06-27 22:34:48 +08:00
|
|
|
self.assertEqual(operation.describe(), "Alter field pink on Pony")
|
2013-06-20 22:12:59 +08:00
|
|
|
new_state = project_state.clone()
|
|
|
|
operation.state_forwards("test_alfl", new_state)
|
2020-04-22 11:14:34 +08:00
|
|
|
self.assertIs(project_state.models['test_alfl', 'pony'].fields['pink'].null, False)
|
|
|
|
self.assertIs(new_state.models['test_alfl', 'pony'].fields['pink'].null, True)
|
2013-06-20 22:12:59 +08:00
|
|
|
# Test the database alteration
|
|
|
|
self.assertColumnNotNull("test_alfl_pony", "pink")
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
operation.database_forwards("test_alfl", editor, project_state, new_state)
|
|
|
|
self.assertColumnNull("test_alfl_pony", "pink")
|
|
|
|
# And test reversal
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
operation.database_backwards("test_alfl", editor, new_state, project_state)
|
|
|
|
self.assertColumnNotNull("test_alfl_pony", "pink")
|
2014-11-23 01:57:13 +08:00
|
|
|
# And deconstruction
|
|
|
|
definition = operation.deconstruct()
|
|
|
|
self.assertEqual(definition[0], "AlterField")
|
|
|
|
self.assertEqual(definition[1], [])
|
|
|
|
self.assertEqual(sorted(definition[2]), ["field", "model_name", "name"])
|
2013-06-20 22:12:59 +08:00
|
|
|
|
2013-12-11 22:19:05 +08:00
|
|
|
def test_alter_field_pk(self):
|
|
|
|
"""
|
|
|
|
Tests the AlterField operation on primary keys (for things like PostgreSQL's SERIAL weirdness)
|
|
|
|
"""
|
|
|
|
project_state = self.set_up_test_model("test_alflpk")
|
|
|
|
# Test the state alteration
|
|
|
|
operation = migrations.AlterField("Pony", "id", models.IntegerField(primary_key=True))
|
|
|
|
new_state = project_state.clone()
|
|
|
|
operation.state_forwards("test_alflpk", new_state)
|
2020-04-22 11:14:34 +08:00
|
|
|
self.assertIsInstance(
|
|
|
|
project_state.models['test_alflpk', 'pony'].fields['id'],
|
|
|
|
models.AutoField,
|
|
|
|
)
|
|
|
|
self.assertIsInstance(
|
|
|
|
new_state.models['test_alflpk', 'pony'].fields['id'],
|
|
|
|
models.IntegerField,
|
|
|
|
)
|
2013-12-11 22:19:05 +08:00
|
|
|
# Test the database alteration
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
operation.database_forwards("test_alflpk", editor, project_state, new_state)
|
|
|
|
# And test reversal
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
operation.database_backwards("test_alflpk", editor, new_state, project_state)
|
|
|
|
|
2015-08-08 23:27:06 +08:00
|
|
|
@skipUnlessDBFeature('supports_foreign_keys')
|
2013-12-11 22:19:05 +08:00
|
|
|
def test_alter_field_pk_fk(self):
|
|
|
|
"""
|
|
|
|
Tests the AlterField operation on primary keys changes any FKs pointing to it.
|
|
|
|
"""
|
|
|
|
project_state = self.set_up_test_model("test_alflpkfk", related_model=True)
|
2020-04-17 12:39:24 +08:00
|
|
|
project_state = self.apply_operations('test_alflpkfk', project_state, [
|
|
|
|
migrations.CreateModel('Stable', fields=[
|
|
|
|
('ponies', models.ManyToManyField('Pony')),
|
|
|
|
]),
|
|
|
|
migrations.AddField(
|
|
|
|
'Pony',
|
|
|
|
'stables',
|
|
|
|
models.ManyToManyField('Stable'),
|
|
|
|
),
|
|
|
|
])
|
2013-12-11 22:19:05 +08:00
|
|
|
# Test the state alteration
|
|
|
|
operation = migrations.AlterField("Pony", "id", models.FloatField(primary_key=True))
|
|
|
|
new_state = project_state.clone()
|
|
|
|
operation.state_forwards("test_alflpkfk", new_state)
|
2020-04-22 11:14:34 +08:00
|
|
|
self.assertIsInstance(
|
|
|
|
project_state.models['test_alflpkfk', 'pony'].fields['id'],
|
|
|
|
models.AutoField,
|
|
|
|
)
|
|
|
|
self.assertIsInstance(
|
|
|
|
new_state.models['test_alflpkfk', 'pony'].fields['id'],
|
|
|
|
models.FloatField,
|
|
|
|
)
|
2014-01-09 23:05:15 +08:00
|
|
|
|
2014-02-03 04:44:01 +08:00
|
|
|
def assertIdTypeEqualsFkType():
|
2014-01-09 23:05:15 +08:00
|
|
|
with connection.cursor() as cursor:
|
2015-04-11 22:10:31 +08:00
|
|
|
id_type, id_null = [
|
|
|
|
(c.type_code, c.null_ok)
|
|
|
|
for c in connection.introspection.get_table_description(cursor, "test_alflpkfk_pony")
|
|
|
|
if c.name == "id"
|
|
|
|
][0]
|
|
|
|
fk_type, fk_null = [
|
|
|
|
(c.type_code, c.null_ok)
|
|
|
|
for c in connection.introspection.get_table_description(cursor, "test_alflpkfk_rider")
|
|
|
|
if c.name == "pony_id"
|
|
|
|
][0]
|
2020-04-17 12:39:24 +08:00
|
|
|
m2m_fk_type, m2m_fk_null = [
|
|
|
|
(c.type_code, c.null_ok)
|
|
|
|
for c in connection.introspection.get_table_description(
|
|
|
|
cursor,
|
|
|
|
'test_alflpkfk_pony_stables',
|
|
|
|
) if c.name == 'pony_id'
|
|
|
|
][0]
|
|
|
|
remote_m2m_fk_type, remote_m2m_fk_null = [
|
|
|
|
(c.type_code, c.null_ok)
|
|
|
|
for c in connection.introspection.get_table_description(
|
|
|
|
cursor,
|
|
|
|
'test_alflpkfk_stable_ponies',
|
|
|
|
) if c.name == 'pony_id'
|
|
|
|
][0]
|
2014-01-09 23:05:15 +08:00
|
|
|
self.assertEqual(id_type, fk_type)
|
2020-04-17 12:39:24 +08:00
|
|
|
self.assertEqual(id_type, m2m_fk_type)
|
|
|
|
self.assertEqual(id_type, remote_m2m_fk_type)
|
2015-04-11 22:10:31 +08:00
|
|
|
self.assertEqual(id_null, fk_null)
|
2020-04-17 12:39:24 +08:00
|
|
|
self.assertEqual(id_null, m2m_fk_null)
|
|
|
|
self.assertEqual(id_null, remote_m2m_fk_null)
|
2014-02-03 04:44:01 +08:00
|
|
|
|
2014-01-09 23:05:15 +08:00
|
|
|
assertIdTypeEqualsFkType()
|
2013-12-11 22:19:05 +08:00
|
|
|
# Test the database alteration
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
operation.database_forwards("test_alflpkfk", editor, project_state, new_state)
|
2014-01-09 23:05:15 +08:00
|
|
|
assertIdTypeEqualsFkType()
|
2013-12-11 22:19:05 +08:00
|
|
|
# And test reversal
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
operation.database_backwards("test_alflpkfk", editor, new_state, project_state)
|
2014-01-09 23:05:15 +08:00
|
|
|
assertIdTypeEqualsFkType()
|
2013-12-11 22:19:05 +08:00
|
|
|
|
2019-08-22 17:46:17 +08:00
|
|
|
@skipUnlessDBFeature('supports_foreign_keys')
|
|
|
|
def test_alter_field_reloads_state_on_fk_with_to_field_target_type_change(self):
|
2019-09-11 18:20:16 +08:00
|
|
|
app_label = 'test_alflrsfkwtflttc'
|
2019-08-22 17:46:17 +08:00
|
|
|
project_state = self.apply_operations(app_label, ProjectState(), operations=[
|
|
|
|
migrations.CreateModel('Rider', fields=[
|
|
|
|
('id', models.AutoField(primary_key=True)),
|
2020-04-17 12:54:37 +08:00
|
|
|
('code', models.IntegerField(unique=True)),
|
2019-08-22 17:46:17 +08:00
|
|
|
]),
|
|
|
|
migrations.CreateModel('Pony', fields=[
|
|
|
|
('id', models.AutoField(primary_key=True)),
|
|
|
|
('rider', models.ForeignKey('%s.Rider' % app_label, models.CASCADE, to_field='code')),
|
|
|
|
]),
|
|
|
|
])
|
|
|
|
operation = migrations.AlterField(
|
|
|
|
'Rider',
|
|
|
|
'code',
|
|
|
|
models.CharField(max_length=100, unique=True),
|
|
|
|
)
|
|
|
|
self.apply_operations(app_label, project_state, operations=[operation])
|
2020-04-17 12:54:37 +08:00
|
|
|
id_type, id_null = [
|
|
|
|
(c.type_code, c.null_ok)
|
|
|
|
for c in self.get_table_description('%s_rider' % app_label)
|
|
|
|
if c.name == 'code'
|
|
|
|
][0]
|
|
|
|
fk_type, fk_null = [
|
|
|
|
(c.type_code, c.null_ok)
|
|
|
|
for c in self.get_table_description('%s_pony' % app_label)
|
|
|
|
if c.name == 'rider_id'
|
|
|
|
][0]
|
|
|
|
self.assertEqual(id_type, fk_type)
|
|
|
|
self.assertEqual(id_null, fk_null)
|
2019-08-22 17:46:17 +08:00
|
|
|
|
|
|
|
@skipUnlessDBFeature('supports_foreign_keys')
|
|
|
|
def test_alter_field_reloads_state_on_fk_with_to_field_related_name_target_type_change(self):
|
2019-09-11 18:20:16 +08:00
|
|
|
app_label = 'test_alflrsfkwtflrnttc'
|
2019-08-22 17:46:17 +08:00
|
|
|
project_state = self.apply_operations(app_label, ProjectState(), operations=[
|
|
|
|
migrations.CreateModel('Rider', fields=[
|
|
|
|
('id', models.AutoField(primary_key=True)),
|
|
|
|
('code', models.PositiveIntegerField(unique=True)),
|
|
|
|
]),
|
|
|
|
migrations.CreateModel('Pony', fields=[
|
|
|
|
('id', models.AutoField(primary_key=True)),
|
|
|
|
('rider', models.ForeignKey(
|
|
|
|
'%s.Rider' % app_label,
|
|
|
|
models.CASCADE,
|
|
|
|
to_field='code',
|
|
|
|
related_name='+',
|
|
|
|
)),
|
|
|
|
]),
|
|
|
|
])
|
|
|
|
operation = migrations.AlterField(
|
|
|
|
'Rider',
|
|
|
|
'code',
|
|
|
|
models.CharField(max_length=100, unique=True),
|
|
|
|
)
|
|
|
|
self.apply_operations(app_label, project_state, operations=[operation])
|
|
|
|
|
2017-07-01 02:51:08 +08:00
|
|
|
def test_alter_field_reloads_state_on_fk_target_changes(self):
|
|
|
|
"""
|
|
|
|
If AlterField doesn't reload state appropriately, the second AlterField
|
|
|
|
crashes on MySQL due to not dropping the PonyRider.pony foreign key
|
|
|
|
constraint before modifying the column.
|
|
|
|
"""
|
|
|
|
app_label = 'alter_alter_field_reloads_state_on_fk_target_changes'
|
|
|
|
project_state = self.apply_operations(app_label, ProjectState(), operations=[
|
|
|
|
migrations.CreateModel('Rider', fields=[
|
|
|
|
('id', models.CharField(primary_key=True, max_length=100)),
|
|
|
|
]),
|
|
|
|
migrations.CreateModel('Pony', fields=[
|
|
|
|
('id', models.CharField(primary_key=True, max_length=100)),
|
|
|
|
('rider', models.ForeignKey('%s.Rider' % app_label, models.CASCADE)),
|
|
|
|
]),
|
|
|
|
migrations.CreateModel('PonyRider', fields=[
|
|
|
|
('id', models.AutoField(primary_key=True)),
|
|
|
|
('pony', models.ForeignKey('%s.Pony' % app_label, models.CASCADE)),
|
|
|
|
]),
|
|
|
|
])
|
|
|
|
project_state = self.apply_operations(app_label, project_state, operations=[
|
|
|
|
migrations.AlterField('Rider', 'id', models.CharField(primary_key=True, max_length=99)),
|
|
|
|
migrations.AlterField('Pony', 'id', models.CharField(primary_key=True, max_length=99)),
|
|
|
|
])
|
|
|
|
|
|
|
|
def test_alter_field_reloads_state_on_fk_with_to_field_target_changes(self):
|
|
|
|
"""
|
|
|
|
If AlterField doesn't reload state appropriately, the second AlterField
|
|
|
|
crashes on MySQL due to not dropping the PonyRider.pony foreign key
|
|
|
|
constraint before modifying the column.
|
|
|
|
"""
|
|
|
|
app_label = 'alter_alter_field_reloads_state_on_fk_with_to_field_target_changes'
|
|
|
|
project_state = self.apply_operations(app_label, ProjectState(), operations=[
|
|
|
|
migrations.CreateModel('Rider', fields=[
|
|
|
|
('id', models.CharField(primary_key=True, max_length=100)),
|
|
|
|
('slug', models.CharField(unique=True, max_length=100)),
|
|
|
|
]),
|
|
|
|
migrations.CreateModel('Pony', fields=[
|
|
|
|
('id', models.CharField(primary_key=True, max_length=100)),
|
|
|
|
('rider', models.ForeignKey('%s.Rider' % app_label, models.CASCADE, to_field='slug')),
|
|
|
|
('slug', models.CharField(unique=True, max_length=100)),
|
|
|
|
]),
|
|
|
|
migrations.CreateModel('PonyRider', fields=[
|
|
|
|
('id', models.AutoField(primary_key=True)),
|
|
|
|
('pony', models.ForeignKey('%s.Pony' % app_label, models.CASCADE, to_field='slug')),
|
|
|
|
]),
|
|
|
|
])
|
|
|
|
project_state = self.apply_operations(app_label, project_state, operations=[
|
|
|
|
migrations.AlterField('Rider', 'slug', models.CharField(unique=True, max_length=99)),
|
|
|
|
migrations.AlterField('Pony', 'slug', models.CharField(unique=True, max_length=99)),
|
|
|
|
])
|
|
|
|
|
|
|
|
def test_rename_field_reloads_state_on_fk_target_changes(self):
|
|
|
|
"""
|
|
|
|
If RenameField doesn't reload state appropriately, the AlterField
|
|
|
|
crashes on MySQL due to not dropping the PonyRider.pony foreign key
|
|
|
|
constraint before modifying the column.
|
|
|
|
"""
|
|
|
|
app_label = 'alter_rename_field_reloads_state_on_fk_target_changes'
|
|
|
|
project_state = self.apply_operations(app_label, ProjectState(), operations=[
|
|
|
|
migrations.CreateModel('Rider', fields=[
|
|
|
|
('id', models.CharField(primary_key=True, max_length=100)),
|
|
|
|
]),
|
|
|
|
migrations.CreateModel('Pony', fields=[
|
|
|
|
('id', models.CharField(primary_key=True, max_length=100)),
|
|
|
|
('rider', models.ForeignKey('%s.Rider' % app_label, models.CASCADE)),
|
|
|
|
]),
|
|
|
|
migrations.CreateModel('PonyRider', fields=[
|
|
|
|
('id', models.AutoField(primary_key=True)),
|
|
|
|
('pony', models.ForeignKey('%s.Pony' % app_label, models.CASCADE)),
|
|
|
|
]),
|
|
|
|
])
|
|
|
|
project_state = self.apply_operations(app_label, project_state, operations=[
|
|
|
|
migrations.RenameField('Rider', 'id', 'id2'),
|
|
|
|
migrations.AlterField('Pony', 'id', models.CharField(primary_key=True, max_length=99)),
|
2017-11-27 11:39:43 +08:00
|
|
|
], atomic=connection.features.supports_atomic_references_rename)
|
2017-07-01 02:51:08 +08:00
|
|
|
|
2013-06-20 22:12:59 +08:00
|
|
|
def test_rename_field(self):
|
|
|
|
"""
|
|
|
|
Tests the RenameField operation.
|
|
|
|
"""
|
2014-11-18 02:04:38 +08:00
|
|
|
project_state = self.set_up_test_model("test_rnfl", unique_together=True, index_together=True)
|
2013-06-20 22:12:59 +08:00
|
|
|
# Test the state alteration
|
|
|
|
operation = migrations.RenameField("Pony", "pink", "blue")
|
2014-06-27 22:34:48 +08:00
|
|
|
self.assertEqual(operation.describe(), "Rename field pink on Pony to blue")
|
2013-06-20 22:12:59 +08:00
|
|
|
new_state = project_state.clone()
|
|
|
|
operation.state_forwards("test_rnfl", new_state)
|
2020-04-22 11:14:34 +08:00
|
|
|
self.assertIn("blue", new_state.models["test_rnfl", "pony"].fields)
|
|
|
|
self.assertNotIn("pink", new_state.models["test_rnfl", "pony"].fields)
|
2014-07-21 18:36:34 +08:00
|
|
|
# Make sure the unique_together has the renamed column too
|
|
|
|
self.assertIn("blue", new_state.models["test_rnfl", "pony"].options['unique_together'][0])
|
|
|
|
self.assertNotIn("pink", new_state.models["test_rnfl", "pony"].options['unique_together'][0])
|
2014-11-18 02:04:38 +08:00
|
|
|
# Make sure the index_together has the renamed column too
|
|
|
|
self.assertIn("blue", new_state.models["test_rnfl", "pony"].options['index_together'][0])
|
|
|
|
self.assertNotIn("pink", new_state.models["test_rnfl", "pony"].options['index_together'][0])
|
2013-06-20 22:12:59 +08:00
|
|
|
# Test the database alteration
|
|
|
|
self.assertColumnExists("test_rnfl_pony", "pink")
|
|
|
|
self.assertColumnNotExists("test_rnfl_pony", "blue")
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
operation.database_forwards("test_rnfl", editor, project_state, new_state)
|
|
|
|
self.assertColumnExists("test_rnfl_pony", "blue")
|
|
|
|
self.assertColumnNotExists("test_rnfl_pony", "pink")
|
2014-07-21 18:36:34 +08:00
|
|
|
# Ensure the unique constraint has been ported over
|
|
|
|
with connection.cursor() as cursor:
|
|
|
|
cursor.execute("INSERT INTO test_rnfl_pony (blue, weight) VALUES (1, 1)")
|
|
|
|
with self.assertRaises(IntegrityError):
|
|
|
|
with atomic():
|
|
|
|
cursor.execute("INSERT INTO test_rnfl_pony (blue, weight) VALUES (1, 1)")
|
|
|
|
cursor.execute("DELETE FROM test_rnfl_pony")
|
2014-11-21 00:08:04 +08:00
|
|
|
# Ensure the index constraint has been ported over
|
2014-12-01 01:11:52 +08:00
|
|
|
self.assertIndexExists("test_rnfl_pony", ["weight", "blue"])
|
2013-06-20 22:12:59 +08:00
|
|
|
# And test reversal
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
operation.database_backwards("test_rnfl", editor, new_state, project_state)
|
|
|
|
self.assertColumnExists("test_rnfl_pony", "pink")
|
|
|
|
self.assertColumnNotExists("test_rnfl_pony", "blue")
|
2014-11-21 00:08:04 +08:00
|
|
|
# Ensure the index constraint has been reset
|
2014-12-01 01:11:52 +08:00
|
|
|
self.assertIndexExists("test_rnfl_pony", ["weight", "pink"])
|
2014-11-23 01:57:13 +08:00
|
|
|
# And deconstruction
|
|
|
|
definition = operation.deconstruct()
|
|
|
|
self.assertEqual(definition[0], "RenameField")
|
|
|
|
self.assertEqual(definition[1], [])
|
|
|
|
self.assertEqual(definition[2], {'model_name': "Pony", 'old_name': "pink", 'new_name': "blue"})
|
2013-07-02 18:19:02 +08:00
|
|
|
|
2017-06-30 20:35:58 +08:00
|
|
|
def test_rename_missing_field(self):
|
|
|
|
state = ProjectState()
|
|
|
|
state.add_model(ModelState('app', 'model', []))
|
|
|
|
with self.assertRaisesMessage(FieldDoesNotExist, "app.model has no field named 'field'"):
|
|
|
|
migrations.RenameField('model', 'field', 'new_field').state_forwards('app', state)
|
|
|
|
|
2017-11-24 16:05:56 +08:00
|
|
|
def test_rename_referenced_field_state_forward(self):
|
|
|
|
state = ProjectState()
|
|
|
|
state.add_model(ModelState('app', 'Model', [
|
|
|
|
('id', models.AutoField(primary_key=True)),
|
|
|
|
('field', models.IntegerField(unique=True)),
|
|
|
|
]))
|
|
|
|
state.add_model(ModelState('app', 'OtherModel', [
|
|
|
|
('id', models.AutoField(primary_key=True)),
|
|
|
|
('fk', models.ForeignKey('Model', models.CASCADE, to_field='field')),
|
|
|
|
('fo', models.ForeignObject('Model', models.CASCADE, from_fields=('fk',), to_fields=('field',))),
|
|
|
|
]))
|
|
|
|
operation = migrations.RenameField('Model', 'field', 'renamed')
|
|
|
|
new_state = state.clone()
|
|
|
|
operation.state_forwards('app', new_state)
|
2020-04-22 11:14:34 +08:00
|
|
|
self.assertEqual(new_state.models['app', 'othermodel'].fields['fk'].remote_field.field_name, 'renamed')
|
|
|
|
self.assertEqual(new_state.models['app', 'othermodel'].fields['fk'].from_fields, ['self'])
|
|
|
|
self.assertEqual(new_state.models['app', 'othermodel'].fields['fk'].to_fields, ('renamed',))
|
|
|
|
self.assertEqual(new_state.models['app', 'othermodel'].fields['fo'].from_fields, ('fk',))
|
|
|
|
self.assertEqual(new_state.models['app', 'othermodel'].fields['fo'].to_fields, ('renamed',))
|
2017-11-24 16:05:56 +08:00
|
|
|
operation = migrations.RenameField('OtherModel', 'fk', 'renamed_fk')
|
|
|
|
new_state = state.clone()
|
|
|
|
operation.state_forwards('app', new_state)
|
2020-04-22 11:14:34 +08:00
|
|
|
self.assertEqual(new_state.models['app', 'othermodel'].fields['renamed_fk'].remote_field.field_name, 'renamed')
|
|
|
|
self.assertEqual(new_state.models['app', 'othermodel'].fields['renamed_fk'].from_fields, ('self',))
|
|
|
|
self.assertEqual(new_state.models['app', 'othermodel'].fields['renamed_fk'].to_fields, ('renamed',))
|
|
|
|
self.assertEqual(new_state.models['app', 'othermodel'].fields['fo'].from_fields, ('renamed_fk',))
|
|
|
|
self.assertEqual(new_state.models['app', 'othermodel'].fields['fo'].to_fields, ('renamed',))
|
2017-11-24 16:05:56 +08:00
|
|
|
|
2013-07-02 18:19:02 +08:00
|
|
|
def test_alter_unique_together(self):
|
|
|
|
"""
|
|
|
|
Tests the AlterUniqueTogether operation.
|
|
|
|
"""
|
|
|
|
project_state = self.set_up_test_model("test_alunto")
|
|
|
|
# Test the state alteration
|
|
|
|
operation = migrations.AlterUniqueTogether("Pony", [("pink", "weight")])
|
2014-06-27 22:34:48 +08:00
|
|
|
self.assertEqual(operation.describe(), "Alter unique_together for Pony (1 constraint(s))")
|
2013-07-02 18:19:02 +08:00
|
|
|
new_state = project_state.clone()
|
|
|
|
operation.state_forwards("test_alunto", new_state)
|
|
|
|
self.assertEqual(len(project_state.models["test_alunto", "pony"].options.get("unique_together", set())), 0)
|
|
|
|
self.assertEqual(len(new_state.models["test_alunto", "pony"].options.get("unique_together", set())), 1)
|
|
|
|
# Make sure we can insert duplicate rows
|
2014-01-09 23:05:15 +08:00
|
|
|
with connection.cursor() as cursor:
|
2014-03-09 08:04:21 +08:00
|
|
|
cursor.execute("INSERT INTO test_alunto_pony (pink, weight) VALUES (1, 1)")
|
|
|
|
cursor.execute("INSERT INTO test_alunto_pony (pink, weight) VALUES (1, 1)")
|
2014-01-09 23:05:15 +08:00
|
|
|
cursor.execute("DELETE FROM test_alunto_pony")
|
|
|
|
# Test the database alteration
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
operation.database_forwards("test_alunto", editor, project_state, new_state)
|
2014-03-09 08:04:21 +08:00
|
|
|
cursor.execute("INSERT INTO test_alunto_pony (pink, weight) VALUES (1, 1)")
|
2014-01-09 23:05:15 +08:00
|
|
|
with self.assertRaises(IntegrityError):
|
|
|
|
with atomic():
|
2014-03-09 08:04:21 +08:00
|
|
|
cursor.execute("INSERT INTO test_alunto_pony (pink, weight) VALUES (1, 1)")
|
2014-01-09 23:05:15 +08:00
|
|
|
cursor.execute("DELETE FROM test_alunto_pony")
|
|
|
|
# And test reversal
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
operation.database_backwards("test_alunto", editor, new_state, project_state)
|
2014-03-09 08:04:21 +08:00
|
|
|
cursor.execute("INSERT INTO test_alunto_pony (pink, weight) VALUES (1, 1)")
|
|
|
|
cursor.execute("INSERT INTO test_alunto_pony (pink, weight) VALUES (1, 1)")
|
2014-01-09 23:05:15 +08:00
|
|
|
cursor.execute("DELETE FROM test_alunto_pony")
|
2013-10-07 15:37:35 +08:00
|
|
|
# Test flat unique_together
|
|
|
|
operation = migrations.AlterUniqueTogether("Pony", ("pink", "weight"))
|
|
|
|
operation.state_forwards("test_alunto", new_state)
|
|
|
|
self.assertEqual(len(new_state.models["test_alunto", "pony"].options.get("unique_together", set())), 1)
|
2014-11-23 01:57:13 +08:00
|
|
|
# And deconstruction
|
|
|
|
definition = operation.deconstruct()
|
|
|
|
self.assertEqual(definition[0], "AlterUniqueTogether")
|
|
|
|
self.assertEqual(definition[1], [])
|
|
|
|
self.assertEqual(definition[2], {'name': "Pony", 'unique_together': {("pink", "weight")}})
|
2013-07-03 01:02:01 +08:00
|
|
|
|
2014-07-14 20:16:35 +08:00
|
|
|
def test_alter_unique_together_remove(self):
|
|
|
|
operation = migrations.AlterUniqueTogether("Pony", None)
|
|
|
|
self.assertEqual(operation.describe(), "Alter unique_together for Pony (0 constraint(s))")
|
|
|
|
|
2016-06-26 00:32:56 +08:00
|
|
|
def test_add_index(self):
|
|
|
|
"""
|
|
|
|
Test the AddIndex operation.
|
|
|
|
"""
|
|
|
|
project_state = self.set_up_test_model("test_adin")
|
2016-07-07 14:30:55 +08:00
|
|
|
msg = (
|
|
|
|
"Indexes passed to AddIndex operations require a name argument. "
|
|
|
|
"<Index: fields='pink'> doesn't have one."
|
|
|
|
)
|
|
|
|
with self.assertRaisesMessage(ValueError, msg):
|
|
|
|
migrations.AddIndex("Pony", models.Index(fields=["pink"]))
|
|
|
|
index = models.Index(fields=["pink"], name="test_adin_pony_pink_idx")
|
2016-06-26 00:32:56 +08:00
|
|
|
operation = migrations.AddIndex("Pony", index)
|
2016-08-16 14:39:03 +08:00
|
|
|
self.assertEqual(operation.describe(), "Create index test_adin_pony_pink_idx on field(s) pink of model Pony")
|
2016-06-26 00:32:56 +08:00
|
|
|
new_state = project_state.clone()
|
|
|
|
operation.state_forwards("test_adin", new_state)
|
|
|
|
# Test the database alteration
|
|
|
|
self.assertEqual(len(new_state.models["test_adin", "pony"].options['indexes']), 1)
|
|
|
|
self.assertIndexNotExists("test_adin_pony", ["pink"])
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
operation.database_forwards("test_adin", editor, project_state, new_state)
|
|
|
|
self.assertIndexExists("test_adin_pony", ["pink"])
|
|
|
|
# And test reversal
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
operation.database_backwards("test_adin", editor, new_state, project_state)
|
|
|
|
self.assertIndexNotExists("test_adin_pony", ["pink"])
|
|
|
|
# And deconstruction
|
|
|
|
definition = operation.deconstruct()
|
|
|
|
self.assertEqual(definition[0], "AddIndex")
|
|
|
|
self.assertEqual(definition[1], [])
|
|
|
|
self.assertEqual(definition[2], {'model_name': "Pony", 'index': index})
|
|
|
|
|
|
|
|
def test_remove_index(self):
|
|
|
|
"""
|
|
|
|
Test the RemoveIndex operation.
|
|
|
|
"""
|
|
|
|
project_state = self.set_up_test_model("test_rmin", multicol_index=True)
|
|
|
|
self.assertTableExists("test_rmin_pony")
|
|
|
|
self.assertIndexExists("test_rmin_pony", ["pink", "weight"])
|
|
|
|
operation = migrations.RemoveIndex("Pony", "pony_test_idx")
|
|
|
|
self.assertEqual(operation.describe(), "Remove index pony_test_idx from Pony")
|
|
|
|
new_state = project_state.clone()
|
|
|
|
operation.state_forwards("test_rmin", new_state)
|
|
|
|
# Test the state alteration
|
|
|
|
self.assertEqual(len(new_state.models["test_rmin", "pony"].options['indexes']), 0)
|
|
|
|
self.assertIndexExists("test_rmin_pony", ["pink", "weight"])
|
|
|
|
# Test the database alteration
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
operation.database_forwards("test_rmin", editor, project_state, new_state)
|
|
|
|
self.assertIndexNotExists("test_rmin_pony", ["pink", "weight"])
|
|
|
|
# And test reversal
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
operation.database_backwards("test_rmin", editor, new_state, project_state)
|
|
|
|
self.assertIndexExists("test_rmin_pony", ["pink", "weight"])
|
|
|
|
# And deconstruction
|
|
|
|
definition = operation.deconstruct()
|
|
|
|
self.assertEqual(definition[0], "RemoveIndex")
|
|
|
|
self.assertEqual(definition[1], [])
|
|
|
|
self.assertEqual(definition[2], {'model_name': "Pony", 'name': "pony_test_idx"})
|
|
|
|
|
2016-06-20 23:50:05 +08:00
|
|
|
# Also test a field dropped with index - sqlite remake issue
|
|
|
|
operations = [
|
|
|
|
migrations.RemoveIndex("Pony", "pony_test_idx"),
|
|
|
|
migrations.RemoveField("Pony", "pink"),
|
|
|
|
]
|
|
|
|
self.assertColumnExists("test_rmin_pony", "pink")
|
|
|
|
self.assertIndexExists("test_rmin_pony", ["pink", "weight"])
|
|
|
|
# Test database alteration
|
|
|
|
new_state = project_state.clone()
|
|
|
|
self.apply_operations('test_rmin', new_state, operations=operations)
|
|
|
|
self.assertColumnNotExists("test_rmin_pony", "pink")
|
|
|
|
self.assertIndexNotExists("test_rmin_pony", ["pink", "weight"])
|
|
|
|
# And test reversal
|
|
|
|
self.unapply_operations("test_rmin", project_state, operations=operations)
|
|
|
|
self.assertIndexExists("test_rmin_pony", ["pink", "weight"])
|
|
|
|
|
2017-04-07 16:44:28 +08:00
|
|
|
def test_add_index_state_forwards(self):
|
|
|
|
project_state = self.set_up_test_model('test_adinsf')
|
|
|
|
index = models.Index(fields=['pink'], name='test_adinsf_pony_pink_idx')
|
|
|
|
old_model = project_state.apps.get_model('test_adinsf', 'Pony')
|
|
|
|
new_state = project_state.clone()
|
|
|
|
|
|
|
|
operation = migrations.AddIndex('Pony', index)
|
|
|
|
operation.state_forwards('test_adinsf', new_state)
|
|
|
|
new_model = new_state.apps.get_model('test_adinsf', 'Pony')
|
|
|
|
self.assertIsNot(old_model, new_model)
|
|
|
|
|
|
|
|
def test_remove_index_state_forwards(self):
|
|
|
|
project_state = self.set_up_test_model('test_rminsf')
|
|
|
|
index = models.Index(fields=['pink'], name='test_rminsf_pony_pink_idx')
|
|
|
|
migrations.AddIndex('Pony', index).state_forwards('test_rminsf', project_state)
|
|
|
|
old_model = project_state.apps.get_model('test_rminsf', 'Pony')
|
|
|
|
new_state = project_state.clone()
|
|
|
|
|
|
|
|
operation = migrations.RemoveIndex('Pony', 'test_rminsf_pony_pink_idx')
|
|
|
|
operation.state_forwards('test_rminsf', new_state)
|
|
|
|
new_model = new_state.apps.get_model('test_rminsf', 'Pony')
|
|
|
|
self.assertIsNot(old_model, new_model)
|
|
|
|
|
2016-06-20 23:50:05 +08:00
|
|
|
def test_alter_field_with_index(self):
|
|
|
|
"""
|
|
|
|
Test AlterField operation with an index to ensure indexes created via
|
|
|
|
Meta.indexes don't get dropped with sqlite3 remake.
|
|
|
|
"""
|
|
|
|
project_state = self.set_up_test_model("test_alflin", index=True)
|
|
|
|
operation = migrations.AlterField("Pony", "pink", models.IntegerField(null=True))
|
|
|
|
new_state = project_state.clone()
|
|
|
|
operation.state_forwards("test_alflin", new_state)
|
|
|
|
# Test the database alteration
|
|
|
|
self.assertColumnNotNull("test_alflin_pony", "pink")
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
operation.database_forwards("test_alflin", editor, project_state, new_state)
|
2016-10-27 15:53:39 +08:00
|
|
|
# Index hasn't been dropped
|
2016-06-20 23:50:05 +08:00
|
|
|
self.assertIndexExists("test_alflin_pony", ["pink"])
|
|
|
|
# And test reversal
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
operation.database_backwards("test_alflin", editor, new_state, project_state)
|
|
|
|
# Ensure the index is still there
|
|
|
|
self.assertIndexExists("test_alflin_pony", ["pink"])
|
|
|
|
|
2013-07-03 01:02:01 +08:00
|
|
|
def test_alter_index_together(self):
|
|
|
|
"""
|
|
|
|
Tests the AlterIndexTogether operation.
|
|
|
|
"""
|
|
|
|
project_state = self.set_up_test_model("test_alinto")
|
|
|
|
# Test the state alteration
|
|
|
|
operation = migrations.AlterIndexTogether("Pony", [("pink", "weight")])
|
2014-06-27 22:34:48 +08:00
|
|
|
self.assertEqual(operation.describe(), "Alter index_together for Pony (1 constraint(s))")
|
2013-07-03 01:02:01 +08:00
|
|
|
new_state = project_state.clone()
|
|
|
|
operation.state_forwards("test_alinto", new_state)
|
|
|
|
self.assertEqual(len(project_state.models["test_alinto", "pony"].options.get("index_together", set())), 0)
|
|
|
|
self.assertEqual(len(new_state.models["test_alinto", "pony"].options.get("index_together", set())), 1)
|
|
|
|
# Make sure there's no matching index
|
|
|
|
self.assertIndexNotExists("test_alinto_pony", ["pink", "weight"])
|
|
|
|
# Test the database alteration
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
operation.database_forwards("test_alinto", editor, project_state, new_state)
|
|
|
|
self.assertIndexExists("test_alinto_pony", ["pink", "weight"])
|
|
|
|
# And test reversal
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
operation.database_backwards("test_alinto", editor, new_state, project_state)
|
|
|
|
self.assertIndexNotExists("test_alinto_pony", ["pink", "weight"])
|
2014-11-23 01:57:13 +08:00
|
|
|
# And deconstruction
|
|
|
|
definition = operation.deconstruct()
|
|
|
|
self.assertEqual(definition[0], "AlterIndexTogether")
|
|
|
|
self.assertEqual(definition[1], [])
|
|
|
|
self.assertEqual(definition[2], {'name': "Pony", 'index_together': {("pink", "weight")}})
|
2013-07-30 19:34:31 +08:00
|
|
|
|
2014-07-14 20:16:35 +08:00
|
|
|
def test_alter_index_together_remove(self):
|
|
|
|
operation = migrations.AlterIndexTogether("Pony", None)
|
|
|
|
self.assertEqual(operation.describe(), "Alter index_together for Pony (0 constraint(s))")
|
|
|
|
|
2016-11-05 21:12:12 +08:00
|
|
|
@skipUnlessDBFeature('supports_table_check_constraints')
|
|
|
|
def test_add_constraint(self):
|
2018-10-21 01:19:44 +08:00
|
|
|
project_state = self.set_up_test_model("test_addconstraint")
|
|
|
|
gt_check = models.Q(pink__gt=2)
|
2018-10-30 21:26:03 +08:00
|
|
|
gt_constraint = models.CheckConstraint(check=gt_check, name="test_add_constraint_pony_pink_gt_2")
|
2018-10-21 01:19:44 +08:00
|
|
|
gt_operation = migrations.AddConstraint("Pony", gt_constraint)
|
2018-10-30 21:26:03 +08:00
|
|
|
self.assertEqual(
|
|
|
|
gt_operation.describe(), "Create constraint test_add_constraint_pony_pink_gt_2 on model Pony"
|
|
|
|
)
|
2018-10-21 01:19:44 +08:00
|
|
|
# Test the state alteration
|
2016-11-05 21:12:12 +08:00
|
|
|
new_state = project_state.clone()
|
2018-10-21 01:19:44 +08:00
|
|
|
gt_operation.state_forwards("test_addconstraint", new_state)
|
|
|
|
self.assertEqual(len(new_state.models["test_addconstraint", "pony"].options["constraints"]), 1)
|
|
|
|
Pony = new_state.apps.get_model("test_addconstraint", "Pony")
|
2018-10-29 17:33:41 +08:00
|
|
|
self.assertEqual(len(Pony._meta.constraints), 1)
|
2018-10-21 01:19:44 +08:00
|
|
|
# Test the database alteration
|
2016-11-05 21:12:12 +08:00
|
|
|
with connection.schema_editor() as editor:
|
2018-10-21 01:19:44 +08:00
|
|
|
gt_operation.database_forwards("test_addconstraint", editor, project_state, new_state)
|
|
|
|
with self.assertRaises(IntegrityError), transaction.atomic():
|
|
|
|
Pony.objects.create(pink=1, weight=1.0)
|
2018-10-29 17:33:41 +08:00
|
|
|
# Add another one.
|
|
|
|
lt_check = models.Q(pink__lt=100)
|
2018-10-30 21:26:03 +08:00
|
|
|
lt_constraint = models.CheckConstraint(check=lt_check, name="test_add_constraint_pony_pink_lt_100")
|
2018-10-29 17:33:41 +08:00
|
|
|
lt_operation = migrations.AddConstraint("Pony", lt_constraint)
|
|
|
|
lt_operation.state_forwards("test_addconstraint", new_state)
|
|
|
|
self.assertEqual(len(new_state.models["test_addconstraint", "pony"].options["constraints"]), 2)
|
|
|
|
Pony = new_state.apps.get_model("test_addconstraint", "Pony")
|
|
|
|
self.assertEqual(len(Pony._meta.constraints), 2)
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
lt_operation.database_forwards("test_addconstraint", editor, project_state, new_state)
|
|
|
|
with self.assertRaises(IntegrityError), transaction.atomic():
|
|
|
|
Pony.objects.create(pink=100, weight=1.0)
|
2016-11-05 21:12:12 +08:00
|
|
|
# Test reversal
|
|
|
|
with connection.schema_editor() as editor:
|
2018-10-21 01:19:44 +08:00
|
|
|
gt_operation.database_backwards("test_addconstraint", editor, new_state, project_state)
|
|
|
|
Pony.objects.create(pink=1, weight=1.0)
|
2016-11-05 21:12:12 +08:00
|
|
|
# Test deconstruction
|
2018-10-21 01:19:44 +08:00
|
|
|
definition = gt_operation.deconstruct()
|
2016-11-05 21:12:12 +08:00
|
|
|
self.assertEqual(definition[0], "AddConstraint")
|
|
|
|
self.assertEqual(definition[1], [])
|
2018-10-21 01:19:44 +08:00
|
|
|
self.assertEqual(definition[2], {'model_name': "Pony", 'constraint': gt_constraint})
|
2016-11-05 21:12:12 +08:00
|
|
|
|
2019-04-30 10:49:45 +08:00
|
|
|
@skipUnlessDBFeature('supports_table_check_constraints')
|
|
|
|
def test_add_constraint_percent_escaping(self):
|
|
|
|
app_label = 'add_constraint_string_quoting'
|
|
|
|
operations = [
|
2020-04-20 18:44:28 +08:00
|
|
|
migrations.CreateModel(
|
2019-04-30 10:49:45 +08:00
|
|
|
'Author',
|
|
|
|
fields=[
|
|
|
|
('id', models.AutoField(primary_key=True)),
|
|
|
|
('name', models.CharField(max_length=100)),
|
|
|
|
('rebate', models.CharField(max_length=100)),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
]
|
|
|
|
from_state = self.apply_operations(app_label, ProjectState(), operations)
|
|
|
|
# "%" generated in startswith lookup should be escaped in a way that is
|
|
|
|
# considered a leading wildcard.
|
|
|
|
check = models.Q(name__startswith='Albert')
|
|
|
|
constraint = models.CheckConstraint(check=check, name='name_constraint')
|
|
|
|
operation = migrations.AddConstraint('Author', constraint)
|
|
|
|
to_state = from_state.clone()
|
|
|
|
operation.state_forwards(app_label, to_state)
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
operation.database_forwards(app_label, editor, from_state, to_state)
|
|
|
|
Author = to_state.apps.get_model(app_label, 'Author')
|
|
|
|
with self.assertRaises(IntegrityError), transaction.atomic():
|
|
|
|
Author.objects.create(name='Artur')
|
|
|
|
# Literal "%" should be escaped in a way that is not a considered a
|
|
|
|
# wildcard.
|
|
|
|
check = models.Q(rebate__endswith='%')
|
|
|
|
constraint = models.CheckConstraint(check=check, name='rebate_constraint')
|
|
|
|
operation = migrations.AddConstraint('Author', constraint)
|
|
|
|
from_state = to_state
|
|
|
|
to_state = from_state.clone()
|
|
|
|
operation.state_forwards(app_label, to_state)
|
|
|
|
Author = to_state.apps.get_model(app_label, 'Author')
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
operation.database_forwards(app_label, editor, from_state, to_state)
|
|
|
|
Author = to_state.apps.get_model(app_label, 'Author')
|
|
|
|
with self.assertRaises(IntegrityError), transaction.atomic():
|
|
|
|
Author.objects.create(name='Albert', rebate='10$')
|
|
|
|
author = Author.objects.create(name='Albert', rebate='10%')
|
|
|
|
self.assertEqual(Author.objects.get(), author)
|
|
|
|
|
2019-04-30 16:20:41 +08:00
|
|
|
@skipUnlessDBFeature('supports_table_check_constraints')
|
|
|
|
def test_add_or_constraint(self):
|
|
|
|
app_label = 'test_addorconstraint'
|
|
|
|
constraint_name = 'add_constraint_or'
|
|
|
|
from_state = self.set_up_test_model(app_label)
|
|
|
|
check = models.Q(pink__gt=2, weight__gt=2) | models.Q(weight__lt=0)
|
|
|
|
constraint = models.CheckConstraint(check=check, name=constraint_name)
|
|
|
|
operation = migrations.AddConstraint('Pony', constraint)
|
|
|
|
to_state = from_state.clone()
|
|
|
|
operation.state_forwards(app_label, to_state)
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
operation.database_forwards(app_label, editor, from_state, to_state)
|
|
|
|
Pony = to_state.apps.get_model(app_label, 'Pony')
|
|
|
|
with self.assertRaises(IntegrityError), transaction.atomic():
|
|
|
|
Pony.objects.create(pink=2, weight=3.0)
|
|
|
|
with self.assertRaises(IntegrityError), transaction.atomic():
|
|
|
|
Pony.objects.create(pink=3, weight=1.0)
|
|
|
|
Pony.objects.bulk_create([
|
|
|
|
Pony(pink=3, weight=-1.0),
|
|
|
|
Pony(pink=1, weight=-1.0),
|
|
|
|
Pony(pink=3, weight=3.0),
|
|
|
|
])
|
|
|
|
|
2020-01-23 17:28:36 +08:00
|
|
|
@skipUnlessDBFeature('supports_table_check_constraints')
|
|
|
|
def test_add_constraint_combinable(self):
|
|
|
|
app_label = 'test_addconstraint_combinable'
|
|
|
|
operations = [
|
2020-04-20 18:44:28 +08:00
|
|
|
migrations.CreateModel(
|
2020-01-23 17:28:36 +08:00
|
|
|
'Book',
|
|
|
|
fields=[
|
|
|
|
('id', models.AutoField(primary_key=True)),
|
|
|
|
('read', models.PositiveIntegerField()),
|
|
|
|
('unread', models.PositiveIntegerField()),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
]
|
|
|
|
from_state = self.apply_operations(app_label, ProjectState(), operations)
|
|
|
|
constraint = models.CheckConstraint(
|
|
|
|
check=models.Q(read=(100 - models.F('unread'))),
|
|
|
|
name='test_addconstraint_combinable_sum_100',
|
|
|
|
)
|
|
|
|
operation = migrations.AddConstraint('Book', constraint)
|
|
|
|
to_state = from_state.clone()
|
|
|
|
operation.state_forwards(app_label, to_state)
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
operation.database_forwards(app_label, editor, from_state, to_state)
|
|
|
|
Book = to_state.apps.get_model(app_label, 'Book')
|
|
|
|
with self.assertRaises(IntegrityError), transaction.atomic():
|
|
|
|
Book.objects.create(read=70, unread=10)
|
|
|
|
Book.objects.create(read=70, unread=30)
|
|
|
|
|
2016-11-05 21:12:12 +08:00
|
|
|
@skipUnlessDBFeature('supports_table_check_constraints')
|
|
|
|
def test_remove_constraint(self):
|
2018-10-29 17:33:41 +08:00
|
|
|
project_state = self.set_up_test_model("test_removeconstraint", constraints=[
|
2018-10-30 21:26:03 +08:00
|
|
|
models.CheckConstraint(check=models.Q(pink__gt=2), name="test_remove_constraint_pony_pink_gt_2"),
|
|
|
|
models.CheckConstraint(check=models.Q(pink__lt=100), name="test_remove_constraint_pony_pink_lt_100"),
|
2018-10-29 17:33:41 +08:00
|
|
|
])
|
2018-10-30 21:26:03 +08:00
|
|
|
gt_operation = migrations.RemoveConstraint("Pony", "test_remove_constraint_pony_pink_gt_2")
|
|
|
|
self.assertEqual(
|
|
|
|
gt_operation.describe(), "Remove constraint test_remove_constraint_pony_pink_gt_2 from model Pony"
|
|
|
|
)
|
2018-10-21 01:19:44 +08:00
|
|
|
# Test state alteration
|
2016-11-05 21:12:12 +08:00
|
|
|
new_state = project_state.clone()
|
2018-10-29 17:33:41 +08:00
|
|
|
gt_operation.state_forwards("test_removeconstraint", new_state)
|
|
|
|
self.assertEqual(len(new_state.models["test_removeconstraint", "pony"].options['constraints']), 1)
|
2018-10-21 01:19:44 +08:00
|
|
|
Pony = new_state.apps.get_model("test_removeconstraint", "Pony")
|
2018-10-29 17:33:41 +08:00
|
|
|
self.assertEqual(len(Pony._meta.constraints), 1)
|
2016-11-05 21:12:12 +08:00
|
|
|
# Test database alteration
|
|
|
|
with connection.schema_editor() as editor:
|
2018-10-29 17:33:41 +08:00
|
|
|
gt_operation.database_forwards("test_removeconstraint", editor, project_state, new_state)
|
2018-10-21 01:19:44 +08:00
|
|
|
Pony.objects.create(pink=1, weight=1.0).delete()
|
2018-10-29 17:33:41 +08:00
|
|
|
with self.assertRaises(IntegrityError), transaction.atomic():
|
|
|
|
Pony.objects.create(pink=100, weight=1.0)
|
|
|
|
# Remove the other one.
|
2018-10-30 21:26:03 +08:00
|
|
|
lt_operation = migrations.RemoveConstraint("Pony", "test_remove_constraint_pony_pink_lt_100")
|
2018-10-29 17:33:41 +08:00
|
|
|
lt_operation.state_forwards("test_removeconstraint", new_state)
|
|
|
|
self.assertEqual(len(new_state.models["test_removeconstraint", "pony"].options['constraints']), 0)
|
|
|
|
Pony = new_state.apps.get_model("test_removeconstraint", "Pony")
|
|
|
|
self.assertEqual(len(Pony._meta.constraints), 0)
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
lt_operation.database_forwards("test_removeconstraint", editor, project_state, new_state)
|
|
|
|
Pony.objects.create(pink=100, weight=1.0).delete()
|
2016-11-05 21:12:12 +08:00
|
|
|
# Test reversal
|
|
|
|
with connection.schema_editor() as editor:
|
2018-10-29 17:33:41 +08:00
|
|
|
gt_operation.database_backwards("test_removeconstraint", editor, new_state, project_state)
|
2018-10-21 01:19:44 +08:00
|
|
|
with self.assertRaises(IntegrityError), transaction.atomic():
|
|
|
|
Pony.objects.create(pink=1, weight=1.0)
|
2016-11-05 21:12:12 +08:00
|
|
|
# Test deconstruction
|
2018-10-29 17:33:41 +08:00
|
|
|
definition = gt_operation.deconstruct()
|
2016-11-05 21:12:12 +08:00
|
|
|
self.assertEqual(definition[0], "RemoveConstraint")
|
|
|
|
self.assertEqual(definition[1], [])
|
2018-10-30 21:26:03 +08:00
|
|
|
self.assertEqual(definition[2], {'model_name': "Pony", 'name': "test_remove_constraint_pony_pink_gt_2"})
|
2016-11-05 21:12:12 +08:00
|
|
|
|
2018-12-28 03:21:59 +08:00
|
|
|
def test_add_partial_unique_constraint(self):
|
|
|
|
project_state = self.set_up_test_model('test_addpartialuniqueconstraint')
|
|
|
|
partial_unique_constraint = models.UniqueConstraint(
|
|
|
|
fields=['pink'],
|
|
|
|
condition=models.Q(weight__gt=5),
|
|
|
|
name='test_constraint_pony_pink_for_weight_gt_5_uniq',
|
|
|
|
)
|
|
|
|
operation = migrations.AddConstraint('Pony', partial_unique_constraint)
|
|
|
|
self.assertEqual(
|
|
|
|
operation.describe(),
|
|
|
|
'Create constraint test_constraint_pony_pink_for_weight_gt_5_uniq '
|
|
|
|
'on model Pony'
|
|
|
|
)
|
|
|
|
# Test the state alteration
|
|
|
|
new_state = project_state.clone()
|
|
|
|
operation.state_forwards('test_addpartialuniqueconstraint', new_state)
|
|
|
|
self.assertEqual(len(new_state.models['test_addpartialuniqueconstraint', 'pony'].options['constraints']), 1)
|
|
|
|
Pony = new_state.apps.get_model('test_addpartialuniqueconstraint', 'Pony')
|
|
|
|
self.assertEqual(len(Pony._meta.constraints), 1)
|
|
|
|
# Test the database alteration
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
operation.database_forwards('test_addpartialuniqueconstraint', editor, project_state, new_state)
|
|
|
|
# Test constraint works
|
|
|
|
Pony.objects.create(pink=1, weight=4.0)
|
|
|
|
Pony.objects.create(pink=1, weight=4.0)
|
|
|
|
Pony.objects.create(pink=1, weight=6.0)
|
|
|
|
if connection.features.supports_partial_indexes:
|
|
|
|
with self.assertRaises(IntegrityError), transaction.atomic():
|
|
|
|
Pony.objects.create(pink=1, weight=7.0)
|
|
|
|
else:
|
|
|
|
Pony.objects.create(pink=1, weight=7.0)
|
|
|
|
# Test reversal
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
operation.database_backwards('test_addpartialuniqueconstraint', editor, new_state, project_state)
|
|
|
|
# Test constraint doesn't work
|
|
|
|
Pony.objects.create(pink=1, weight=7.0)
|
|
|
|
# Test deconstruction
|
|
|
|
definition = operation.deconstruct()
|
|
|
|
self.assertEqual(definition[0], 'AddConstraint')
|
|
|
|
self.assertEqual(definition[1], [])
|
|
|
|
self.assertEqual(definition[2], {'model_name': 'Pony', 'constraint': partial_unique_constraint})
|
|
|
|
|
|
|
|
def test_remove_partial_unique_constraint(self):
|
|
|
|
project_state = self.set_up_test_model('test_removepartialuniqueconstraint', constraints=[
|
|
|
|
models.UniqueConstraint(
|
|
|
|
fields=['pink'],
|
|
|
|
condition=models.Q(weight__gt=5),
|
|
|
|
name='test_constraint_pony_pink_for_weight_gt_5_uniq',
|
|
|
|
),
|
|
|
|
])
|
|
|
|
gt_operation = migrations.RemoveConstraint('Pony', 'test_constraint_pony_pink_for_weight_gt_5_uniq')
|
|
|
|
self.assertEqual(
|
|
|
|
gt_operation.describe(), 'Remove constraint test_constraint_pony_pink_for_weight_gt_5_uniq from model Pony'
|
|
|
|
)
|
|
|
|
# Test state alteration
|
|
|
|
new_state = project_state.clone()
|
|
|
|
gt_operation.state_forwards('test_removepartialuniqueconstraint', new_state)
|
|
|
|
self.assertEqual(len(new_state.models['test_removepartialuniqueconstraint', 'pony'].options['constraints']), 0)
|
|
|
|
Pony = new_state.apps.get_model('test_removepartialuniqueconstraint', 'Pony')
|
|
|
|
self.assertEqual(len(Pony._meta.constraints), 0)
|
|
|
|
# Test database alteration
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
gt_operation.database_forwards('test_removepartialuniqueconstraint', editor, project_state, new_state)
|
|
|
|
# Test constraint doesn't work
|
|
|
|
Pony.objects.create(pink=1, weight=4.0)
|
|
|
|
Pony.objects.create(pink=1, weight=4.0)
|
|
|
|
Pony.objects.create(pink=1, weight=6.0)
|
|
|
|
Pony.objects.create(pink=1, weight=7.0).delete()
|
|
|
|
# Test reversal
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
gt_operation.database_backwards('test_removepartialuniqueconstraint', editor, new_state, project_state)
|
|
|
|
# Test constraint works
|
|
|
|
if connection.features.supports_partial_indexes:
|
|
|
|
with self.assertRaises(IntegrityError), transaction.atomic():
|
|
|
|
Pony.objects.create(pink=1, weight=7.0)
|
|
|
|
else:
|
|
|
|
Pony.objects.create(pink=1, weight=7.0)
|
|
|
|
# Test deconstruction
|
|
|
|
definition = gt_operation.deconstruct()
|
|
|
|
self.assertEqual(definition[0], 'RemoveConstraint')
|
|
|
|
self.assertEqual(definition[1], [])
|
|
|
|
self.assertEqual(definition[2], {
|
|
|
|
'model_name': 'Pony',
|
|
|
|
'name': 'test_constraint_pony_pink_for_weight_gt_5_uniq',
|
|
|
|
})
|
|
|
|
|
2014-06-16 03:34:02 +08:00
|
|
|
def test_alter_model_options(self):
|
|
|
|
"""
|
|
|
|
Tests the AlterModelOptions operation.
|
|
|
|
"""
|
|
|
|
project_state = self.set_up_test_model("test_almoop")
|
|
|
|
# Test the state alteration (no DB alteration to test)
|
|
|
|
operation = migrations.AlterModelOptions("Pony", {"permissions": [("can_groom", "Can groom")]})
|
2014-06-27 22:34:48 +08:00
|
|
|
self.assertEqual(operation.describe(), "Change Meta options on Pony")
|
2014-06-16 03:34:02 +08:00
|
|
|
new_state = project_state.clone()
|
|
|
|
operation.state_forwards("test_almoop", new_state)
|
|
|
|
self.assertEqual(len(project_state.models["test_almoop", "pony"].options.get("permissions", [])), 0)
|
|
|
|
self.assertEqual(len(new_state.models["test_almoop", "pony"].options.get("permissions", [])), 1)
|
|
|
|
self.assertEqual(new_state.models["test_almoop", "pony"].options["permissions"][0][0], "can_groom")
|
2014-11-23 01:57:13 +08:00
|
|
|
# And deconstruction
|
|
|
|
definition = operation.deconstruct()
|
|
|
|
self.assertEqual(definition[0], "AlterModelOptions")
|
|
|
|
self.assertEqual(definition[1], [])
|
|
|
|
self.assertEqual(definition[2], {'name': "Pony", 'options': {"permissions": [("can_groom", "Can groom")]}})
|
2014-06-16 03:34:02 +08:00
|
|
|
|
2014-07-29 01:47:28 +08:00
|
|
|
def test_alter_model_options_emptying(self):
|
|
|
|
"""
|
2016-10-27 15:53:39 +08:00
|
|
|
The AlterModelOptions operation removes keys from the dict (#23121)
|
2014-07-29 01:47:28 +08:00
|
|
|
"""
|
|
|
|
project_state = self.set_up_test_model("test_almoop", options=True)
|
|
|
|
# Test the state alteration (no DB alteration to test)
|
|
|
|
operation = migrations.AlterModelOptions("Pony", {})
|
|
|
|
self.assertEqual(operation.describe(), "Change Meta options on Pony")
|
|
|
|
new_state = project_state.clone()
|
|
|
|
operation.state_forwards("test_almoop", new_state)
|
|
|
|
self.assertEqual(len(project_state.models["test_almoop", "pony"].options.get("permissions", [])), 1)
|
|
|
|
self.assertEqual(len(new_state.models["test_almoop", "pony"].options.get("permissions", [])), 0)
|
2014-11-23 01:57:13 +08:00
|
|
|
# And deconstruction
|
|
|
|
definition = operation.deconstruct()
|
|
|
|
self.assertEqual(definition[0], "AlterModelOptions")
|
|
|
|
self.assertEqual(definition[1], [])
|
|
|
|
self.assertEqual(definition[2], {'name': "Pony", 'options': {}})
|
2014-07-29 01:47:28 +08:00
|
|
|
|
2014-06-16 05:55:44 +08:00
|
|
|
def test_alter_order_with_respect_to(self):
|
|
|
|
"""
|
|
|
|
Tests the AlterOrderWithRespectTo operation.
|
|
|
|
"""
|
|
|
|
project_state = self.set_up_test_model("test_alorwrtto", related_model=True)
|
|
|
|
# Test the state alteration
|
|
|
|
operation = migrations.AlterOrderWithRespectTo("Rider", "pony")
|
2014-06-27 22:34:48 +08:00
|
|
|
self.assertEqual(operation.describe(), "Set order_with_respect_to on Rider to pony")
|
2014-06-16 05:55:44 +08:00
|
|
|
new_state = project_state.clone()
|
|
|
|
operation.state_forwards("test_alorwrtto", new_state)
|
2015-09-12 07:33:12 +08:00
|
|
|
self.assertIsNone(
|
|
|
|
project_state.models["test_alorwrtto", "rider"].options.get("order_with_respect_to", None)
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
new_state.models["test_alorwrtto", "rider"].options.get("order_with_respect_to", None),
|
|
|
|
"pony"
|
|
|
|
)
|
2014-06-16 05:55:44 +08:00
|
|
|
# Make sure there's no matching index
|
|
|
|
self.assertColumnNotExists("test_alorwrtto_rider", "_order")
|
2014-12-16 08:22:28 +08:00
|
|
|
# Create some rows before alteration
|
2014-11-05 17:43:31 +08:00
|
|
|
rendered_state = project_state.apps
|
2014-12-16 08:22:28 +08:00
|
|
|
pony = rendered_state.get_model("test_alorwrtto", "Pony").objects.create(weight=50)
|
|
|
|
rendered_state.get_model("test_alorwrtto", "Rider").objects.create(pony=pony, friend_id=1)
|
|
|
|
rendered_state.get_model("test_alorwrtto", "Rider").objects.create(pony=pony, friend_id=2)
|
2014-06-16 05:55:44 +08:00
|
|
|
# Test the database alteration
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
operation.database_forwards("test_alorwrtto", editor, project_state, new_state)
|
|
|
|
self.assertColumnExists("test_alorwrtto_rider", "_order")
|
2014-12-16 08:22:28 +08:00
|
|
|
# Check for correct value in rows
|
2014-11-05 17:43:31 +08:00
|
|
|
updated_riders = new_state.apps.get_model("test_alorwrtto", "Rider").objects.all()
|
2014-12-16 08:22:28 +08:00
|
|
|
self.assertEqual(updated_riders[0]._order, 0)
|
|
|
|
self.assertEqual(updated_riders[1]._order, 0)
|
2014-06-16 05:55:44 +08:00
|
|
|
# And test reversal
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
operation.database_backwards("test_alorwrtto", editor, new_state, project_state)
|
|
|
|
self.assertColumnNotExists("test_alorwrtto_rider", "_order")
|
2014-11-23 01:57:13 +08:00
|
|
|
# And deconstruction
|
|
|
|
definition = operation.deconstruct()
|
|
|
|
self.assertEqual(definition[0], "AlterOrderWithRespectTo")
|
|
|
|
self.assertEqual(definition[1], [])
|
|
|
|
self.assertEqual(definition[2], {'name': "Rider", 'order_with_respect_to': "pony"})
|
2014-06-16 05:55:44 +08:00
|
|
|
|
2014-12-13 06:19:58 +08:00
|
|
|
def test_alter_model_managers(self):
|
|
|
|
"""
|
2016-10-27 15:53:39 +08:00
|
|
|
The managers on a model are set.
|
2014-12-13 06:19:58 +08:00
|
|
|
"""
|
|
|
|
project_state = self.set_up_test_model("test_almoma")
|
|
|
|
# Test the state alteration
|
|
|
|
operation = migrations.AlterModelManagers(
|
|
|
|
"Pony",
|
|
|
|
managers=[
|
|
|
|
("food_qs", FoodQuerySet.as_manager()),
|
|
|
|
("food_mgr", FoodManager("a", "b")),
|
|
|
|
("food_mgr_kwargs", FoodManager("x", "y", 3, 4)),
|
|
|
|
]
|
|
|
|
)
|
|
|
|
self.assertEqual(operation.describe(), "Change managers on Pony")
|
|
|
|
managers = project_state.models["test_almoma", "pony"].managers
|
|
|
|
self.assertEqual(managers, [])
|
|
|
|
|
|
|
|
new_state = project_state.clone()
|
|
|
|
operation.state_forwards("test_almoma", new_state)
|
|
|
|
self.assertIn(("test_almoma", "pony"), new_state.models)
|
|
|
|
managers = new_state.models["test_almoma", "pony"].managers
|
|
|
|
self.assertEqual(managers[0][0], "food_qs")
|
|
|
|
self.assertIsInstance(managers[0][1], models.Manager)
|
|
|
|
self.assertEqual(managers[1][0], "food_mgr")
|
|
|
|
self.assertIsInstance(managers[1][1], FoodManager)
|
|
|
|
self.assertEqual(managers[1][1].args, ("a", "b", 1, 2))
|
|
|
|
self.assertEqual(managers[2][0], "food_mgr_kwargs")
|
|
|
|
self.assertIsInstance(managers[2][1], FoodManager)
|
|
|
|
self.assertEqual(managers[2][1].args, ("x", "y", 3, 4))
|
2015-12-14 14:20:10 +08:00
|
|
|
rendered_state = new_state.apps
|
|
|
|
model = rendered_state.get_model('test_almoma', 'pony')
|
|
|
|
self.assertIsInstance(model.food_qs, models.Manager)
|
|
|
|
self.assertIsInstance(model.food_mgr, FoodManager)
|
|
|
|
self.assertIsInstance(model.food_mgr_kwargs, FoodManager)
|
2014-12-13 06:19:58 +08:00
|
|
|
|
|
|
|
def test_alter_model_managers_emptying(self):
|
|
|
|
"""
|
2016-10-27 15:53:39 +08:00
|
|
|
The managers on a model are set.
|
2014-12-13 06:19:58 +08:00
|
|
|
"""
|
|
|
|
project_state = self.set_up_test_model("test_almomae", manager_model=True)
|
|
|
|
# Test the state alteration
|
|
|
|
operation = migrations.AlterModelManagers("Food", managers=[])
|
|
|
|
self.assertEqual(operation.describe(), "Change managers on Food")
|
|
|
|
self.assertIn(("test_almomae", "food"), project_state.models)
|
|
|
|
managers = project_state.models["test_almomae", "food"].managers
|
|
|
|
self.assertEqual(managers[0][0], "food_qs")
|
|
|
|
self.assertIsInstance(managers[0][1], models.Manager)
|
|
|
|
self.assertEqual(managers[1][0], "food_mgr")
|
|
|
|
self.assertIsInstance(managers[1][1], FoodManager)
|
|
|
|
self.assertEqual(managers[1][1].args, ("a", "b", 1, 2))
|
|
|
|
self.assertEqual(managers[2][0], "food_mgr_kwargs")
|
|
|
|
self.assertIsInstance(managers[2][1], FoodManager)
|
|
|
|
self.assertEqual(managers[2][1].args, ("x", "y", 3, 4))
|
|
|
|
|
|
|
|
new_state = project_state.clone()
|
|
|
|
operation.state_forwards("test_almomae", new_state)
|
|
|
|
managers = new_state.models["test_almomae", "food"].managers
|
|
|
|
self.assertEqual(managers, [])
|
|
|
|
|
2014-08-04 09:58:44 +08:00
|
|
|
def test_alter_fk(self):
|
|
|
|
"""
|
2016-10-27 15:53:39 +08:00
|
|
|
Creating and then altering an FK works correctly
|
2014-08-04 09:58:44 +08:00
|
|
|
and deals with the pending SQL (#23091)
|
|
|
|
"""
|
|
|
|
project_state = self.set_up_test_model("test_alfk")
|
|
|
|
# Test adding and then altering the FK in one go
|
|
|
|
create_operation = migrations.CreateModel(
|
|
|
|
name="Rider",
|
|
|
|
fields=[
|
|
|
|
("id", models.AutoField(primary_key=True)),
|
2015-07-22 22:43:21 +08:00
|
|
|
("pony", models.ForeignKey("Pony", models.CASCADE)),
|
2014-08-04 09:58:44 +08:00
|
|
|
],
|
|
|
|
)
|
|
|
|
create_state = project_state.clone()
|
|
|
|
create_operation.state_forwards("test_alfk", create_state)
|
|
|
|
alter_operation = migrations.AlterField(
|
|
|
|
model_name='Rider',
|
|
|
|
name='pony',
|
2015-07-22 22:43:21 +08:00
|
|
|
field=models.ForeignKey("Pony", models.CASCADE, editable=False),
|
2014-08-04 09:58:44 +08:00
|
|
|
)
|
|
|
|
alter_state = create_state.clone()
|
|
|
|
alter_operation.state_forwards("test_alfk", alter_state)
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
create_operation.database_forwards("test_alfk", editor, project_state, create_state)
|
|
|
|
alter_operation.database_forwards("test_alfk", editor, create_state, alter_state)
|
|
|
|
|
2014-08-07 09:52:03 +08:00
|
|
|
def test_alter_fk_non_fk(self):
|
|
|
|
"""
|
2016-10-27 15:53:39 +08:00
|
|
|
Altering an FK to a non-FK works (#23244)
|
2014-08-07 09:52:03 +08:00
|
|
|
"""
|
|
|
|
# Test the state alteration
|
|
|
|
operation = migrations.AlterField(
|
|
|
|
model_name="Rider",
|
|
|
|
name="pony",
|
|
|
|
field=models.FloatField(),
|
|
|
|
)
|
|
|
|
project_state, new_state = self.make_test_state("test_afknfk", operation, related_model=True)
|
|
|
|
# Test the database alteration
|
|
|
|
self.assertColumnExists("test_afknfk_rider", "pony_id")
|
|
|
|
self.assertColumnNotExists("test_afknfk_rider", "pony")
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
operation.database_forwards("test_afknfk", editor, project_state, new_state)
|
|
|
|
self.assertColumnExists("test_afknfk_rider", "pony")
|
|
|
|
self.assertColumnNotExists("test_afknfk_rider", "pony_id")
|
|
|
|
# And test reversal
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
operation.database_backwards("test_afknfk", editor, new_state, project_state)
|
|
|
|
self.assertColumnExists("test_afknfk_rider", "pony_id")
|
|
|
|
self.assertColumnNotExists("test_afknfk_rider", "pony")
|
|
|
|
|
2013-09-08 00:03:23 +08:00
|
|
|
def test_run_sql(self):
|
|
|
|
"""
|
2013-09-25 20:58:07 +08:00
|
|
|
Tests the RunSQL operation.
|
2013-09-08 00:03:23 +08:00
|
|
|
"""
|
|
|
|
project_state = self.set_up_test_model("test_runsql")
|
|
|
|
# Create the operation
|
|
|
|
operation = migrations.RunSQL(
|
2014-08-15 23:01:49 +08:00
|
|
|
# Use a multi-line string with a comment to test splitting on SQLite and MySQL respectively
|
2014-09-24 05:17:00 +08:00
|
|
|
"CREATE TABLE i_love_ponies (id int, special_thing varchar(15));\n"
|
|
|
|
"INSERT INTO i_love_ponies (id, special_thing) VALUES (1, 'i love ponies'); -- this is magic!\n"
|
|
|
|
"INSERT INTO i_love_ponies (id, special_thing) VALUES (2, 'i love django');\n"
|
|
|
|
"UPDATE i_love_ponies SET special_thing = 'Ponies' WHERE special_thing LIKE '%%ponies';"
|
|
|
|
"UPDATE i_love_ponies SET special_thing = 'Django' WHERE special_thing LIKE '%django';",
|
|
|
|
|
|
|
|
# Run delete queries to test for parameter substitution failure
|
|
|
|
# reported in #23426
|
|
|
|
"DELETE FROM i_love_ponies WHERE special_thing LIKE '%Django%';"
|
|
|
|
"DELETE FROM i_love_ponies WHERE special_thing LIKE '%%Ponies%%';"
|
2013-09-08 00:03:23 +08:00
|
|
|
"DROP TABLE i_love_ponies",
|
2014-09-24 05:17:00 +08:00
|
|
|
|
2013-11-04 02:17:58 +08:00
|
|
|
state_operations=[migrations.CreateModel("SomethingElse", [("id", models.AutoField(primary_key=True))])],
|
2013-09-08 00:03:23 +08:00
|
|
|
)
|
2014-06-27 22:34:48 +08:00
|
|
|
self.assertEqual(operation.describe(), "Raw SQL operation")
|
2013-09-08 00:03:23 +08:00
|
|
|
# Test the state alteration
|
|
|
|
new_state = project_state.clone()
|
|
|
|
operation.state_forwards("test_runsql", new_state)
|
|
|
|
self.assertEqual(len(new_state.models["test_runsql", "somethingelse"].fields), 1)
|
|
|
|
# Make sure there's no table
|
|
|
|
self.assertTableNotExists("i_love_ponies")
|
2014-12-01 18:13:01 +08:00
|
|
|
# Test SQL collection
|
|
|
|
with connection.schema_editor(collect_sql=True) as editor:
|
|
|
|
operation.database_forwards("test_runsql", editor, project_state, new_state)
|
|
|
|
self.assertIn("LIKE '%%ponies';", "\n".join(editor.collected_sql))
|
|
|
|
operation.database_backwards("test_runsql", editor, project_state, new_state)
|
|
|
|
self.assertIn("LIKE '%%Ponies%%';", "\n".join(editor.collected_sql))
|
2013-09-08 00:03:23 +08:00
|
|
|
# Test the database alteration
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
operation.database_forwards("test_runsql", editor, project_state, new_state)
|
|
|
|
self.assertTableExists("i_love_ponies")
|
2014-04-26 16:22:48 +08:00
|
|
|
# Make sure all the SQL was processed
|
|
|
|
with connection.cursor() as cursor:
|
|
|
|
cursor.execute("SELECT COUNT(*) FROM i_love_ponies")
|
|
|
|
self.assertEqual(cursor.fetchall()[0][0], 2)
|
2014-09-24 05:17:00 +08:00
|
|
|
cursor.execute("SELECT COUNT(*) FROM i_love_ponies WHERE special_thing = 'Django'")
|
|
|
|
self.assertEqual(cursor.fetchall()[0][0], 1)
|
|
|
|
cursor.execute("SELECT COUNT(*) FROM i_love_ponies WHERE special_thing = 'Ponies'")
|
|
|
|
self.assertEqual(cursor.fetchall()[0][0], 1)
|
2013-09-08 00:03:23 +08:00
|
|
|
# And test reversal
|
2014-02-20 07:44:57 +08:00
|
|
|
self.assertTrue(operation.reversible)
|
2013-09-08 00:03:23 +08:00
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
operation.database_backwards("test_runsql", editor, new_state, project_state)
|
|
|
|
self.assertTableNotExists("i_love_ponies")
|
2014-11-23 01:57:13 +08:00
|
|
|
# And deconstruction
|
|
|
|
definition = operation.deconstruct()
|
|
|
|
self.assertEqual(definition[0], "RunSQL")
|
|
|
|
self.assertEqual(definition[1], [])
|
|
|
|
self.assertEqual(sorted(definition[2]), ["reverse_sql", "sql", "state_operations"])
|
2016-01-09 16:12:46 +08:00
|
|
|
# And elidable reduction
|
|
|
|
self.assertIs(False, operation.reduce(operation, []))
|
|
|
|
elidable_operation = migrations.RunSQL('SELECT 1 FROM void;', elidable=True)
|
|
|
|
self.assertEqual(elidable_operation.reduce(operation, []), [operation])
|
2013-09-08 00:03:23 +08:00
|
|
|
|
2014-09-16 08:25:02 +08:00
|
|
|
def test_run_sql_params(self):
|
|
|
|
"""
|
|
|
|
#23426 - RunSQL should accept parameters.
|
|
|
|
"""
|
|
|
|
project_state = self.set_up_test_model("test_runsql")
|
|
|
|
# Create the operation
|
|
|
|
operation = migrations.RunSQL(
|
2014-10-10 00:38:23 +08:00
|
|
|
["CREATE TABLE i_love_ponies (id int, special_thing varchar(15));"],
|
|
|
|
["DROP TABLE i_love_ponies"],
|
2014-09-16 08:25:02 +08:00
|
|
|
)
|
|
|
|
param_operation = migrations.RunSQL(
|
|
|
|
# forwards
|
|
|
|
(
|
|
|
|
"INSERT INTO i_love_ponies (id, special_thing) VALUES (1, 'Django');",
|
|
|
|
["INSERT INTO i_love_ponies (id, special_thing) VALUES (2, %s);", ['Ponies']],
|
|
|
|
("INSERT INTO i_love_ponies (id, special_thing) VALUES (%s, %s);", (3, 'Python',)),
|
|
|
|
),
|
|
|
|
# backwards
|
|
|
|
[
|
|
|
|
"DELETE FROM i_love_ponies WHERE special_thing = 'Django';",
|
|
|
|
["DELETE FROM i_love_ponies WHERE special_thing = 'Ponies';", None],
|
|
|
|
("DELETE FROM i_love_ponies WHERE id = %s OR special_thing = %s;", [3, 'Python']),
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
# Make sure there's no table
|
|
|
|
self.assertTableNotExists("i_love_ponies")
|
|
|
|
new_state = project_state.clone()
|
|
|
|
# Test the database alteration
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
operation.database_forwards("test_runsql", editor, project_state, new_state)
|
|
|
|
|
|
|
|
# Test parameter passing
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
param_operation.database_forwards("test_runsql", editor, project_state, new_state)
|
|
|
|
# Make sure all the SQL was processed
|
|
|
|
with connection.cursor() as cursor:
|
|
|
|
cursor.execute("SELECT COUNT(*) FROM i_love_ponies")
|
|
|
|
self.assertEqual(cursor.fetchall()[0][0], 3)
|
|
|
|
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
param_operation.database_backwards("test_runsql", editor, new_state, project_state)
|
|
|
|
with connection.cursor() as cursor:
|
|
|
|
cursor.execute("SELECT COUNT(*) FROM i_love_ponies")
|
|
|
|
self.assertEqual(cursor.fetchall()[0][0], 0)
|
|
|
|
|
|
|
|
# And test reversal
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
operation.database_backwards("test_runsql", editor, new_state, project_state)
|
|
|
|
self.assertTableNotExists("i_love_ponies")
|
|
|
|
|
|
|
|
def test_run_sql_params_invalid(self):
|
|
|
|
"""
|
|
|
|
#23426 - RunSQL should fail when a list of statements with an incorrect
|
|
|
|
number of tuples is given.
|
|
|
|
"""
|
|
|
|
project_state = self.set_up_test_model("test_runsql")
|
|
|
|
new_state = project_state.clone()
|
|
|
|
operation = migrations.RunSQL(
|
|
|
|
# forwards
|
|
|
|
[
|
|
|
|
["INSERT INTO foo (bar) VALUES ('buz');"]
|
|
|
|
],
|
|
|
|
# backwards
|
|
|
|
(
|
|
|
|
("DELETE FROM foo WHERE bar = 'buz';", 'invalid', 'parameter count'),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
with connection.schema_editor() as editor:
|
2016-01-18 16:45:45 +08:00
|
|
|
with self.assertRaisesMessage(ValueError, "Expected a 2-tuple but got 1"):
|
|
|
|
operation.database_forwards("test_runsql", editor, project_state, new_state)
|
2014-09-16 08:25:02 +08:00
|
|
|
|
|
|
|
with connection.schema_editor() as editor:
|
2016-01-18 16:45:45 +08:00
|
|
|
with self.assertRaisesMessage(ValueError, "Expected a 2-tuple but got 3"):
|
|
|
|
operation.database_backwards("test_runsql", editor, new_state, project_state)
|
2014-09-16 08:25:02 +08:00
|
|
|
|
2015-01-09 01:00:04 +08:00
|
|
|
def test_run_sql_noop(self):
|
|
|
|
"""
|
|
|
|
#24098 - Tests no-op RunSQL operations.
|
|
|
|
"""
|
|
|
|
operation = migrations.RunSQL(migrations.RunSQL.noop, migrations.RunSQL.noop)
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
operation.database_forwards("test_runsql", editor, None, None)
|
|
|
|
operation.database_backwards("test_runsql", editor, None, None)
|
|
|
|
|
2013-09-25 20:58:07 +08:00
|
|
|
def test_run_python(self):
|
|
|
|
"""
|
|
|
|
Tests the RunPython operation
|
|
|
|
"""
|
|
|
|
|
2014-01-29 14:41:04 +08:00
|
|
|
project_state = self.set_up_test_model("test_runpython", mti_model=True)
|
2014-02-09 19:13:10 +08:00
|
|
|
|
2013-09-25 20:58:07 +08:00
|
|
|
# Create the operation
|
2014-02-09 18:54:02 +08:00
|
|
|
def inner_method(models, schema_editor):
|
2013-09-25 20:58:07 +08:00
|
|
|
Pony = models.get_model("test_runpython", "Pony")
|
2014-02-09 18:54:02 +08:00
|
|
|
Pony.objects.create(pink=1, weight=3.55)
|
|
|
|
Pony.objects.create(weight=5)
|
2014-02-24 01:07:52 +08:00
|
|
|
|
2014-02-20 07:44:57 +08:00
|
|
|
def inner_method_reverse(models, schema_editor):
|
|
|
|
Pony = models.get_model("test_runpython", "Pony")
|
|
|
|
Pony.objects.filter(pink=1, weight=3.55).delete()
|
|
|
|
Pony.objects.filter(weight=5).delete()
|
|
|
|
operation = migrations.RunPython(inner_method, reverse_code=inner_method_reverse)
|
2014-06-27 22:34:48 +08:00
|
|
|
self.assertEqual(operation.describe(), "Raw Python operation")
|
2013-09-25 20:58:07 +08:00
|
|
|
# Test the state alteration does nothing
|
|
|
|
new_state = project_state.clone()
|
|
|
|
operation.state_forwards("test_runpython", new_state)
|
|
|
|
self.assertEqual(new_state, project_state)
|
|
|
|
# Test the database alteration
|
2014-11-05 17:43:31 +08:00
|
|
|
self.assertEqual(project_state.apps.get_model("test_runpython", "Pony").objects.count(), 0)
|
2013-09-25 20:58:07 +08:00
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
operation.database_forwards("test_runpython", editor, project_state, new_state)
|
2014-11-05 17:43:31 +08:00
|
|
|
self.assertEqual(project_state.apps.get_model("test_runpython", "Pony").objects.count(), 2)
|
2014-02-20 07:44:57 +08:00
|
|
|
# Now test reversal
|
|
|
|
self.assertTrue(operation.reversible)
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
operation.database_backwards("test_runpython", editor, project_state, new_state)
|
2014-11-05 17:43:31 +08:00
|
|
|
self.assertEqual(project_state.apps.get_model("test_runpython", "Pony").objects.count(), 0)
|
2014-02-09 18:54:02 +08:00
|
|
|
# Now test we can't use a string
|
2017-05-29 03:37:21 +08:00
|
|
|
with self.assertRaisesMessage(ValueError, 'RunPython must be supplied with a callable'):
|
2014-11-23 01:57:13 +08:00
|
|
|
migrations.RunPython("print 'ahahaha'")
|
|
|
|
# And deconstruction
|
|
|
|
definition = operation.deconstruct()
|
|
|
|
self.assertEqual(definition[0], "RunPython")
|
|
|
|
self.assertEqual(definition[1], [])
|
|
|
|
self.assertEqual(sorted(definition[2]), ["code", "reverse_code"])
|
2013-09-25 20:58:07 +08:00
|
|
|
|
2014-02-20 07:44:57 +08:00
|
|
|
# Also test reversal fails, with an operation identical to above but without reverse_code set
|
|
|
|
no_reverse_operation = migrations.RunPython(inner_method)
|
|
|
|
self.assertFalse(no_reverse_operation.reversible)
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
no_reverse_operation.database_forwards("test_runpython", editor, project_state, new_state)
|
|
|
|
with self.assertRaises(NotImplementedError):
|
|
|
|
no_reverse_operation.database_backwards("test_runpython", editor, new_state, project_state)
|
2014-11-05 17:43:31 +08:00
|
|
|
self.assertEqual(project_state.apps.get_model("test_runpython", "Pony").objects.count(), 2)
|
2014-02-20 07:44:57 +08:00
|
|
|
|
2014-01-29 14:41:04 +08:00
|
|
|
def create_ponies(models, schema_editor):
|
|
|
|
Pony = models.get_model("test_runpython", "Pony")
|
|
|
|
pony1 = Pony.objects.create(pink=1, weight=3.55)
|
|
|
|
self.assertIsNot(pony1.pk, None)
|
|
|
|
pony2 = Pony.objects.create(weight=5)
|
|
|
|
self.assertIsNot(pony2.pk, None)
|
|
|
|
self.assertNotEqual(pony1.pk, pony2.pk)
|
|
|
|
|
|
|
|
operation = migrations.RunPython(create_ponies)
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
operation.database_forwards("test_runpython", editor, project_state, new_state)
|
2014-11-05 17:43:31 +08:00
|
|
|
self.assertEqual(project_state.apps.get_model("test_runpython", "Pony").objects.count(), 4)
|
2014-11-23 01:57:13 +08:00
|
|
|
# And deconstruction
|
|
|
|
definition = operation.deconstruct()
|
|
|
|
self.assertEqual(definition[0], "RunPython")
|
|
|
|
self.assertEqual(definition[1], [])
|
|
|
|
self.assertEqual(sorted(definition[2]), ["code"])
|
2014-01-29 14:41:04 +08:00
|
|
|
|
|
|
|
def create_shetlandponies(models, schema_editor):
|
|
|
|
ShetlandPony = models.get_model("test_runpython", "ShetlandPony")
|
|
|
|
pony1 = ShetlandPony.objects.create(weight=4.0)
|
|
|
|
self.assertIsNot(pony1.pk, None)
|
|
|
|
pony2 = ShetlandPony.objects.create(weight=5.0)
|
|
|
|
self.assertIsNot(pony2.pk, None)
|
|
|
|
self.assertNotEqual(pony1.pk, pony2.pk)
|
|
|
|
|
|
|
|
operation = migrations.RunPython(create_shetlandponies)
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
operation.database_forwards("test_runpython", editor, project_state, new_state)
|
2014-11-05 17:43:31 +08:00
|
|
|
self.assertEqual(project_state.apps.get_model("test_runpython", "Pony").objects.count(), 6)
|
|
|
|
self.assertEqual(project_state.apps.get_model("test_runpython", "ShetlandPony").objects.count(), 2)
|
2016-01-09 16:12:46 +08:00
|
|
|
# And elidable reduction
|
|
|
|
self.assertIs(False, operation.reduce(operation, []))
|
|
|
|
elidable_operation = migrations.RunPython(inner_method, elidable=True)
|
|
|
|
self.assertEqual(elidable_operation.reduce(operation, []), [operation])
|
2014-02-20 07:44:57 +08:00
|
|
|
|
2014-05-08 05:28:34 +08:00
|
|
|
def test_run_python_atomic(self):
|
|
|
|
"""
|
|
|
|
Tests the RunPython operation correctly handles the "atomic" keyword
|
|
|
|
"""
|
|
|
|
project_state = self.set_up_test_model("test_runpythonatomic", mti_model=True)
|
2014-05-09 03:49:54 +08:00
|
|
|
|
2014-05-08 05:28:34 +08:00
|
|
|
def inner_method(models, schema_editor):
|
|
|
|
Pony = models.get_model("test_runpythonatomic", "Pony")
|
|
|
|
Pony.objects.create(pink=1, weight=3.55)
|
|
|
|
raise ValueError("Adrian hates ponies.")
|
2014-05-09 03:49:54 +08:00
|
|
|
|
2017-08-06 08:34:18 +08:00
|
|
|
# Verify atomicity when applying.
|
2014-05-08 05:28:34 +08:00
|
|
|
atomic_migration = Migration("test", "test_runpythonatomic")
|
2017-08-06 08:34:18 +08:00
|
|
|
atomic_migration.operations = [migrations.RunPython(inner_method, reverse_code=inner_method)]
|
2014-05-08 05:28:34 +08:00
|
|
|
non_atomic_migration = Migration("test", "test_runpythonatomic")
|
2017-08-06 08:34:18 +08:00
|
|
|
non_atomic_migration.operations = [migrations.RunPython(inner_method, reverse_code=inner_method, atomic=False)]
|
2014-05-08 05:28:34 +08:00
|
|
|
# If we're a fully-transactional database, both versions should rollback
|
|
|
|
if connection.features.can_rollback_ddl:
|
2014-11-05 17:43:31 +08:00
|
|
|
self.assertEqual(project_state.apps.get_model("test_runpythonatomic", "Pony").objects.count(), 0)
|
2014-05-08 05:28:34 +08:00
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
atomic_migration.apply(project_state, editor)
|
2014-11-05 17:43:31 +08:00
|
|
|
self.assertEqual(project_state.apps.get_model("test_runpythonatomic", "Pony").objects.count(), 0)
|
2014-05-08 05:28:34 +08:00
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
non_atomic_migration.apply(project_state, editor)
|
2014-11-05 17:43:31 +08:00
|
|
|
self.assertEqual(project_state.apps.get_model("test_runpythonatomic", "Pony").objects.count(), 0)
|
2014-05-08 05:28:34 +08:00
|
|
|
# Otherwise, the non-atomic operation should leave a row there
|
|
|
|
else:
|
2014-11-05 17:43:31 +08:00
|
|
|
self.assertEqual(project_state.apps.get_model("test_runpythonatomic", "Pony").objects.count(), 0)
|
2014-05-08 05:28:34 +08:00
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
atomic_migration.apply(project_state, editor)
|
2014-11-05 17:43:31 +08:00
|
|
|
self.assertEqual(project_state.apps.get_model("test_runpythonatomic", "Pony").objects.count(), 0)
|
2014-05-08 05:28:34 +08:00
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
non_atomic_migration.apply(project_state, editor)
|
2014-11-05 17:43:31 +08:00
|
|
|
self.assertEqual(project_state.apps.get_model("test_runpythonatomic", "Pony").objects.count(), 1)
|
2017-08-06 08:34:18 +08:00
|
|
|
# Reset object count to zero and verify atomicity when unapplying.
|
|
|
|
project_state.apps.get_model("test_runpythonatomic", "Pony").objects.all().delete()
|
|
|
|
# On a fully-transactional database, both versions rollback.
|
|
|
|
if connection.features.can_rollback_ddl:
|
|
|
|
self.assertEqual(project_state.apps.get_model("test_runpythonatomic", "Pony").objects.count(), 0)
|
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
atomic_migration.unapply(project_state, editor)
|
|
|
|
self.assertEqual(project_state.apps.get_model("test_runpythonatomic", "Pony").objects.count(), 0)
|
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
non_atomic_migration.unapply(project_state, editor)
|
|
|
|
self.assertEqual(project_state.apps.get_model("test_runpythonatomic", "Pony").objects.count(), 0)
|
|
|
|
# Otherwise, the non-atomic operation leaves a row there.
|
|
|
|
else:
|
|
|
|
self.assertEqual(project_state.apps.get_model("test_runpythonatomic", "Pony").objects.count(), 0)
|
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
atomic_migration.unapply(project_state, editor)
|
2017-07-12 00:40:18 +08:00
|
|
|
self.assertEqual(project_state.apps.get_model("test_runpythonatomic", "Pony").objects.count(), 0)
|
2017-08-06 08:34:18 +08:00
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
non_atomic_migration.unapply(project_state, editor)
|
2017-07-12 00:40:18 +08:00
|
|
|
self.assertEqual(project_state.apps.get_model("test_runpythonatomic", "Pony").objects.count(), 1)
|
2017-08-06 08:34:18 +08:00
|
|
|
# Verify deconstruction.
|
2014-11-23 01:57:13 +08:00
|
|
|
definition = non_atomic_migration.operations[0].deconstruct()
|
|
|
|
self.assertEqual(definition[0], "RunPython")
|
|
|
|
self.assertEqual(definition[1], [])
|
2017-08-06 08:34:18 +08:00
|
|
|
self.assertEqual(sorted(definition[2]), ["atomic", "code", "reverse_code"])
|
2014-05-08 05:28:34 +08:00
|
|
|
|
2015-02-13 23:42:44 +08:00
|
|
|
def test_run_python_related_assignment(self):
|
|
|
|
"""
|
2016-10-27 15:53:39 +08:00
|
|
|
#24282 - Model changes to a FK reverse side update the model
|
2015-02-13 23:42:44 +08:00
|
|
|
on the FK side as well.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def inner_method(models, schema_editor):
|
|
|
|
Author = models.get_model("test_authors", "Author")
|
|
|
|
Book = models.get_model("test_books", "Book")
|
|
|
|
author = Author.objects.create(name="Hemingway")
|
|
|
|
Book.objects.create(title="Old Man and The Sea", author=author)
|
|
|
|
|
|
|
|
create_author = migrations.CreateModel(
|
|
|
|
"Author",
|
|
|
|
[
|
|
|
|
("id", models.AutoField(primary_key=True)),
|
|
|
|
("name", models.CharField(max_length=100)),
|
|
|
|
],
|
|
|
|
options={},
|
|
|
|
)
|
|
|
|
create_book = migrations.CreateModel(
|
|
|
|
"Book",
|
|
|
|
[
|
|
|
|
("id", models.AutoField(primary_key=True)),
|
|
|
|
("title", models.CharField(max_length=100)),
|
2015-07-22 22:43:21 +08:00
|
|
|
("author", models.ForeignKey("test_authors.Author", models.CASCADE))
|
2015-02-13 23:42:44 +08:00
|
|
|
],
|
|
|
|
options={},
|
|
|
|
)
|
|
|
|
add_hometown = migrations.AddField(
|
|
|
|
"Author",
|
|
|
|
"hometown",
|
|
|
|
models.CharField(max_length=100),
|
|
|
|
)
|
|
|
|
create_old_man = migrations.RunPython(inner_method, inner_method)
|
|
|
|
|
|
|
|
project_state = ProjectState()
|
|
|
|
new_state = project_state.clone()
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
create_author.state_forwards("test_authors", new_state)
|
|
|
|
create_author.database_forwards("test_authors", editor, project_state, new_state)
|
2015-02-19 04:44:31 +08:00
|
|
|
project_state = new_state
|
|
|
|
new_state = new_state.clone()
|
|
|
|
with connection.schema_editor() as editor:
|
2015-02-13 23:42:44 +08:00
|
|
|
create_book.state_forwards("test_books", new_state)
|
|
|
|
create_book.database_forwards("test_books", editor, project_state, new_state)
|
2015-02-19 04:44:31 +08:00
|
|
|
project_state = new_state
|
|
|
|
new_state = new_state.clone()
|
|
|
|
with connection.schema_editor() as editor:
|
2015-02-13 23:42:44 +08:00
|
|
|
add_hometown.state_forwards("test_authors", new_state)
|
|
|
|
add_hometown.database_forwards("test_authors", editor, project_state, new_state)
|
2015-02-19 04:44:31 +08:00
|
|
|
project_state = new_state
|
|
|
|
new_state = new_state.clone()
|
|
|
|
with connection.schema_editor() as editor:
|
2015-02-13 23:42:44 +08:00
|
|
|
create_old_man.state_forwards("test_books", new_state)
|
|
|
|
create_old_man.database_forwards("test_books", editor, project_state, new_state)
|
|
|
|
|
2015-07-02 16:43:15 +08:00
|
|
|
def test_model_with_bigautofield(self):
|
|
|
|
"""
|
|
|
|
A model with BigAutoField can be created.
|
|
|
|
"""
|
|
|
|
def create_data(models, schema_editor):
|
|
|
|
Author = models.get_model("test_author", "Author")
|
|
|
|
Book = models.get_model("test_book", "Book")
|
|
|
|
author1 = Author.objects.create(name="Hemingway")
|
|
|
|
Book.objects.create(title="Old Man and The Sea", author=author1)
|
|
|
|
Book.objects.create(id=2 ** 33, title="A farewell to arms", author=author1)
|
|
|
|
|
|
|
|
author2 = Author.objects.create(id=2 ** 33, name="Remarque")
|
|
|
|
Book.objects.create(title="All quiet on the western front", author=author2)
|
|
|
|
Book.objects.create(title="Arc de Triomphe", author=author2)
|
|
|
|
|
|
|
|
create_author = migrations.CreateModel(
|
|
|
|
"Author",
|
|
|
|
[
|
|
|
|
("id", models.BigAutoField(primary_key=True)),
|
|
|
|
("name", models.CharField(max_length=100)),
|
|
|
|
],
|
|
|
|
options={},
|
|
|
|
)
|
|
|
|
create_book = migrations.CreateModel(
|
|
|
|
"Book",
|
|
|
|
[
|
|
|
|
("id", models.BigAutoField(primary_key=True)),
|
|
|
|
("title", models.CharField(max_length=100)),
|
|
|
|
("author", models.ForeignKey(to="test_author.Author", on_delete=models.CASCADE))
|
|
|
|
],
|
|
|
|
options={},
|
|
|
|
)
|
|
|
|
fill_data = migrations.RunPython(create_data)
|
|
|
|
|
|
|
|
project_state = ProjectState()
|
|
|
|
new_state = project_state.clone()
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
create_author.state_forwards("test_author", new_state)
|
|
|
|
create_author.database_forwards("test_author", editor, project_state, new_state)
|
|
|
|
|
|
|
|
project_state = new_state
|
|
|
|
new_state = new_state.clone()
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
create_book.state_forwards("test_book", new_state)
|
|
|
|
create_book.database_forwards("test_book", editor, project_state, new_state)
|
|
|
|
|
|
|
|
project_state = new_state
|
|
|
|
new_state = new_state.clone()
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
fill_data.state_forwards("fill_data", new_state)
|
|
|
|
fill_data.database_forwards("fill_data", editor, project_state, new_state)
|
|
|
|
|
2019-07-27 05:05:22 +08:00
|
|
|
def _test_autofield_foreignfield_growth(self, source_field, target_field, target_value):
|
2015-07-02 16:43:15 +08:00
|
|
|
"""
|
2019-07-27 05:05:22 +08:00
|
|
|
A field may be migrated in the following ways:
|
|
|
|
|
|
|
|
- AutoField to BigAutoField
|
|
|
|
- SmallAutoField to AutoField
|
|
|
|
- SmallAutoField to BigAutoField
|
2015-07-02 16:43:15 +08:00
|
|
|
"""
|
|
|
|
def create_initial_data(models, schema_editor):
|
|
|
|
Article = models.get_model("test_article", "Article")
|
|
|
|
Blog = models.get_model("test_blog", "Blog")
|
|
|
|
blog = Blog.objects.create(name="web development done right")
|
|
|
|
Article.objects.create(name="Frameworks", blog=blog)
|
|
|
|
Article.objects.create(name="Programming Languages", blog=blog)
|
|
|
|
|
|
|
|
def create_big_data(models, schema_editor):
|
|
|
|
Article = models.get_model("test_article", "Article")
|
|
|
|
Blog = models.get_model("test_blog", "Blog")
|
2019-07-27 05:05:22 +08:00
|
|
|
blog2 = Blog.objects.create(name="Frameworks", id=target_value)
|
2015-07-02 16:43:15 +08:00
|
|
|
Article.objects.create(name="Django", blog=blog2)
|
2019-07-27 05:05:22 +08:00
|
|
|
Article.objects.create(id=target_value, name="Django2", blog=blog2)
|
2015-07-02 16:43:15 +08:00
|
|
|
|
|
|
|
create_blog = migrations.CreateModel(
|
|
|
|
"Blog",
|
|
|
|
[
|
2019-07-27 05:05:22 +08:00
|
|
|
("id", source_field(primary_key=True)),
|
2015-07-02 16:43:15 +08:00
|
|
|
("name", models.CharField(max_length=100)),
|
|
|
|
],
|
|
|
|
options={},
|
|
|
|
)
|
|
|
|
create_article = migrations.CreateModel(
|
|
|
|
"Article",
|
|
|
|
[
|
2019-07-27 05:05:22 +08:00
|
|
|
("id", source_field(primary_key=True)),
|
2015-07-02 16:43:15 +08:00
|
|
|
("blog", models.ForeignKey(to="test_blog.Blog", on_delete=models.CASCADE)),
|
|
|
|
("name", models.CharField(max_length=100)),
|
|
|
|
("data", models.TextField(default="")),
|
|
|
|
],
|
|
|
|
options={},
|
|
|
|
)
|
|
|
|
fill_initial_data = migrations.RunPython(create_initial_data, create_initial_data)
|
|
|
|
fill_big_data = migrations.RunPython(create_big_data, create_big_data)
|
|
|
|
|
2019-07-27 05:05:22 +08:00
|
|
|
grow_article_id = migrations.AlterField('Article', 'id', target_field(primary_key=True))
|
|
|
|
grow_blog_id = migrations.AlterField('Blog', 'id', target_field(primary_key=True))
|
2015-07-02 16:43:15 +08:00
|
|
|
|
|
|
|
project_state = ProjectState()
|
|
|
|
new_state = project_state.clone()
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
create_blog.state_forwards("test_blog", new_state)
|
|
|
|
create_blog.database_forwards("test_blog", editor, project_state, new_state)
|
|
|
|
|
|
|
|
project_state = new_state
|
|
|
|
new_state = new_state.clone()
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
create_article.state_forwards("test_article", new_state)
|
|
|
|
create_article.database_forwards("test_article", editor, project_state, new_state)
|
|
|
|
|
|
|
|
project_state = new_state
|
|
|
|
new_state = new_state.clone()
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
fill_initial_data.state_forwards("fill_initial_data", new_state)
|
|
|
|
fill_initial_data.database_forwards("fill_initial_data", editor, project_state, new_state)
|
|
|
|
|
|
|
|
project_state = new_state
|
|
|
|
new_state = new_state.clone()
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
grow_article_id.state_forwards("test_article", new_state)
|
|
|
|
grow_article_id.database_forwards("test_article", editor, project_state, new_state)
|
|
|
|
|
|
|
|
state = new_state.clone()
|
|
|
|
article = state.apps.get_model("test_article.Article")
|
2019-07-27 05:05:22 +08:00
|
|
|
self.assertIsInstance(article._meta.pk, target_field)
|
2015-07-02 16:43:15 +08:00
|
|
|
|
|
|
|
project_state = new_state
|
|
|
|
new_state = new_state.clone()
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
grow_blog_id.state_forwards("test_blog", new_state)
|
|
|
|
grow_blog_id.database_forwards("test_blog", editor, project_state, new_state)
|
|
|
|
|
|
|
|
state = new_state.clone()
|
|
|
|
blog = state.apps.get_model("test_blog.Blog")
|
2019-07-27 05:05:22 +08:00
|
|
|
self.assertIsInstance(blog._meta.pk, target_field)
|
2015-07-02 16:43:15 +08:00
|
|
|
|
|
|
|
project_state = new_state
|
|
|
|
new_state = new_state.clone()
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
fill_big_data.state_forwards("fill_big_data", new_state)
|
|
|
|
fill_big_data.database_forwards("fill_big_data", editor, project_state, new_state)
|
|
|
|
|
2019-07-27 05:05:22 +08:00
|
|
|
def test_autofield__bigautofield_foreignfield_growth(self):
|
|
|
|
"""A field may be migrated from AutoField to BigAutoField."""
|
|
|
|
self._test_autofield_foreignfield_growth(
|
|
|
|
models.AutoField,
|
|
|
|
models.BigAutoField,
|
|
|
|
2 ** 33,
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_smallfield_autofield_foreignfield_growth(self):
|
|
|
|
"""A field may be migrated from SmallAutoField to AutoField."""
|
|
|
|
self._test_autofield_foreignfield_growth(
|
|
|
|
models.SmallAutoField,
|
|
|
|
models.AutoField,
|
|
|
|
2 ** 22,
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_smallfield_bigautofield_foreignfield_growth(self):
|
|
|
|
"""A field may be migrated from SmallAutoField to BigAutoField."""
|
|
|
|
self._test_autofield_foreignfield_growth(
|
|
|
|
models.SmallAutoField,
|
|
|
|
models.BigAutoField,
|
|
|
|
2 ** 33,
|
|
|
|
)
|
|
|
|
|
2015-01-09 01:00:04 +08:00
|
|
|
def test_run_python_noop(self):
|
|
|
|
"""
|
|
|
|
#24098 - Tests no-op RunPython operations.
|
|
|
|
"""
|
|
|
|
project_state = ProjectState()
|
|
|
|
new_state = project_state.clone()
|
|
|
|
operation = migrations.RunPython(migrations.RunPython.noop, migrations.RunPython.noop)
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
operation.database_forwards("test_runpython", editor, project_state, new_state)
|
|
|
|
operation.database_backwards("test_runpython", editor, new_state, project_state)
|
|
|
|
|
2014-09-01 17:30:00 +08:00
|
|
|
def test_separate_database_and_state(self):
|
|
|
|
"""
|
|
|
|
Tests the SeparateDatabaseAndState operation.
|
|
|
|
"""
|
|
|
|
project_state = self.set_up_test_model("test_separatedatabaseandstate")
|
|
|
|
# Create the operation
|
|
|
|
database_operation = migrations.RunSQL(
|
|
|
|
"CREATE TABLE i_love_ponies (id int, special_thing int);",
|
|
|
|
"DROP TABLE i_love_ponies;"
|
|
|
|
)
|
|
|
|
state_operation = migrations.CreateModel("SomethingElse", [("id", models.AutoField(primary_key=True))])
|
|
|
|
operation = migrations.SeparateDatabaseAndState(
|
|
|
|
state_operations=[state_operation],
|
|
|
|
database_operations=[database_operation]
|
|
|
|
)
|
|
|
|
self.assertEqual(operation.describe(), "Custom state/database change combination")
|
|
|
|
# Test the state alteration
|
|
|
|
new_state = project_state.clone()
|
|
|
|
operation.state_forwards("test_separatedatabaseandstate", new_state)
|
|
|
|
self.assertEqual(len(new_state.models["test_separatedatabaseandstate", "somethingelse"].fields), 1)
|
|
|
|
# Make sure there's no table
|
|
|
|
self.assertTableNotExists("i_love_ponies")
|
|
|
|
# Test the database alteration
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
operation.database_forwards("test_separatedatabaseandstate", editor, project_state, new_state)
|
|
|
|
self.assertTableExists("i_love_ponies")
|
|
|
|
# And test reversal
|
|
|
|
self.assertTrue(operation.reversible)
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
operation.database_backwards("test_separatedatabaseandstate", editor, new_state, project_state)
|
|
|
|
self.assertTableNotExists("i_love_ponies")
|
2014-11-23 01:57:13 +08:00
|
|
|
# And deconstruction
|
|
|
|
definition = operation.deconstruct()
|
|
|
|
self.assertEqual(definition[0], "SeparateDatabaseAndState")
|
|
|
|
self.assertEqual(definition[1], [])
|
|
|
|
self.assertEqual(sorted(definition[2]), ["database_operations", "state_operations"])
|
2014-09-01 17:30:00 +08:00
|
|
|
|
2015-12-09 01:03:31 +08:00
|
|
|
def test_separate_database_and_state2(self):
|
|
|
|
"""
|
|
|
|
A complex SeparateDatabaseAndState operation: Multiple operations both
|
|
|
|
for state and database. Verify the state dependencies within each list
|
|
|
|
and that state ops don't affect the database.
|
|
|
|
"""
|
|
|
|
app_label = "test_separatedatabaseandstate2"
|
|
|
|
project_state = self.set_up_test_model(app_label)
|
|
|
|
# Create the operation
|
|
|
|
database_operations = [
|
|
|
|
migrations.CreateModel(
|
|
|
|
"ILovePonies",
|
|
|
|
[("id", models.AutoField(primary_key=True))],
|
|
|
|
options={"db_table": "iloveponies"},
|
|
|
|
),
|
|
|
|
migrations.CreateModel(
|
|
|
|
"ILoveMorePonies",
|
2015-12-10 08:12:04 +08:00
|
|
|
# We use IntegerField and not AutoField because
|
|
|
|
# the model is going to be deleted immediately
|
|
|
|
# and with an AutoField this fails on Oracle
|
|
|
|
[("id", models.IntegerField(primary_key=True))],
|
2015-12-09 01:03:31 +08:00
|
|
|
options={"db_table": "ilovemoreponies"},
|
|
|
|
),
|
|
|
|
migrations.DeleteModel("ILoveMorePonies"),
|
|
|
|
migrations.CreateModel(
|
|
|
|
"ILoveEvenMorePonies",
|
|
|
|
[("id", models.AutoField(primary_key=True))],
|
|
|
|
options={"db_table": "iloveevenmoreponies"},
|
|
|
|
),
|
|
|
|
]
|
|
|
|
state_operations = [
|
|
|
|
migrations.CreateModel(
|
|
|
|
"SomethingElse",
|
|
|
|
[("id", models.AutoField(primary_key=True))],
|
|
|
|
options={"db_table": "somethingelse"},
|
|
|
|
),
|
|
|
|
migrations.DeleteModel("SomethingElse"),
|
|
|
|
migrations.CreateModel(
|
|
|
|
"SomethingCompletelyDifferent",
|
|
|
|
[("id", models.AutoField(primary_key=True))],
|
|
|
|
options={"db_table": "somethingcompletelydifferent"},
|
|
|
|
),
|
|
|
|
]
|
|
|
|
operation = migrations.SeparateDatabaseAndState(
|
|
|
|
state_operations=state_operations,
|
|
|
|
database_operations=database_operations,
|
|
|
|
)
|
|
|
|
# Test the state alteration
|
|
|
|
new_state = project_state.clone()
|
|
|
|
operation.state_forwards(app_label, new_state)
|
|
|
|
|
|
|
|
def assertModelsAndTables(after_db):
|
2016-10-27 15:53:39 +08:00
|
|
|
# Tables and models exist, or don't, as they should:
|
2015-12-09 01:03:31 +08:00
|
|
|
self.assertNotIn((app_label, "somethingelse"), new_state.models)
|
|
|
|
self.assertEqual(len(new_state.models[app_label, "somethingcompletelydifferent"].fields), 1)
|
|
|
|
self.assertNotIn((app_label, "iloveponiesonies"), new_state.models)
|
|
|
|
self.assertNotIn((app_label, "ilovemoreponies"), new_state.models)
|
|
|
|
self.assertNotIn((app_label, "iloveevenmoreponies"), new_state.models)
|
|
|
|
self.assertTableNotExists("somethingelse")
|
|
|
|
self.assertTableNotExists("somethingcompletelydifferent")
|
|
|
|
self.assertTableNotExists("ilovemoreponies")
|
|
|
|
if after_db:
|
|
|
|
self.assertTableExists("iloveponies")
|
|
|
|
self.assertTableExists("iloveevenmoreponies")
|
|
|
|
else:
|
|
|
|
self.assertTableNotExists("iloveponies")
|
|
|
|
self.assertTableNotExists("iloveevenmoreponies")
|
|
|
|
|
|
|
|
assertModelsAndTables(after_db=False)
|
|
|
|
# Test the database alteration
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
operation.database_forwards(app_label, editor, project_state, new_state)
|
|
|
|
assertModelsAndTables(after_db=True)
|
|
|
|
# And test reversal
|
|
|
|
self.assertTrue(operation.reversible)
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
operation.database_backwards(app_label, editor, new_state, project_state)
|
|
|
|
assertModelsAndTables(after_db=False)
|
|
|
|
|
2014-05-08 05:28:34 +08:00
|
|
|
|
2014-06-18 08:45:38 +08:00
|
|
|
class SwappableOperationTests(OperationTestBase):
|
|
|
|
"""
|
2016-10-27 15:53:39 +08:00
|
|
|
Key operations ignore swappable models
|
2014-06-18 08:45:38 +08:00
|
|
|
(we don't want to replicate all of them here, as the functionality
|
|
|
|
is in a common base class anyway)
|
|
|
|
"""
|
|
|
|
|
2017-01-12 12:09:41 +08:00
|
|
|
available_apps = ['migrations']
|
2014-06-18 08:45:38 +08:00
|
|
|
|
|
|
|
@override_settings(TEST_SWAP_MODEL="migrations.SomeFakeModel")
|
|
|
|
def test_create_ignore_swapped(self):
|
|
|
|
"""
|
2016-10-27 15:53:39 +08:00
|
|
|
The CreateTable operation ignores swapped models.
|
2014-06-18 08:45:38 +08:00
|
|
|
"""
|
|
|
|
operation = migrations.CreateModel(
|
|
|
|
"Pony",
|
|
|
|
[
|
|
|
|
("id", models.AutoField(primary_key=True)),
|
|
|
|
("pink", models.IntegerField(default=1)),
|
|
|
|
],
|
2014-06-18 22:47:13 +08:00
|
|
|
options={
|
2014-06-18 08:45:38 +08:00
|
|
|
"swappable": "TEST_SWAP_MODEL",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
# Test the state alteration (it should still be there!)
|
|
|
|
project_state = ProjectState()
|
|
|
|
new_state = project_state.clone()
|
|
|
|
operation.state_forwards("test_crigsw", new_state)
|
|
|
|
self.assertEqual(new_state.models["test_crigsw", "pony"].name, "Pony")
|
|
|
|
self.assertEqual(len(new_state.models["test_crigsw", "pony"].fields), 2)
|
|
|
|
# Test the database alteration
|
|
|
|
self.assertTableNotExists("test_crigsw_pony")
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
operation.database_forwards("test_crigsw", editor, project_state, new_state)
|
|
|
|
self.assertTableNotExists("test_crigsw_pony")
|
|
|
|
# And test reversal
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
operation.database_backwards("test_crigsw", editor, new_state, project_state)
|
|
|
|
self.assertTableNotExists("test_crigsw_pony")
|
|
|
|
|
|
|
|
@override_settings(TEST_SWAP_MODEL="migrations.SomeFakeModel")
|
|
|
|
def test_delete_ignore_swapped(self):
|
|
|
|
"""
|
|
|
|
Tests the DeleteModel operation ignores swapped models.
|
|
|
|
"""
|
|
|
|
operation = migrations.DeleteModel("Pony")
|
|
|
|
project_state, new_state = self.make_test_state("test_dligsw", operation)
|
|
|
|
# Test the database alteration
|
|
|
|
self.assertTableNotExists("test_dligsw_pony")
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
operation.database_forwards("test_dligsw", editor, project_state, new_state)
|
|
|
|
self.assertTableNotExists("test_dligsw_pony")
|
|
|
|
# And test reversal
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
operation.database_backwards("test_dligsw", editor, new_state, project_state)
|
|
|
|
self.assertTableNotExists("test_dligsw_pony")
|
|
|
|
|
|
|
|
@override_settings(TEST_SWAP_MODEL="migrations.SomeFakeModel")
|
|
|
|
def test_add_field_ignore_swapped(self):
|
|
|
|
"""
|
|
|
|
Tests the AddField operation.
|
|
|
|
"""
|
|
|
|
# Test the state alteration
|
|
|
|
operation = migrations.AddField(
|
|
|
|
"Pony",
|
|
|
|
"height",
|
|
|
|
models.FloatField(null=True, default=5),
|
|
|
|
)
|
|
|
|
project_state, new_state = self.make_test_state("test_adfligsw", operation)
|
|
|
|
# Test the database alteration
|
2014-08-07 09:52:03 +08:00
|
|
|
self.assertTableNotExists("test_adfligsw_pony")
|
2014-06-18 08:45:38 +08:00
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
operation.database_forwards("test_adfligsw", editor, project_state, new_state)
|
2014-08-07 09:52:03 +08:00
|
|
|
self.assertTableNotExists("test_adfligsw_pony")
|
2014-06-18 08:45:38 +08:00
|
|
|
# And test reversal
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
operation.database_backwards("test_adfligsw", editor, new_state, project_state)
|
2014-08-07 09:52:03 +08:00
|
|
|
self.assertTableNotExists("test_adfligsw_pony")
|
2016-07-07 14:32:00 +08:00
|
|
|
|
|
|
|
@override_settings(TEST_SWAP_MODEL='migrations.SomeFakeModel')
|
|
|
|
def test_indexes_ignore_swapped(self):
|
|
|
|
"""
|
|
|
|
Add/RemoveIndex operations ignore swapped models.
|
|
|
|
"""
|
|
|
|
operation = migrations.AddIndex('Pony', models.Index(fields=['pink'], name='my_name_idx'))
|
|
|
|
project_state, new_state = self.make_test_state('test_adinigsw', operation)
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
# No database queries should be run for swapped models
|
|
|
|
operation.database_forwards('test_adinigsw', editor, project_state, new_state)
|
|
|
|
operation.database_backwards('test_adinigsw', editor, new_state, project_state)
|
|
|
|
|
|
|
|
operation = migrations.RemoveIndex('Pony', models.Index(fields=['pink'], name='my_name_idx'))
|
|
|
|
project_state, new_state = self.make_test_state("test_rminigsw", operation)
|
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
operation.database_forwards('test_rminigsw', editor, project_state, new_state)
|
|
|
|
operation.database_backwards('test_rminigsw', editor, new_state, project_state)
|
2016-09-28 22:04:52 +08:00
|
|
|
|
|
|
|
|
|
|
|
class TestCreateModel(SimpleTestCase):
|
|
|
|
|
|
|
|
def test_references_model_mixin(self):
|
2020-04-20 18:44:28 +08:00
|
|
|
migrations.CreateModel(
|
|
|
|
'name',
|
|
|
|
fields=[],
|
|
|
|
bases=(Mixin, models.Model),
|
|
|
|
).references_model('other_model', 'migrations')
|
2017-02-03 14:32:53 +08:00
|
|
|
|
|
|
|
|
|
|
|
class FieldOperationTests(SimpleTestCase):
|
|
|
|
def test_references_model(self):
|
2017-02-17 01:50:43 +08:00
|
|
|
operation = FieldOperation('MoDel', 'field', models.ForeignKey('Other', models.CASCADE))
|
2017-02-03 14:32:53 +08:00
|
|
|
# Model name match.
|
2020-04-04 11:05:16 +08:00
|
|
|
self.assertIs(operation.references_model('mOdEl', 'migrations'), True)
|
2017-02-03 14:32:53 +08:00
|
|
|
# Referenced field.
|
2020-04-04 11:05:16 +08:00
|
|
|
self.assertIs(operation.references_model('oTher', 'migrations'), True)
|
2017-02-03 14:32:53 +08:00
|
|
|
# Doesn't reference.
|
2020-04-04 11:05:16 +08:00
|
|
|
self.assertIs(operation.references_model('Whatever', 'migrations'), False)
|
2017-02-03 14:32:53 +08:00
|
|
|
|
|
|
|
def test_references_field_by_name(self):
|
|
|
|
operation = FieldOperation('MoDel', 'field', models.BooleanField(default=False))
|
2020-04-04 11:05:16 +08:00
|
|
|
self.assertIs(operation.references_field('model', 'field', 'migrations'), True)
|
2017-02-03 14:32:53 +08:00
|
|
|
|
|
|
|
def test_references_field_by_remote_field_model(self):
|
|
|
|
operation = FieldOperation('Model', 'field', models.ForeignKey('Other', models.CASCADE))
|
2020-04-04 11:05:16 +08:00
|
|
|
self.assertIs(operation.references_field('Other', 'whatever', 'migrations'), True)
|
|
|
|
self.assertIs(operation.references_field('Missing', 'whatever', 'migrations'), False)
|
2017-02-03 14:32:53 +08:00
|
|
|
|
|
|
|
def test_references_field_by_from_fields(self):
|
|
|
|
operation = FieldOperation(
|
|
|
|
'Model', 'field', models.fields.related.ForeignObject('Other', models.CASCADE, ['from'], ['to'])
|
|
|
|
)
|
2020-04-04 11:05:16 +08:00
|
|
|
self.assertIs(operation.references_field('Model', 'from', 'migrations'), True)
|
|
|
|
self.assertIs(operation.references_field('Model', 'to', 'migrations'), False)
|
|
|
|
self.assertIs(operation.references_field('Other', 'from', 'migrations'), False)
|
|
|
|
self.assertIs(operation.references_field('Model', 'to', 'migrations'), False)
|
2017-02-03 14:32:53 +08:00
|
|
|
|
|
|
|
def test_references_field_by_to_fields(self):
|
|
|
|
operation = FieldOperation('Model', 'field', models.ForeignKey('Other', models.CASCADE, to_field='field'))
|
2020-04-04 11:05:16 +08:00
|
|
|
self.assertIs(operation.references_field('Other', 'field', 'migrations'), True)
|
|
|
|
self.assertIs(operation.references_field('Other', 'whatever', 'migrations'), False)
|
|
|
|
self.assertIs(operation.references_field('Missing', 'whatever', 'migrations'), False)
|
2017-02-03 14:32:53 +08:00
|
|
|
|
|
|
|
def test_references_field_by_through(self):
|
|
|
|
operation = FieldOperation('Model', 'field', models.ManyToManyField('Other', through='Through'))
|
2020-04-04 11:05:16 +08:00
|
|
|
self.assertIs(operation.references_field('Other', 'whatever', 'migrations'), True)
|
|
|
|
self.assertIs(operation.references_field('Through', 'whatever', 'migrations'), True)
|
|
|
|
self.assertIs(operation.references_field('Missing', 'whatever', 'migrations'), False)
|
2017-02-03 14:32:53 +08:00
|
|
|
|
|
|
|
def test_reference_field_by_through_fields(self):
|
|
|
|
operation = FieldOperation(
|
|
|
|
'Model', 'field', models.ManyToManyField('Other', through='Through', through_fields=('first', 'second'))
|
|
|
|
)
|
2020-04-04 11:05:16 +08:00
|
|
|
self.assertIs(operation.references_field('Other', 'whatever', 'migrations'), True)
|
|
|
|
self.assertIs(operation.references_field('Through', 'whatever', 'migrations'), False)
|
|
|
|
self.assertIs(operation.references_field('Through', 'first', 'migrations'), True)
|
|
|
|
self.assertIs(operation.references_field('Through', 'second', 'migrations'), True)
|