From bea844ff51b4da8796f296631334d6e70fd27426 Mon Sep 17 00:00:00 2001 From: Michael DeHaan Date: Mon, 15 Jul 2013 17:23:55 -0400 Subject: [PATCH] Keep from unneccessary DB row additions in changing group memberships during inventory import. --- .../management/commands/inventory_import.py | 22 +++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/awx/main/management/commands/inventory_import.py b/awx/main/management/commands/inventory_import.py index e488a9dba3..2c62979d78 100644 --- a/awx/main/management/commands/inventory_import.py +++ b/awx/main/management/commands/inventory_import.py @@ -471,10 +471,24 @@ class Command(BaseCommand): # FIXME: only clear the ones that should not exist if overwrite: LOGGER.info("clearing any child relationships to rebuild from remote source") - groups = Group.objects.all() - for g in groups: - g.children.clear() - g.save() + db_groups = Group.objects.all() + #for g in db_groups: + # g.children.clear() + # g.save() + + for db_group in db_groups: + db_kids = db_group.children.all() + mem_kids = group_names[db_group.name].child_groups + mem_kid_names = [ k.name for k in mem_kids ] + removed = False + for db_kid in db_kids: + if db_kid.name not in mem_kid_names: + removed = True + print "DEBUG: removing non-DB kid: %s" % (db_kid.name) + db_group.children.remove(db_kid) + if removed: + db_group.save() + # this will be slightly inaccurate, but attribute to first superuser. user = User.objects.filter(is_superuser=True)[0]