Annotation: assert.if_views_compile

CREATE OR REPLACE FUNCTION assert.if_views_compile(VARIADIC _schema_name text[], OUT message text, OUT result boolean)
RETURNS record

Information: assert.if_views_compile

Schema assert
Function Name if_views_compile
Arguments VARIADIC _schema_name text[], OUT message text, OUT result boolean
Owner postgres
Result Type record
Description

Implementation: assert.if_views_compile

CREATE OR REPLACE FUNCTION assert.if_views_compile(VARIADIC _schema_name text[], OUT message text, OUT result boolean)
 RETURNS record
 LANGUAGE plpgsql
AS $function$

    DECLARE message                     test_result;
    DECLARE current_view                RECORD;
    DECLARE current_view_name           text;
    DECLARE command_text                text;
    DECLARE failed_views                text;
BEGIN
    FOR current_view IN 
        SELECT table_name, table_schema 
        FROM information_schema.views
        WHERE table_schema = ANY($1) 
    LOOP

    BEGIN
        current_view_name := quote_ident(current_view.table_schema)  || '.' || quote_ident(current_view.table_name);
        command_text := 'SELECT * FROM ' || current_view_name || ' LIMIT 1;';

        RAISE NOTICE '%', command_text;
        
        EXECUTE command_text;

        EXCEPTION WHEN OTHERS THEN
            IF(failed_views IS NULL) THEN 
                failed_views := '';
            END IF;

            failed_views := failed_views || E'\n' || command_text || E'\n' || SQLERRM || E'\n';                
    END;


    END LOOP;

    IF(failed_views != '') THEN
        message := E'The test if_views_compile failed. The following views failed to compile : \n\n' || failed_views;
        result := false;
        PERFORM assert.fail(message);
        RETURN;
    END IF;

    RETURN;
END;
$function$