Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

doc: How to use Postgres variables in test SQL file #22

Open
coolaj86 opened this issue Feb 2, 2025 · 0 comments
Open

doc: How to use Postgres variables in test SQL file #22

coolaj86 opened this issue Feb 2, 2025 · 0 comments

Comments

@coolaj86
Copy link
Contributor

coolaj86 commented Feb 2, 2025

For testing a dynamic query with variables:

Simple Example

set session my.vars.customer_id = '0192bad7-f177-7578-8b03-700c33509ea1' ;

SELECT * FROM customers WHERE id = current_setting('my.vars.customer_id')::uuid \gx

More Complete Example

set session my.vars.customer_id = '0192bad7-f177-7578-8b03-700c33509ea1' ;
set session my.vars.types = 'email,password,phone' ;
set session my.vars.include_revoked = TRUE ;

SELECT
    "authc"."id" AS "id",
    "authc"."priority" AS "priority",
    "authc"."type" AS "type",
    "authc"."value" AS "value",
    "authc"."verified_at" AS "verified_at",
    "authc"."created_at" AS "created_at",
    "authc"."updated_at" AS "updated_at",
    "authc"."revoked_at" AS "revoked_at",
    CASE
        WHEN "authc"."details" IS NULL THEN NULL
        ELSE my_decrypt( "authc"."details" )
    END AS "details"
FROM
    "authenticators" AS "authc"
WHERE
    "authc"."customer_id" = current_setting('my.vars.customer_id')::uuid
    AND (
        "authc"."type" = ANY(string_to_array(current_setting('my.vars.types')::varchar, ','))
         OR string_to_array(current_setting('my.vars.types')::varchar, ',') IS NULL
        OR array_length(string_to_array(current_setting('my.vars.types')::varchar, ','), 1) = 0
    )
    AND (
        current_setting('my.vars.include_revoked')::bool = TRUE
            OR "revoked_at" IS NULL
    )
ORDER BY
    "authc"."priority" ASC,
    "authc"."value" ASC
\gx
@coolaj86 coolaj86 changed the title doc: How to use Postgres variables in raw SQL query doc: How to use Postgres variables in test SQL file Feb 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant