Skip to content

Commit

Permalink
Merge pull request #379 from OHDSI/check_index
Browse files Browse the repository at this point in the history
Fix bug when reason clumn name for entry/exit is already in cohort
  • Loading branch information
edward-burn authored Nov 1, 2024
2 parents d5c7305 + b3d49b8 commit 9c1d98f
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 3 deletions.
7 changes: 7 additions & 0 deletions R/exitAtColumnDate.R
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,13 @@ exitAtColumnDate <- function(cohort,
reason <- "entry_reason"
}

if (reason %in% colnames(cohort) & returnReason) {
cli::cli_inform("Column {reason} will be overwritten.")
cohort <- cohort |> dplyr::select(!dplyr::all_of(reason))
} else if (reason %in% colnames(cohort) & !returnReason) {
reason <- "date_column_name_1234"
}

# check NA
checkNA <- cohort |>
dplyr::filter(dplyr::if_all(
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-entryAtColumnDate.R
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ test_that("test indexes - postgres", {
cohort_end_date = as.Date("2009-01-03"),
other_date = as.Date("2009-01-01")))
cdm$my_cohort <- omopgenerics::newCohortTable(cdm$my_cohort)
cdm$my_cohort <- entryAtFirstDate(cdm$my_cohort, dateColumns = c("cohort_end_date", "other_date"), returnReason = FALSE)
cdm$my_cohort <- entryAtFirstDate(cdm$my_cohort, dateColumns = c("cohort_end_date", "other_date"), returnReason = TRUE)

expect_true(
DBI::dbGetQuery(db, paste0("SELECT * FROM pg_indexes WHERE tablename = 'cc_my_cohort';")) |> dplyr::pull("indexdef") ==
Expand Down
4 changes: 2 additions & 2 deletions tests/testthat/test-exitAtColumnDate.R
Original file line number Diff line number Diff line change
Expand Up @@ -237,14 +237,14 @@ test_that("test indexes - postgres", {
cohort_end_date = as.Date("2009-01-03"),
other_date = as.Date("2009-01-01")))
cdm$my_cohort <- omopgenerics::newCohortTable(cdm$my_cohort)
cdm$my_cohort <- exitAtLastDate(cdm$my_cohort, dateColumns = c("cohort_end_date", "other_date"), returnReason = FALSE)
cdm$my_cohort <- exitAtLastDate(cdm$my_cohort, dateColumns = c("cohort_end_date", "other_date"), returnReason = TRUE)

expect_true(
DBI::dbGetQuery(db, paste0("SELECT * FROM pg_indexes WHERE tablename = 'cc_my_cohort';")) |> dplyr::pull("indexdef") ==
"CREATE INDEX cc_my_cohort_subject_id_cohort_start_date_idx ON public.cc_my_cohort USING btree (subject_id, cohort_start_date)"
)

cdm$my_cohort <- exitAtFirstDate(cdm$my_cohort, dateColumns = c("cohort_start_date", "cohort_end_date"), returnReason = FALSE)
cdm$my_cohort <- exitAtFirstDate(cdm$my_cohort, dateColumns = c("cohort_start_date", "cohort_end_date"), returnReason = TRUE)

expect_true(
DBI::dbGetQuery(db, paste0("SELECT * FROM pg_indexes WHERE tablename = 'cc_my_cohort';")) |> dplyr::pull("indexdef") ==
Expand Down
7 changes: 7 additions & 0 deletions tests/testthat/test-requireIsEntry.R
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,12 @@ test_that("test indexes - postgres", {
"CREATE INDEX cc_my_cohort_subject_id_cohort_start_date_idx ON public.cc_my_cohort USING btree (subject_id, cohort_start_date)"
)

cdm$my_cohort <- requireIsEntry(cdm$my_cohort, entryRange = c(1,Inf))
expect_true(
DBI::dbGetQuery(db, paste0("SELECT * FROM pg_indexes WHERE tablename = 'cc_my_cohort';")) |> dplyr::pull("indexdef") ==
"CREATE INDEX cc_my_cohort_subject_id_cohort_start_date_idx ON public.cc_my_cohort USING btree (subject_id, cohort_start_date)"
)

cdm$my_cohort <- requireIsFirstEntry(cdm$my_cohort)
expect_true(
DBI::dbGetQuery(db, paste0("SELECT * FROM pg_indexes WHERE tablename = 'cc_my_cohort';")) |> dplyr::pull("indexdef") ==
Expand All @@ -258,5 +264,6 @@ test_that("test indexes - postgres", {
"CREATE INDEX cc_my_cohort_subject_id_cohort_start_date_idx ON public.cc_my_cohort USING btree (subject_id, cohort_start_date)"
)

omopgenerics::dropTable(cdm = cdm, name = dplyr::starts_with("my_cohort"))
CDMConnector::cdm_disconnect(cdm = cdm)
})

0 comments on commit 9c1d98f

Please sign in to comment.