This set of function adds a simple locking system to database tables.
lock_table()
adds a record in the schema.locks table with the current time and R-session process id.unlock_table()
removes records in the schema.locks table with the target table and the R-session process id.
When locking a table, the function will check for existing locks on the table and produce an error a lock is held by a process which no longer exists. In this case, the lock needs to be removed manually by removing the record from the lock table. In addition, the error implies that a table may have partial updates that needs to be manually rolled back.
Usage
lock_table(conn, db_table, schema = NULL)
unlock_table(conn, db_table, schema = NULL, pid = Sys.getpid())
Value
lock_table()
returns theTRUE
(FALSE
) if the lock was (un)successfully added. If a lock exists for a non-active process, an error is thrown.unlock_table()
returnsNULL
(called for side effects).
Examples
conn <- DBI::dbConnect(RSQLite::SQLite())
lock_table(conn, "test_table") # TRUE
#> [1] TRUE
unlock_table(conn, "test_table")
#> NULL
DBI::dbDisconnect(conn)