Skip to content

Commit

Permalink
add transaction utilities (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-martin authored Jan 10, 2025
1 parent 2d22fee commit b040e75
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
stack-*.lock
stack.*.lock
.direnv
.envrc
13 changes: 9 additions & 4 deletions persistent-sql-lifted/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
## [_Unreleased_](https://github.com/freckle/persistent-sql-lifted/compare/persistent-sql-lifted-v0.1.0.0...main)
## [_Unreleased_](https://github.com/freckle/persistent-sql-lifted/compare/persistent-sql-lifted-v0.1.1.0...main)

## [v0.0.0.1](https://github.com/freckle/freckle-app/compare/persistent-sql-lifted-v0.0.0.0...persistent-sql-lifted-v0.1.0.0)
## [v0.1.1.0](https://github.com/freckle/persistent-sql-lifted/compare/persistent-sql-lifted-v0.1.0.0...persistent-sql-lifted-v0.1.1.0)

Major expansion, adding query runners for Persistent and Esqueleto.
New:

## [v0.0.0.0](https://github.com/freckle/freckle-app/tree/persistent-sql-lifted-v0.0.0.0/persistent-sql-lifted)
- `transactionSave`
- `transactionSaveWithIsolation`
- `transactionUndo`
- `transactionUndoWithIsolation`

## [v0.1.0.0](https://github.com/freckle/persistent-sql-lifted/tree/persistent-sql-lifted-v0.0.0.0/persistent-sql-lifted)

First release, sprouted from `freckle-app-1.20.3.0`.
6 changes: 6 additions & 0 deletions persistent-sql-lifted/library/Database/Persist/Sql/Lifted.hs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ module Database.Persist.Sql.Lifted
, deleteWhere
, deleteCount

-- * Transactions
, transactionSave
, transactionSaveWithIsolation
, transactionUndo
, transactionUndoWithIsolation

-- * Rendering queries to text
, renderQueryDelete
, renderQueryInsertInto
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ module Database.Persist.Sql.Lifted.Persistent
, selectKeys
, selectKeysList
, selectList
, transactionSave
, transactionSaveWithIsolation
, transactionUndo
, transactionUndoWithIsolation
, update
, updateGet
, updateWhere
Expand Down Expand Up @@ -66,6 +70,8 @@ import Database.Persist
)
import Database.Persist.Class qualified as P
import Database.Persist.Class.PersistEntity (SafeToInsert)
import Database.Persist.Sql (IsolationLevel)
import Database.Persist.Sql qualified as P
import Database.Persist.Sql.Lifted.Core (MonadSqlBackend, SqlBackend, liftSql)
import GHC.Stack (HasCallStack)

Expand Down Expand Up @@ -557,6 +563,32 @@ selectList
-- ^ Entities corresponding to the filters and options provided
selectList fs os = liftSql $ P.selectList fs os

-- | Commit the current transaction and begin a new one
transactionSave :: forall m. (MonadSqlBackend m, HasCallStack) => m ()
transactionSave = liftSql P.transactionSave

-- | Commit the current transaction and begin a new one
transactionSaveWithIsolation
:: forall m
. (MonadSqlBackend m, HasCallStack)
=> IsolationLevel
-- ^ Isolation level
-> m ()
transactionSaveWithIsolation il = liftSql $ transactionSaveWithIsolation il

-- | Roll back the current transaction and begin a new one
transactionUndo :: forall m. (MonadSqlBackend m, HasCallStack) => m ()
transactionUndo = liftSql transactionUndo

-- | Roll back the current transaction and begin a new one
transactionUndoWithIsolation
:: forall m
. (MonadSqlBackend m, HasCallStack)
=> IsolationLevel
-- ^ Isolation level
-> m ()
transactionUndoWithIsolation il = liftSql $ transactionUndoWithIsolation il

-- | Update individual fields on a specific record
update
:: forall a m
Expand Down
2 changes: 1 addition & 1 deletion persistent-sql-lifted/package.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: persistent-sql-lifted
version: 0.1.0.0
version: 0.1.1.0

maintainer: Freckle Education
category: Database
Expand Down
4 changes: 2 additions & 2 deletions persistent-sql-lifted/persistent-sql-lifted.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ cabal-version: 1.18
-- see: https://github.com/sol/hpack

name: persistent-sql-lifted
version: 0.1.0.0
version: 0.1.1.0
synopsis: Monad classes for running queries with Persistent and Esqueleto
description: This package introduces two classes: MonadSqlBackend for monadic contexts in
which a SqlBackend is available, and MonadSqlBackend for contexts in which we
which a SqlBackend is available, and MonadSqlTx for contexts in which we
can execute a SQL transaction.
.
Additionally, this package provides variants of query-running utilities from
Expand Down

0 comments on commit b040e75

Please sign in to comment.