From 8d292225635e3c3b0ac5db897cfe1d2d935523df Mon Sep 17 00:00:00 2001 From: Maxim Moinat Date: Wed, 18 Aug 2021 19:42:01 +0200 Subject: [PATCH] refactor index creation and cleanup codeToRun --- R/writeIndex.R | 37 +++-- extras/codeToRun.R | 131 +----------------- ...v5_3_1.sql => OMOP CDM indices v5.3.1.sql} | 0 ...ces v6_0.sql => OMOP CDM indices v6.0.sql} | 0 man/writeIndex.Rd | 2 +- 5 files changed, 24 insertions(+), 146 deletions(-) rename inst/sql/sql_server/{OMOP CDM indices v5_3_1.sql => OMOP CDM indices v5.3.1.sql} (100%) rename inst/sql/sql_server/{OMOP CDM indices v6_0.sql => OMOP CDM indices v6.0.sql} (100%) diff --git a/R/writeIndex.R b/R/writeIndex.R index e9777b8..ca90f4f 100644 --- a/R/writeIndex.R +++ b/R/writeIndex.R @@ -17,27 +17,22 @@ #' Write Index script #' #' @param targetdialect The dialect of the target database. Choices are "oracle", "postgresql", "pdw", "redshift", "impala", "netezza", "bigquery", "sql server" -#' @param cdmVersion The version of the CDM that you are creating the primary keys for -#' @param cdmDatabaseSchema The name of the schema where the cdm sits. -#' @param sqlFilename The name of the file that should be rendered and translated to a different dbms. This is either "OMOP CDM indices v5_3_1.sql" or "OMOP CDM indices v6_0.sql". +#' @param cdmVersion The version of the CDM that you are creating the indices for. e.g. 5.3.1 +#' @param cdmDatabaseSchema The name of the schema where the cdm sits. Defaults to "@cdmDatabaseSchema" #' #' @export -writeIndex <- function(targetdialect, cdmVersion, cdmDatabaseSchema, sqlFilename) { -if(!dir.exists("output")){ - dir.create("output") -} - -if(!dir.exists(paste0("output/",targetdialect))){ - dir.create(paste0("output/",targetdialect)) -} - -sql <- SqlRender::loadRenderTranslateSql(sqlFilename = sqlFilename, - packageName = "CdmDdlBase", - dbms = targetdialect, - targetdialect = targetdialect, - cdmDatabaseSchema = cdmDatabaseSchema) - -SqlRender::writeSql(sql = sql, - targetFile = paste0("output/",targetdialect,"/OMOP CDM ",targetdialect," ", cdmVersion, " indices.sql")) - +writeIndex <- function(targetdialect, cdmVersion, cdmDatabaseSchema = "@cdmDatabaseSchema") { + outputpath <- file.path("ddl", cdmVersion, targetdialect) + dir.create(outputpath, showWarnings = FALSE, recursive = TRUE) + + sqlFilename <- paste0("OMOP CDM indices v", cdmVersion, ".sql") + sql <- SqlRender::loadRenderTranslateSql(sqlFilename = sqlFilename, + packageName = "CdmDdlBase", + dbms = targetdialect, + targetdialect = targetdialect, + cdmDatabaseSchema = cdmDatabaseSchema) + + filename <- paste("OMOPCDM", targetdialect, cdmVersion, "indices.sql", sep = " ") + SqlRender::writeSql(sql = sql, + targetFile = file.path(outputpath, filename)) } diff --git a/extras/codeToRun.R b/extras/codeToRun.R index 838aec7..2f8da06 100644 --- a/extras/codeToRun.R +++ b/extras/codeToRun.R @@ -1,105 +1,8 @@ -#This script is meant to create the OMOP Common Data Model DDLs for each dialect as well as the pdf of the documentation. - -# Step 1-2 from README: Create new csv files "..._Field_Level.csv" and "..._Table_Level.csv" in inst/csv for the new version and make changes to the files reflecting -# the new CDM versions. Set the below variable to indicate the version of the cdm you are creating. This will be used for the name of the pdf so, for -# example, write v5.3 as v5_3. - - cdmVersion <- "5.3.1" - -# Step 3: After creating the csv files for the new version, create the sql server DDL from the file - - ddl <- CdmDdlBase::createDdlFromFile(cdmVersion) - - # Step 3.1: Create the primary key constraints for the new version - - primaryKeys <- CdmDdlBase::createPkFromFile(cdmVersion) - - # Step 3.2: Create the foreign key constraints for the new version - - foreignKeys <- CdmDdlBase::createFkFromFile(cdmVersion) -# At this point you should rebuild the package - -# Step 4: Run the following code to render the DDLs for each dialect. These will be used for testing on the ohdsi servers which is why the cdmDatabaseSchema is specified. - -writeDDL(targetdialect = "oracle", - cdmVersion = cdmVersion, - sqlFilename = paste0("OMOP CDM ddl ", cdmVersion, " ", Sys.Date(), ".sql"), - cdmDatabaseSchema = "OHDSI", - cleanUpScript = F) #oracle syntax for removing tables is weird, set this to F and make any changes to the raw file - -writeDDL(targetdialect = "postgresql", - cdmVersion = cdmVersion, - sqlFilename = paste0("OMOP CDM ddl ", cdmVersion, " ", Sys.Date(), ".sql"), - cdmDatabaseSchema = "ohdsi", - cleanUpScript = F) #This needs to be updated manually right now - -writeDDL(targetdialect = "sql server", - cdmVersion = cdmVersion, - sqlFilename = paste0("OMOP CDM ddl ", cdmVersion, " ", Sys.Date(), ".sql"), - cdmDatabaseSchema = "ohdsi.dbo", - cleanUpScript = F) #This needs to be updated manually right now - -# Step 5: Run the following code to render the primary key constraints for Oracle, Postgres, and Sql Server - -writePrimaryKeys(targetdialect = "oracle", - cdmVersion = cdmVersion, - sqlFilename = paste0("OMOP CDM pk ", cdmVersion, " ", Sys.Date(), ".sql"), - cdmDatabaseSchema = "OHDSI") - - -writePrimaryKeys(targetdialect = "postgresql", - cdmVersion = cdmVersion, - sqlFilename = paste0("OMOP CDM pk ", cdmVersion, " ", Sys.Date(), ".sql"), - cdmDatabaseSchema = "ohdsi") - - -writePrimaryKeys(targetdialect = "sql server", - cdmVersion = cdmVersion, - sqlFilename = paste0("OMOP CDM pk ", cdmVersion, " ", Sys.Date(), ".sql"), - cdmDatabaseSchema = "ohdsi.dbo") - -# Step 6: Run the following code to render the foreign key constraints for Oracle, Postgres, and Sql Server - -writeConstraints("oracle", - cdmVersion, - sqlFileName = paste0("OMOP CDM fk ", cdmVersion, " ", Sys.Date(), ".sql"), - "OHDSI") - -writeConstraints("postgresql", - cdmVersion, - sqlFileName = paste0("OMOP CDM fk ", cdmVersion, " ", Sys.Date(), ".sql"), - "ohdsi") - -writeConstraints("sql server", - cdmVersion, - sqlFileName = paste0("OMOP CDM fk ", cdmVersion, " ", Sys.Date(), ".sql"), - "ohdsi.dbo") - -# Step 7: Run the following code to render the indices for Oracle, Postgres, and Sql Server - -writeIndex("oracle", ### NOTE: ORACLE CREATES AUTO INDEXING AND NEED TO UPDATE INST/INDEX FILE TO REPRESENT IT - cdmVersion, - sqlFilename = "OMOP CDM indices v6_0.sql", - "OHDSI") - -writeIndex("postgresql", - cdmVersion, - sqlFilename = "OMOP CDM indices v6_0.sql", - "ohdsi") - -writeIndex("sql server", - cdmVersion, - sqlFilename = "OMOP CDM indices v6_0.sql", - "ohdsi.dbo") - - -############################## -# RUN THE TESTTHAT.R TO TEST ORACLE, POSTGRES, AND SQLSERVER - - -# Step 8: After testing the files for Oracle, Postgres, and Sql Server run the following to create the files for all dialects. Oracle -# Postgres and Sql Server are rewritten to overwrite the cdmDatabaseSchema with a token. +# This script is meant to create the OMOP Common Data Model DDLs for each dialect as well as the pdf of the documentation. +# For a given cdmVersion, create all ddl sql files for every sql dialect +# Results are written to ddl/cdm_version/dialect. +cdmVersion <- "5.3.1" for (targetdialect in c("oracle", "postgresql", "pdw", "redshift", "impala", "netezza", "bigquery", "sql server")) { writeDDL(targetdialect = targetdialect, @@ -110,32 +13,12 @@ for (targetdialect in c("oracle", "postgresql", "pdw", "redshift", "impala", "ne writeConstraints(targetdialect = targetdialect, cdmVersion = cdmVersion) + + writeIndex(targetdialect = targetdialect, + cdmVersion = cdmVersion) } -## write all indices - -writeIndex("oracle", ### NOTE: ORACLE CREATES AUTO INDEXING AND NEED TO UPDATE INST/INDEX FILE TO REPRESENT IT - cdmVersion, - sqlFilename = "OMOP CDM indices v5_3_1.sql", - "@cdmDatabaseSchema") - -writeIndex("postgresql", - cdmVersion, - sqlFilename = "OMOP CDM indices v5_3_1.sql", - "@cdmDatabaseSchema") - -writeIndex("sql server", - cdmVersion, - sqlFilename = "OMOP CDM indices v5_3_1.sql", - "@cdmDatabaseSchema") - -writeIndex("pdw", - cdmVersion, - sqlFilename = "OMOP CDM indices v5_3_1.sql", - "@cdmDatabaseSchema") - - ############# # BE SURE TO RUN THE EXTRAS/SITEMAINTENANCE.R BEFORE CREATING THE PDF diff --git a/inst/sql/sql_server/OMOP CDM indices v5_3_1.sql b/inst/sql/sql_server/OMOP CDM indices v5.3.1.sql similarity index 100% rename from inst/sql/sql_server/OMOP CDM indices v5_3_1.sql rename to inst/sql/sql_server/OMOP CDM indices v5.3.1.sql diff --git a/inst/sql/sql_server/OMOP CDM indices v6_0.sql b/inst/sql/sql_server/OMOP CDM indices v6.0.sql similarity index 100% rename from inst/sql/sql_server/OMOP CDM indices v6_0.sql rename to inst/sql/sql_server/OMOP CDM indices v6.0.sql diff --git a/man/writeIndex.Rd b/man/writeIndex.Rd index 1b2983d..9ee74bd 100644 --- a/man/writeIndex.Rd +++ b/man/writeIndex.Rd @@ -13,7 +13,7 @@ writeIndex(targetdialect, cdmVersion, cdmDatabaseSchema, sqlFilename) \item{cdmDatabaseSchema}{The name of the schema where the cdm sits.} -\item{sqlFilename}{The name of the file that should be rendered and translated to a different dbms. This is either "OMOP CDM indices v5_3_1.sql" or "OMOP CDM indices v6_0.sql".} +\item{sqlFilename}{The name of the file that should be rendered and translated to a different dbms. This is either "OMOP CDM indices v5.3.1.sql" or "OMOP CDM indices v6.0.sql".} } \description{ Write Index script