<commit_before>CREATE OR REPLACE FUNCTION update_entity_relationship_activation_history() RETURNS trigger AS $body$
DECLARE
    row RECORD;
    last_history_row RECORD;
    last_history_row_was_activated BOOL;
BEGIN
    -----------------------------------------------------------------
    -- Default values
    -----------------------------------------------------------------
    last_history_row_was_activated = FALSE;
    last_history_row = NULL;
    row = NULL;

    -----------------------------------------------------------------
    -- Get the row
    -----------------------------------------------------------------
    IF (TG_OP = 'INSERT') THEN
        row = NEW;
    ELSE
        row = OLD;
    END IF;

    -----------------------------------------------------------------
    -- Get the last history row that exists
    -----------------------------------------------------------------
    SELECT
        *
    INTO
        last_history_row
    FROM
        entity_history_entityrelationshipactivationevent
    WHERE
        sub_entity_id = row.sub_entity_id
    AND
        super_entity_id = row.super_entity_id
    ORDER BY
        time DESC
    LIMIT
        1;

    -----------------------------------------------------------------
    -- Compute what the last history row was_activated flag was
    -----------------------------------------------------------------
    IF last_history_row IS NOT NULL THEN
        last_history_row_was_activated = last_history_row.was_activated;
    END IF;

    ----------------------------------------------------------------------------------------------------
    -- Handle the case where this is a new relationship, and the last row was not found or was active
    ----------------------------------------------------------------------------------------------------
    IF (TG_OP = 'INSERT' AND last_history_row_was_activated IS FALSE) THEN
        INSERT INTO entity_history_entityrelationshipactivationevent(
            sub_entity_id,
            super_entity_id,
            time,
            was_activated
        )
        VALUES (
            NEW.sub_entity_id,
            NEW.super_entity_id,
            CAST(CLOCK_TIMESTAMP() at time zone 'utc' AS timestamp),
            TRUE
        );
    END IF;

    ----------------------------------------------------------------------------------------------------
    -- Handle the case where the relationship is removed.
    -- We check the table name to ensure this was not a cascade delete.
    ----------------------------------------------------------------------------------------------------
    IF (TG_OP = 'DELETE' AND last_history_row_was_activated IS TRUE) THEN
        INSERT INTO entity_history_entityrelationshipactivationevent(
            sub_entity_id,
            super_entity_id,
            time,
            was_activated
        )
        VALUES (
            OLD.sub_entity_id,
            OLD.super_entity_id,
            CAST(CLOCK_TIMESTAMP() at time zone 'utc' AS timestamp),
            FALSE
        );
    END IF;
    RETURN NEW;

EXCEPTION WHEN OTHERS THEN
    RETURN NEW;
<commit_msg>Remove exception handler<commit_after>CREATE OR REPLACE FUNCTION update_entity_relationship_activation_history() RETURNS trigger AS $body$
DECLARE
    row RECORD;
    last_history_row RECORD;
    last_history_row_was_activated BOOL;
BEGIN
    -----------------------------------------------------------------
    -- Default values
    -----------------------------------------------------------------
    last_history_row_was_activated = FALSE;
    last_history_row = NULL;
    row = NULL;

    -----------------------------------------------------------------
    -- Get the row
    -----------------------------------------------------------------
    IF (TG_OP = 'INSERT') THEN
        row = NEW;
    ELSE
        row = OLD;
    END IF;

    -----------------------------------------------------------------
    -- Get the last history row that exists
    -----------------------------------------------------------------
    SELECT
        *
    INTO
        last_history_row
    FROM
        entity_history_entityrelationshipactivationevent
    WHERE
        sub_entity_id = row.sub_entity_id
    AND
        super_entity_id = row.super_entity_id
    ORDER BY
        time DESC
    LIMIT
        1;

    -----------------------------------------------------------------
    -- Compute what the last history row was_activated flag was
    -----------------------------------------------------------------
    IF last_history_row IS NOT NULL THEN
        last_history_row_was_activated = last_history_row.was_activated;
    END IF;

    ----------------------------------------------------------------------------------------------------
    -- Handle the case where this is a new relationship, and the last row was not found or was active
    ----------------------------------------------------------------------------------------------------
    IF (TG_OP = 'INSERT' AND last_history_row_was_activated IS FALSE) THEN
        INSERT INTO entity_history_entityrelationshipactivationevent(
            sub_entity_id,
            super_entity_id,
            time,
            was_activated
        )
        VALUES (
            NEW.sub_entity_id,
            NEW.super_entity_id,
            CAST(CLOCK_TIMESTAMP() at time zone 'utc' AS timestamp),
            TRUE
        );
    END IF;

    ----------------------------------------------------------------------------------------------------
    -- Handle the case where the relationship is removed.
    ----------------------------------------------------------------------------------------------------
    IF (TG_OP = 'DELETE' AND last_history_row_was_activated IS TRUE) THEN
        INSERT INTO entity_history_entityrelationshipactivationevent(
            sub_entity_id,
            super_entity_id,
            time,
            was_activated
        )
        VALUES (
            OLD.sub_entity_id,
            OLD.super_entity_id,
            CAST(CLOCK_TIMESTAMP() at time zone 'utc' AS timestamp),
            FALSE
        );
    END IF;
    RETURN NEW;
{'fraction_non_alphanumeric': 0.3326829268292683, 'fraction_numerical': 0.0003252032520325203, 'mean_word_length': 0.8471471471471471, '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': '2498241', 'n_tokens_mistral': 1353, 'n_tokens_neox': 1248, 'n_words': 355}