<commit_before>SET search_path = notification, pg_catalog;


CREATE OR REPLACE FUNCTION action(anyelement, text)
    RETURNS anyelement
AS $$
BEGIN
    EXECUTE $2;

    RETURN $1;
END;
$$ LANGUAGE plpgsql VOLATILE;


CREATE OR REPLACE FUNCTION to_char(notification.notificationstore)
    RETURNS text
AS $$
    SELECT datasource.name
    FROM directory.datasource
    WHERE datasource.id = $1.datasource_id;
$$ LANGUAGE SQL STABLE STRICT;


CREATE OR REPLACE FUNCTION get_notificationstore(datasource_name name)
    RETURNS notificationstore
AS $$
    SELECT ns
    FROM notification.notificationstore ns
    JOIN directory.datasource ds ON ds.id = ns.datasource_id
    WHERE ds.name = $1;
$$ LANGUAGE SQL STABLE;


CREATE OR REPLACE FUNCTION table_name(notification.notificationstore)
    RETURNS name
AS $$
    SELECT ds.name::name
        FROM directory.datasource ds
        WHERE
            ds.id = $1.datasource_id;
$$ LANGUAGE SQL STABLE;


CREATE OR REPLACE FUNCTION staging_table_name(notification.notificationstore)
    RETURNS name
AS $$
    SELECT (notification.table_name($1) || '_staging')::name;
$$ LANGUAGE SQL STABLE;


CREATE OR REPLACE FUNCTION notification.create_table(notificationstore)
    RETURNS notificationstore
AS $$
BEGIN
    EXECUTE format(
        'CREATE TABLE notification.%I ('
        '  id serial PRIMARY KEY,'
        '  entity_id integer NOT NULL,'
        '  "timestamp" timestamp with time zone NOT NULL'
        ');', notification.table_name($1));

    EXECUTE format('ALTER TABLE notification.%I OWNER TO minerva_writer;',
        notification.table_name($1));

    EXECUTE format('GRANT SELECT ON TABLE notification.%I TO minerva;',
        notification.table_name($1));

    EXECUTE format(
        'GRANT INSERT,DELETE,UPDATE '
        'ON TABLE notification.%I TO minerva_writer;',
        notification.table_name($1));

    EXECUTE format('CREATE INDEX %I ON notification.%I USING btree (timestamp);',
        'idx_notification_' || notification.table_name($1) || '_timestamp', notification.table_name($1));

    RETURN $1;
END;
$$ LANGUAGE plpgsql VOLATILE;


CREATE OR REPLACE FUNCTION notification.create_staging_table(notificationstore)
    RETURNS notificationstore
AS $$
BEGIN
    EXECUTE format(
        'CREATE UNLOGGED TABLE notification.%I ('
        '  entity_id integer NOT NULL,'
        '  "timestamp" timestamp with time zone NOT NULL'
        ');', notification.staging_table_name($1));

    EXECUTE format('ALTER TABLE notification.%I OWNER TO minerva_writer;',
        notification.staging_table_name($1));

    EXECUTE format('GRANT SELECT ON TABLE notification.%I TO minerva;',
        notification.staging_table_name($1));

    EXECUTE format(
        'GRANT INSERT,DELETE,UPDATE '
        'ON TABLE notification.%I TO minerva_writer;',
        notification.staging_table_name($1));

    RETURN $1;
END;
$$ LANGUAGE plpgsql VOLATILE;


CREATE OR REPLACE FUNCTION table_exists(schema_name name, table_name name)
    RETURNS boolean
AS $$
    SELECT exists(
        SELECT 1
        FROM pg_class
        JOIN pg_namespace ON pg_class.relnamespace = pg_namespace.oid
        WHERE relname=$2 AND relkind = 'r' AND pg_namespace.nspname = $1
    );
$$ LANGUAGE SQL STABLE;


CREATE OR REPLACE FUNCTION table_exists(name)
    RETURNS boolean
AS $$
    SELECT notification.table_exists('notification', $1);
$$ LANGUAGE SQL STABLE;


CREATE OR REPLACE FUNCTION column_exists(schema_name name, table_name name, column_name name)
    RETURNS boolean
AS $$
    SELECT EXISTS(
        SELECT 1
        FROM pg_attribute a
        JOIN pg_class c ON c.oid = a.attrelid
        JOIN pg_namespace n ON c.relnamespace = n.oid
        WHERE n.nspname = $1 AND c.relname = $2 AND a.attname = $3;
    );
$$ LANGUAGE sql STABLE;


CREATE OR REPLACE FUNCTION column_exists(table_name name, column_name name)
    RETURNS boolean
AS $$
    SELECT notification.column_exists('notification', $1, $2);
$$ LANGUAGE sql STABLE;


CREATE OR REPLACE FUNCTION create_notificationstore(datasource_id integer)
    RETURNS notification.notificationstore
AS $$
    INSERT INTO notification.notificationstore(datasource_id, version) VALUES ($1, 1) RETURNING *;
$$ LANGUAGE SQL VOLATILE;


CREATE OR REPLACE FUNCTION create_notificationstore(datasource_name text)
    RETURNS notification.notificationstore
AS $$
    SELECT notification.create_notificationstore((directory.name_to_datasource($1)).id);
$$ LANGUAGE SQL VOLATILE;
<commit_msg>Fix typo<commit_after>SET search_path = notification, pg_catalog;


CREATE OR REPLACE FUNCTION action(anyelement, text)
    RETURNS anyelement
AS $$
BEGIN
    EXECUTE $2;

    RETURN $1;
END;
$$ LANGUAGE plpgsql VOLATILE;


CREATE OR REPLACE FUNCTION to_char(notification.notificationstore)
    RETURNS text
AS $$
    SELECT datasource.name
    FROM directory.datasource
    WHERE datasource.id = $1.datasource_id;
$$ LANGUAGE SQL STABLE STRICT;


CREATE OR REPLACE FUNCTION get_notificationstore(datasource_name name)
    RETURNS notificationstore
AS $$
    SELECT ns
    FROM notification.notificationstore ns
    JOIN directory.datasource ds ON ds.id = ns.datasource_id
    WHERE ds.name = $1;
$$ LANGUAGE SQL STABLE;


CREATE OR REPLACE FUNCTION table_name(notification.notificationstore)
    RETURNS name
AS $$
    SELECT ds.name::name
        FROM directory.datasource ds
        WHERE
            ds.id = $1.datasource_id;
$$ LANGUAGE SQL STABLE;


CREATE OR REPLACE FUNCTION staging_table_name(notification.notificationstore)
    RETURNS name
AS $$
    SELECT (notification.table_name($1) || '_staging')::name;
$$ LANGUAGE SQL STABLE;


CREATE OR REPLACE FUNCTION notification.create_table(notificationstore)
    RETURNS notificationstore
AS $$
BEGIN
    EXECUTE format(
        'CREATE TABLE notification.%I ('
        '  id serial PRIMARY KEY,'
        '  entity_id integer NOT NULL,'
        '  "timestamp" timestamp with time zone NOT NULL'
        ');', notification.table_name($1));

    EXECUTE format('ALTER TABLE notification.%I OWNER TO minerva_writer;',
        notification.table_name($1));

    EXECUTE format('GRANT SELECT ON TABLE notification.%I TO minerva;',
        notification.table_name($1));

    EXECUTE format(
        'GRANT INSERT,DELETE,UPDATE '
        'ON TABLE notification.%I TO minerva_writer;',
        notification.table_name($1));

    EXECUTE format('CREATE INDEX %I ON notification.%I USING btree (timestamp);',
        'idx_notification_' || notification.table_name($1) || '_timestamp', notification.table_name($1));

    RETURN $1;
END;
$$ LANGUAGE plpgsql VOLATILE;


CREATE OR REPLACE FUNCTION notification.create_staging_table(notificationstore)
    RETURNS notificationstore
AS $$
BEGIN
    EXECUTE format(
        'CREATE UNLOGGED TABLE notification.%I ('
        '  entity_id integer NOT NULL,'
        '  "timestamp" timestamp with time zone NOT NULL'
        ');', notification.staging_table_name($1));

    EXECUTE format('ALTER TABLE notification.%I OWNER TO minerva_writer;',
        notification.staging_table_name($1));

    EXECUTE format('GRANT SELECT ON TABLE notification.%I TO minerva;',
        notification.staging_table_name($1));

    EXECUTE format(
        'GRANT INSERT,DELETE,UPDATE '
        'ON TABLE notification.%I TO minerva_writer;',
        notification.staging_table_name($1));

    RETURN $1;
END;
$$ LANGUAGE plpgsql VOLATILE;


CREATE OR REPLACE FUNCTION table_exists(schema_name name, table_name name)
    RETURNS boolean
AS $$
    SELECT exists(
        SELECT 1
        FROM pg_class
        JOIN pg_namespace ON pg_class.relnamespace = pg_namespace.oid
        WHERE relname=$2 AND relkind = 'r' AND pg_namespace.nspname = $1
    );
$$ LANGUAGE SQL STABLE;


CREATE OR REPLACE FUNCTION table_exists(name)
    RETURNS boolean
AS $$
    SELECT notification.table_exists('notification', $1);
$$ LANGUAGE SQL STABLE;


CREATE OR REPLACE FUNCTION column_exists(schema_name name, table_name name, column_name name)
    RETURNS boolean
AS $$
    SELECT EXISTS(
        SELECT 1
        FROM pg_attribute a
        JOIN pg_class c ON c.oid = a.attrelid
        JOIN pg_namespace n ON c.relnamespace = n.oid
        WHERE n.nspname = $1 AND c.relname = $2 AND a.attname = $3
    );
$$ LANGUAGE sql STABLE;


CREATE OR REPLACE FUNCTION column_exists(table_name name, column_name name)
    RETURNS boolean
AS $$
    SELECT notification.column_exists('notification', $1, $2);
$$ LANGUAGE sql STABLE;


CREATE OR REPLACE FUNCTION create_notificationstore(datasource_id integer)
    RETURNS notification.notificationstore
AS $$
    INSERT INTO notification.notificationstore(datasource_id, version) VALUES ($1, 1) RETURNING *;
$$ LANGUAGE SQL VOLATILE;


CREATE OR REPLACE FUNCTION create_notificationstore(datasource_name text)
    RETURNS notification.notificationstore
AS $$
    SELECT notification.create_notificationstore((directory.name_to_datasource($1)).id);
$$ LANGUAGE SQL VOLATILE;

{'fraction_non_alphanumeric': 0.08847874720357941, 'fraction_numerical': 0.006935123042505592, 'mean_word_length': 3.632642487046632, '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': '7656403', 'n_tokens_mistral': 2878, 'n_tokens_neox': 2683, 'n_words': 759}