R/upsert_locations.R

Defines functions upsert_locations

Documented in upsert_locations

# ----------------------------------------------#
# Upsert location table                      ####
# ----------------------------------------------#

#' @title Location upsert
#' @description Upserts location tables to the NaTron database.
#'
#' @param conn Database connection object with write permissions (see \code{?natron_connect}). It's the connection object that determines if the data is upserted to the sandbox or not.
#' @param location_data Location table to be upserted/uploaded to NaTron (see \code{?location_table})
#' @family upsert functions
#' @return Pushes and upserts data to database. Returns nothing.
#' @examples
#' \dontrun{
#' upsert_location(location_data = myLocationTable, conn = myConnection)
#' }
#'
#' @import RPostgreSQL
#' @import dplyr
#'
#' @export
#'



  # function UPSERT m_dataset----
  upsert_locations <- function(location_data, conn){




  # get column names from NaTron
  tableinfo <- RPostgreSQL::dbGetQuery(conn,
                                       "select column_name
                        from information_schema.columns
                        where table_name = 'Locations'
                        ;")


  # check that they are equal
  if(!identical(tableinfo$column_name, colnames(location_data))) stop("The column in your data don't perfectly match those in NaTron")

  #remove the gid column as these values are created in natron. Mapping of columns to the right of gid is automatic.
  location_data2 <- dplyr::select(location_data, -gid)

  # append to tha NaTron table
  RPostgreSQL::dbWriteTable(conn, c("data", "Locations"),
                            value = location_data2,
                            row.names = FALSE,
                            append = T)



}
NTNU-VM/natronbatchupload documentation built on Oct. 12, 2019, 5:49 a.m.