From e5613a59ce716bdeeb432dad818c5fcb815c84a1 Mon Sep 17 00:00:00 2001 From: bmedx Date: Fri, 26 Oct 2018 16:07:59 -0400 Subject: [PATCH] Add dry run option to populate_retirement_states command --- .../commands/populate_retirement_states.py | 22 ++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/openedx/core/djangoapps/user_api/management/commands/populate_retirement_states.py b/openedx/core/djangoapps/user_api/management/commands/populate_retirement_states.py index 8a2922c44a..42793bc8f8 100644 --- a/openedx/core/djangoapps/user_api/management/commands/populate_retirement_states.py +++ b/openedx/core/djangoapps/user_api/management/commands/populate_retirement_states.py @@ -36,6 +36,13 @@ class Command(BaseCommand): """ help = 'Populates the RetirementState table with the states present in settings.' + def add_arguments(self, parser): + parser.add_argument( + '--dry_run', + action='store_true', + help='Run checks without making any changes' + ) + def _validate_new_states(self, new_states): """ Check settings for existence of states, required states @@ -72,7 +79,7 @@ class Command(BaseCommand): raise CommandError('Users exist in a state that is marked for deletion! States to delete' 'are: {}'.format(states_to_delete)) - def _delete_old_states_and_create_new(self, new_states): + def _delete_old_states_and_create_new(self, new_states, dry_run=False): """ Wipes the RetirementState table and creates new entries based on new_states - Note that the state_execution_order is incremented by 10 for each entry @@ -91,6 +98,10 @@ class Command(BaseCommand): states_remaining = set_current_states.intersection(set_new_states) states_to_delete = set_current_states - set_new_states + # If this is a dry run we have everything we need. + if dry_run: + return states_to_create, states_remaining, states_to_delete + # In theory this should not happen, this would have failed _check_current_users # if the state was not required, and failed _validate_new_states if we're trying # to remove a required state, but playing it extra safe here since a state delete @@ -129,13 +140,18 @@ class Command(BaseCommand): """ Execute the command. """ + dry_run = options['dry_run'] + + if dry_run: + print("--- Dry run, no changes will be made ---") + new_states = settings.RETIREMENT_STATES self._validate_new_states(new_states) self._check_current_users() - created, existed, deleted = self._delete_old_states_and_create_new(new_states) + created, existed, deleted = self._delete_old_states_and_create_new(new_states, dry_run=dry_run) # Report - print("All states removed and new states added. Differences:") + print("States have been synchronized. Differences:") print(" Added: {}".format(created)) print(" Removed: {}".format(deleted)) print(" Remaining: {}".format(existed))