# Generated by Django 3.2.13 on 2022-07-20 09:04

from django.db import migrations, models, OperationalError, ProgrammingError
import django_migration_linter as linter


class AddFieldIfNotExists(migrations.AddField):
    """
    Adds a field and ignores "duplicate column" error in case the field already exists.
    When migrating back it will not delete the field.
    """

    def database_forwards(self, app_label, schema_editor, from_state, to_state):
        try:
            super().database_forwards(app_label, schema_editor, from_state, to_state)
        except OperationalError:
            pass
        except ProgrammingError as e:  # ignore if the field already exists
            if "already exists" in str(e):
                pass

    def database_backwards(self, app_label, schema_editor, from_state, to_state):
        pass


class Migration(migrations.Migration):
    """
    This migration tries to create two fields cached_render_for_web and active_cache_for_web_calculation_id.
    In case these fields already exist, this migration will do nothing.
    In case the database was already affected by the previous version of the 0004 migration,
    it will recreate these fields.
    """

    atomic = False
    dependencies = [
        ('alerts', '0004_auto_20220711_1106'),
    ]

    operations = [
        linter.IgnoreMigration(),
        AddFieldIfNotExists(
            model_name='alertgroup',
            name='cached_render_for_web',
            field=models.JSONField(default=dict),
        ),
        AddFieldIfNotExists(
            model_name='alertgroup',
            name='active_cache_for_web_calculation_id',
            field=models.CharField(default=None, max_length=100, null=True),
        ),
    ]
