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

from django.db import models, migrations, connection

class Migration(migrations.Migration):

    dependencies = [
        ('channels', '0020_auto_20150714_0258'),
    ]

    def install_updated_increment(apps, schema_editor):
        #language=SQL
        update_trigger = """
            CREATE OR REPLACE FUNCTION temba_decrement_channelcount(_channel_id INTEGER, _count_type VARCHAR(2), _count_day DATE) RETURNS VOID AS $$
              BEGIN
                INSERT INTO channels_channelcount("channel_id", "count_type", "day", "count")
                  VALUES(_channel_id, _count_type, _count_day, -1);
                PERFORM temba_maybe_squash_channelcount(_channel_id, _count_type, _count_day);
              END;
            $$ LANGUAGE plpgsql;

            CREATE OR REPLACE FUNCTION temba_increment_channelcount(_channel_id INTEGER, _count_type VARCHAR(2), _count_day DATE) RETURNS VOID AS $$
              BEGIN
                INSERT INTO channels_channelcount("channel_id", "count_type", "day", "count")
                  VALUES(_channel_id, _count_type, _count_day, 1);
                PERFORM temba_maybe_squash_channelcount(_channel_id, _count_type, _count_day);
              END;
            $$ LANGUAGE plpgsql;

            CREATE OR REPLACE FUNCTION temba_maybe_squash_channelcount(_channel_id INTEGER, _count_type VARCHAR(2), _count_day DATE) RETURNS VOID AS $$
              DECLARE
               current_count INT;
              BEGIN
                IF RANDOM() < .01 THEN
                  IF _count_day IS NULL THEN
                    WITH D as (DELETE FROM channels_channelcount
                      WHERE "channel_id" = _channel_id AND "count_type" = _count_type AND "day" IS NULL
                      RETURNING "count")
                      INSERT INTO channels_channelcount("channel_id", "count_type", "count")
                      VALUES (_channel_id, _count_type, GREATEST(0, (SELECT SUM("count") FROM D)));
                  ELSE
                    WITH D as (DELETE FROM channels_channelcount
                      WHERE "channel_id" = _channel_id AND "count_type" = _count_type AND "day" = _count_day
                      RETURNING "count")
                      INSERT INTO channels_channelcount("channel_id", "count_type", "day", "count")
                      VALUES (_channel_id, _count_type, _count_day, GREATEST(0, (SELECT SUM("count") FROM D)));
                  END IF;
                END IF;
              END;
            $$ LANGUAGE plpgsql;<commit_msg>Better naming of removed, remove unneeded DECLARE<commit_after># -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations, connection

class Migration(migrations.Migration):

    dependencies = [
        ('channels', '0020_auto_20150714_0258'),
    ]

    def install_updated_increment(apps, schema_editor):
        #language=SQL
        update_trigger = """
            CREATE OR REPLACE FUNCTION temba_decrement_channelcount(_channel_id INTEGER, _count_type VARCHAR(2), _count_day DATE) RETURNS VOID AS $$
              BEGIN
                INSERT INTO channels_channelcount("channel_id", "count_type", "day", "count")
                  VALUES(_channel_id, _count_type, _count_day, -1);
                PERFORM temba_maybe_squash_channelcount(_channel_id, _count_type, _count_day);
              END;
            $$ LANGUAGE plpgsql;

            CREATE OR REPLACE FUNCTION temba_increment_channelcount(_channel_id INTEGER, _count_type VARCHAR(2), _count_day DATE) RETURNS VOID AS $$
              BEGIN
                INSERT INTO channels_channelcount("channel_id", "count_type", "day", "count")
                  VALUES(_channel_id, _count_type, _count_day, 1);
                PERFORM temba_maybe_squash_channelcount(_channel_id, _count_type, _count_day);
              END;
            $$ LANGUAGE plpgsql;

            CREATE OR REPLACE FUNCTION temba_maybe_squash_channelcount(_channel_id INTEGER, _count_type VARCHAR(2), _count_day DATE) RETURNS VOID AS $$
              BEGIN
                IF RANDOM() < .005 THEN
                  IF _count_day IS NULL THEN
                    WITH removed as (DELETE FROM channels_channelcount
                      WHERE "channel_id" = _channel_id AND "count_type" = _count_type AND "day" IS NULL
                      RETURNING "count")
                      INSERT INTO channels_channelcount("channel_id", "count_type", "count")
                      VALUES (_channel_id, _count_type, GREATEST(0, (SELECT SUM("count") FROM removed)));
                  ELSE
                    WITH removed as (DELETE FROM channels_channelcount
                      WHERE "channel_id" = _channel_id AND "count_type" = _count_type AND "day" = _count_day
                      RETURNING "count")
                      INSERT INTO channels_channelcount("channel_id", "count_type", "day", "count")
                      VALUES (_channel_id, _count_type, _count_day, GREATEST(0, (SELECT SUM("count") FROM removed)));
                  END IF;
                END IF;
              END;
            $$ LANGUAGE plpgsql;
{'fraction_non_alphanumeric': 0.0805239838181468, 'fraction_numerical': 0.010209978809477943, 'mean_word_length': 2.1447607510599638, 'pattern_counts': {'":': 0, '<': 5, '<?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': '4259324', 'n_tokens_mistral': 1571, 'n_tokens_neox': 1397, 'n_words': 366}