Skip to content

Commit f575a7b

Browse files
committed
Manually set id for types loaded from remote
1 parent c25567c commit f575a7b

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

ansible_base/rbac/models/content_type.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from django.apps import apps
66
from django.db import models as django_models
77
from django.db.models.options import Options
8+
from django.db.models import Max
89
from django.utils.translation import gettext_lazy as _
910

1011
from ..remote import RemoteObject, get_local_resource_prefix, get_resource_prefix
@@ -176,12 +177,17 @@ def get_for_id(self, id: int) -> django_models.Model:
176177

177178
def load_remote_objects(self, remote_data: list[dict]):
178179
parent_mapping: dict[django_models.Model, str] = {}
180+
# For test reasons, it can be very hard to assure the post_migrate logic runs in all cases
181+
# so we just put in the id field manually which avoids any conflict with existing records
182+
max_id = self.aggregate(Max('id'))['id__max'] or 0
179183
for remote_type_raw in remote_data:
180-
remote_type = remote_type_raw.copy()
181-
service = remote_type.pop('service')
182-
model = remote_type.pop('model')
183-
pct_slug = remote_type.pop('parent_content_type')
184-
ct, _ = self.get_or_create(service=service, model=model, defaults=remote_type)
184+
defaults = remote_type_raw.copy()
185+
service = defaults.pop('service')
186+
model = defaults.pop('model')
187+
pct_slug = defaults.pop('parent_content_type')
188+
max_id += 1
189+
defaults['id'] = max_id
190+
ct, _ = self.get_or_create(service=service, model=model, defaults=defaults)
185191
parent_mapping[ct] = pct_slug
186192

187193
# The parent type link needs to be filled in via a second pass

0 commit comments

Comments
 (0)