Skip to content

Commit

Permalink
chore: move getTableSignature to separate files
Browse files Browse the repository at this point in the history
  • Loading branch information
RasmusSkytte committed Feb 1, 2024
1 parent e2c4934 commit 5f043bd
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 55 deletions.
47 changes: 0 additions & 47 deletions R/create_table.R
Original file line number Diff line number Diff line change
Expand Up @@ -70,50 +70,3 @@ create_table <- function(.data, conn = NULL, db_table_id, ...) {

return(invisible(dplyr::tbl(conn, db_table_id, check_from = FALSE)))
}



#' @importFrom methods setGeneric
methods::setGeneric("getTableSignature",
function(.data, conn = NULL) standardGeneric("getTableSignature"),
signature = "conn")

methods::setMethod("getTableSignature", "DBIConnection", function(.data, conn) {
# Define the column types to be updated based on backend class
col_types <- DBI::dbDataType(conn, .data)

backend_coltypes <- list(
"PqConnection" = c(
checksum = "TEXT",
from_ts = "TIMESTAMP",
until_ts = "TIMESTAMP"
),
"SQLiteConnection" = c(
checksum = "TEXT",
from_ts = "TEXT",
until_ts = "TEXT"
),
"Microsoft SQL Server" = c(
checksum = "varchar(32)",
from_ts = "DATETIME2",
until_ts = "DATETIME2"
)
)

checkmate::assert_choice(class(conn), names(backend_coltypes))

# Update columns with indices instead of names to avoid conflicts
special_cols <- backend_coltypes[[class(conn)]]
special_indices <- (1 + length(.data) - length(special_cols)):length(.data)

return(replace(col_types, special_indices, special_cols))
})

methods::setMethod("getTableSignature", "NULL", function(.data, conn) {
# Emulate product of DBI::dbDataType
signature <- dplyr::summarise(.data, dplyr::across(tidyselect::everything(), ~ class(.)[1]))

stats::setNames(as.character(signature), names(signature))

return(signature)
})
44 changes: 44 additions & 0 deletions R/getTableSignature.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#' @importFrom methods setGeneric
methods::setGeneric("getTableSignature",
function(.data, conn = NULL) standardGeneric("getTableSignature"),
signature = "conn")

methods::setMethod("getTableSignature", "DBIConnection", function(.data, conn) {
# Define the column types to be updated based on backend class
col_types <- DBI::dbDataType(conn, .data)

Check warning on line 8 in R/getTableSignature.R

View check run for this annotation

Codecov / codecov/patch

R/getTableSignature.R#L8

Added line #L8 was not covered by tests

backend_coltypes <- list(
"PqConnection" = c(
checksum = "TEXT",
from_ts = "TIMESTAMP",
until_ts = "TIMESTAMP"

Check warning on line 14 in R/getTableSignature.R

View check run for this annotation

Codecov / codecov/patch

R/getTableSignature.R#L10-L14

Added lines #L10 - L14 were not covered by tests
),
"SQLiteConnection" = c(
checksum = "TEXT",
from_ts = "TEXT",
until_ts = "TEXT"

Check warning on line 19 in R/getTableSignature.R

View check run for this annotation

Codecov / codecov/patch

R/getTableSignature.R#L16-L19

Added lines #L16 - L19 were not covered by tests
),
"Microsoft SQL Server" = c(
checksum = "varchar(32)",
from_ts = "DATETIME2",
until_ts = "DATETIME2"

Check warning on line 24 in R/getTableSignature.R

View check run for this annotation

Codecov / codecov/patch

R/getTableSignature.R#L21-L24

Added lines #L21 - L24 were not covered by tests
)
)

checkmate::assert_choice(class(conn), names(backend_coltypes))

Check warning on line 28 in R/getTableSignature.R

View check run for this annotation

Codecov / codecov/patch

R/getTableSignature.R#L28

Added line #L28 was not covered by tests

# Update columns with indices instead of names to avoid conflicts
special_cols <- backend_coltypes[[class(conn)]]
special_indices <- (1 + length(.data) - length(special_cols)):length(.data)

Check warning on line 32 in R/getTableSignature.R

View check run for this annotation

Codecov / codecov/patch

R/getTableSignature.R#L31-L32

Added lines #L31 - L32 were not covered by tests

return(replace(col_types, special_indices, special_cols))

Check warning on line 34 in R/getTableSignature.R

View check run for this annotation

Codecov / codecov/patch

R/getTableSignature.R#L34

Added line #L34 was not covered by tests
})

methods::setMethod("getTableSignature", "NULL", function(.data, conn) {
# Emulate product of DBI::dbDataType
signature <- dplyr::summarise(.data, dplyr::across(tidyselect::everything(), ~ class(.)[1]))

stats::setNames(as.character(signature), names(signature))

return(signature)
})
8 changes: 0 additions & 8 deletions tests/testthat/test-create_table.R
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,3 @@ test_that("create_table() does not overwrite tables", {
DBI::dbDisconnect(conn)
}
})


test_that("getTableSignature() generates a signature for NULL connections", {
expect_identical(
lapply(cars, class),
as.list(getTableSignature(cars, conn = NULL))
)
})
6 changes: 6 additions & 0 deletions tests/testthat/test-getTableSignature.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
test_that("getTableSignature() generates a signature for NULL connections", {
expect_identical(
lapply(cars, class),
as.list(getTableSignature(cars, conn = NULL))
)
})

0 comments on commit 5f043bd

Please sign in to comment.