'Allows testing management commands in a temporary migrations module.
Wrap all invocations to makemigrations and squashmigrations with this
context manager in order to avoid creating migration files in your
source tree inadvertently.
Takes the application label that will be passed to makemigrations or
squashmigrations and the Python path to a migrations module.
The migrations module is used as a template for creating the temporary
migrations module. If it isn\'t provided, the application\'s migrations
module is used, if it exists.
Returns the filesystem path to the temporary migrations module.'

@contextmanager
def temporary_migration_module(self, app_label='migrations', module=None):

    with tempfile.TemporaryDirectory() as temp_dir:
        target_dir = tempfile.mkdtemp(dir=temp_dir)
        with open(os.path.join(target_dir, '__init__.py'), 'w'):
            pass
        target_migrations_dir = os.path.join(target_dir, 'migrations')
        if (module is None):
            module = (apps.get_app_config(app_label).name + '.migrations')
        try:
            source_migrations_dir = module_dir(import_module(module))
        except (ImportError, ValueError):
            pass
        else:
            shutil.copytree(source_migrations_dir, target_migrations_dir)
        with extend_sys_path(temp_dir):
            new_module = (os.path.basename(target_dir) + '.migrations')
            with self.settings(MIGRATION_MODULES={app_label: new_module}):
                (yield target_migrations_dir)
