From 0738c7957e971b84a2a156d297596206b948c4f6 Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Tue, 30 Aug 2022 12:04:28 +0100 Subject: [PATCH 1/4] use SQLCipher (#1) * rename to sqlcipher-simple, use direct-sqlcipher * cabal.project * update direct-sqlcipher * use local direct-sqlcipher * use direct-sqlcipher from git * update direct-sqlcipher commit --- .gitignore | 1 + cabal.project | 7 +++++++ sqlite-simple.cabal => sqlcipher-simple.cabal | 20 +++++++++---------- stack.yaml | 7 +++++-- 4 files changed, 23 insertions(+), 12 deletions(-) create mode 100644 cabal.project rename sqlite-simple.cabal => sqlcipher-simple.cabal (81%) diff --git a/.gitignore b/.gitignore index 5ce26b9..9722583 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ TAGS /hpc_*.html /test.tix .stack-work/ +dist-newstyle/ diff --git a/cabal.project b/cabal.project new file mode 100644 index 0000000..e131a96 --- /dev/null +++ b/cabal.project @@ -0,0 +1,7 @@ +packages: . +-- packages: . ../direct-sqlcipher + +source-repository-package + type: git + location: https://github.com/simplex-chat/direct-sqlcipher.git + tag: 477955063df65a2776c2a958b656ff359b76374d diff --git a/sqlite-simple.cabal b/sqlcipher-simple.cabal similarity index 81% rename from sqlite-simple.cabal rename to sqlcipher-simple.cabal index 30ea3a3..fd685b5 100644 --- a/sqlite-simple.cabal +++ b/sqlcipher-simple.cabal @@ -1,23 +1,23 @@ -Name: sqlite-simple +Name: sqlcipher-simple Version: 0.4.18.1 Synopsis: Mid-Level SQLite client library Description: - Mid-level SQLite client library, based on postgresql-simple. + Mid-level SQLCipher client library, forked from sqlite-simple. . Main documentation (with examples): . - You can view the project page at + You can view the project page at for more information. License: BSD3 License-file: LICENSE Author: Bryan O'Sullivan, Leon P Smith, Janne Hellsten -Maintainer: Janne Hellsten +Maintainer: Evgeny Poberezkin Copyright: (c) 2011 MailRank, Inc., (c) 2011-2012 Leon P Smith, (c) 2012-2014 Janne Hellsten -Homepage: http://github.com/nurpax/sqlite-simple -bug-reports: http://github.com/nurpax/sqlite-simple/issues +Homepage: https://github.com/simplex-chat/sqlcipher-simple +bug-reports: https://github.com/simplex-chat/sqlcipher-simple/issues Stability: stable Category: Database Build-type: Simple @@ -50,7 +50,7 @@ Library blaze-textual, bytestring >= 0.9, containers, - direct-sqlite >= 2.3.13 && < 2.4, + direct-sqlcipher >= 2.3.13 && < 2.4, exceptions >= 0.4, template-haskell, text >= 0.11, @@ -72,7 +72,7 @@ Library source-repository head type: git - location: http://github.com/nurpax/sqlite-simple + location: http://github.com/simplex-chat/sqlcipher-simple test-suite test @@ -107,7 +107,7 @@ test-suite test , base16-bytestring , bytestring >= 0.9 , HUnit - , sqlite-simple - , direct-sqlite + , sqlcipher-simple + , direct-sqlcipher , text , time diff --git a/stack.yaml b/stack.yaml index f527822..6c9ec98 100644 --- a/stack.yaml +++ b/stack.yaml @@ -1,4 +1,4 @@ -resolver: lts-15.1 # 8.8.2 - base-4.13 +resolver: lts-15.1 # 8.8.2 - base-4.13 # resolver: lts-14.27 # 8.6.5 - base-4.12 # resolver: lts-12.26 # 8.4.4 - base-4.11 # resolver: lts-11.22 # 8.2.2 - base-4.10 @@ -6,4 +6,7 @@ resolver: lts-15.1 # 8.8.2 - base-4.13 # resolver: lts-6.35 # 7.10.3 - base-4.8 extra-deps: - - direct-sqlite-2.3.26 + # - direct-sqlcipher-2.3.26 + # - ../direct-sqlcipher + - github: simplex-chat/direct-sqlcipher + commit: 477955063df65a2776c2a958b656ff359b76374d From 5e154a2aeccc33ead6c243ec07195ab673137221 Mon Sep 17 00:00:00 2001 From: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> Date: Tue, 30 Aug 2022 14:16:15 +0100 Subject: [PATCH 2/4] update direct-sqlcipher --- cabal.project | 2 +- stack.yaml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/cabal.project b/cabal.project index e131a96..dcdf91d 100644 --- a/cabal.project +++ b/cabal.project @@ -4,4 +4,4 @@ packages: . source-repository-package type: git location: https://github.com/simplex-chat/direct-sqlcipher.git - tag: 477955063df65a2776c2a958b656ff359b76374d + tag: 34309410eb2069b029b8fc1872deb1e0db123294 diff --git a/stack.yaml b/stack.yaml index 6c9ec98..a482ea3 100644 --- a/stack.yaml +++ b/stack.yaml @@ -9,4 +9,4 @@ extra-deps: # - direct-sqlcipher-2.3.26 # - ../direct-sqlcipher - github: simplex-chat/direct-sqlcipher - commit: 477955063df65a2776c2a958b656ff359b76374d + commit: 34309410eb2069b029b8fc1872deb1e0db123294 From 2b8c18c811f41d5703a2498b2703f81d6565bb28 Mon Sep 17 00:00:00 2001 From: "Evgeny @ SimpleX Chat" <259188159+evgeny-simplex@users.noreply.github.com> Date: Thu, 19 Nov 2026 21:49:55 +0000 Subject: [PATCH 3/4] ci: haskell-ci workflow for sqlcipher-simple (#3) --- .github/workflows/haskell-ci.yml | 230 +++++++++++++++++++++++++++++++ 1 file changed, 230 insertions(+) create mode 100644 .github/workflows/haskell-ci.yml diff --git a/.github/workflows/haskell-ci.yml b/.github/workflows/haskell-ci.yml new file mode 100644 index 0000000..d17436d --- /dev/null +++ b/.github/workflows/haskell-ci.yml @@ -0,0 +1,230 @@ +# This GitHub workflow config has been generated by a script via +# +# haskell-ci '--distribution' 'noble' 'github' 'sqlcipher-simple.cabal' +# +# To regenerate the script (for example after adjusting tested-with) run +# +# haskell-ci regenerate +# +# For more information, see https://github.com/haskell-CI/haskell-ci +# +# version: 0.19.20250506 +# +# REGENDATA ("0.19.20250506",["--distribution","noble","github","sqlcipher-simple.cabal"]) +# +name: Haskell-CI +on: + - push + - pull_request +jobs: + linux: + name: Haskell-CI - Linux - ${{ matrix.compiler }} + runs-on: ubuntu-24.04 + timeout-minutes: + 60 + container: + image: buildpack-deps:noble + continue-on-error: ${{ matrix.allow-failure }} + strategy: + matrix: + include: + - compiler: ghc-9.10.3 + compilerKind: ghc + compilerVersion: 9.10.3 + setup-method: ghcup + allow-failure: false + - compiler: ghc-9.8.4 + compilerKind: ghc + compilerVersion: 9.8.4 + setup-method: ghcup + allow-failure: false + - compiler: ghc-9.6.7 + compilerKind: ghc + compilerVersion: 9.6.7 + setup-method: ghcup + allow-failure: false + - compiler: ghc-9.4.8 + compilerKind: ghc + compilerVersion: 9.4.8 + setup-method: ghcup + allow-failure: false + - compiler: ghc-9.2.8 + compilerKind: ghc + compilerVersion: 9.2.8 + setup-method: ghcup + allow-failure: false + - compiler: ghc-9.0.2 + compilerKind: ghc + compilerVersion: 9.0.2 + setup-method: ghcup + allow-failure: false + - compiler: ghc-8.10.7 + compilerKind: ghc + compilerVersion: 8.10.7 + setup-method: ghcup + allow-failure: false + fail-fast: false + steps: + - name: apt-get install + run: | + apt-get update + apt-get install -y --no-install-recommends gnupg ca-certificates dirmngr curl git software-properties-common libtinfo6 + - name: Install GHCup + run: | + mkdir -p "$HOME/.ghcup/bin" + curl -sL https://downloads.haskell.org/ghcup/0.1.50.1/x86_64-linux-ghcup-0.1.50.1 > "$HOME/.ghcup/bin/ghcup" + chmod a+x "$HOME/.ghcup/bin/ghcup" + - name: Install cabal-install + run: | + "$HOME/.ghcup/bin/ghcup" install cabal 3.14.2.0 || (cat "$HOME"/.ghcup/logs/*.* && false) + echo "CABAL=$HOME/.ghcup/bin/cabal-3.14.2.0 -vnormal+nowrap" >> "$GITHUB_ENV" + - name: Install GHC (GHCup) + if: matrix.setup-method == 'ghcup' + run: | + "$HOME/.ghcup/bin/ghcup" install ghc "$HCVER" || (cat "$HOME"/.ghcup/logs/*.* && false) + HC=$("$HOME/.ghcup/bin/ghcup" whereis ghc "$HCVER") + HCPKG=$(echo "$HC" | sed 's#ghc$#ghc-pkg#') + HADDOCK=$(echo "$HC" | sed 's#ghc$#haddock#') + echo "HC=$HC" >> "$GITHUB_ENV" + echo "HCPKG=$HCPKG" >> "$GITHUB_ENV" + echo "HADDOCK=$HADDOCK" >> "$GITHUB_ENV" + env: + HCKIND: ${{ matrix.compilerKind }} + HCNAME: ${{ matrix.compiler }} + HCVER: ${{ matrix.compilerVersion }} + - name: Set PATH and environment variables + run: | + echo "$HOME/.cabal/bin" >> $GITHUB_PATH + echo "LANG=C.UTF-8" >> "$GITHUB_ENV" + echo "CABAL_DIR=$HOME/.cabal" >> "$GITHUB_ENV" + echo "CABAL_CONFIG=$HOME/.cabal/config" >> "$GITHUB_ENV" + HCNUMVER=$(${HC} --numeric-version|perl -ne '/^(\d+)\.(\d+)\.(\d+)(\.(\d+))?$/; print(10000 * $1 + 100 * $2 + ($3 == 0 ? $5 != 1 : $3))') + echo "HCNUMVER=$HCNUMVER" >> "$GITHUB_ENV" + echo "ARG_TESTS=--enable-tests" >> "$GITHUB_ENV" + echo "ARG_BENCH=--enable-benchmarks" >> "$GITHUB_ENV" + echo "HEADHACKAGE=false" >> "$GITHUB_ENV" + echo "ARG_COMPILER=--$HCKIND --with-compiler=$HC" >> "$GITHUB_ENV" + env: + HCKIND: ${{ matrix.compilerKind }} + HCNAME: ${{ matrix.compiler }} + HCVER: ${{ matrix.compilerVersion }} + - name: env + run: | + env + - name: write cabal config + run: | + mkdir -p $CABAL_DIR + cat >> $CABAL_CONFIG <> $CABAL_CONFIG < cabal-plan.xz + echo 'f62ccb2971567a5f638f2005ad3173dba14693a45154c1508645c52289714cb2 cabal-plan.xz' | sha256sum -c - + xz -d < cabal-plan.xz > $HOME/.cabal/bin/cabal-plan + rm -f cabal-plan.xz + chmod a+x $HOME/.cabal/bin/cabal-plan + cabal-plan --version + - name: checkout + uses: actions/checkout@v4 + with: + path: source + - name: initial cabal.project for sdist + run: | + touch cabal.project + echo "packages: $GITHUB_WORKSPACE/source/." >> cabal.project + cat cabal.project + - name: sdist + run: | + mkdir -p sdist + $CABAL sdist all --output-dir $GITHUB_WORKSPACE/sdist + - name: unpack + run: | + mkdir -p unpacked + find sdist -maxdepth 1 -type f -name '*.tar.gz' -exec tar -C $GITHUB_WORKSPACE/unpacked -xzvf {} \; + - name: generate cabal.project + run: | + PKGDIR_sqlcipher_simple="$(find "$GITHUB_WORKSPACE/unpacked" -maxdepth 1 -type d -regex '.*/sqlcipher-simple-[0-9.]*')" + echo "PKGDIR_sqlcipher_simple=${PKGDIR_sqlcipher_simple}" >> "$GITHUB_ENV" + rm -f cabal.project cabal.project.local + touch cabal.project + touch cabal.project.local + echo "packages: ${PKGDIR_sqlcipher_simple}" >> cabal.project + echo "package sqlcipher-simple" >> cabal.project + echo " ghc-options: -Werror=missing-methods" >> cabal.project + cat >> cabal.project <> cabal.project.local + cat cabal.project + cat cabal.project.local + - name: dump install plan + run: | + $CABAL v2-build $ARG_COMPILER $ARG_TESTS $ARG_BENCH --dry-run all + cabal-plan + - name: restore cache + uses: actions/cache/restore@v4 + with: + key: ${{ runner.os }}-${{ matrix.compiler }}-${{ github.sha }} + path: ~/.cabal/store + restore-keys: ${{ runner.os }}-${{ matrix.compiler }}- + - name: install dependencies + run: | + $CABAL v2-build $ARG_COMPILER --disable-tests --disable-benchmarks --dependencies-only -j2 all + $CABAL v2-build $ARG_COMPILER $ARG_TESTS $ARG_BENCH --dependencies-only -j2 all + - name: build w/o tests + run: | + $CABAL v2-build $ARG_COMPILER --disable-tests --disable-benchmarks all + - name: build + run: | + $CABAL v2-build $ARG_COMPILER $ARG_TESTS $ARG_BENCH all --write-ghc-environment-files=always + - name: tests + run: | + $CABAL v2-test $ARG_COMPILER $ARG_TESTS $ARG_BENCH all --test-show-details=direct + - name: cabal check + run: | + cd ${PKGDIR_sqlcipher_simple} || false + ${CABAL} -vnormal check + - name: haddock + run: | + $CABAL v2-haddock --disable-documentation --haddock-all $ARG_COMPILER --with-haddock $HADDOCK $ARG_TESTS $ARG_BENCH all + - name: unconstrained build + run: | + rm -f cabal.project.local + $CABAL v2-build $ARG_COMPILER --disable-tests --disable-benchmarks all + - name: save cache + if: always() + uses: actions/cache/save@v4 + with: + key: ${{ runner.os }}-${{ matrix.compiler }}-${{ github.sha }} + path: ~/.cabal/store From 6bf2796de625dca678c70e98560010c6ec7ed635 Mon Sep 17 00:00:00 2001 From: "Evgeny @ SimpleX Chat" <259188159+evgeny-simplex@users.noreply.github.com> Date: Fri, 20 Nov 2026 07:03:57 +0000 Subject: [PATCH 4/4] keep reading numbers into text fields; roll back the transaction when its commit fails (#5) * build with the direct-sqlcipher commit used in CI and SimpleX * test transactions after a failed commit or rollback * roll back the transaction when its commit fails * count committed rows through the second connection in the busy test * keep reading numbers into text fields --- Database/SQLite/Simple.hs | 18 ++++++----- Database/SQLite/Simple/FromField.hs | 13 ++++---- cabal.project | 2 +- sqlcipher-simple.cabal | 1 + stack.yaml | 2 +- test/Errors.hs | 47 ++++++++++++++++++++++------- test/Main.hs | 3 ++ test/With.hs | 2 +- 8 files changed, 61 insertions(+), 27 deletions(-) diff --git a/Database/SQLite/Simple.hs b/Database/SQLite/Simple.hs index b12a5ae..669eb18 100644 --- a/Database/SQLite/Simple.hs +++ b/Database/SQLite/Simple.hs @@ -109,7 +109,7 @@ module Database.SQLite.Simple ( ) where import Control.Exception -import Control.Monad (void, when, forM_) +import Control.Monad (void, when, unless, forM_) import Control.Monad.Trans.Reader import Control.Monad.Trans.State.Strict import Data.Int (Int64) @@ -542,7 +542,7 @@ withTransactionPrivate conn action ttype = mask $ \restore -> do begin r <- restore action `onException` rollback - commit + commit `onException` rollback return r where begin = execute_ conn $ case ttype of @@ -553,9 +553,11 @@ withTransactionPrivate conn action ttype = commit = execute_ conn $ case ttype of Savepoint name -> Query $ "RELEASE '" <> name <> "'" _ -> "COMMIT TRANSACTION" - rollback = execute_ conn $ case ttype of - Savepoint name -> Query $ "ROLLBACK TO '" <> name <> "'" - _ -> "ROLLBACK TRANSACTION" + rollback = do + autocommit <- BaseD.getAutoCommit $ connectionHandle conn + unless autocommit $ execute_ conn $ case ttype of + Savepoint name -> Query $ "ROLLBACK TO '" <> name <> "'" + _ -> "ROLLBACK TRANSACTION" -- | Run an IO action inside a SQL transaction started with @BEGIN IMMEDIATE @@ -611,15 +613,17 @@ withTransaction conn action = -- | Run an IO action inside an SQLite @SAVEPOINT@. If the action throws any -- kind of an exception, the transaction will be rolled back to the savepoint -- with @ROLLBACK TO@. Otherwise the results are released to the outer --- transaction if any with @RELEASE@. +-- transaction with @RELEASE@. Outside of a transaction, the action is run +-- with 'withTransaction'. -- -- See for a full description of -- savepoint semantics. withSavepoint :: Connection -> IO a -> IO a withSavepoint conn action = do n <- atomicModifyIORef' (connectionTempNameCounter conn) $ \n -> (n + 1, n) + autocommit <- BaseD.getAutoCommit $ connectionHandle conn withTransactionPrivate conn action $ - Savepoint $ "sqlite_simple_savepoint_" <> T.pack (show n) + if autocommit then Deferred else Savepoint $ "sqlite_simple_savepoint_" <> T.pack (show n) fmtError :: Show v => String -> Query -> [v] -> a fmtError msg q xs = diff --git a/Database/SQLite/Simple/FromField.hs b/Database/SQLite/Simple/FromField.hs index 5ade933..ca9f0d8 100644 --- a/Database/SQLite/Simple/FromField.hs +++ b/Database/SQLite/Simple/FromField.hs @@ -159,16 +159,17 @@ instance FromField Bool where fromField f = returnError ConversionFailed f "expecting an SQLInteger column type" instance FromField T.Text where - fromField (Field (SQLText txt) _) = Ok txt - fromField f = returnError ConversionFailed f "need a text" + fromField f@(Field sqlData _) = case sqlData of + SQLText v -> Ok v + SQLInteger v -> Ok $ T.pack $ show v + SQLFloat v -> Ok $ T.pack $ show v + _ -> returnError ConversionFailed f "need convertible to text" instance FromField LT.Text where - fromField (Field (SQLText txt) _) = Ok . LT.fromStrict $ txt - fromField f = returnError ConversionFailed f "need a text" + fromField f = LT.fromStrict <$> fromField f instance FromField [Char] where - fromField (Field (SQLText t) _) = Ok $ T.unpack t - fromField f = returnError ConversionFailed f "expecting SQLText column type" + fromField f = T.unpack <$> fromField f instance FromField ByteString where fromField (Field (SQLBlob blb) _) = Ok blb diff --git a/cabal.project b/cabal.project index dcdf91d..f8d4688 100644 --- a/cabal.project +++ b/cabal.project @@ -4,4 +4,4 @@ packages: . source-repository-package type: git location: https://github.com/simplex-chat/direct-sqlcipher.git - tag: 34309410eb2069b029b8fc1872deb1e0db123294 + tag: 2330df3dc8c4b660674d3c02d76bb86f66c2abee diff --git a/sqlcipher-simple.cabal b/sqlcipher-simple.cabal index 2a4641e..d010878 100644 --- a/sqlcipher-simple.cabal +++ b/sqlcipher-simple.cabal @@ -108,6 +108,7 @@ test-suite test build-depends: base , base16-bytestring , bytestring >= 0.9 + , directory , HUnit , sqlcipher-simple , direct-sqlcipher diff --git a/stack.yaml b/stack.yaml index a482ea3..f05b0df 100644 --- a/stack.yaml +++ b/stack.yaml @@ -9,4 +9,4 @@ extra-deps: # - direct-sqlcipher-2.3.26 # - ../direct-sqlcipher - github: simplex-chat/direct-sqlcipher - commit: 34309410eb2069b029b8fc1872deb1e0db123294 + commit: 2330df3dc8c4b660674d3c02d76bb86f66c2abee diff --git a/test/Errors.hs b/test/Errors.hs index 40ab7a4..3fb74b1 100644 --- a/test/Errors.hs +++ b/test/Errors.hs @@ -10,6 +10,8 @@ module Errors ( , testErrorsImmediateTransaction , testErrorsExclusiveTransaction , testErrorsSavepoint + , testErrorsBusyCommit + , testErrorsEndedTransaction ) where import Control.Exception @@ -19,6 +21,8 @@ import qualified Data.Text as T import qualified Data.Text.Lazy as LT import Data.Word import Data.Time (Day, UTCTime) +import System.Directory (getTemporaryDirectory, removeFile) +import System.IO (hClose, openTempFile) import Common import Database.SQLite.Simple.Types (Null) @@ -29,6 +33,12 @@ assertResultErrorThrown action expectedError = (action >> assertFailure ("Expected error: " ++ show expectedError ++ ", but nothing was thrown")) (\(e :: ResultError) -> assertEqual "assertResultErrorThrown" expectedError e) +assertBusyThrown :: IO a -> Assertion +assertBusyThrown action = + catch + (action >> assertFailure "Expected ErrorBusy, but nothing was thrown") + (\(e :: SQLError) -> assertEqual "assertBusyThrown" ErrorBusy (sqlError e)) + assertFormatErrorCaught :: IO a -> Assertion assertFormatErrorCaught action = do catch (action >> return False) (\(e :: FormatError) -> length (show e) `seq` return True) >>= @@ -67,7 +77,7 @@ testErrorsColumns TestEnv{..} = TestCase $ do } -- Mismatching types (source int,text doesn't match dst string,int) assertResultErrorThrown (query_ conn "SELECT id, t FROM cols" :: IO [(String, Int)]) - ConversionFailed {errSQLType = "INTEGER", errHaskellType = "[Char]", errMessage = "expecting SQLText column type"} + ConversionFailed {errSQLType = "TEXT", errHaskellType = "Int", errMessage = "need an int"} -- Mismatching types (source string doesn't match dst integer) assertResultErrorThrown (query_ conn "SELECT 'foo'" :: IO [Only Integer]) ConversionFailed {errSQLType = "TEXT", errHaskellType = "Integer", errMessage = "need an int"} @@ -85,15 +95,8 @@ testErrorsColumns TestEnv{..} = TestCase $ do ConversionFailed {errSQLType = "TEXT", errHaskellType = "Bool", errMessage = "expecting an SQLInteger column type"} assertResultErrorThrown (query_ conn "SELECT 2" :: IO [Only Bool]) ConversionFailed {errSQLType = "INTEGER", errHaskellType = "Bool", errMessage = "bool must be 0 or 1, got 2"} - -- Mismatching types (sources don't match destination string types (text, string) - assertResultErrorThrown (query_ conn "SELECT 1" :: IO [Only T.Text]) - ConversionFailed {errSQLType = "INTEGER", errHaskellType = "Text", errMessage = "need a text"} - assertResultErrorThrown (query_ conn "SELECT 1" :: IO [Only LT.Text]) - ConversionFailed {errSQLType = "INTEGER", errHaskellType = "Text", errMessage = "need a text"} - assertResultErrorThrown (query_ conn "SELECT 1.0" :: IO [Only T.Text]) - ConversionFailed {errSQLType = "FLOAT", errHaskellType = "Text", errMessage = "need a text"} - assertResultErrorThrown (query_ conn "SELECT 1.0" :: IO [Only LT.Text]) - ConversionFailed {errSQLType = "FLOAT", errHaskellType = "Text", errMessage = "need a text"} + numbers <- query_ conn "SELECT 1, 1.0" :: IO [(T.Text, LT.Text)] + [("1", "1.0")] @=? numbers -- Mismatching types (sources don't match destination string types (time/date) assertResultErrorThrown (query_ conn "SELECT 1" :: IO [Only UTCTime]) ConversionFailed {errSQLType = "INTEGER", errHaskellType = "UTCTime", errMessage = "expecting SQLText column type"} @@ -116,7 +119,7 @@ testErrorsColumns TestEnv{..} = TestCase $ do assertResultErrorThrown (do [Only _t1] <- query conn "SELECT b FROM cols_blobs WHERE id = ?" (Only (1 :: Int)) :: IO [Only String] return ()) - ConversionFailed {errSQLType = "BLOB", errHaskellType = "[Char]", errMessage = "expecting SQLText column type"} + ConversionFailed {errSQLType = "BLOB", errHaskellType = "Text", errMessage = "need convertible to text"} execute_ conn "CREATE TABLE cols_bools (id INTEGER PRIMARY KEY, b BOOLEAN)" -- 3 = invalid value for bool, must be 0 or 1 execute_ conn "INSERT INTO cols_bools (b) VALUES (3)" @@ -297,6 +300,7 @@ testErrorsSavepoint TestEnv{..} = TestCase $ do "INSERT INTO strans (t) VALUES (:txt)" [":missing" := ("foo" :: String)]) e <- rowExists False @=? e + withTransaction conn $ return () where rowExists = do rows <- query_ conn "SELECT t FROM strans" :: IO [Only String] @@ -307,3 +311,24 @@ testErrorsSavepoint TestEnv{..} = TestCase $ do [] -> return False _ -> error "should have only one row" + +testErrorsBusyCommit :: (Connection -> IO () -> IO ()) -> TestEnv -> Test +testErrorsBusyCommit transaction _ = TestCase $ do + dir <- getTemporaryDirectory + bracket (openTempFile dir "busy.db") (removeFile . fst) $ \(path, h) -> do + hClose h + withConnection path $ \writer -> withConnection path $ \reader -> do + let insert = transaction writer $ execute_ writer "INSERT INTO busy DEFAULT VALUES" + execute_ writer "CREATE TABLE busy (id INTEGER PRIMARY KEY)" + withTransaction reader $ do + _ <- query_ reader "SELECT id FROM busy" :: IO [Only Int] + assertBusyThrown insert + insert + [Only n] <- query_ reader "SELECT count(*) FROM busy" + (1 :: Int) @=? n + +testErrorsEndedTransaction :: TestEnv -> Test +testErrorsEndedTransaction TestEnv{..} = TestCase $ + catch + (withTransaction conn (execute_ conn "ROLLBACK") >> assertFailure "Expected SQLError, but nothing was thrown") + (\(e :: SQLError) -> "cannot commit - no transaction is active" @=? sqlErrorDetails e) diff --git a/test/Main.hs b/test/Main.hs index 5a6188b..88d063b 100644 --- a/test/Main.hs +++ b/test/Main.hs @@ -54,6 +54,9 @@ tests = , TestLabel "Errors" . testErrorsImmediateTransaction , TestLabel "Errors" . testErrorsExclusiveTransaction , TestLabel "Errors" . testErrorsSavepoint + , TestLabel "Errors" . testErrorsBusyCommit withImmediateTransaction + , TestLabel "Errors" . testErrorsBusyCommit withSavepoint + , TestLabel "Errors" . testErrorsEndedTransaction , TestLabel "Utf8" . testUtf8Simplest , TestLabel "Utf8" . testBlobs , TestLabel "Instances" . testUserFromField diff --git a/test/With.hs b/test/With.hs index 43a8cc3..b704fd7 100644 --- a/test/With.hs +++ b/test/With.hs @@ -119,7 +119,7 @@ expectedTypeError = ConversionFailed { errSQLType = "NULL" , errHaskellType = "Text" - , errMessage = "need a text" + , errMessage = "need convertible to text" } mkTest :: AccessMode -> BindingMode -> Table -> PathKind -> TestEnv -> Test