Skip to content

Commit

Permalink
add all postgres row-level locking options
Browse files Browse the repository at this point in the history
add FOR NO KEY UPDATE and FOR KEY SHARE
  • Loading branch information
ac251 committed Sep 13, 2024
1 parent 30a5e80 commit 47e8843
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 1 deletion.
6 changes: 6 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
3.5.11.3
=======
- @ac251
- [#402](https://github.com/bitemyapp/esqueleto/pull/402)
- Add `forNoKeyUpdate` and `forKeyShare` locking kinds for postgres

3.5.11.2
========
- @arguri
Expand Down
4 changes: 4 additions & 0 deletions src/Database/Esqueleto/Internal/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1485,7 +1485,9 @@ data PostgresLockingKind =
-- Arranged in order of lock strength
data PostgresRowLevelLockStrength =
PostgresForUpdate
| PostgresForNoKeyUpdate
| PostgresForShare
| PostgresForKeyShare
deriving (Ord, Eq)

data LockingOfClause where
Expand Down Expand Up @@ -3254,7 +3256,9 @@ makeLocking info (PostgresLockingClauses clauses) =
<> makeLockingBehavior (postgresOnLockedBehavior l)
makeLockingStrength :: PostgresRowLevelLockStrength -> (TLB.Builder, [PersistValue])
makeLockingStrength PostgresForUpdate = plain "FOR UPDATE"
makeLockingStrength PostgresForNoKeyUpdate = plain "FOR NO KEY UPDATE"
makeLockingStrength PostgresForShare = plain "FOR SHARE"
makeLockingStrength PostgresForKeyShare = plain "FOR KEY SHARE"

makeLockingBehavior :: OnLockedBehavior -> (TLB.Builder, [PersistValue])
makeLockingBehavior NoWait = plain "NOWAIT"
Expand Down
15 changes: 14 additions & 1 deletion src/Database/Esqueleto/PostgreSQL.hs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ module Database.Esqueleto.PostgreSQL
, wait
, skipLocked
, forUpdateOf
, forNoKeyUpdateOf
, forShareOf
, forKeyShareOf
, filterWhere
, values
-- * Internal
Expand Down Expand Up @@ -469,11 +471,22 @@ forUpdateOf :: LockableEntity a => a -> OnLockedBehavior -> SqlQuery ()
forUpdateOf lockableEntities onLockedBehavior =
putLocking $ PostgresLockingClauses [PostgresLockingKind PostgresForUpdate (Just $ LockingOfClause lockableEntities) onLockedBehavior]

-- | `FOR NO KEY UPDATE OF` syntax for postgres locking
-- allows locking of specific tables with a no key update lock in a view or join
forNoKeyUpdateOf :: LockableEntity a => a -> OnLockedBehavior -> SqlQuery ()
forNoKeyUpdateOf lockableEntities onLockedBehavior =
putLocking $ PostgresLockingClauses [PostgresLockingKind PostgresForNoKeyUpdate (Just $ LockingOfClause lockableEntities) onLockedBehavior]

-- | `FOR SHARE OF` syntax for postgres locking
-- allows locking of specific tables with a share lock in a view or join
--
-- @since 3.5.9.0

forShareOf :: LockableEntity a => a -> OnLockedBehavior -> SqlQuery ()
forShareOf lockableEntities onLockedBehavior =
putLocking $ PostgresLockingClauses [PostgresLockingKind PostgresForShare (Just $ LockingOfClause lockableEntities) onLockedBehavior]

-- | `FOR KEY SHARE OF` syntax for postgres locking
-- allows locking of specific tables with a key share lock in a view or join
forKeyShareOf :: LockableEntity a => a -> OnLockedBehavior -> SqlQuery ()
forKeyShareOf lockableEntities onLockedBehavior =
putLocking $ PostgresLockingClauses [PostgresLockingKind PostgresForKeyShare (Just $ LockingOfClause lockableEntities) onLockedBehavior]
4 changes: 4 additions & 0 deletions test/PostgreSQL/Test.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1243,7 +1243,9 @@ testPostgresqlLocking = do
p <- Experimental.from $ table @Person
EP.forUpdateOf p EP.skipLocked
EP.forUpdateOf p EP.skipLocked
EP.forNoKeyUpdateOf p EP.skipLocked
EP.forShareOf p EP.skipLocked
EP.forKeyShareOf p EP.skipLocked
conn <- ask
let res1 = toText conn multipleLockingQuery
resExpected =
Expand All @@ -1253,7 +1255,9 @@ testPostgresqlLocking = do
,"FROM \"Person\""
,"FOR UPDATE OF \"Person\" SKIP LOCKED"
,"FOR UPDATE OF \"Person\" SKIP LOCKED"
,"FOR NO KEY UPDATE OF \"Person\" SKIP LOCKED"
,"FOR SHARE OF \"Person\" SKIP LOCKED"
,"FOR KEY SHARE OF \"Person\" SKIP LOCKED"
]

asserting $ res1 `shouldBe` resExpected
Expand Down

0 comments on commit 47e8843

Please sign in to comment.