<commit_before>"""
Migration that addresses the problem reported in GH#3006
On core.migrations.0061, a couple of settings got switched from the general
group to the styling group. The codebase was also change to lookup the settings
using the styling group.
However, the journal_defaults.json was never changed, which has lead to 2
possible wrong states.

Case A: pre-existing installation ran migration core.0061:
    - In this state the settings where swapped correctly to styling. however, 
      due to the setting group not being updated, a duplicate setting with the
      group general was re-introduced. This setting will not have any relevant
      values attached, since the codebase has been using the correct setting
      since
    - Action: Delete the setting associated with the general group
Case B: New installations since core.0061 was introduced:
    - In this state, migration 0061 would have run before the journal_defaults
      would've have been loaded. As a result, the styling group exists but
      the settings have always been on the general group. It is safe to delete
      these settings, because the codebase would have never allowed their values
      to be changed.
    - Action: Swap the group setting of the existing setting
      (essentially run 0061 again)

"""
from __future__ import unicode_literals

from django.db import migrations, models


STYLING_SETTINGS = [
    "enable_editorial_images",
    "multi_page_editorial",
]


def update_settings_group(apps, setting_names, from_group, to_group):
    Setting = apps.get_model("core", "Setting")
    SettingGroup = apps.get_model("core", "SettingGroup")
    setting_group, _ = SettingGroup.objects.get_or_create(name=to_group)
    settings = Setting.objects.filter(
        name__in=STYLING_SETTINGS, group__name=from_group,
    )
    settings_with_from = settings.filter(group__name=from_group)
    settings_with_to = settings.filter(group__name=to_group)
    if settings_with_from.exists() and settings_with_to.exists():
        # Case A
        settings_with_from.delete()
    elif settings_with_from.exists() and not settings_with_to.exists():
        # Case B
        setting = settings_with_from.first()
        setting.group = setting_group
        setting.save()
        settings_with_from.delete()


def general_to_styling(apps, schema_editor):
    update_settings_group(apps, STYLING_SETTINGS, "general", "styling")


class Migration(migrations.Migration):

    dependencies = [
        ('core', '0073_auto_20220630_1608'),
    ]

    operations = [
        migrations.RunPython(
            general_to_styling, reverse_code=migrations.RunPython.noop),
    ]<commit_msg>Fixes bad migration<commit_after>"""
Migration that addresses the problem reported in GH#3006
On core.migrations.0061, a couple of settings got switched from the general
group to the styling group. The codebase was also change to lookup the settings
using the styling group.
However, the journal_defaults.json was never changed, which has lead to 2
possible wrong states.

Case A: pre-existing installation ran migration core.0061:
    - In this state the settings where swapped correctly to styling. however, 
      due to the setting group not being updated, a duplicate setting with the
      group general was re-introduced. This setting will not have any relevant
      values attached, since the codebase has been using the correct setting
      since
    - Action: Delete the setting associated with the general group
Case B: New installations since core.0061 was introduced:
    - In this state, migration 0061 would have run before the journal_defaults
      would've have been loaded. As a result, the styling group exists but
      the settings have always been on the general group. It is safe to delete
      these settings, because the codebase would have never allowed their values
      to be changed.
    - Action: Swap the group setting of the existing setting
      (essentially run 0061 again)

"""
from __future__ import unicode_literals

from django.db import migrations, models


STYLING_SETTINGS = [
    "enable_editorial_images",
    "multi_page_editorial",
]


def update_settings_group(apps, setting_names, from_group, to_group):
    Setting = apps.get_model("core", "Setting")
    SettingGroup = apps.get_model("core", "SettingGroup")
    setting_group, _ = SettingGroup.objects.get_or_create(name=to_group)
    for setting_name in STYLING_SETTINGS:
        import pdb;pdb.set_trace()
        settings = Setting.objects.filter(
            name=setting_name,
        )
        settings_with_from = settings.filter(group__name=from_group)
        settings_with_to = settings.filter(group__name=to_group)
        if settings_with_from.exists() and settings_with_to.exists():
            # Case A
            settings_with_from.delete()
        elif settings_with_from.exists() and not settings_with_to.exists():
            # Case B
            setting = settings_with_from.first()
            setting.group = setting_group
            setting.save()
            settings_with_from.delete()


def general_to_styling(apps, schema_editor):
    update_settings_group(apps, STYLING_SETTINGS, "general", "styling")


class Migration(migrations.Migration):

    dependencies = [
        ('core', '0073_auto_20220630_1608'),
    ]

    operations = [
        migrations.RunPython(
            general_to_styling, reverse_code=migrations.RunPython.noop),
    ]
{'fraction_non_alphanumeric': 0.06588364837584879, 'fraction_numerical': 0.015048632776656266, 'mean_word_length': 3.5378850957535386, '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': '1457967', 'n_tokens_mistral': 1538, 'n_tokens_neox': 1470, 'n_words': 519}