Skip to content

Move the drug era script into its own SQL file and add buildDrugEra() - #824

Open
DG47 wants to merge 1 commit into
OHDSI:mainfrom
DG47:refactor/drug-era-sql-function
Open

DG47 wants to merge 1 commit into
OHDSI:mainfrom
DG47:refactor/drug-era-sql-function

Conversation

@DG47

@DG47 DG47 commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

First of the two stacked PRs @MaximMoinat asked for in #822. This one only moves code and adds a function; the SQL is unchanged. The #761 fix goes on top of it in #822.

Change

  • inst/sql/sql_server/OMOP_CDM_drug_era.sql: the drug era script from the SQL Scripts page, byte for byte the block that was in site/sqlScripts.qmd.
  • site/sqlScripts.qmd: the "Drug Eras" section includes that file with a non-evaluated sql chunk. The page also gets engine: knitr, because otherwise Quarto picks Jupyter for a page whose only chunk is sql.
  • R/buildDrugEra.R: new exported buildDrugEra(connectionDetails, cdmDatabaseSchema, tempEmulationSchema = getOption("sqlRenderTempEmulationSchema"), ...), modelled on executeDdl(). It renders the script for the CDM schema, translates it to connectionDetails$dbms and runs it with DatabaseConnector::executeSql. tempEmulationSchema is there because the script uses a temp table (#tmp_de). man/buildDrugEra.Rd and the NAMESPACE export come from roxygen2 8.1.0, the version in DESCRIPTION.
  • tests/testthat/test-buildDrugEra.R: runs buildDrugEra() on a SQLite CDM (tables from createDdl()) for CDM 5.3, 5.4 and 5.5, with the two scenarios from Drug era calculation gap_days calculation #761 plus one that checks a gap of more than 30 days starts a new era and that persons are kept apart. It checks era dates and exposure counts. It doesn't check gap_days, since that's the value Drug era calculation gap_days calculation #761 changes; Fix off-by-one in drug era gap_days calculation (#761) #822 adds those assertions.
  • docs/sqlScripts.html: only the changes from re-rendering the page with Quarto 1.10.19, applied as the diff between renders of main and this branch. They're the knitr code block wrapper and shifted line ids; the visible text is the same. docs/search.json is left for the next full render.

The script inserts into DRUG_ERA without deleting anything first, and buildDrugEra() keeps that, so the docs say DRUG_ERA is expected to be empty.

Testing

R CMD INSTALL --library=<tmp> .
testthat::test_dir("tests/testthat")   # 81 expectations passed, 0 failed, 1 test skipped (executeDdl needs database env vars)
R CMD check --no-manual               # no WARNINGs or ERRORs
quarto render site/sqlScripts.qmd     # Quarto 1.10.19

The drug era example script on the SQL Scripts page now lives in
inst/sql/sql_server/OMOP_CDM_drug_era.sql, unchanged, and the page includes it from there
(engine: knitr, so the page renders the sql chunk without Jupyter).

buildDrugEra() renders that script for a CDM schema, translates it with SqlRender and runs it with
DatabaseConnector, like executeDdl(). The tests run it on a SQLite CDM for every supported CDM
version with the scenarios from OHDSI#761 and check the era dates and exposure counts.

docs/sqlScripts.html only has the changes from re-rendering the page.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant