nautobot / nautobot-app-bgp-models

Nautobot BGP models plugin
https://docs.nautobot.com/projects/bgp-models/en/latest/
Other
18 stars 8 forks source link

Leftover PeeringRole ObjectChange records after migration to 2.0 causes remove_stale_contenttypes to fail #144

Closed glennmatthews closed 11 months ago

glennmatthews commented 1 year ago

Environment

Expected Behavior

Successful migration of existing data to 2.0 and successful run of nautobot-server post_upgrade

Observed Behavior

Removing stale content types...
Traceback (most recent call last):
  File "/opt/nautobot/bin/nautobot-server", line 8, in <module>
    sys.exit(main())
  File "/opt/nautobot/lib/python3.9/site-packages/nautobot/core/cli/__init__.py", line 54, in main
    run_app(
  File "/opt/nautobot/lib/python3.9/site-packages/nautobot/core/runner/runner.py", line 297, in run_app
    management.execute_from_command_line([runner_name, command] + command_args)
  File "/opt/nautobot/lib/python3.9/site-packages/django/core/management/__init__.py", line 419, in execute_from_command_line
    utility.execute()
  File "/opt/nautobot/lib/python3.9/site-packages/django/core/management/__init__.py", line 413, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/opt/nautobot/lib/python3.9/site-packages/django/core/management/base.py", line 354, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/opt/nautobot/lib/python3.9/site-packages/django/core/management/base.py", line 398, in execute
    output = self.handle(*args, **options)
  File "/opt/nautobot/lib/python3.9/site-packages/nautobot/core/management/commands/post_upgrade.py", line 129, in handle
    call_command("remove_stale_contenttypes", interactive=False)
  File "/opt/nautobot/lib/python3.9/site-packages/django/core/management/__init__.py", line 181, in call_command
    return command.execute(*args, **defaults)
  File "/opt/nautobot/lib/python3.9/site-packages/django/core/management/base.py", line 398, in execute
    output = self.handle(*args, **options)
  File "/opt/nautobot/lib/python3.9/site-packages/django/contrib/contenttypes/management/commands/remove_stale_contenttypes.py", line 83, in handle
    ct.delete()
  File "/opt/nautobot/lib/python3.9/site-packages/django/db/models/base.py", line 966, in delete
    collector.collect([self], keep_parents=keep_parents)
  File "/opt/nautobot/lib/python3.9/site-packages/django/db/models/deletion.py", line 302, in collect
    raise ProtectedError(
django.db.models.deletion.ProtectedError: ("Cannot delete some instances of model 'ContentType' because they are referenced through protected foreign keys: 'ObjectChange.changed_object_type'.", {<ObjectChange: peeringrole eBGP Overlay EVPN-VXLAN updated by admin>, ...

Steps to Reproduce

  1. Set up BGP PeeringRoles under Nautobot 1.x and nautobot-bgp-models 0.9
  2. Migrate to Nautobot 2.0 and nautobot-bgp-models 0.20
  3. Run nautobot-server post_upgrade

See also nautobot/nautobot#4604 (a similar issue with a model removed from core).

Most likely code needs to be added in nautobot_bgp_models/migrations/0005_use_upstream_role_part2.py to migrate existing ObjectChange records pertaining to PeeringRoles over to reference the equivalent Role content-type and records before the PeeringRole model is deleted in migration 0006.

Workaround in the meantime is to use nbshell to run something like ObjectChange.objects.filter(changed_object_type=ContentType.objects.get(app_label="nautobot_bgp_models", model="peeringrole")).delete().

mzbroch commented 11 months ago

Fixed in #158