<commit_before># -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations, connection

class Migration(migrations.Migration):

    def calculate_counts(apps, schema_editor):
        """
        Iterate across all our channels, calculate how many errors and non-errors they have
        """
        Channel = apps.get_model('channels', 'Channel')
        for channel in Channel.objects.all():
            channel.success_log_count = channel.logs.filter(is_error=False).count()
            channel.error_log_count = channel.logs.filter(is_error=True).count()
            channel.save()

    def install_channellog_trigger(apps, schema_editor):
        """
        Installs a Postgres trigger that will increment or decrement our our success and error
        log counts based on insertion in channels_channellog
        """
        #language=SQL
        install_trigger = """
            CREATE OR REPLACE FUNCTION update_channellog_count() RETURNS TRIGGER AS $$
            BEGIN
              -- ChannelLog being added
              IF TG_OP = 'INSERT' THEN
                -- Error, increment our error count
                IF NEW.is_error THEN
                  UPDATE channels_channel SET error_log_count=error_log_count+1 WHERE id=NEW.channel_id;
                -- Success, increment that count instead
                ELSE
                  UPDATE channels_channel SET success_log_count=success_log_count+1 WHERE id=NEW.channel_id;
                END IF;

              -- ChannelLog being removed
              ELSIF TG_OP = 'DELETE' THEN
                -- Error, decrement our error count
                if OLD.is_error THEN
                  UPDATE channels_channel SET error_log_count=error_log_count-1 WHERE id=OLD.channel_id;
                -- Success, decrement that count instead
                ELSE
                  UPDATE channels_channel SET success_log_count=success_log_count-1 WHERE id=OLD.channel_id;
                END IF;

              -- Updating is_error is forbidden
              ELSIF TG_OP = 'UPDATE' THEN
                RAISE EXCEPTION 'Cannot update is_error or channel_id on ChannelLog events';

              -- Table being cleared, reset all counts
              ELSIF TG_OP = 'TRUNCATE' THEN
                UPDATE channels_channel SET error_log_count=0, success_log_count=0;
              END IF;

              RETURN NEW;
            END;
            $$ LANGUAGE plpgsql;

            -- Install INSERT, UPDATE and DELETE triggers<commit_msg>NULL is more better than NEW<commit_after># -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations, connection

class Migration(migrations.Migration):

    def calculate_counts(apps, schema_editor):
        """
        Iterate across all our channels, calculate how many errors and non-errors they have
        """
        Channel = apps.get_model('channels', 'Channel')
        for channel in Channel.objects.all():
            channel.success_log_count = channel.logs.filter(is_error=False).count()
            channel.error_log_count = channel.logs.filter(is_error=True).count()
            channel.save()

    def install_channellog_trigger(apps, schema_editor):
        """
        Installs a Postgres trigger that will increment or decrement our our success and error
        log counts based on insertion in channels_channellog
        """
        #language=SQL
        install_trigger = """
            CREATE OR REPLACE FUNCTION update_channellog_count() RETURNS TRIGGER AS $$
            BEGIN
              -- ChannelLog being added
              IF TG_OP = 'INSERT' THEN
                -- Error, increment our error count
                IF NEW.is_error THEN
                  UPDATE channels_channel SET error_log_count=error_log_count+1 WHERE id=NEW.channel_id;
                -- Success, increment that count instead
                ELSE
                  UPDATE channels_channel SET success_log_count=success_log_count+1 WHERE id=NEW.channel_id;
                END IF;

              -- ChannelLog being removed
              ELSIF TG_OP = 'DELETE' THEN
                -- Error, decrement our error count
                if OLD.is_error THEN
                  UPDATE channels_channel SET error_log_count=error_log_count-1 WHERE id=OLD.channel_id;
                -- Success, decrement that count instead
                ELSE
                  UPDATE channels_channel SET success_log_count=success_log_count-1 WHERE id=OLD.channel_id;
                END IF;

              -- Updating is_error is forbidden
              ELSIF TG_OP = 'UPDATE' THEN
                RAISE EXCEPTION 'Cannot update is_error or channel_id on ChannelLog events';

              -- Table being cleared, reset all counts
              ELSIF TG_OP = 'TRUNCATE' THEN
                UPDATE channels_channel SET error_log_count=0, success_log_count=0;
              END IF;

              RETURN NULL;
            END;
            $$ LANGUAGE plpgsql;

            -- Install INSERT, UPDATE and DELETE triggers
{'fraction_non_alphanumeric': 0.06326129666011787, 'fraction_numerical': 0.00275049115913556, 'mean_word_length': 1.9158075601374571, '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': '6884024', 'n_tokens_mistral': 1372, 'n_tokens_neox': 1261, 'n_words': 394}