Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invalid connection when doing initial DB build with custom db_connection #102

Closed
awgymer opened this issue Sep 26, 2020 · 5 comments · Fixed by #107
Closed

Invalid connection when doing initial DB build with custom db_connection #102

awgymer opened this issue Sep 26, 2020 · 5 comments · Fixed by #107
Assignees
Labels
bug Something isn't working

Comments

@awgymer
Copy link

awgymer commented Sep 26, 2020

When using building the DB for the first time or using force_rebuild an Error: Invalid connection is raised (using RPostgres - suspect error message may differ between drivers) which I believe is caused by build_db calling DBI::dbDisconnect(connection). This is not a problem when using the default RSQLite connection because the following code is run after the initial build/rebuild code which re-establishes an SQLite connection:

if (is.null(db_connection)) {
    connection <- DBI::dbConnect(RSQLite::SQLite(), db)
  } else {
    connection <- db_connection
  }

I'm not overly familiar with DBI::DBIConnection objects but as far as I know there isn't a way to reconnect? Perhaps the best solution to this would be a disconnect parameter to build_db - so that it can still run independently from update_db where necessary - but when a custom connection is used in update_db and you are calling it you don't disconnect?

Something like the following:

build_db <- function(dbdir = ".", dbname = "pbp_db", tblname = "nflfastR_pbp",  db_conn, disconnect=TRUE) {
     ...
     if(disconnect){
          DBI::dbDisconnect(connection
     }
}

update_db <- function(dbdir = ".",
                      dbname = "pbp_db",
                      tblname = "nflfastR_pbp",
                      force_rebuild = FALSE,
                      db_connection = NULL) {

    ...
    if (!file.exists(db) & is.null(db_connection)) {
    message(glue::glue("Can't find database {db}. Will try to create it and load the play by play data into the data table \"{tblname}\"."))
    build_db(dbdir, dbname, tblname, db_connection)
  } else if (file.exists(db) & force_rebuild & is.null(db_connection)) {
    message(glue::glue("Start rebuilding the data table \"{tblname}\" in your database {db}."))
    build_db(dbdir, dbname, tblname, db_connection)
  } else if (force_rebuild & !is.null(db_connection)) {
    message(glue::glue("Start rebuilding the data table in your connected database."))
    build_db(dbdir, dbname, tblname, db_connection, disconnect=FALSE)
  }
@awgymer
Copy link
Author

awgymer commented Sep 26, 2020

I think this will also be problematic when there are lots of missing games actually:

if(length(missing) >= 50) {
    DBI::dbDisconnect(connection)
    message("The number of missing games is so large that rebuilding the database is more efficient.")
    build_db(dbdir, dbname, tblname, db_connection)
    if (is.null(db_connection)) {
      connection <- DBI::dbConnect(RSQLite::SQLite(), db)
    } else {
      connection <- db_connection
    }
    missing <- get_missing_games(completed_games, connection, tblname)
  }

would probably need to become:

if(length(missing) >= 50) {
   if(is.null(db_connection)){DBI::dbDisconnect(connection)}
    message("The number of missing games is so large that rebuilding the database is more efficient.")
    if(is.null(db_connection)){build_db(dbdir, dbname, tblname, db_connection, FALSE)}
    else{build_db(dbdir, dbname, tblname, db_connection)}
    if (is.null(db_connection)) {
      connection <- DBI::dbConnect(RSQLite::SQLite(), db)
    } else {
      connection <- db_connection
    }
    missing <- get_missing_games(completed_games, connection, tblname)
  }

@mrcaseb
Copy link
Member

mrcaseb commented Sep 26, 2020

Can you please provide a code example to reproduce the Error message? I am wondering if you are doing the connection correct as nobody else had those problems.

@mrcaseb
Copy link
Member

mrcaseb commented Sep 26, 2020

Looked at it and think you are right. Should not disconnect if a custom connection is provided. Good catch, thanks!

@mrcaseb mrcaseb self-assigned this Sep 26, 2020
@mrcaseb mrcaseb added the bug Something isn't working label Sep 26, 2020
@awgymer
Copy link
Author

awgymer commented Sep 26, 2020

Just in case it does make a difference this is the code I use to connect. It's a local Postgres instance on my Mac.

conn <- DBI::dbConnect(RPostgres::Postgres(), dbname = dbname)
nflfastR::update_db(db_connection = conn, tblname = 'pbp', dbname = dbname, force_rebuild = force_rebuild)
DBI::dbDisconnect(conn)

@mrcaseb mrcaseb mentioned this issue Sep 26, 2020
@mrcaseb mrcaseb linked a pull request Sep 26, 2020 that will close this issue
@mrcaseb
Copy link
Member

mrcaseb commented Sep 26, 2020

The issue got closed automatically as I merged in a potential fix now. If you are still having problems @awgymer please feel free to reopen the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants