# Path: django_docker_helpers/utils.py
# def run_env_once(f: t.Callable) -> t.Callable:
#     """
#     A decorator to prevent ``manage.py`` from running code twice for everything.
#     (https://stackoverflow.com/questions/16546652/why-does-django-run-everything-twice)
# 
#     :param f: function or method to decorate
#     :return: callable
#     """
# 
#     @wraps(f)
#     def wrapper(*args, **kwargs):
#         key = _build_cache_key_for_env(wrapper.__name__, *args, **kwargs)
#         has_run = os.environ.get(key)
#         if has_run == '1':
#             return
#         result = f(*args, **kwargs)
#         os.environ[key] = '1'
#         return result
# 
#     return wrapper
# 
# def wf(raw_str: str,
#        flush: bool = True,
#        prevent_completion_polluting: bool = True,
#        stream: t.TextIO = sys.stdout):
#     """
#     Writes a given ``raw_str`` into a ``stream``. Ignores output if ``prevent_completion_polluting`` is set and there's
#     no extra ``sys.argv`` arguments present (a bash completion issue).
# 
#     :param raw_str: a raw string to print
#     :param flush: execute ``flush()``
#     :param prevent_completion_polluting: don't write anything if ``len(sys.argv) <= 1``
#     :param stream: ``sys.stdout`` by default
#     :return: None
#     """
#     if prevent_completion_polluting and len(sys.argv) <= 1:
#         return
# 
#     stream.write(raw_str)
#     flush and hasattr(stream, 'flush') and stream.flush()


from time import sleep
from django.conf import settings
from django.core.cache import caches
from django.core.management import execute_from_command_line
from django.db import OperationalError, connections
from django_docker_helpers.utils import run_env_once, wf
            return False
    return True


@run_env_once
def migrate(*argv) -> bool:
    """
    Runs Django migrate command.

    :return: always ``True``
    """
    wf('Applying migrations... ', False)
    execute_from_command_line(['./manage.py', 'migrate'] + list(argv))
    wf('[+]\n')
    return True


@run_env_once
def modeltranslation_sync_translation_fields() -> bool:
    """
    Runs ``modeltranslation``'s ``sync_translation_fields`` manage.py command:
    ``execute_from_command_line(['./manage.py', 'sync_translation_fields', '--noinput'])``

    :return: ``None`` if modeltranslation is not specified is ``INSTALLED_APPS``, ``True`` if all synced.
    """
    # if modeltranslation present ensure it's migrations applied too

    if 'modeltranslation' in settings.INSTALLED_APPS:
        wf('Applying translations for models... ', False)
        execute_from_command_line(['./manage.py', 'sync_translation_fields', '--noinput'])

# Path: django_docker_helpers/utils.py
# def run_env_once(f: t.Callable) -> t.Callable:
#     """
#     A decorator to prevent ``manage.py`` from running code twice for everything.
#     (https://stackoverflow.com/questions/16546652/why-does-django-run-everything-twice)
# 
#     :param f: function or method to decorate
#     :return: callable
#     """
# 
#     @wraps(f)
#     def wrapper(*args, **kwargs):
#         key = _build_cache_key_for_env(wrapper.__name__, *args, **kwargs)
#         has_run = os.environ.get(key)
#         if has_run == '1':
#             return
#         result = f(*args, **kwargs)
#         os.environ[key] = '1'
#         return result
# 
#     return wrapper
# 
# def wf(raw_str: str,
#        flush: bool = True,
#        prevent_completion_polluting: bool = True,
#        stream: t.TextIO = sys.stdout):
#     """
#     Writes a given ``raw_str`` into a ``stream``. Ignores output if ``prevent_completion_polluting`` is set and there's
#     no extra ``sys.argv`` arguments present (a bash completion issue).
# 
#     :param raw_str: a raw string to print
#     :param flush: execute ``flush()``
#     :param prevent_completion_polluting: don't write anything if ``len(sys.argv) <= 1``
#     :param stream: ``sys.stdout`` by default
#     :return: None
#     """
#     if prevent_completion_polluting and len(sys.argv) <= 1:
#         return
# 
#     stream.write(raw_str)
#     flush and hasattr(stream, 'flush') and stream.flush()


# Path: django_docker_helpers/db.py
from time import sleep
from django.conf import settings
from django.core.cache import caches
from django.core.management import execute_from_command_line
from django.db import OperationalError, connections
from django_docker_helpers.utils import run_env_once, wf

            return False
    return True


@run_env_once
def migrate(*argv) -> bool:
    """
    Runs Django migrate command.

    :return: always ``True``
    """
    wf('Applying migrations... ', False)
    execute_from_command_line(['./manage.py', 'migrate'] + list(argv))
    wf('[+]\n')
    return True


@run_env_once
def modeltranslation_sync_translation_fields() -> bool:
    """
    Runs ``modeltranslation``'s ``sync_translation_fields`` manage.py command:
    ``execute_from_command_line(['./manage.py', 'sync_translation_fields', '--noinput'])``

    :return: ``None`` if modeltranslation is not specified is ``INSTALLED_APPS``, ``True`` if all synced.
    """
    # if modeltranslation present ensure it's migrations applied too

    if 'modeltranslation' in settings.INSTALLED_APPS:
        wf('Applying translations for models... ', False)
        execute_from_command_line(['./manage.py', 'sync_translation_fields', '--noinput'])

        wf('[+]\n')