Skip to content

Commit

Permalink
Date fix (#47)
Browse files Browse the repository at this point in the history
* non-default rownames fix

* POSIXlt and POSIXct fix

* rownames test

* spacing change

* increment release
  • Loading branch information
tomwilsonsco authored Oct 9, 2023
1 parent d84a6d1 commit ce54af9
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 2 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: RtoSQLServer
Title: Load R Dataframes into SQL Server Database Tables
Version: 0.1.4
Version: 0.1.5
Authors@R: c(
person("Tom", "Wilson", email = "thomas.wilson@gov.scot", role = "cre"),
person("Miles", "Drake", email = "miles.drake@gov.scot", role = "aut"),
Expand Down
10 changes: 10 additions & 0 deletions R/create_replace_table.R
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,14 @@ nan_to_na <- function(dataframe) {
dataframe
}

clean_row_names <- function(dataframe) {
if (.row_names_info(dataframe) >= 0) {
warning("non-default rownames in input df will not be loaded")
rownames(dataframe) <- NULL
}
dataframe
}


#' Write an R dataframe to SQL Server table optionally with system
#' versioning on.
Expand Down Expand Up @@ -520,6 +528,8 @@ write_dataframe_to_db <- function(server,
dataframe <- clean_column_names(dataframe, table_name)
# Replace NaN values with NA
dataframe <- nan_to_na(dataframe)
# Check for and remove rownames
dataframe <- clean_row_names(dataframe)
# Create staging table
create_staging_table(db_params, dataframe)
# Check if target table already exists
Expand Down
2 changes: 1 addition & 1 deletion R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ df_to_metadata <- function(dataframe) {


r_to_sql_data_type <- function(col_v) {
r_data_type <- class(col_v)
r_data_type <- class(col_v)[1]
if (r_data_type %in% c("character", "factor")) {
col_v <- as.character(col_v) # to ensure factor cols are character
max_string <- max(nchar(col_v), na.rm = TRUE)
Expand Down
4 changes: 4 additions & 0 deletions tests/testthat/test-create_replace_table.R
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,7 @@ test_that("insert sql created correctly", {

expect_identical(args[[1]][[3]], check_sql)
})

test_that("non-default rownames removed", {
expect_warning(clean_row_names(mtcars))
})

0 comments on commit ce54af9

Please sign in to comment.