refactor index creation and cleanup codeToRun

This commit is contained in:
Maxim Moinat 2021-08-18 19:42:01 +02:00
parent 5d9887a356
commit 8d29222563
5 changed files with 24 additions and 146 deletions

View File

@ -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))
}
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 = paste0("output/",targetdialect,"/OMOP CDM ",targetdialect," ", cdmVersion, " indices.sql"))
targetFile = file.path(outputpath, filename))
}

View File

@ -1,106 +1,9 @@
# 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.
# 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"
# 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.
for (targetdialect in c("oracle", "postgresql", "pdw", "redshift", "impala", "netezza", "bigquery", "sql server")) {
writeDDL(targetdialect = targetdialect,
cdmVersion = cdmVersion)
@ -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

View File

@ -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