Skip to content

Commit

Permalink
Handle data types for primary keys - character conversion only
Browse files Browse the repository at this point in the history
  • Loading branch information
anthonysena committed Apr 30, 2024
1 parent 02a5906 commit 0af8648
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: ResultModelManager
Title: Result Model Manager
Version: 0.5.6
Version: 0.5.7
Authors@R:
person("Jamie", "Gilbert", , "gilbert@ohdsi.org", role = c("aut", "cre"))
Description: Database data model management utilities for OHDSI packages.
Expand Down
36 changes: 36 additions & 0 deletions R/DataModel.R
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,10 @@ uploadChunk <- function(chunk, pos, env, specifications, resultsFolder, connecti

# Check if inserting data would violate primary key constraints:
if (!is.null(env$primaryKeyValuesInDb)) {
chunk <- formatChunk(
pkValuesInDb = env$primaryKeyValuesInDb,
chunk = chunk
)
primaryKeyValuesInChunk <- unique(chunk[env$primaryKey])
duplicates <-
dplyr::inner_join(env$primaryKeyValuesInDb,
Expand Down Expand Up @@ -791,3 +795,35 @@ loadResultsDataModelSpecifications <- function(filePath) {
assertSpecificationColumns(colnames(spec))
return(spec)
}


#' This helper function will convert the data in the
#' primary key values in the `chunk` which is read from
#' the csv file to the format of the primary key data
#' retrieved from the database (`pkValuesInDb`). The assumption made
#' by this function is that the `pkValuesInDb` reflect the proper data
#' types while `chunk` is the best guess from the readr
#' package. In the future, if we adopt strongly-types data.frames
#' this will no longer be necessary.
#'
#' Another assumption of this function is that we're only attempting to
#' recast to a character data type and not try to handle different type
#' conversions.
formatChunk <- function(pkValuesInDb, chunk) {
for (columnName in names(pkValuesInDb)) {
if (class(pkValuesInDb[[columnName]]) != class(chunk[[columnName]])) {
if (class(pkValuesInDb[[columnName]]) == "character") {
chunk <- chunk %>% dplyr::mutate_at(columnName, as.character)

Check warning on line 816 in R/DataModel.R

View check run for this annotation

Codecov / codecov/patch

R/DataModel.R#L815-L816

Added lines #L815 - L816 were not covered by tests
} else {
errorMsg <- paste0(
columnName,
" is of type ",
class(pkValuesInDb[[columnName]]),
" which cannot be converted between data frames pkValuesInDb and chunk"

Check warning on line 822 in R/DataModel.R

View check run for this annotation

Codecov / codecov/patch

R/DataModel.R#L818-L822

Added lines #L818 - L822 were not covered by tests
)
stop(errorMsg)

Check warning on line 824 in R/DataModel.R

View check run for this annotation

Codecov / codecov/patch

R/DataModel.R#L824

Added line #L824 was not covered by tests
}
}
}
return(chunk)
}

0 comments on commit 0af8648

Please sign in to comment.