-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Full history: 699813b refactor `dbListTables()` with `list_tables()`, now orders result by `table_type` and `table_name` refactor `dbExistsTable()` with `list_tables()` refactor `dbListObjects()` with `list_tables()` merge `find_table()` code into `list_fields()` `find_table()` isn't used anywhere else anymore (e.g. `exists_table()`) simplify the "get current_schemas() as table" code pass full `id` to `list_fields()` align `dbExistsTable()` with `dbListFields()` add some comments and whitespace simplify `where_schema` in `list_tables()` align `where_table` with `where_schema` in `list_tables()` add `system_catalogs` argument to `dbConnect()` add materialized view tests `list_tables()`: query system catalogs if available `list_fields()`: query system catalogs if available
- Loading branch information
Showing
9 changed files
with
221 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#' Run an expression that creates a materialized view and clean up afterwards. | ||
#' | ||
#' @param con PqConnection. The database connection. | ||
#' @param matview character. The materialized view name. | ||
#' @param query character. A SELECT, TABLE, or VALUES command to populate the | ||
#' materialized view. | ||
#' @param expr expression. The R expression to execute. | ||
#' @return the return value of the \code{expr}. | ||
#' @seealso https://www.postgresql.org/docs/current/sql-creatematerializedview.html | ||
with_matview <- function(con, matview, query, expr) { | ||
on.exit( | ||
DBI::dbExecute(con, paste0("DROP MATERIALIZED VIEW IF EXISTS ", matview)), | ||
add = TRUE | ||
) | ||
dbExecute( | ||
con, | ||
paste0( | ||
"CREATE MATERIALIZED VIEW IF NOT EXISTS ", matview, | ||
" AS ", query | ||
) | ||
) | ||
|
||
force(expr) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#' Run an expression that creates a schema and clean up afterwards. | ||
#' | ||
#' @param con PqConnection. The database connection. | ||
#' @param schema character. The schema name. | ||
#' @param expr expression. The R expression to execute. | ||
#' @return the return value of the \code{expr}. | ||
with_schema <- function(con, schema, expr) { | ||
on.exit( | ||
DBI::dbExecute(con, paste0("DROP SCHEMA IF EXISTS ", schema)), | ||
add = TRUE | ||
) | ||
DBI::dbExecute(con, paste0("CREATE SCHEMA IF NOT EXISTS ", schema)) | ||
force(expr) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
if (postgresHasDefault()) { | ||
with_database_connection(con = postgresDefault(system_catalogs = TRUE), { | ||
|
||
skip_if( | ||
is(con, "RedshiftConnection"), | ||
"Redshift doesn't expose system catalogs" | ||
) | ||
skip_if_not( | ||
dbExistsTable(con, Id(schema = "pg_catalog", table = "pg_class")), | ||
"`pg_catalog`.`pg_class` not available" | ||
) | ||
|
||
test_that("Materialized View is listed", { | ||
with_matview(con, "matview1", "SELECT 1 AS col1", { | ||
|
||
expect_true(dbExistsTable(con, "matview1")) | ||
expect_true("matview1" %in% dbListTables(con)) | ||
|
||
objects <- dbListObjects(con) | ||
quoted_tables <- | ||
vapply( | ||
objects$table, | ||
dbQuoteIdentifier, | ||
conn = con, | ||
character(1) | ||
) | ||
expect_true(dbQuoteIdentifier(con, "matview1") %in% quoted_tables) | ||
|
||
expect_true("col1" %in% dbListFields(con, "matview1")) | ||
}) | ||
}) | ||
|
||
test_that("Materialized View in custom schema is listed", { | ||
with_schema(con, "matschema1", { | ||
matview_id <- Id(schema = "matschema1", table = "matview1") | ||
matview_chr <- "matschema1.matview1" | ||
|
||
with_matview(con, matview_chr, "SELECT 1 AS col1", { | ||
|
||
expect_true(dbExistsTable(con, matview_id)) | ||
|
||
objects <- dbListObjects(con, prefix = Id(schema = "matschema1")) | ||
quoted_tables <- | ||
vapply( | ||
objects$table, | ||
dbQuoteIdentifier, | ||
conn = con, | ||
character(1) | ||
) | ||
expect_true(dbQuoteIdentifier(con, matview_id) %in% quoted_tables) | ||
|
||
expect_true("col1" %in% dbListFields(con, matview_id)) | ||
}) | ||
}) | ||
}) | ||
}) | ||
} |