Repository navigation
Fix existsTable() ignoring databaseSchema on DBI Postgres connections - #342
Conversation
RPostgres' dbListTables() silently ignores the schema argument, so dbListTables(), getTableNames() and existsTable() on a connection made with createDbiConnectionDetails() listed the tables on the search path instead of the requested schema. Use the information schema query that is already used for DuckDB for PostgreSQL and Redshift as well. Fixes OHDSI#327
|
@schuemie requesting review |
|
Hi, would someone have time to look at this? It fixes #327: on connections from |
|
@schuemie could you have a look at this one? It fixes #327: with RPostgres connections from |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## develop #342 +/- ##
===========================================
- Coverage 56.43% 0.00% -56.44%
===========================================
Files 16 16
Lines 2803 2805 +2
===========================================
- Hits 1582 0 -1582
- Misses 1221 2805 +1584 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
A note on the two codecov failures, so they don't read as a problem with the change: the report comes from the fork run, where the repository secrets are not available. The job log shows |
|
|
|
I'll merge this, but note that bringing your own DBI driver is not recommended. All OHDSI packages (including DatabaseConnector itself) are tested using the default drivers in DatabaseConnector (JDBC for Postgres), so no guarantees can be made outside that set of drivers. |
Fixes #327.
Problem
For connections made with
createDbiConnectionDetails(),dbListTables(conn, databaseSchema = ...)falls through toDBI::dbListTables(conn@dbiConnection, schema = databaseSchema).RPostgres::dbListTables()does not have aschemaargument and silently drops it, so it returns the tables on the search path (usuallypublic) instead of the tables in the requested schema. BecausegetTableNames()andexistsTable()build on this, they give wrong answers:It can also return a false positive when a table with the same name exists in
public:existsTable(connection, "non_existing_schema", "person")returnsTRUE.Change
dbListTables()for DBI connections already has a branch that queriesinformation_schema.tablesfor DuckDB. This PR uses that same branch forpostgresqlandredshift, which is also whatgetTableNames()already does for a plainPqConnection/RedshiftConnection. Nothing changes for other platforms or for JDBC connections.Tests
I added a test to
tests/testthat/test-getTableNames.Rthat connects throughcreateDbiConnectionDetails()withRPostgres::Postgres(), using the existingCDM5_POSTGRESQL_*environment variables. It checks thatpersonis listed in the CDM schema, thatexistsTable()finds it, and thatexistsTable()returnsFALSEfor a schema that doesn't exist. The test is skipped when no Postgres test server is configured or RPostgres is not installed.CDM5_POSTGRESQL_*variables pointing at it). Without the fix it fails in both setups: whenpersonis only in the CDM schema, the positive checks fail, and when there is also apublic.person, the negative check fails. With the fix it passes in both.testthat::test_local()with only the local SQLite/DuckDB servers: 112 expectations passed, 0 failures. There were 4 errors, and I got the same 4 on unmodifieddevelop. They come from my local setup (no JDBC driver folder, bigrquery not installed).R CMD check --no-manual --ignore-vignettes: 0 errors, 0 warnings, 0 notes.I did not touch NEWS.md.