Skip to content

Commit f90f9df

Browse files
committed
Fix data migration bug to new content type
1 parent b2d1f92 commit f90f9df

File tree

4 files changed

+11
-7
lines changed

4 files changed

+11
-7
lines changed

ansible_base/rbac/managed.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def get_or_create(self, apps):
6767
# Better error handling for debugging
6868
db_codenames = list(permission_cls.objects.values_list('codename', flat=True))
6969
raise permission_cls.DoesNotExist(
70-
f'Permission codename {str_perm} does not exist. Manged role {self} expected: {permissions}\n Database permissions: {db_codenames}'
70+
f'Permission codename {str_perm} does not exist. Manged role {self}\n expected: {permissions}\n Database permissions: {db_codenames}'
7171
)
7272
rd.permissions.add(*perm_list)
7373
logger.info(f'Created {self.shortname} managed role definition, name={self.name}')

ansible_base/rbac/management/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def create_dab_permissions(app_config, verbosity=2, interactive=True, using=DEFA
3131
try:
3232
DABContentType = apps.get_model("dab_rbac", "DABContentType")
3333
except LookupError:
34-
logger.debug('Skipping DAB RBAC type and permission creation since models are not available')
34+
logger.warning('Skipping DAB RBAC type and permission creation since models are not available')
3535
return
3636

3737
if not router.allow_migrate_model(using, DABContentType):

ansible_base/rbac/migrations/0005_remote_permissions_migration.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def create_types_and_permissions(apps, schema_editor):
1010
"""
1111
from ansible_base.rbac.management import create_dab_permissions
1212

13-
create_dab_permissions(apps=apps)
13+
create_dab_permissions(apps.get_app_config('dab_rbac'), apps=apps)
1414

1515

1616
def migrate_content_type(apps, schema_editor):
@@ -35,7 +35,7 @@ class Migration(migrations.Migration):
3535
]
3636

3737
operations = [
38-
migrations.RunPython(migrate_content_type, migrations.RunPython.noop),
38+
migrations.RunPython(create_types_and_permissions, migrations.RunPython.noop),
3939
migrations.RunPython(migrate_content_type, migrations.RunPython.noop),
4040
migrations.RemoveField(
4141
model_name='dabpermission',

ansible_base/rbac/models/content_type.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,16 +143,20 @@ def get_by_natural_key(self, *args: str) -> "DABContentType":
143143
kwargs = {'service__in': [get_local_resource_prefix(), 'shared'], 'app_label': app_label, 'model': model}
144144
# This ask here is actually ambiguous, so we try this extra lookup
145145
shared_key = ('shared', app_label, model)
146-
if (self.db in self._cache) and (('shared', app_label, model) in self._cache[self.db]):
147-
return self._cache[self.db][shared_key]
146+
if self.db in self._cache:
147+
if shared_key in self._cache[self.db]:
148+
return self._cache[self.db][shared_key]
148149
else:
149150
service, app_label, model = args
150151
kwargs = {'service': service, 'app_label': app_label, 'model': model}
151152
key = (service, app_label, model)
152153
try:
153154
return self._cache[self.db][key]
154155
except KeyError:
155-
ct = self.get(**kwargs)
156+
# Here we are adding additional error details for migration problems
157+
ct = self.filter(**kwargs).first()
158+
if ct is None:
159+
raise self.model.DoesNotExist(f'Could not get ContentType {args}, existing: {list(self.values_list("model", flat=True))}')
156160
self._add_to_cache(self.db, ct)
157161
return ct
158162

0 commit comments

Comments
 (0)