Esempio n. 1
0
 def test_create_rename_model(self):
     """
     CreateModel should absorb RenameModels.
     """
     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.RenameModel("Foo", "Bar"),
         ],
         [
             migrations.CreateModel(
                 "Bar",
                 [("name", models.CharField(max_length=255))],
                 options={'verbose_name': 'Foo'},
                 bases=(UnicodeModel, ),
                 managers=managers,
             )
         ],
     )
Esempio n. 2
0
 def test_create_model_reordering_circular_fk(self):
     """
     CreateModel reordering behavior doesn't result in an infinite loop if
     there are FKs in both directions.
     """
     self.assertOptimizesTo(
         [
             migrations.CreateModel('Bar', [('url', models.TextField())]),
             migrations.CreateModel(
                 'Foo', [('name', models.CharField(max_length=255))]),
             migrations.AddField(
                 'Bar', 'foo_fk',
                 models.ForeignKey('migrations.Foo', models.CASCADE)),
             migrations.AddField(
                 'Foo', 'bar_fk',
                 models.ForeignKey('migrations.Bar', models.CASCADE)),
         ],
         [
             migrations.CreateModel(
                 'Foo', [('name', models.CharField(max_length=255))]),
             migrations.CreateModel('Bar', [
                 ('url', models.TextField()),
                 ('foo_fk',
                  models.ForeignKey('migrations.Foo', models.CASCADE)),
             ]),
             migrations.AddField(
                 'Foo', 'bar_fk',
                 models.ForeignKey('migrations.Foo', models.CASCADE)),
         ],
     )
Esempio n. 3
0
class Migration(migrations.Migration):

    initial = True

    operations = [
        migrations.CreateModel(
            "Author",
            [
                ("id", models.AutoField(primary_key=True)),
                ("name", models.CharField(max_length=255)),
                ("slug", models.SlugField(null=True)),
                ("age", models.IntegerField(default=0)),
                ("silly_field", models.BooleanField(default=False)),
            ],
        ),
        migrations.CreateModel(
            "Tribble",
            [
                ("id", models.AutoField(primary_key=True)),
                ("fluffy", models.BooleanField(default=True)),
            ],
        ),
        migrations.AlterUniqueTogether(
            name='author',
            unique_together={('name', 'slug')},
        ),
    ]
Esempio n. 4
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())]),
         ],
     )
Esempio n. 5
0
class Migration(migrations.Migration):

    initial = True

    dependencies = [
    ]

    operations = [
        migrations.CreateModel(
            name='Project',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
            ],
        ),
        migrations.CreateModel(
            name='Task',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
            ],
        ),
        migrations.AddField(
            model_name='project',
            name='tasks',
            field=models.ManyToManyField(to='Task'),
        ),
    ]
Esempio n. 6
0
class Migration(migrations.Migration):

    replaces = [
        ("migrations", "0001_initial"),
        ("migrations", "0002_second"),
    ]

    operations = [

        migrations.CreateModel(
            "Author",
            [
                ("id", models.AutoField(primary_key=True)),
                ("name", models.CharField(max_length=255)),
                ("slug", models.SlugField(null=True)),
                ("age", models.IntegerField(default=0)),
                ("rating", models.IntegerField(default=0)),
            ],
        ),

        migrations.CreateModel(
            "Book",
            [
                ("id", models.AutoField(primary_key=True)),
                ("author", models.ForeignKey("migrations.Author", models.SET_NULL, null=True)),
            ],
        ),

    ]
Esempio n. 7
0
class Migration(migrations.Migration):

    dependencies = [
        ('migrations', '0002_second'),
    ]

    operations = [
        migrations.CreateModel(
            name='ModelWithCustomBase',
            fields=[
                ('id',
                 models.AutoField(verbose_name='ID',
                                  serialize=False,
                                  auto_created=True,
                                  primary_key=True)),
            ],
            options={},
            bases=(models.Model, ),
        ),
        migrations.CreateModel(
            name='UnmigratedModel',
            fields=[
                ('id',
                 models.AutoField(verbose_name='ID',
                                  serialize=False,
                                  auto_created=True,
                                  primary_key=True)),
            ],
            options={},
            bases=(models.Model, ),
        ),
        migrations.DeleteModel(name='Author', ),
        migrations.DeleteModel(name='Book', ),
    ]
Esempio n. 8
0
 def test_create_model_remove_field(self):
     """
     RemoveField should optimize into CreateModel.
     """
     managers = [('objects', EmptyManager())]
     self.assertOptimizesTo(
         [
             migrations.CreateModel(
                 name="Foo",
                 fields=[
                     ("name", models.CharField(max_length=255)),
                     ("age", models.IntegerField()),
                 ],
                 options={'verbose_name': 'Foo'},
                 bases=(UnicodeModel, ),
                 managers=managers,
             ),
             migrations.RemoveField("Foo", "age"),
         ],
         [
             migrations.CreateModel(
                 name="Foo",
                 fields=[
                     ("name", models.CharField(max_length=255)),
                 ],
                 options={'verbose_name': 'Foo'},
                 bases=(UnicodeModel, ),
                 managers=managers,
             ),
         ],
     )
Esempio n. 9
0
 def test_create_alter_model_options(self):
     self.assertOptimizesTo([
         migrations.CreateModel('Foo', fields=[]),
         migrations.AlterModelOptions(
             name='Foo', options={'verbose_name_plural': 'Foozes'}),
     ], [
         migrations.CreateModel(
             'Foo', fields=[], options={'verbose_name_plural': 'Foozes'}),
     ])
Esempio n. 10
0
class Migration(migrations.Migration):

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

    operations = [
        migrations.CreateModel(
            name='CustomArticle',
            fields=[
                ('id',
                 models.AutoField(verbose_name='ID',
                                  serialize=False,
                                  auto_created=True,
                                  primary_key=True)),
                ('title', models.CharField(max_length=50)),
                ('places_this_article_should_appear',
                 models.ForeignKey('sites.Site', models.CASCADE)),
            ],
            options={
                'abstract': False,
            },
            bases=(models.Model, ),
        ),
        migrations.CreateModel(
            name='ExclusiveArticle',
            fields=[
                ('id',
                 models.AutoField(verbose_name='ID',
                                  serialize=False,
                                  auto_created=True,
                                  primary_key=True)),
                ('title', models.CharField(max_length=50)),
                ('site', models.ForeignKey('sites.Site', models.CASCADE)),
            ],
            options={
                'abstract': False,
            },
            bases=(models.Model, ),
        ),
        migrations.CreateModel(
            name='SyndicatedArticle',
            fields=[
                ('id',
                 models.AutoField(verbose_name='ID',
                                  serialize=False,
                                  auto_created=True,
                                  primary_key=True)),
                ('title', models.CharField(max_length=50)),
                ('sites', models.ManyToManyField('sites.Site')),
            ],
            options={
                'abstract': False,
            },
            bases=(models.Model, ),
        ),
    ]
Esempio n. 11
0
 def test_create_model_no_reordering_for_unrelated_fk(self):
     """
     CreateModel order remains unchanged if the later AddField operation
     isn't a FK between them.
     """
     self.assertDoesNotOptimize([
         migrations.CreateModel(
             'Foo', [('name', models.CharField(max_length=255))]),
         migrations.CreateModel('Link', [('url', models.TextField())]),
         migrations.AddField(
             'Other', 'link',
             models.ForeignKey('migrations.Link', models.CASCADE)),
     ], )
Esempio n. 12
0
class Migration(migrations.Migration):
    """
    This is a wee bit crazy, but it's just to show that run_before works.
    """

    dependencies = [
        ("migrations", "0001_initial"),
    ]

    run_before = [
        ("migrations", "0002_second"),
    ]

    operations = [

        migrations.CreateModel(
            "Author",
            [
                ("id", models.AutoField(primary_key=True)),
                ("name", models.CharField(max_length=255)),
                ("slug", models.SlugField(null=True)),
                ("age", models.IntegerField(default=0)),
            ],
        )

    ]
Esempio n. 13
0
class Migration(migrations.Migration):

    dependencies = []

    operations = [
        migrations.CreateModel(
            name='CharTextArrayIndexModel',
            fields=[
                ('id',
                 models.AutoField(verbose_name='ID',
                                  serialize=False,
                                  auto_created=True,
                                  primary_key=True)),
                ('char',
                 djmodels.contrib.postgres.fields.ArrayField(
                     models.CharField(max_length=10), db_index=True,
                     size=100)),
                ('char2', models.CharField(max_length=11, db_index=True)),
                ('text',
                 djmodels.contrib.postgres.fields.ArrayField(
                     models.TextField(), db_index=True)),
            ],
            options={},
            bases=(models.Model, ),
        ),
    ]
Esempio n. 14
0
 def set_up_test_model(self, force_raster_creation=False):
     test_fields = [('id', models.AutoField(primary_key=True)),
                    ('name', models.CharField(max_length=100, unique=True)),
                    ('geom', fields.MultiPolygonField(srid=4326))]
     if connection.features.supports_raster or force_raster_creation:
         test_fields += [('rast', fields.RasterField(srid=4326, null=True))]
     operations = [migrations.CreateModel('Neighborhood', test_fields)]
     self.current_state = self.apply_operations('gis', ProjectState(),
                                                operations)
Esempio n. 15
0
class Migration(migrations.Migration):

    operations = [
        migrations.CreateModel(
            'Foo',
            [
                ('id', models.AutoField(primary_key=True)),
            ],
        ),
    ]
Esempio n. 16
0
class Migration(migrations.Migration):

    operations = [
        migrations.CreateModel(
            "Signal",
            [
                ("id", models.AutoField(primary_key=True)),
            ],
        ),
    ]
Esempio n. 17
0
class Migration(migrations.Migration):
    atomic = False

    operations = [
        migrations.CreateModel(
            "Publisher",
            [
                ("name", models.CharField(primary_key=True, max_length=255)),
            ],
        ),
        migrations.RunPython(raise_error),
        migrations.CreateModel(
            "Book",
            [
                ("title", models.CharField(primary_key=True, max_length=255)),
                ("publisher",
                 models.ForeignKey(
                     "migrations.Publisher", models.SET_NULL, null=True)),
            ],
        ),
    ]
Esempio n. 18
0
class Migration(migrations.Migration):

    operations = [
        migrations.CreateModel(
            "Author",
            [
                ("id", models.AutoField(primary_key=True)),
                ("name", models.CharField(max_length=255)),
                ("slug", models.SlugField(null=True)),
                ("age", models.IntegerField(default=0)),
                ("silly_field", models.BooleanField(default=False)),
            ],
        ),
        migrations.CreateModel(
            "Tribble",
            [
                ("id", models.AutoField(primary_key=True)),
                ("fluffy", models.BooleanField(default=True)),
            ],
        )
    ]
Esempio n. 19
0
class Migration(migrations.Migration):

    dependencies = [("migrations", "0001_initial")]

    operations = [
        migrations.CreateModel(
            "Something",
            [
                ("id", models.AutoField(primary_key=True)),
            ],
        )
    ]
Esempio n. 20
0
class Migration(migrations.Migration):

    operations = [
        migrations.CreateModel(
            "Salamander",
            [
                ("id", models.AutoField(primary_key=True)),
                ("size", models.IntegerField(default=0)),
                ("silly_field", models.BooleanField(default=False)),
            ],
        ),
    ]
Esempio n. 21
0
class Migration(migrations.Migration):

    dependencies = [("unspecified_app_with_conflict", "0001_initial")]

    operations = [
        migrations.CreateModel(
            "Something",
            [
                ("id", models.AutoField(primary_key=True)),
            ],
        )
    ]
Esempio n. 22
0
 def test_create_model_add_field_not_through_m2m_through(self):
     """
     AddField should NOT optimize into CreateModel if it's an M2M using a
     through that's created between them.
     """
     self.assertDoesNotOptimize([
         migrations.CreateModel('Employee', []),
         migrations.CreateModel('Employer', []),
         migrations.CreateModel('Employment', [
             ('employee',
              models.ForeignKey('migrations.Employee', models.CASCADE)),
             ('employment',
              models.ForeignKey('migrations.Employer', models.CASCADE)),
         ]),
         migrations.AddField(
             'Employer', 'employees',
             models.ManyToManyField(
                 'migrations.Employee',
                 through='migrations.Employment',
             )),
     ], )
Esempio n. 23
0
 def test_optimize_elidable_operation(self):
     elidable_operation = operations.base.Operation()
     elidable_operation.elidable = True
     self.assertOptimizesTo(
         [
             elidable_operation,
             migrations.CreateModel(
                 "Foo", [("name", models.CharField(max_length=255))]),
             elidable_operation,
             migrations.CreateModel("Bar",
                                    [("size", models.IntegerField())]),
             elidable_operation,
             migrations.RenameModel("Foo", "Phou"),
             migrations.DeleteModel("Bar"),
             elidable_operation,
         ],
         [
             migrations.CreateModel(
                 "Phou", [("name", models.CharField(max_length=255))]),
         ],
     )
Esempio n. 24
0
 def test_create_delete_model(self):
     """
     CreateModel and DeleteModel should collapse into nothing.
     """
     self.assertOptimizesTo(
         [
             migrations.CreateModel(
                 "Foo", [("name", models.CharField(max_length=255))]),
             migrations.DeleteModel("Foo"),
         ],
         [],
     )
Esempio n. 25
0
    def test_simple_migration(self):
        """
        Tests serializing a simple migration.
        """
        fields = {
            'charfield':
            models.DateTimeField(default=datetime.datetime.utcnow),
            'datetimefield':
            models.DateTimeField(default=datetime.datetime.utcnow),
        }

        options = {
            'verbose_name': 'My model',
            'verbose_name_plural': 'My models',
        }

        migration = type(
            "Migration", (migrations.Migration, ), {
                "operations": [
                    migrations.CreateModel("MyModel", tuple(fields.items()),
                                           options, (models.Model, )),
                    migrations.CreateModel("MyModel2",
                                           tuple(fields.items()),
                                           bases=(models.Model, )),
                    migrations.CreateModel(name="MyModel3",
                                           fields=tuple(fields.items()),
                                           options=options,
                                           bases=(models.Model, )),
                    migrations.DeleteModel("MyModel"),
                    migrations.AddField("OtherModel", "datetimefield",
                                        fields["datetimefield"]),
                ],
                "dependencies": [("testapp", "some_other_one")],
            })
        writer = MigrationWriter(migration)
        output = writer.as_string()
        # We don't test the output formatting - that's too fragile.
        # Just make sure it runs for now, and that things look alright.
        result = self.safe_exec(output)
        self.assertIn("Migration", result)
Esempio n. 26
0
class Migration(migrations.Migration):

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

    operations = [
        migrations.CreateModel(
            name='FlatPage',
            fields=[
                ('id',
                 models.AutoField(verbose_name='ID',
                                  serialize=False,
                                  auto_created=True,
                                  primary_key=True)),
                ('url',
                 models.CharField(max_length=100,
                                  verbose_name='URL',
                                  db_index=True)),
                ('title', models.CharField(max_length=200,
                                           verbose_name='title')),
                ('content', models.TextField(verbose_name='content',
                                             blank=True)),
                ('enable_comments',
                 models.BooleanField(default=False,
                                     verbose_name='enable comments')),
                ('template_name',
                 models.CharField(help_text=(
                     "Example: 'flatpages/contact_page.html'. If this isn't provided, the system will use "
                     "'flatpages/default.html'."),
                                  max_length=70,
                                  verbose_name='template name',
                                  blank=True)),
                ('registration_required',
                 models.BooleanField(
                     default=False,
                     help_text=
                     'If this is checked, only logged-in users will be able to view the page.',
                     verbose_name='registration required')),
                ('sites',
                 models.ManyToManyField(to='sites.Site',
                                        verbose_name='sites')),
            ],
            options={
                'ordering': ('url', ),
                'db_table': 'django_flatpage',
                'verbose_name': 'flat page',
                'verbose_name_plural': 'flat pages',
            },
            bases=(models.Model, ),
        ),
    ]
Esempio n. 27
0
class Migration(migrations.Migration):

    dependencies = [("migrations2", "0001_initial")]

    operations = [
        migrations.CreateModel(
            "Bookstore",
            [
                ("id", models.AutoField(primary_key=True)),
                ("name", models.CharField(max_length=255)),
                ("slug", models.SlugField(null=True)),
            ],
        ),
    ]
Esempio n. 28
0
class Migration(migrations.Migration):
    atomic = False

    operations = [
        migrations.CreateModel(
            "Editor",
            [
                ("name", models.CharField(primary_key=True, max_length=255)),
            ],
        ),
        migrations.RunPython(raise_error,
                             reverse_code=raise_error,
                             atomic=True),
    ]
Esempio n. 29
0
class Migration(migrations.Migration):

    initial = True

    operations = [
        migrations.CreateModel(
            'Salamander',
            [
                ('id', models.AutoField(primary_key=True)),
                ('tail', models.IntegerField(default=0)),
                ('silly_field', models.BooleanField(default=False)),
            ],
        ),
        migrations.RunPython(grow_tail, shrink_tail),
    ]
Esempio n. 30
0
 def test_create_model_reordering(self):
     """
     AddField optimizes into CreateModel if it's a FK to a model that's
     between them (and there's no FK in the other direction), by changing
     the order of the CreateModel operations.
     """
     self.assertOptimizesTo(
         [
             migrations.CreateModel(
                 'Foo', [('name', models.CharField(max_length=255))]),
             migrations.CreateModel('Link', [('url', models.TextField())]),
             migrations.AddField(
                 'Foo', 'link',
                 models.ForeignKey('migrations.Link', models.CASCADE)),
         ],
         [
             migrations.CreateModel('Link', [('url', models.TextField())]),
             migrations.CreateModel('Foo', [
                 ('name', models.CharField(max_length=255)),
                 ('link',
                  models.ForeignKey('migrations.Link', models.CASCADE))
             ]),
         ],
     )