<commit_before>import os

from django.db import migrations
from django.template import Context
from django.template.loader import get_template_from_string

from corehq.sql_db.routers import allow_migrate


def noop_migration_fn(apps, schema_editor):
    pass


def noop_migration():
    """
    A migration that does nothing. Used to replace old migrations that are no longer required e.g moved.
    """
    return migrations.RunPython(noop_migration_fn, noop_migration_fn)


class RunSqlLazy(migrations.RunSQL):
    """
    Extension of RunSQL that reads the SQL contents from a file "just in time".

    Also supports reading the SQL as a Django template and rendering
    it with the provided template context.
    """
    def __init__(self, sql_template_path, reverse_sql_template_path, template_context=None):
        self.template_context = template_context or {}
        super(RunSqlLazy, self).__init__(sql_template_path, reverse_sql_template_path)

    def database_forwards(self, app_label, schema_editor, from_state, to_state):
        db_alias = schema_editor.connection.alias
        if allow_migrate(db_alias, app_label):
            self.sql = self._render_template(self.sql)
            super(RunSqlLazy, self).database_forwards(app_label, schema_editor, from_state, to_state)

    def database_backwards(self, app_label, schema_editor, from_state, to_state):
        db_alias = schema_editor.connection.alias
        if allow_migrate(db_alias, app_label):
            if self.reverse_sql:
                self.reverse_sql = self._render_template(self.reverse_sql)
            super(RunSqlLazy, self).database_backwards(app_label, schema_editor, from_state, to_state)

    def _render_template(self, path):
        with open(self.sql) as f:
            template_string = f.read()

        template = get_template_from_string(template_string)
        return template.render(Context(self.template_context))


class RawSQLMigration(object):
    """
    Helper class for running raw SQL migrations.
    Usage:

        migrator = RawSQLMigration(('base', 'path'), {'variable': 'value'})
        migrator.get_migration('sql_template.sql')<commit_msg>don't attempt to render sql twice<commit_after>import os

from django.db import migrations
from django.template import Context
from django.template.loader import get_template_from_string

from corehq.sql_db.routers import allow_migrate


def noop_migration_fn(apps, schema_editor):
    pass


def noop_migration():
    """
    A migration that does nothing. Used to replace old migrations that are no longer required e.g moved.
    """
    return migrations.RunPython(noop_migration_fn, noop_migration_fn)


class RunSqlLazy(migrations.RunSQL):
    """
    Extension of RunSQL that reads the SQL contents from a file "just in time".

    Also supports reading the SQL as a Django template and rendering
    it with the provided template context.
    """
    def __init__(self, sql_template_path, reverse_sql_template_path, template_context=None):
        self.template_context = template_context or {}
        self.rendered_forwards = False
        self.rendered_backwards = False
        super(RunSqlLazy, self).__init__(sql_template_path, reverse_sql_template_path)

    def database_forwards(self, app_label, schema_editor, from_state, to_state):
        db_alias = schema_editor.connection.alias
        if allow_migrate(db_alias, app_label):
            if not self.rendered_forwards:
                self.sql = self._render_template(self.sql)
                self.rendered_forwards = True
            super(RunSqlLazy, self).database_forwards(app_label, schema_editor, from_state, to_state)

    def database_backwards(self, app_label, schema_editor, from_state, to_state):
        db_alias = schema_editor.connection.alias
        if allow_migrate(db_alias, app_label):
            if self.reverse_sql:
                if not self.rendered_backwards:
                    self.reverse_sql = self._render_template(self.reverse_sql)
                    self.rendered_backwards = True
            super(RunSqlLazy, self).database_backwards(app_label, schema_editor, from_state, to_state)

    def _render_template(self, path):
        with open(self.sql) as f:
            template_string = f.read()

        template = get_template_from_string(template_string)
        return template.render(Context(self.template_context))


class RawSQLMigration(object):
    """
    Helper class for running raw SQL migrations.
    Usage:

        migrator = RawSQLMigration(('base', 'path'), {'variable': 'value'})
        migrator.get_migration('sql_template.sql')
{'fraction_non_alphanumeric': 0.07818930041152264, 'fraction_numerical': 0.0, 'mean_word_length': 3.5274509803921568, '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': '1171196', 'n_tokens_mistral': 1346, 'n_tokens_neox': 1326, 'n_words': 328}