R/initialise.R

Defines functions dettl_create_log_table

Documented in dettl_create_log_table

#' Initialise the database by creating log table if it doesn't already exist
#'
#' @param path Path to import directory containing db connection configuration.
#' @param db_name The name of the db to connect to. Connection info must be
#' configured via the \code{dettl_config.yml}.
#'
#' @export
#' @examples
#' path <- dettl:::prepare_test_import(
#'   system.file("examples", "person_information", package = "dettl"),
#'   system.file("examples", "dettl_config.yml", package = "dettl"),
#'   add_log_table = FALSE
#' )
#' dettl::dettl_create_log_table(file.path(path, "person_information"), "test")
dettl_create_log_table <- function(path, db_name) {
  ## Get the connection info
  path <- normalizePath(path, winslash = "/", mustWork = TRUE)
  con <- db_connect(db_name, path)
  on.exit(DBI::dbDisconnect(con))
  ## Create the table
  dialect <- sql_dialect(con)
  if (is.null(dialect)) {
    stop(sprintf(
      "Can't initialise DB %s as not a SQLite or Postgres type. Got %s.",
      db_name, class(con)
    ))
  }
  path <- dettl_file("sql", dialect, "create_log_table.sql")
  query <- read_lines(path)
  message(sprintf("Creating log table in DB %s.", db_name))
  DBI::dbExecute(con, query)
}
vimc/dettl documentation built on Oct. 6, 2022, 2:13 p.m.