<commit_before>create or replace function add_item_key_to_entry(item_content_key text) returns void as $$
BEGIN
    IF item_content_key is NULL or item_content_key='' then
        RAISE EXCEPTION 'Item key parameter cannot be null or empty';
    end if;

    IF exists(select 1 from item) and not exists(select 1 from item where content ? item_content_key) then
        RAISE EXCEPTION 'Item content does not have key: %', item_content_key;
    end if;

    IF NOT EXISTS(SELECT 1 FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME='entry' AND COLUMN_NAME='key') then
        alter table entry add column key varchar;
    ELSE
        RAISE NOTICE 'Column key already exists in entry table';
    END IF;

    IF EXISTS(select 1 from entry where not exists(select 1 from item where item.content->>item_content_key=entry.key)) THEN
        update entry set key = content->>item_content_key from item where entry.sha256hex=item.sha256hex;
    ELSE
        RAISE NOTICE 'Data already copied from items[%]', item_content_key;
    END IF;
END;
$$ language plpgsql;

BEGIN;
-- item content has a "primary" key which maps to the register name
select add_item_key_to_entry('${registerName}');
COMMIT;

drop function if exists add_item_key_to_entry(item_content_key text);<commit_msg>Remove additional checks for column and data existance from 'add_item_key_to_entry' migration script<commit_after>create or replace function add_item_key_to_entry(item_content_key text) returns void as $$
BEGIN
    IF item_content_key is NULL or item_content_key='' then
        RAISE EXCEPTION 'Item key parameter cannot be null or empty';
    end if;

    IF exists(select 1 from item) and not exists(select 1 from item where content ? item_content_key) then
        RAISE EXCEPTION 'Item content does not have key: %', item_content_key;
    end if;

    alter table entry add column key varchar;
    update entry set key = content->>item_content_key from item where entry.sha256hex=item.sha256hex;
END;
$$ language plpgsql;

BEGIN;
-- item content has a "primary" key which maps to the register name
select add_item_key_to_entry('${registerName}');
COMMIT;

drop function if exists add_item_key_to_entry(item_content_key text);
{'fraction_non_alphanumeric': 0.06584922797456857, 'fraction_numerical': 0.00862851952770209, 'mean_word_length': 4.3341404358353515, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '6609207', 'n_tokens_mistral': 677, 'n_tokens_neox': 641, 'n_words': 252}