Opened 7 years ago

Closed 7 years ago

#27717 closed Cleanup/optimization (fixed)

Squashmigrations doesn't optimize AlterModelOptions verbose_name_plural into CreateModel

Reported by: Ed Morley Owned by: Ed Morley
Component: Migrations Version: dev
Severity: Normal Keywords: squashmigrations
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

The squashmigrations command doesn't combine a AlterModelOptions() with CreateModel() in the case below.

For example (using Django master):

testapp/models.py

from django.db import models


class Credentials(models.Model):
    description = models.TextField()

    class Meta:
        db_table = 'credentials'
        # This was added after 0001_initial.py was generated:
        verbose_name_plural = 'credentials'

testapp/migrations/0001_initial.py

class Migration(migrations.Migration):

    initial = True

    dependencies = [
    ]

    operations = [
        migrations.CreateModel(
            name='Credentials',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('description', models.TextField()),
            ],
            options={
                'db_table': 'credentials',
            },
        ),
    ]

testapp/migrations/0002_add_verbose_name_plural.py

class Migration(migrations.Migration):

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

    operations = [
        migrations.AlterModelOptions(
            name='credentials',
            options={'verbose_name_plural': 'credentials'},
        ),
    ]

Expected squashed migration (via ./manage.py squashmigrations testapp 0002):

class Migration(migrations.Migration):

    replaces = [('testapp', '0001_initial'), ('testapp', '0002_add_verbose_name_plural')]

    initial = True

    dependencies = [
    ]

    operations = [
        migrations.CreateModel(
            name='Credentials',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('description', models.TextField()),
            ],
            options={
                'db_table': 'credentials',
                'verbose_name_plural': 'credentials',
            },
        ),
    ]

Actual squashed migration:

class Migration(migrations.Migration):

    replaces = [('testapp', '0001_initial'), ('testapp', '0002_add_verbose_name_plural')]

    initial = True

    dependencies = [
    ]

    operations = [
        migrations.CreateModel(
            name='Credentials',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('description', models.TextField()),
            ],
            options={
                'db_table': 'credentials',
            },
        ),
        migrations.AlterModelOptions(
            name='credentials',
            options={'verbose_name_plural': 'credentials'},
        ),
    ]

Attachments (1)

0001-Refs-27717-Added-a-regression-test.patch (1.3 KB ) - added by Simon Charette 7 years ago.

Download all attachments as: .zip

Change History (6)

comment:1 by Simon Charette, 7 years ago

Triage Stage: UnreviewedAccepted
Type: BugCleanup/optimization

Managed to reproduce with the provided patch.

by Simon Charette, 7 years ago

comment:2 by Ed Morley, 7 years ago

Owner: changed from nobody to Ed Morley
Status: newassigned

Thank you for the testcase - PR with that and fix opened :-)

comment:3 by Markus Holtermann, 7 years ago

Has patch: set

comment:4 by Markus Holtermann, 7 years ago

Triage Stage: AcceptedReady for checkin

Patch and tests look sensible to me.

comment:5 by Tim Graham <timograham@…>, 7 years ago

Resolution: fixed
Status: assignedclosed

In 7156a6c9:

Fixed #27717 -- Allowed migration optimization across AlterModelOptions.

Note: See TracTickets for help on using tickets.
Back to Top