Beispiel #1
0
 def test_optimize_through_fields(self):
     """
     field-level through checking is working. This should manage to collapse
     model Foo to nonexistence, and model Bar to a single IntegerField
     called "width".
     """
     self.assertOptimizesTo(
         [
             migrations.CreateModel(
                 "Foo", [("name", models.CharField(max_length=255))]),
             migrations.CreateModel("Bar",
                                    [("size", models.IntegerField())]),
             migrations.AddField("Foo", "age", models.IntegerField()),
             migrations.AddField("Bar", "width", models.IntegerField()),
             migrations.AlterField("Foo", "age", models.IntegerField()),
             migrations.RenameField("Bar", "size", "dimensions"),
             migrations.RemoveField("Foo", "age"),
             migrations.RenameModel("Foo", "Phou"),
             migrations.RemoveField("Bar", "dimensions"),
             migrations.RenameModel("Phou", "Fou"),
             migrations.DeleteModel("Fou"),
         ],
         [
             migrations.CreateModel("Bar",
                                    [("width", models.IntegerField())]),
         ],
     )
Beispiel #2
0
 def test_create_model_alter_field(self):
     """
     AlterField should optimize into CreateModel.
     """
     managers = [('objects', EmptyManager())]
     self.assertOptimizesTo(
         [
             migrations.CreateModel(
                 name="Foo",
                 fields=[("name", models.CharField(max_length=255))],
                 options={'verbose_name': 'Foo'},
                 bases=(UnicodeModel, ),
                 managers=managers,
             ),
             migrations.AlterField("Foo", "name", models.IntegerField()),
         ],
         [
             migrations.CreateModel(
                 name="Foo",
                 fields=[
                     ("name", models.IntegerField()),
                 ],
                 options={'verbose_name': 'Foo'},
                 bases=(UnicodeModel, ),
                 managers=managers,
             ),
         ],
     )
class Migration(migrations.Migration):

    dependencies = [
        ('contenttypes', '0001_initial'),
    ]

    operations = [
        migrations.AlterModelOptions(
            name='contenttype',
            options={
                'verbose_name': 'content type',
                'verbose_name_plural': 'content types'
            },
        ),
        migrations.AlterField(
            model_name='contenttype',
            name='name',
            field=models.CharField(max_length=100, null=True),
        ),
        migrations.RunPython(
            migrations.RunPython.noop,
            add_legacy_name,
            hints={'model_name': 'contenttype'},
        ),
        migrations.RemoveField(
            model_name='contenttype',
            name='name',
        ),
    ]
Beispiel #4
0
 def test_alter_field_rename_field(self):
     """
     RenameField should optimize to the other side of AlterField,
     and into itself.
     """
     self.assertOptimizesTo(
         [
             migrations.AlterField("Foo", "name",
                                   models.CharField(max_length=255)),
             migrations.RenameField("Foo", "name", "title"),
             migrations.RenameField("Foo", "title", "nom"),
         ],
         [
             migrations.RenameField("Foo", "name", "nom"),
             migrations.AlterField("Foo", "nom",
                                   models.CharField(max_length=255)),
         ],
     )
Beispiel #5
0
 def test_alter_field_delete_field(self):
     """
     RemoveField should absorb AlterField
     """
     self.assertOptimizesTo(
         [
             migrations.AlterField("Foo", "age", models.IntegerField()),
             migrations.RemoveField("Foo", "age"),
         ],
         [
             migrations.RemoveField("Foo", "age"),
         ],
     )
Beispiel #6
0
class Migration(migrations.Migration):

    dependencies = [
        ('author_app', '0001_initial'),
        ('book_app', '0001_initial'),  # Forces the book table to alter the FK
    ]

    operations = [
        migrations.AlterField(
            model_name='author',
            name='id',
            field=models.CharField(max_length=10, primary_key=True),
        ),
    ]
Beispiel #7
0
 def test_add_field_alter_field(self):
     """
     AlterField should optimize into AddField.
     """
     self.assertOptimizesTo(
         [
             migrations.AddField("Foo", "age", models.IntegerField()),
             migrations.AlterField("Foo", "age",
                                   models.FloatField(default=2.4)),
         ],
         [
             migrations.AddField(
                 "Foo", name="age", field=models.FloatField(default=2.4)),
         ],
     )
Beispiel #8
0
    def _test_create_alter_foo_field(self, alter):
        """
        CreateModel, AlterFooTogether/AlterOrderWithRespectTo followed by an
        add/alter/rename field should optimize to CreateModel with options.
        """
        option_value = getattr(alter, alter.option_name)
        options = {alter.option_name: option_value}

        # AddField
        self.assertOptimizesTo(
            [
                migrations.CreateModel("Foo", [
                    ("a", models.IntegerField()),
                    ("b", models.IntegerField()),
                ]),
                alter,
                migrations.AddField("Foo", "c", models.IntegerField()),
            ],
            [
                migrations.CreateModel("Foo", [
                    ("a", models.IntegerField()),
                    ("b", models.IntegerField()),
                    ("c", models.IntegerField()),
                ],
                                       options=options),
            ],
        )

        # AlterField
        self.assertOptimizesTo(
            [
                migrations.CreateModel("Foo", [
                    ("a", models.IntegerField()),
                    ("b", models.IntegerField()),
                ]),
                alter,
                migrations.AlterField("Foo", "b",
                                      models.CharField(max_length=255)),
            ],
            [
                migrations.CreateModel("Foo", [
                    ("a", models.IntegerField()),
                    ("b", models.CharField(max_length=255)),
                ],
                                       options=options),
            ],
        )

        self.assertOptimizesTo(
            [
                migrations.CreateModel("Foo", [
                    ("a", models.IntegerField()),
                    ("b", models.IntegerField()),
                    ("c", models.IntegerField()),
                ]),
                alter,
                migrations.AlterField("Foo", "c",
                                      models.CharField(max_length=255)),
            ],
            [
                migrations.CreateModel("Foo", [
                    ("a", models.IntegerField()),
                    ("b", models.IntegerField()),
                    ("c", models.CharField(max_length=255)),
                ],
                                       options=options),
            ],
        )

        # RenameField
        if isinstance(option_value, str):
            renamed_options = {alter.option_name: 'c'}
        else:
            renamed_options = {
                alter.option_name: {
                    tuple('c' if value == 'b' else value for value in item)
                    for item in option_value
                }
            }
        self.assertOptimizesTo(
            [
                migrations.CreateModel("Foo", [
                    ("a", models.IntegerField()),
                    ("b", models.IntegerField()),
                ]),
                alter,
                migrations.RenameField("Foo", "b", "c"),
            ],
            [
                migrations.CreateModel("Foo", [
                    ("a", models.IntegerField()),
                    ("c", models.IntegerField()),
                ],
                                       options=renamed_options),
            ],
        )

        self.assertOptimizesTo(
            [
                migrations.CreateModel("Foo", [
                    ("a", models.IntegerField()),
                    ("b", models.IntegerField()),
                ]),
                alter,
                migrations.RenameField("Foo", "b", "x"),
                migrations.RenameField("Foo", "x", "c"),
            ],
            [
                migrations.CreateModel("Foo", [
                    ("a", models.IntegerField()),
                    ("c", models.IntegerField()),
                ],
                                       options=renamed_options),
            ],
        )

        self.assertOptimizesTo(
            [
                migrations.CreateModel("Foo", [
                    ("a", models.IntegerField()),
                    ("b", models.IntegerField()),
                    ("c", models.IntegerField()),
                ]),
                alter,
                migrations.RenameField("Foo", "c", "d"),
            ],
            [
                migrations.CreateModel("Foo", [
                    ("a", models.IntegerField()),
                    ("b", models.IntegerField()),
                    ("d", models.IntegerField()),
                ],
                                       options=options),
            ],
        )

        # RemoveField
        if isinstance(option_value, str):
            removed_options = None
        else:
            removed_options = {
                alter.option_name: {
                    tuple(value for value in item if value != 'b')
                    for item in option_value
                }
            }
        self.assertOptimizesTo([
            migrations.CreateModel("Foo", [
                ("a", models.IntegerField()),
                ("b", models.IntegerField()),
            ]),
            alter,
            migrations.RemoveField("Foo", "b"),
        ], [
            migrations.CreateModel("Foo", [
                ("a", models.IntegerField()),
            ],
                                   options=removed_options),
        ])

        self.assertOptimizesTo(
            [
                migrations.CreateModel("Foo", [
                    ("a", models.IntegerField()),
                    ("b", models.IntegerField()),
                    ("c", models.IntegerField()),
                ]),
                alter,
                migrations.RemoveField("Foo", "c"),
            ],
            [
                migrations.CreateModel("Foo", [
                    ("a", models.IntegerField()),
                    ("b", models.IntegerField()),
                ],
                                       options=options),
            ],
        )