<commit_before>from django.db import models
from django.db.migrations.operations import AlterField

from sortedm2m.fields import SORT_VALUE_FIELD_NAME


class AlterSortedManyToManyField(AlterField):
    """A migration operation to transform a ManyToManyField into a
    SortedManyToManyField and vice versa."""

    def database_forwards(self, app_label, schema_editor, from_state, to_state):
        super(AlterSortedManyToManyField, self).database_forwards(
            app_label, schema_editor, from_state, to_state)

        to_model = to_state.render().get_model(app_label, self.model_name)
        to_field = to_model._meta.get_field_by_name(self.name)[0]

        from_model = from_state.render().get_model(app_label, self.model_name)
        from_field = from_model._meta.get_field_by_name(self.name)[0]

        to_m2m_model = to_field.rel.through
        from_m2m_model = from_field.rel.through

        # M2M -> SortedM2M
        if getattr(to_field, 'sorted', False):
            self.add_sort_value_field(schema_editor, to_m2m_model)
        # SortedM2M -> M2M
        elif getattr(from_field, 'sorted', False):
            self.remove_sort_value_field(schema_editor, from_m2m_model)
        else:
            raise TypeError(
                '{operation} should only be used when changing a '
                'SortedManyToManyField into a ManyToManyField or a '
                'ManyToManyField into a SortedManyToManyField.'
                .format(operation=self.__class__.__name__))

    def database_backwards(self, app_label, schema_editor, from_state, to_state):
        super(AlterSortedManyToManyField, self).database_backwards(
            app_label, schema_editor, from_state, to_state)

        from_model = from_state.render().get_model(app_label, self.model_name)
        from_field = from_model._meta.get_field_by_name(self.name)[0]

        to_model = to_state.render().get_model(app_label, self.model_name)
        to_field = to_model._meta.get_field_by_name(self.name)[0]

        from_m2m_model = from_field.rel.through
        to_m2m_model = to_field.rel.through

        # The `to_state` is the OLDER state.

        # M2M -> SortedM2M (backwards)
        if getattr(to_field, 'sorted', False):
            self.add_sort_value_field(schema_editor, to_m2m_model)
        # SortedM2M -> M2M (backwards)
        elif getattr(from_field, 'sorted', False):
            self.remove_sort_value_field(schema_editor, from_m2m_model)
        else:
            raise TypeError(
                '{operation} should only be used when changing a '
                'SortedManyToManyField into a ManyToManyField or a '
                'ManyToManyField into a SortedManyToManyField.'
                .format(operation=self.__class__.__name__))

    def add_sort_value_field(self, schema_editor, model):
        field = self.make_sort_by_field(model)
        schema_editor.add_field(model, field)

    def remove_sort_value_field(self, schema_editor, model):
        field = model._meta.get_field_by_name(SORT_VALUE_FIELD_NAME)[0]
        schema_editor.remove_field(model, field)

    def make_sort_by_field(self, model):
        field = models.IntegerField(name=SORT_VALUE_FIELD_NAME, default=0)
        field.set_attributes_from_name(SORT_VALUE_FIELD_NAME)
        return field
<commit_msg>Fixing AlterSortedManyToManyField operation for postgres databases.<commit_after>from django.db import models
from django.db.migrations.operations import AlterField

from sortedm2m.fields import SORT_VALUE_FIELD_NAME


class AlterSortedManyToManyField(AlterField):
    """A migration operation to transform a ManyToManyField into a
    SortedManyToManyField and vice versa."""

    def database_forwards(self, app_label, schema_editor, from_state, to_state):
        to_model = to_state.render().get_model(app_label, self.model_name)
        to_field = to_model._meta.get_field_by_name(self.name)[0]

        from_model = from_state.render().get_model(app_label, self.model_name)
        from_field = from_model._meta.get_field_by_name(self.name)[0]

        to_m2m_model = to_field.rel.through
        from_m2m_model = from_field.rel.through

        # M2M -> SortedM2M
        if getattr(to_field, 'sorted', False):
            self.add_sort_value_field(schema_editor, to_m2m_model)
        # SortedM2M -> M2M
        elif getattr(from_field, 'sorted', False):
            self.remove_sort_value_field(schema_editor, from_m2m_model)
        else:
            raise TypeError(
                '{operation} should only be used when changing a '
                'SortedManyToManyField into a ManyToManyField or a '
                'ManyToManyField into a SortedManyToManyField.'
                .format(operation=self.__class__.__name__))

    def database_backwards(self, app_label, schema_editor, from_state, to_state):
        from_model = from_state.render().get_model(app_label, self.model_name)
        from_field = from_model._meta.get_field_by_name(self.name)[0]

        to_model = to_state.render().get_model(app_label, self.model_name)
        to_field = to_model._meta.get_field_by_name(self.name)[0]

        from_m2m_model = from_field.rel.through
        to_m2m_model = to_field.rel.through

        # The `to_state` is the OLDER state.

        # M2M -> SortedM2M (backwards)
        if getattr(to_field, 'sorted', False):
            self.add_sort_value_field(schema_editor, to_m2m_model)
        # SortedM2M -> M2M (backwards)
        elif getattr(from_field, 'sorted', False):
            self.remove_sort_value_field(schema_editor, from_m2m_model)
        else:
            raise TypeError(
                '{operation} should only be used when changing a '
                'SortedManyToManyField into a ManyToManyField or a '
                'ManyToManyField into a SortedManyToManyField.'
                .format(operation=self.__class__.__name__))

    def add_sort_value_field(self, schema_editor, model):
        field = self.make_sort_by_field(model)
        schema_editor.add_field(model, field)

    def remove_sort_value_field(self, schema_editor, model):
        field = model._meta.get_field_by_name(SORT_VALUE_FIELD_NAME)[0]
        schema_editor.remove_field(model, field)

    def make_sort_by_field(self, model):
        field = models.IntegerField(name=SORT_VALUE_FIELD_NAME, default=0)
        field.set_attributes_from_name(SORT_VALUE_FIELD_NAME)
        return field

{'fraction_non_alphanumeric': 0.08248869483860907, 'fraction_numerical': 0.0071729299859660065, 'mean_word_length': 3.1089045483664317, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, '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': '7185645', 'n_tokens_mistral': 2070, 'n_tokens_neox': 2030, 'n_words': 369}