#32263: squashmigrations produces incorrect result with a RenameModel on a
ForeignKey target.
-------------------------------------+-------------------------------------
     Reporter:  InvalidInterrupt     |                    Owner:  Anvesh
                                     |  Mishra
         Type:  Bug                  |                   Status:  assigned
    Component:  Migrations           |                  Version:  3.1
     Severity:  Normal               |               Resolution:
     Keywords:                       |             Triage Stage:  Accepted
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by Anvesh Mishra):

 A regression test for this ticket would look something like this:
 {{{#!python
     def test_squashmigrations_rename_foreign_key(self):

         out = io.StringIO()
         with self.temporary_migration_module(
             module="migrations.test_migrations_rename"
         ) as migration_dir:
             call_command(
                 "squashmigrations",
                 "migrations",
                 "0003",
                 interactive=False,
                 stdout=out,
                 no_color=True,
             )
             squashed_migration_file = os.path.join(
                 migration_dir, "0001_squashed_0003_third.py"
             )
             with open(squashed_migration_file, encoding="utf-8") as fp:
                 content = fp.read()
                 self.assertIn('to="migrations.person"',content)
             self.assertTrue(os.path.exists(squashed_migration_file))
         self.assertEqual(
             out.getvalue(),
             f"Will squash the following migrations:\n"
             f" - 0001_initial\n"
             f" - 0002_second\n"
             f" - 0003_third\n"
             f"Optimizing...\n"
             f"  Optimized from 4 operations to 2 operations.\n"
             f"Created new squashed migration {squashed_migration_file}\n"
             f"  You should commit this migration but leave the old ones in
 place;\n"
             f"  the new migration will be used for new installs. Once you
 are sure\n"
             f"  all instances of the codebase have applied the migrations
 you "
             f"squashed,\n"
             f"  you can delete them.\n",
         )
 }}}
 By running this test I found out that the base migration objects created
 are of `CreateModel` so we need to somehow change the functionality of
 this section of `CreateModel.reduce()`:
 {{{#!python
 def reduce(self,operation,app_label):
         ...
         elif (
             isinstance(operation, RenameModel)
             and self.name_lower == operation.old_name_lower
         ):
             return [
                 CreateModel(
                     operation.new_name,
                     fields=self.fields,
                     options=self.options,
                     bases=self.bases,
                     managers=self.managers,
                 ),
             ]
 }}}
 to something like:
 {{{#!python
 def reduce(self,operation,app_label):
         ...
         elif (
             isinstance(operation, RenameModel)
             and self.name_lower == operation.old_name_lower
         ):
             return operation.reduce(operation,app_label)
 }}}
 and changing the functionality of `RenameModel.reduce()`. I hope I'm
 clear.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/32263#comment:13>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/01070184a64e6dd9-8bc9b483-03fa-4c85-8e15-d484a37c7a58-000000%40eu-central-1.amazonses.com.

Reply via email to