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

add_user_read_access() fails for usernames requiring double quotes #30

Open
mthomas-ketchbrook opened this issue Dec 12, 2024 · 0 comments · May be fixed by #31
Open

add_user_read_access() fails for usernames requiring double quotes #30

mthomas-ketchbrook opened this issue Dec 12, 2024 · 0 comments · May be fixed by #31
Assignees
Labels
bug Something isn't working

Comments

@mthomas-ketchbrook
Copy link
Collaborator

Issue

For database usernames that must be double-quoted in a PostgreSQL expression (e.g., usr-abc would require a statement such as GRANT SELECT ON ALL TABLES IN SCHEMA public TO "usr-abc";, the add_user_read_access() function is currently failing.

The expression is currently being created with single quotes instead of the double quotes required by PostgreSQL:

> fcall:::add_user_read_access(conn, "usr-abc")
Error: Failed to prepare query : ERROR:  syntax error at or near "'usr-abc'"
LINE 1: GRANT SELECT ON ALL TABLES IN SCHEMA public TO 'usr-abc';
                                                       ^

Proposed Solution

Wrap the username in DBI::SQL() before passing it to glue::glue_sql(), and provide double-quotes around the {glue} string, i.e.:

add_user_read_access <- function(conn, username) {

  username <- DBI::SQL(username)

  stmt <- glue::glue_sql(
    'GRANT SELECT ON ALL TABLES IN SCHEMA public TO "{username}";',
    .con = conn
  )

  DBI::dbExecute(
    conn = conn,
    statement = stmt
  )

}
@mthomas-ketchbrook mthomas-ketchbrook added the bug Something isn't working label Dec 12, 2024
@mthomas-ketchbrook mthomas-ketchbrook self-assigned this Dec 12, 2024
mthomas-ketchbrook added a commit that referenced this issue Dec 12, 2024
@mthomas-ketchbrook mthomas-ketchbrook linked a pull request Dec 12, 2024 that will close this issue
@mthomas-ketchbrook mthomas-ketchbrook linked a pull request Dec 12, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant