<commit_before>''' Syncing and migrating test database '''

from django.db.models.signals import post_syncdb
from django.dispatch import receiver
from django.core.management import call_command
from test_tools.test_runner import get_test_db_name
from django.db import connections
from django.conf import settings


def reset_connection(connection, new_name):
    ''' Change database name '''
    connection.close()
    connection.settings_dict["NAME"] = new_name
    connection.features.confirm()
    connection.cursor()


def call_test_db_command(command):
    ''' Call command on test database '''
    for alias in connections:
        connection = connections[alias]
        old_name = connection.settings_dict["NAME"]
        test_db_name = get_test_db_name(connection)
        if not old_name.startswith('test_'):
            reset_connection(connection, test_db_name)
            call_command(command,
                interactive=False,
                database=connection.alias,
                load_initial_data=False)
            reset_connection(connection, old_name)


@receiver(post_syncdb)
def sync_test_db(sender, **kwargs):
    ''' Syncing test database '''
    app_label = '.'.join(sender.__name__.split('.')[:-1])
    if app_label == settings.INSTALLED_APPS[-1]:
        call_test_db_command('syncdb --noinput')


if 'south' in settings.INSTALLED_APPS:
    from south.signals import post_migrate

    @receiver(post_migrate)
    def migrate_test_db(sender, **kwargs):
        ''' Migrate test db '''
        call_test_db_command('migrate')<commit_msg>input<commit_after>''' Syncing and migrating test database '''

from django.db.models.signals import post_syncdb
from django.dispatch import receiver
from django.core.management import call_command
from test_tools.test_runner import get_test_db_name
from django.db import connections
from django.conf import settings


def reset_connection(connection, new_name):
    ''' Change database name '''
    connection.close()
    connection.settings_dict["NAME"] = new_name
    connection.features.confirm()
    connection.cursor()


def call_test_db_command(command):
    ''' Call command on test database '''
    for alias in connections:
        connection = connections[alias]
        old_name = connection.settings_dict["NAME"]
        test_db_name = get_test_db_name(connection)
        if not old_name.startswith('test_'):
            reset_connection(connection, test_db_name)
            call_command(command,
                interactive=False,
                database=connection.alias,
                load_initial_data=False)
            reset_connection(connection, old_name)


@receiver(post_syncdb)
def sync_test_db(sender, **kwargs):
    ''' Syncing test database '''
    app_label = '.'.join(sender.__name__.split('.')[:-1])
    if app_label == settings.INSTALLED_APPS[-1]:
        call_test_db_command('syncdb')


if 'south' in settings.INSTALLED_APPS:
    from south.signals import post_migrate

    @receiver(post_migrate)
    def migrate_test_db(sender, **kwargs):
        ''' Migrate test db '''
        call_test_db_command('migrate')
{'fraction_non_alphanumeric': 0.10006414368184734, 'fraction_numerical': 0.0012828736369467607, 'mean_word_length': 3.5466472303206995, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4893438', 'n_tokens_mistral': 904, 'n_tokens_neox': 885, 'n_words': 196}