|
@@ -2651,9 +2651,13 @@ class OperationTests(OperationTestBase):
|
|
|
fill_data.state_forwards("fill_data", new_state)
|
|
|
fill_data.database_forwards("fill_data", editor, project_state, new_state)
|
|
|
|
|
|
- def test_autofield_foreignfield_growth(self):
|
|
|
+ def _test_autofield_foreignfield_growth(self, source_field, target_field, target_value):
|
|
|
"""
|
|
|
- A field may be migrated from AutoField to BigAutoField.
|
|
|
+ A field may be migrated in the following ways:
|
|
|
+
|
|
|
+ - AutoField to BigAutoField
|
|
|
+ - SmallAutoField to AutoField
|
|
|
+ - SmallAutoField to BigAutoField
|
|
|
"""
|
|
|
def create_initial_data(models, schema_editor):
|
|
|
Article = models.get_model("test_article", "Article")
|
|
@@ -2665,14 +2669,14 @@ class OperationTests(OperationTestBase):
|
|
|
def create_big_data(models, schema_editor):
|
|
|
Article = models.get_model("test_article", "Article")
|
|
|
Blog = models.get_model("test_blog", "Blog")
|
|
|
- blog2 = Blog.objects.create(name="Frameworks", id=2 ** 33)
|
|
|
+ blog2 = Blog.objects.create(name="Frameworks", id=target_value)
|
|
|
Article.objects.create(name="Django", blog=blog2)
|
|
|
- Article.objects.create(id=2 ** 33, name="Django2", blog=blog2)
|
|
|
+ Article.objects.create(id=target_value, name="Django2", blog=blog2)
|
|
|
|
|
|
create_blog = migrations.CreateModel(
|
|
|
"Blog",
|
|
|
[
|
|
|
- ("id", models.AutoField(primary_key=True)),
|
|
|
+ ("id", source_field(primary_key=True)),
|
|
|
("name", models.CharField(max_length=100)),
|
|
|
],
|
|
|
options={},
|
|
@@ -2680,7 +2684,7 @@ class OperationTests(OperationTestBase):
|
|
|
create_article = migrations.CreateModel(
|
|
|
"Article",
|
|
|
[
|
|
|
- ("id", models.AutoField(primary_key=True)),
|
|
|
+ ("id", source_field(primary_key=True)),
|
|
|
("blog", models.ForeignKey(to="test_blog.Blog", on_delete=models.CASCADE)),
|
|
|
("name", models.CharField(max_length=100)),
|
|
|
("data", models.TextField(default="")),
|
|
@@ -2690,8 +2694,8 @@ class OperationTests(OperationTestBase):
|
|
|
fill_initial_data = migrations.RunPython(create_initial_data, create_initial_data)
|
|
|
fill_big_data = migrations.RunPython(create_big_data, create_big_data)
|
|
|
|
|
|
- grow_article_id = migrations.AlterField("Article", "id", models.BigAutoField(primary_key=True))
|
|
|
- grow_blog_id = migrations.AlterField("Blog", "id", models.BigAutoField(primary_key=True))
|
|
|
+ grow_article_id = migrations.AlterField('Article', 'id', target_field(primary_key=True))
|
|
|
+ grow_blog_id = migrations.AlterField('Blog', 'id', target_field(primary_key=True))
|
|
|
|
|
|
project_state = ProjectState()
|
|
|
new_state = project_state.clone()
|
|
@@ -2719,7 +2723,7 @@ class OperationTests(OperationTestBase):
|
|
|
|
|
|
state = new_state.clone()
|
|
|
article = state.apps.get_model("test_article.Article")
|
|
|
- self.assertIsInstance(article._meta.pk, models.BigAutoField)
|
|
|
+ self.assertIsInstance(article._meta.pk, target_field)
|
|
|
|
|
|
project_state = new_state
|
|
|
new_state = new_state.clone()
|
|
@@ -2729,7 +2733,7 @@ class OperationTests(OperationTestBase):
|
|
|
|
|
|
state = new_state.clone()
|
|
|
blog = state.apps.get_model("test_blog.Blog")
|
|
|
- self.assertIsInstance(blog._meta.pk, models.BigAutoField)
|
|
|
+ self.assertIsInstance(blog._meta.pk, target_field)
|
|
|
|
|
|
project_state = new_state
|
|
|
new_state = new_state.clone()
|
|
@@ -2737,6 +2741,30 @@ class OperationTests(OperationTestBase):
|
|
|
fill_big_data.state_forwards("fill_big_data", new_state)
|
|
|
fill_big_data.database_forwards("fill_big_data", editor, project_state, new_state)
|
|
|
|
|
|
+ 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,
|
|
|
+ )
|
|
|
+
|
|
|
def test_run_python_noop(self):
|
|
|
"""
|
|
|
#24098 - Tests no-op RunPython operations.
|