Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

start persistent-sql-lifted package #208

Merged
merged 4 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion freckle-app/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
## [_Unreleased_](https://github.com/freckle/freckle-app/compare/freckle-app-v1.20.3.0...main)
## [_Unreleased_](https://github.com/freckle/freckle-app/compare/freckle-app-v1.21.0.0...main)

## [v1.21.0.0](https://github.com/freckle/freckle-app/compare/freckle-app-vv1.20.3.0...freckle-app-v1.21.0.0)

The following have moved from `Freckle.App.Database` to a new package, `persistent-sql-lifted`:

- `MonadSqlTx (..)`
- `HasSqlBackend (..)`
- `MonadSqlBackend (..)`
- `liftSql`

The definitions in `freckle-app` have been replaced with reëxports,
so no code needs to change for users, except that a dependency on
chris-martin marked this conversation as resolved.
Show resolved Hide resolved
the `persistent-sql-lifted` package is now required.

## [v1.20.3.0](https://github.com/freckle/freckle-app/compare/freckle-app-v1.20.2.1...freckle-app-v1.20.3.0)

Expand Down
3 changes: 2 additions & 1 deletion freckle-app/freckle-app.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ cabal-version: 1.22
-- see: https://github.com/sol/hpack

name: freckle-app
version: 1.20.3.0
version: 1.21.0.0
synopsis: Haskell application toolkit used at Freckle
description: Please see README.md
category: Utils
Expand Down Expand Up @@ -147,6 +147,7 @@ library
, path-pieces
, persistent
, persistent-postgresql
, persistent-sql-lifted
, postgresql-simple
, primitive
, resource-pool >=0.4.0.0
Expand Down
28 changes: 1 addition & 27 deletions freckle-app/library/Freckle/App/Database.hs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ import Database.Persist.Postgresql
, runSqlPool
, runSqlPoolWithExtensibleHooks
)
import Database.Persist.Sql.Lifted
import Database.Persist.SqlBackend.Internal.SqlPoolHooks (SqlPoolHooks (..))
import Database.Persist.SqlBackend.SqlPoolHooks
import Database.PostgreSQL.Simple
Expand All @@ -67,7 +68,6 @@ import Database.PostgreSQL.Simple
import Database.PostgreSQL.Simple.SqlQQ (sql)
import Freckle.App.Env (Timeout (..))
import Freckle.App.Env qualified as Env
import Freckle.App.Exception.MonadUnliftIO
import Freckle.App.OpenTelemetry
import Freckle.App.Stats (HasStatsClient)
import Freckle.App.Stats qualified as Stats
Expand All @@ -78,32 +78,6 @@ import UnliftIO.Concurrent (threadDelay)
import UnliftIO.IORef
import Yesod.Core.Types (HandlerData (..), RunHandlerEnv (..))

-- | A monadic context in which a SQL backend is available
-- for running database queries
class MonadUnliftIO m => MonadSqlBackend m where
getSqlBackendM :: m SqlBackend

instance (HasSqlBackend r, MonadUnliftIO m) => MonadSqlBackend (ReaderT r m) where
getSqlBackendM = asks getSqlBackend

-- | Generalize from 'SqlPersistT' to 'MonadSqlBackend'
liftSql :: (MonadSqlBackend m, HasCallStack) => ReaderT SqlBackend m a -> m a
liftSql (ReaderT f) = checkpointCallStack $ getSqlBackendM >>= f

-- | The constraint @'MonadSqlTx' db m@ indicates that @m@ is a monadic
-- context that can run @db@ actions, usually as a SQL transaction.
-- Typically, this means that @db@ needs a connection and @m@ can
-- provide one, e.g. from a connection pool.
class (MonadSqlBackend db, MonadUnliftIO m) => MonadSqlTx db m | m -> db where
-- | Runs the action in a SQL transaction
runSqlTx :: HasCallStack => db a -> m a

class HasSqlBackend a where
getSqlBackend :: a -> SqlBackend

instance HasSqlBackend SqlBackend where
getSqlBackend = id

type SqlPool = Pool SqlBackend

class HasSqlPool app where
Expand Down
3 changes: 2 additions & 1 deletion freckle-app/package.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: freckle-app
version: 1.20.3.0
version: 1.21.0.0

maintainer: Freckle Education
category: Utils
Expand Down Expand Up @@ -124,6 +124,7 @@ library:
- openapi3
- path-pieces
- persistent
- persistent-sql-lifted
- persistent-postgresql
- postgresql-simple
- primitive
Expand Down
21 changes: 12 additions & 9 deletions hie.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
cradle:
cabal:
stack:
- path: "freckle-app/library"
component: "lib:freckle-app"
component: "freckle-app:lib"

- path: "freckle-app/doctest"
component: "freckle-app:test:doctest"
Expand All @@ -10,34 +10,37 @@ cradle:
component: "freckle-app:test:spec"

- path: "freckle-env/library"
component: "lib:freckle-env"
component: "freckle-env:lib"

- path: "freckle-env/doctest"
component: "freckle-env:test:doctest"

- path: "freckle-exception/library"
component: "lib:freckle-exception"
component: "freckle-exception:lib"

- path: "freckle-http/library"
component: "lib:freckle-http"
component: "freckle-http:lib"

- path: "freckle-http/tests"
component: "freckle-http:test:spec"

- path: "freckle-kafka/library"
component: "lib:freckle-kafka"
component: "freckle-kafka:lib"

- path: "freckle-memcached/library"
component: "lib:freckle-memcached"
component: "freckle-memcached:lib"

- path: "freckle-memcached/tests"
component: "freckle-memcached:test:spec"

- path: "freckle-otel/library"
component: "lib:freckle-otel"
component: "freckle-otel:lib"

- path: "freckle-otel/tests"
component: "freckle-otel:test:spec"

- path: "freckle-prelude/library"
component: "lib:freckle-prelude"
component: "freckle-prelude:lib"

- path: "persistent-sql-lifted/library"
component: "persistent-sql-lifted:lib"
5 changes: 5 additions & 0 deletions persistent-sql-lifted/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
## [_Unreleased_](https://github.com/freckle/freckle-app/compare/persistent-sql-lifted-v0.0.0.0...main)

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

First release, sprouted from `freckle-app-1.20.3.0`.
13 changes: 13 additions & 0 deletions persistent-sql-lifted/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# persistent-sql-lifted

How to migrate from vanilla [persistent]:

- Instead of [SqlPersistT], use a `MonadSqlBackend` constraint.
- Define an instance of `MonadSqlTx` for your application Monad that specifies how
your application runs database transactions, e.g. by running [runSqlPool].
- Instead of calling `runSqlPool` directly from the rest of your application code,
use the `runSqlTx` method from the `MonadSqlTx` class.

[persistent]: https://hackage.haskell.org/package/persistent
[SqlPersistT]: https://hackage.haskell.org/package/persistent-2.14.6.3/docs/Database-Persist-Sql.html#t:SqlPersistT
[runSqlPool]: https://hackage.haskell.org/package/persistent-2.14.6.3/docs/Database-Persist-Sql.html#v:runSqlPool
10 changes: 10 additions & 0 deletions persistent-sql-lifted/library/Database/Persist/Sql/Lifted.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module Database.Persist.Sql.Lifted
( MonadSqlTx (..)
, HasSqlBackend (..)
, MonadSqlBackend (..)
, liftSql
) where

import Database.Persist.Sql.Lifted.HasSqlBackend
import Database.Persist.Sql.Lifted.MonadSqlBackend
import Database.Persist.Sql.Lifted.MonadSqlTx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module Database.Persist.Sql.Lifted.HasSqlBackend
( HasSqlBackend (..)
) where

import Prelude

import Database.Persist.Sql (SqlBackend)

class HasSqlBackend a where
getSqlBackend :: a -> SqlBackend

instance HasSqlBackend SqlBackend where
getSqlBackend = id
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module Database.Persist.Sql.Lifted.MonadSqlBackend
( MonadSqlBackend (..)
, liftSql
) where

import Prelude

import Control.Exception.Annotated.UnliftIO (checkpointCallStack)
import Control.Monad.IO.Unlift (MonadUnliftIO)
import Control.Monad.Reader (ReaderT (..), asks)
import Database.Persist.Sql (SqlBackend)
import Database.Persist.Sql.Lifted.HasSqlBackend (HasSqlBackend, getSqlBackend)
import GHC.Stack (HasCallStack)

-- | A monadic context in which a SQL backend is available
-- for running database queries
class MonadUnliftIO m => MonadSqlBackend m where
getSqlBackendM :: m SqlBackend

instance (HasSqlBackend r, MonadUnliftIO m) => MonadSqlBackend (ReaderT r m) where
getSqlBackendM = asks getSqlBackend

-- | Generalize from 'SqlPersistT' to 'MonadSqlBackend'
liftSql :: (MonadSqlBackend m, HasCallStack) => ReaderT SqlBackend m a -> m a
liftSql (ReaderT f) = checkpointCallStack $ getSqlBackendM >>= f
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module Database.Persist.Sql.Lifted.MonadSqlTx
( MonadSqlTx (..)
) where

import Control.Monad.IO.Unlift (MonadUnliftIO)
import Database.Persist.Sql.Lifted.MonadSqlBackend (MonadSqlBackend)
import GHC.Stack (HasCallStack)

-- | The constraint @'MonadSqlTx' db m@ indicates that @m@ is a monadic
-- context that can run @db@ actions, usually as a SQL transaction.
-- Typically, this means that @db@ needs a connection and @m@ can
-- provide one, e.g. from a connection pool.
class (MonadSqlBackend db, MonadUnliftIO m) => MonadSqlTx db m | m -> db where
-- | Runs the action in a SQL transaction
runSqlTx :: HasCallStack => db a -> m a
64 changes: 64 additions & 0 deletions persistent-sql-lifted/package.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
name: persistent-sql-lifted
version: 1.20.3.0

maintainer: Freckle Education
category: Utils
github: freckle/freckle-app
synopsis: ...
description: Please see README.md

extra-doc-files:
- README.md
- CHANGELOG.md

extra-source-files:
- package.yaml

language: GHC2021

ghc-options:
- -fignore-optim-changes
- -fwrite-ide-info
- -Weverything
- -Wno-all-missed-specialisations
- -Wno-missing-exported-signatures # re-enables missing-signatures
- -Wno-missing-import-lists
- -Wno-missing-kind-signatures
- -Wno-missing-local-signatures
- -Wno-missing-safe-haskell-mode
- -Wno-monomorphism-restriction
- -Wno-prepositive-qualified-module
- -Wno-safe
- -Wno-unsafe

when:
- condition: "impl(ghc >= 9.8)"
ghc-options:
- -Wno-missing-role-annotations
- -Wno-missing-poly-kind-signatures

dependencies:
- base < 5

default-extensions:
- DataKinds
- DeriveAnyClass
- DerivingVia
- DerivingStrategies
- FunctionalDependencies
- GADTs
- LambdaCase
- NoImplicitPrelude
- NoMonomorphismRestriction
- OverloadedStrings
- RecordWildCards
- TypeFamilies

library:
source-dirs: library
dependencies:
- annotated-exception
- base
- mtl
- persistent
- unliftio-core
58 changes: 58 additions & 0 deletions persistent-sql-lifted/persistent-sql-lifted.cabal
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
cabal-version: 1.18

-- This file has been generated from package.yaml by hpack version 0.36.0.
--
-- see: https://github.com/sol/hpack

name: persistent-sql-lifted
version: 1.20.3.0
synopsis: ...
description: Please see README.md
category: Utils
homepage: https://github.com/freckle/freckle-app#readme
bug-reports: https://github.com/freckle/freckle-app/issues
maintainer: Freckle Education
build-type: Simple
extra-source-files:
package.yaml
extra-doc-files:
README.md
CHANGELOG.md

source-repository head
type: git
location: https://github.com/freckle/freckle-app

library
exposed-modules:
Database.Persist.Sql.Lifted
Database.Persist.Sql.Lifted.HasSqlBackend
Database.Persist.Sql.Lifted.MonadSqlBackend
Database.Persist.Sql.Lifted.MonadSqlTx
other-modules:
Paths_persistent_sql_lifted
hs-source-dirs:
library
default-extensions:
DataKinds
DeriveAnyClass
DerivingVia
DerivingStrategies
FunctionalDependencies
GADTs
LambdaCase
NoImplicitPrelude
NoMonomorphismRestriction
OverloadedStrings
RecordWildCards
TypeFamilies
ghc-options: -fignore-optim-changes -fwrite-ide-info -Weverything -Wno-all-missed-specialisations -Wno-missing-exported-signatures -Wno-missing-import-lists -Wno-missing-kind-signatures -Wno-missing-local-signatures -Wno-missing-safe-haskell-mode -Wno-monomorphism-restriction -Wno-prepositive-qualified-module -Wno-safe -Wno-unsafe
build-depends:
annotated-exception
, base
, mtl
, persistent
, unliftio-core
default-language: GHC2021
if impl(ghc >= 9.8)
ghc-options: -Wno-missing-role-annotations -Wno-missing-poly-kind-signatures
1 change: 1 addition & 0 deletions stack-lts-20.26.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,4 @@ packages:
- freckle-memcached
- freckle-otel
- freckle-prelude
- persistent-sql-lifted
1 change: 1 addition & 0 deletions stack-lts-21.25.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,4 @@ packages:
- freckle-memcached
- freckle-otel
- freckle-prelude
- persistent-sql-lifted
1 change: 1 addition & 0 deletions stack-nightly.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,4 @@ packages:
- freckle-memcached
- freckle-otel
- freckle-prelude
- persistent-sql-lifted
1 change: 1 addition & 0 deletions stack.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,4 @@ packages:
- freckle-memcached
- freckle-otel
- freckle-prelude
- persistent-sql-lifted
Loading