'Context manager to temporarily apply the migration context.
Calling .save() from within the context manager means that the mutated
context will be saved which can cause incorrect resource tracking, and
should be avoided.'

@contextlib.contextmanager
def mutated_migration_context(self):

    if (('migration_context' in self) and (self.migration_context is None)):
        (yield)
        return
    current_values = {}
    for attr_name in _MIGRATION_CONTEXT_ATTRS:
        current_values[attr_name] = getattr(self, attr_name)
    self.apply_migration_context()
    try:
        (yield)
    finally:
        for attr_name in _MIGRATION_CONTEXT_ATTRS:
            setattr(self, attr_name, current_values[attr_name])
