Skip to content
Snippets Groups Projects
Unverified Commit fde2ff79 authored by Jean-Paul Calderone's avatar Jean-Paul Calderone Committed by GitHub
Browse files

Merge pull request #67 from PrivateStorageio/65.close-connection-on-error

Handle exceptions during SQLite3 schema initialization and close the connection if necessary
parents 43be019a 6d3e391d
No related branches found
No related tags found
No related merge requests found
......@@ -18,6 +18,7 @@ import Control.Exception
, throwIO
, catch
, bracket
, bracketOnError
)
import Data.Text
......@@ -332,9 +333,8 @@ insertVoucherAndFingerprint dbConn (voucher, counter) fingerprint =
sqlite :: Text -> IO VoucherDatabaseState
sqlite path =
let
connect :: IO Sqlite.Connection
connect = do
dbConn <- Sqlite.open (unpack path)
initialize :: Sqlite.Connection -> IO Sqlite.Connection
initialize dbConn = do
let exec = Sqlite.execute_ dbConn
exec "PRAGMA busy_timeout = 1000"
exec "PRAGMA foreign_keys = ON"
......@@ -342,5 +342,9 @@ sqlite path =
exec "CREATE TABLE IF NOT EXISTS vouchers (id INTEGER PRIMARY KEY, name TEXT UNIQUE)"
exec "CREATE TABLE IF NOT EXISTS redeemed (id INTEGER PRIMARY KEY, voucher_id INTEGER, counter INTEGER, fingerprint TEXT, FOREIGN KEY (voucher_id) REFERENCES vouchers(id))"
return dbConn
connect :: IO Sqlite.Connection
connect =
bracketOnError (Sqlite.open . unpack $ path) Sqlite.close initialize
in
return . SQLiteDB $ connect
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment