From d1f684a4f3e4c05998ed83d3e4d07ae88703d581 Mon Sep 17 00:00:00 2001 From: Costa <47926492N@ICO.SCS.local> Date: Fri, 29 Oct 2021 15:01:45 +0200 Subject: [PATCH] =?UTF-8?q?A=C3=B1adida=20funci=C3=B3n=20para=20crear=20un?= =?UTF-8?q?a=20copia=20de=20seguridad=20de=20la=20base=20de=20datos.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Docs/sqlFunctions-doc.Rmd | 42 ++++++++++++++ Docs/sqlFunctions-doc.html | 116 ++++++++++++++++++++++++++++--------- sqlFunctions.R | 11 ++++ workflow.R | 5 +- 4 files changed, 145 insertions(+), 29 deletions(-) diff --git a/Docs/sqlFunctions-doc.Rmd b/Docs/sqlFunctions-doc.Rmd index fe3eb3f..0929061 100644 --- a/Docs/sqlFunctions-doc.Rmd +++ b/Docs/sqlFunctions-doc.Rmd @@ -87,6 +87,48 @@ sqlInitialize<-function(){ --- +## sqlBackUp + +### Description +Creates a Back Up copy of the database. + +### Usage +sqlBackUp(dbfile=file,bu.dir="BU_OVARIO") + +### Arguments +Argument|Description +---|--- +dbfile| Database File location. +bu.dir| Directory under the DB file where the back up will be placed. + +### Details +Creates a Back Up copy of the database. It adds the date in front of the back up file. + +### Value +Invisibly for success (and failures cause errors). + +### Examples +```r +sqlInitialize() +sqlBackUp() +``` + +### Function +```r +sqlBackUp<-function(dbfile=file,bu.dir="BU_OVARIO"){ + db=strsplit(dbfile, "/")[[1]]%>% tail(n=1) + bu_path<-gsub(db,bu.dir,dbfile) + if (!dir.exists(bu_path)){ + dir.create(bu_path) + print(paste0("Back Up directory ", bu_path, " created")) + } + cp_bu<-paste0(bu_path, "/", format(Sys.time(), format="%Y%m%d"),"-",db) + file.copy(dbfile, cp_bu) +} +``` + +--- + ## sqlShowSamples ### Description diff --git a/Docs/sqlFunctions-doc.html b/Docs/sqlFunctions-doc.html index 6afce93..e3eb3f8 100644 --- a/Docs/sqlFunctions-doc.html +++ b/Docs/sqlFunctions-doc.html @@ -171,6 +171,7 @@ pre code {
Creates a Back Up copy of the database.
+sqlBackUp(dbfile=file,bu.dir=“BU_OVARIO”)
+Argument | +Description | +
---|---|
dbfile | +Database File location. | +
bu.dir | +Directory under the DB file where the back up will be placed. | +
Creates a Back Up copy of the database. It adds the date in front of the back up file.
+Invisibly for success (and failures cause errors).
+sqlInitialize()
+sqlBackUp()
+sqlBackUp<-function(dbfile=file,bu.dir="BU_OVARIO"){
+ db=strsplit(dbfile, "/")[[1]]%>% tail(n=1)
+ bu_path<-gsub(db,bu.dir,dbfile)
+ if (!dir.exists(bu_path)){
+ dir.create(bu_path)
+ print(paste0("Back Up directory ", bu_path, " created"))
+ }
+ cp_bu<-paste0(bu_path, "/", format(Sys.time(), format="%Y%m%d"),"-",db)
+ file.copy(dbfile, cp_bu)
+}
+Shows if there are already samples from the specified NHCs.
sqlShowSamples(conn=dta, nhcs=nhc.test, verb=F)
Takes the NHCs listed in the nhcs vector and checks if there are already samples from those patients.
A data.frame with information about the patients.
dta<-odbcConnect("test")
nhc.test<-c("XXXXXXXX","XXXXXXX")
sqlShowSamples()
sqlShowSamples<-function(conn=dta, nhcs=nhc.test, verb=F){
if (isFALSE(verb)){
@@ -359,15 +419,15 @@ sqlShowSamples()
Generates new consecutive OVID code for the patients that are not found in the DB.
sqlGenOVID(conn=dta, nhcs=nhc.test, verb=T, sinc=F)
Generates new consecutive OVID code for the patients that are not found in the DB.
If verb is TRUE, it returns a data.frame.
dta<-odbcConnect("test")
nhc.test<-c("XXXXXXXX","XXXXXXX")
sqlGenOVID(sinc=T)
sqlGenOVID<-function(conn=dta, nhcs=nhc.test, verb=T, sinc=F){
ovid<-sqlFetch(conn,"OVID")
@@ -440,15 +500,15 @@ sqlGenOVID(sinc=T)
Fills the Query Template file with the OVID and OV newly generated codes.
sqlWriteTemp(conn=dta, nhcs=nhc.test, file=“queryOV.xlsx”, samples.mod=T, clinics.mod=T)
Fills the Query Template file with the OVID and OV newly generated codes. It is required that the DB has been updated with the sqlGenOVID function. It replaces previous content in the template file sheets that are filled. In the case of “CLINICS” table, if there were already an entry in the DB for that OVID code, the template file is filled with that information.
Invisibly for success (and failures cause errors).
dta<-odbcConnect("test")
nhc.test<-c("XXXXXXXX","XXXXXXX")
sqlGenOVID(sinc=T)
sqlWriteTemp()
sqlWriteTemp<-function(conn=dta, nhcs=nhc.test, file="queryOV.xlsx", samples.mod=T, clinics.mod=T){
upd.ovid<-sqlFetch(conn, "OVID") %>% filter(NHC %in% nhcs)
@@ -535,15 +595,15 @@ sqlWriteTemp()
Updates the DB with the information filled in the template file.
sqlSincBD(conn=dta, filetemp=“QueryOV.xlsx”, sinc.samples=F, sinc.clinics=F)
Updates the DB with the information filled in the template file. All the “samples” entries are added as new rows (as all samples are new even if the patient was already in the DB). The new patients included in the “CLINICS” sheet are introduced in the DB as new rows and the ones that were already there are modified in its previous row location.
Invisibly for success (and failures cause errors).
dta<-odbcConnect("test")
nhc.test<-c("XXXXXXXX","XXXXXXX")
@@ -592,7 +652,7 @@ sqlGenOVID(sinc=T)
sqlWriteTemp()
sqlSincBD(sinc.samples=T, sinc.clinics=T)
sqlSincBD<-function(conn=dta, filetemp="QueryOV.xlsx", sinc.samples=F, sinc.clinics=F){
## Añadir código de muestra nueva a la base de datos
diff --git a/sqlFunctions.R b/sqlFunctions.R
index 4414782..f488453 100644
--- a/sqlFunctions.R
+++ b/sqlFunctions.R
@@ -15,6 +15,17 @@ sqlInitialize<-function(){
source("ruta_database.R", encoding = "UTF-8")
}
+sqlBackUp<-function(dbfile=file,bu.dir="BU_OVARIO"){
+ db=strsplit(dbfile, "/")[[1]]%>% tail(n=1)
+ bu_path<-gsub(db,bu.dir,dbfile)
+ if (!dir.exists(bu_path)){
+ dir.create(bu_path)
+ print(paste0("Back Up directory ", bu_path, " created"))
+ }
+ cp_bu<-paste0(bu_path, "/", format(Sys.time(), format="%Y%m%d"),"-",db)
+ file.copy(dbfile, cp_bu)
+}
+
sqlShowSamples<-function(conn=dta, nhcs=nhc.test, verb=F){
if (isFALSE(verb)){
sqlQuery(conn, "SELECT O.NHC,S.*
diff --git a/workflow.R b/workflow.R
index 24beb52..5acb390 100644
--- a/workflow.R
+++ b/workflow.R
@@ -1,5 +1,8 @@
source("sqlFunctions.R")
+# Crear copia de seguridad
+sqlBackUp()
+
# Cargar dependencias
sqlInitialize()
@@ -21,4 +24,4 @@ sqlWriteTemp()
####
# Sincronizar lo rellenado en las pestañas samples y clinics
-sqlSincBD(sinc.samples = T, sinc.clinics = T)
\ No newline at end of file
+sqlSincBD(sinc.samples = T, sinc.clinics = T)