generic-persistence 0.7.0.0 → 0.7.0.1
raw patch · 4 files changed
+45/−7 lines, 4 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
Files
- generic-persistence.cabal +4/−2
- src/Database/GP/SqlGenerator.hs +4/−4
- test/GenericPersistenceSpec.hs +18/−0
- test/PostgresSpec.hs +19/−1
generic-persistence.cabal view
@@ -1,12 +1,14 @@ cabal-version: 1.12 name: generic-persistence-version: 0.7.0.0+version: 0.7.0.1 license: BSD3 license-file: LICENSE copyright: 2023,2024 Thomas Mahler maintainer: thma@apache.org author: Thomas Mahler-tested-with: ghc ==9.2 ghc ==9.4 ghc ==9.6 ghc ==9.8+tested-with:+ ghc ==9.2 ghc ==9.4 ghc ==9.6 ghc ==9.8 ghc ==9.10 ghc ==9.12+ homepage: https://github.com/thma/generic-persistence#readme bug-reports: https://github.com/thma/generic-persistence/issues synopsis: Database persistence using generics
src/Database/GP/SqlGenerator.hs view
@@ -182,8 +182,8 @@ columnTypeFor columnTypeMapping fieldName = columnTypeMapping fType where fType = - if isIdField @a fieldName - then "primaryKey"+ if autoIncrement @a && isIdField @a fieldName + then "AUTOINCREMENT" else maybe "OTHER" show $ maybeFieldTypeFor @a fieldName -- | A type alias for mapping a Haskell field type to a SQL column type.@@ -194,7 +194,7 @@ -- This mapping is used when no custom mapping is provided. defaultSqliteMapping :: ColumnTypeMapping defaultSqliteMapping = \case- "primaryKey" -> "INTEGER"+ "AUTOINCREMENT" -> "INTEGER" "Int" -> "INTEGER" "[Char]" -> "TEXT" "Double" -> "REAL"@@ -206,7 +206,7 @@ -- This mapping is used when no custom mapping is provided. defaultPostgresMapping :: ColumnTypeMapping defaultPostgresMapping = \case- "primaryKey" -> "serial"+ "AUTOINCREMENT" -> "serial" "Int" -> "numeric" "[Char]" -> "varchar" "Double" -> "numeric"
test/GenericPersistenceSpec.hs view
@@ -28,6 +28,7 @@ setupTable @Book conn defaultSqliteMapping setupTable @Car conn defaultSqliteMapping setupTable @Boat conn defaultSqliteMapping+ setupTable @BearerToken conn defaultSqliteMapping return conn -- | A data type with several fields, using record syntax.@@ -89,6 +90,17 @@ toRow _c b = pure [toSql (book_id b), toSql (title b), toSql (author b), toSql (year b), toSql (category b)] +data BearerToken = BearerToken+ { + token :: String,+ expires :: Int+ }+ deriving (Generic, Show, Eq)++instance Entity BearerToken where+ autoIncrement = False+ idField = "token"+ person :: Person person = Person 123456 "Alice" 25 "123 Main St" @@ -200,6 +212,12 @@ upsert conn boat2 boat2' <- selectById conn (1 :: Int) :: IO (Maybe Boat) boat2' `shouldBe` Just boat2+ it "can handle tables with non-numeric primary keys" $ do+ conn <- prepareDB+ let token = BearerToken "secret token" 202411271659+ upsert conn token+ token' <- selectById @BearerToken conn "secret token"+ token' `shouldBe` Just token it "persists new Entities using Generics" $ do conn <- prepareDB allPersons <- select conn allEntries :: IO [Person]
test/PostgresSpec.hs view
@@ -24,6 +24,7 @@ setupTable @Person conn defaultPostgresMapping setupTable @Book conn defaultPostgresMapping setupTable @Boat conn defaultPostgresMapping + setupTable @BearerToken conn defaultPostgresMapping _ <- run conn "DROP TABLE IF EXISTS Car;" [] _ <- run conn "CREATE TABLE Car (carID serial4 PRIMARY KEY, carType varchar);" [] commit conn@@ -84,6 +85,17 @@ toRow _c b = pure [toSql (book_id b), toSql (title b), toSql (author b), toSql (year b), toSql (category b)] +data BearerToken = BearerToken+ { + token :: String,+ expires :: Int+ }+ deriving (Generic, Show, Eq)++instance Entity BearerToken where+ autoIncrement = False+ idField = "token"+ person :: Person person = Person 123456 "Alice" 25 "123 Main St" @@ -248,7 +260,13 @@ boat2' <- selectById conn (1 :: Int) :: IO (Maybe Boat) boat2' `shouldBe` Just boat2 commit conn-+ it "can handle tables with non-numeric primary keys" $ do+ conn <- prepareDB+ let token = BearerToken "secret token" 202411271659+ upsert conn token+ token' <- selectById @BearerToken conn "secret token"+ token' `shouldBe` Just token + commit conn it "inserts Entities using Generics" $ do conn <- prepareDB allPersons <- select conn allEntries :: IO [Person]