Skip to content

Commit 83abd1c

Browse files
committed
make DAB RBAC migrations reversible
Put in the nulling cases
1 parent 8839827 commit 83abd1c

File tree

4 files changed

+59
-3
lines changed

4 files changed

+59
-3
lines changed

ansible_base/rbac/migrations/0005_remote_permissions_data.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Generated by Django 4.2.23 on 2025-06-30 12:48
22

3-
from django.db import migrations
3+
from django.db import migrations, models
44

55

66
def create_types_if_needed(apps, schema_editor):
@@ -54,4 +54,15 @@ class Migration(migrations.Migration):
5454
operations = [
5555
migrations.RunPython(create_types_if_needed, migrations.RunPython.noop),
5656
migrations.RunPython(migrate_content_type, migrations.RunPython.noop),
57+
# Make the content_type fields nullable so the reverse migrations are possible
58+
migrations.AlterField(
59+
model_name='dabpermission',
60+
name='content_type',
61+
field=models.ForeignKey(help_text='The content type this permission will apply to.', null=True, on_delete=models.deletion.CASCADE, to='contenttypes.contenttype', verbose_name='content type'),
62+
),
63+
migrations.AlterField(
64+
model_name='objectrole',
65+
name='content_type',
66+
field=models.ForeignKey(help_text='The content type of the subject of permission assignments. Duplicated from related RoleDefinition.', null=True, on_delete=models.deletion.CASCADE, to='contenttypes.contenttype'),
67+
),
5768
]
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Generated by Django 4.2.23 on 2025-06-30 12:48
2+
3+
from django.db import migrations
4+
5+
6+
def migrate_content_type_forward_noop(apps, schema_editor):
7+
"""Forward no-op: data migration already happened in 0005."""
8+
pass
9+
10+
11+
def migrate_content_type_reverse(apps, schema_editor):
12+
"""Reverse migration: undo what 0005 did by copying data back from new_content_type to content_type.
13+
14+
When this reverse runs, we're going from state after 0005 back to state after 0004.
15+
After 0005: data was copied from content_type to new_content_type
16+
To reverse: copy from new_content_type back to content_type, and clear new_content_type
17+
"""
18+
django_ct_cls = apps.get_model('contenttypes', 'ContentType')
19+
20+
for model_name in ('dabpermission', 'objectrole', 'roledefinition', 'roleuserassignment', 'roleteamassignment'):
21+
cls = apps.get_model('dab_rbac', model_name)
22+
for obj in cls.objects.all():
23+
new_ct = obj.new_content_type
24+
if new_ct:
25+
try:
26+
# Find the corresponding Django ContentType by app_label and model
27+
django_ct = django_ct_cls.objects.get(app_label=new_ct.app_label, model=new_ct.model)
28+
obj.content_type = django_ct
29+
obj.save()
30+
except Exception as e:
31+
raise RuntimeError(
32+
f"Failed to get Django content type for a {model_name} pk={obj.pk}, "
33+
f"DABContentType app_label='{new_ct.app_label}', model='{new_ct.model}'"
34+
) from e
35+
36+
37+
class Migration(migrations.Migration):
38+
39+
dependencies = [
40+
('dab_rbac', '0005_remote_permissions_data'),
41+
]
42+
43+
operations = [
44+
migrations.RunPython(migrate_content_type_forward_noop, migrate_content_type_reverse),
45+
]

ansible_base/rbac/migrations/0006_remote_permissions_removals.py renamed to ansible_base/rbac/migrations/0007_remote_permissions_removals.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
class Migration(migrations.Migration):
77

88
dependencies = [
9-
('dab_rbac', '0005_remote_permissions_data'),
9+
('dab_rbac', '0006_remote_data_reverse'),
1010
]
1111

1212
operations = [

ansible_base/rbac/migrations/0007_remote_permissions_cleanup.py renamed to ansible_base/rbac/migrations/0008_remote_permissions_cleanup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
class Migration(migrations.Migration):
77

88
dependencies = [
9-
('dab_rbac', '0006_remote_permissions_removals'),
9+
('dab_rbac', '0007_remote_permissions_removals'),
1010
]
1111

1212
operations = [

0 commit comments

Comments
 (0)