summaryrefslogtreecommitdiffstats
path: root/tools
diff options
context:
space:
mode:
authorJeff Bradberry <jeff.bradberry@gmail.com>2024-05-06 20:48:06 +0200
committerJeff Bradberry <jeff.bradberry@gmail.com>2024-06-10 22:36:22 +0200
commita0b376a6cac4ac118a0b646d3d4b9e5c40928c8e (patch)
treeacde0f5cec56365183724f2d409c23e5b428ebb4 /tools
parentHandle the case where a resource points to a Role which isn't in the db (diff)
downloadawx-a0b376a6cac4ac118a0b646d3d4b9e5c40928c8e.tar.xz
awx-a0b376a6cac4ac118a0b646d3d4b9e5c40928c8e.zip
Set up a scenario where IG.use_role_id points to something no longer there
This is actually happening for one customer, though it seems like it shouldn't be if the foreign key constraint is set back up properly. In order to recreate it, I had to add the constraint back with 'NOT VALID' added on to prevent the check.
Diffstat (limited to 'tools')
-rw-r--r--tools/scripts/ig-hotfix/test3.py28
1 files changed, 28 insertions, 0 deletions
diff --git a/tools/scripts/ig-hotfix/test3.py b/tools/scripts/ig-hotfix/test3.py
new file mode 100644
index 0000000000..2bf17d705e
--- /dev/null
+++ b/tools/scripts/ig-hotfix/test3.py
@@ -0,0 +1,28 @@
+from django.db import connection
+from awx.main.models import InstanceGroup
+
+InstanceGroup.objects.filter(name__in=('green', 'yellow', 'red', 'blue')).delete()
+
+green = InstanceGroup.objects.create(name='green')
+red = InstanceGroup.objects.create(name='red')
+yellow = InstanceGroup.objects.create(name='yellow')
+blue = InstanceGroup.objects.create(name='blue')
+
+for ig in InstanceGroup.objects.all():
+ print((ig.id, ig.name, ig.use_role_id))
+
+with connection.cursor() as cursor:
+ cursor.execute("ALTER TABLE main_instancegroup DROP CONSTRAINT main_instancegroup_use_role_id_48ea7ecc_fk_main_rbac_roles_id")
+
+ cursor.execute(f"UPDATE main_rbac_roles SET object_id = NULL WHERE id = {red.use_role_id}")
+ cursor.execute(f"DELETE FROM main_rbac_roles_parents WHERE from_role_id = {blue.use_role_id} OR to_role_id = {blue.use_role_id}")
+ cursor.execute(f"DELETE FROM main_rbac_role_ancestors WHERE ancestor_id = {blue.use_role_id} OR descendent_id = {blue.use_role_id}")
+ cursor.execute(f"DELETE FROM main_rbac_roles WHERE id = {blue.use_role_id}")
+ cursor.execute("UPDATE main_instancegroup SET use_role_id = NULL WHERE name = 'red'")
+ cursor.execute(f"UPDATE main_instancegroup SET use_role_id = {green.use_role_id} WHERE name = 'yellow'")
+
+ cursor.execute("ALTER TABLE main_instancegroup ADD CONSTRAINT main_instancegroup_use_role_id_48ea7ecc_fk_main_rbac_roles_id FOREIGN KEY (use_role_id) REFERENCES public.main_rbac_roles(id) DEFERRABLE INITIALLY DEFERRED NOT VALID")
+
+print("=====================================")
+for ig in InstanceGroup.objects.all():
+ print((ig.id, ig.name, ig.use_role_id))