From 2669e43dd35d41f4e8dd5cfc9d6df23ba4cb6a69 Mon Sep 17 00:00:00 2001 From: RoelofWobben <40333092+RoelofWobben@users.noreply.github.com> Date: Thu, 19 Jul 2018 14:43:18 +0200 Subject: [PATCH 1/2] Update persistent.asciidoc --- book/asciidoc/persistent.asciidoc | 42 +++++++++++++++++++++---------- 1 file changed, 29 insertions(+), 13 deletions(-) diff --git a/book/asciidoc/persistent.asciidoc b/book/asciidoc/persistent.asciidoc index fb7243a6..d8d5a052 100644 --- a/book/asciidoc/persistent.asciidoc +++ b/book/asciidoc/persistent.asciidoc @@ -79,7 +79,7 @@ BlogPost main :: IO () main = runSqlite ":memory:" $ do - runMigration migrateAll :: IO () + runMigration migrateAll johnId <- insert $ Person "John Doe" $ Just 35 janeId <- insert $ Person "Jane Doe" Nothing @@ -330,20 +330,29 @@ pass it off to other Persistent functions. {-# LANGUAGE QuasiQuotes #-} {-# LANGUAGE TemplateHaskell #-} {-# LANGUAGE TypeFamilies #-} + import Control.Monad.IO.Class (liftIO) import Database.Persist import Database.Persist.Sqlite import Database.Persist.TH +import Control.Monad.IO.Unlift +import Data.Text +import Control.Monad.Reader +import Control.Monad.Logger +import Conduit -share [mkPersist sqlSettings, mkMigrate "migrateAll"] [persistLowerCase| +share [mkPersist sqlSettings, mkSave "entityDefs"] [persistLowerCase| Person name String age Int Maybe deriving Show |] +runSqlite' :: (MonadUnliftIO m) => Text -> ReaderT SqlBackend (NoLoggingT (ResourceT m)) a -> m a +runSqlite' = runSqlite + main :: IO () -main = runSqlite ":memory:" $ do +main = runSqlite' ":memory:" $ do michaelId <- insert $ Person "Michael" $ Just 26 michael <- get michaelId liftIO $ print michael @@ -435,27 +444,34 @@ to _ask_ it to help. Let's see what this looks like: {-# LANGUAGE QuasiQuotes #-} {-# LANGUAGE TemplateHaskell #-} {-# LANGUAGE TypeFamilies #-} -import Database.Persist -import Database.Persist.TH -import Database.Persist.Sqlite -import Control.Monad.IO.Class (liftIO) + +import Control.Monad.IO.Class (liftIO) +import Database.Persist +import Database.Persist.Sqlite +import Database.Persist.TH +import Control.Monad.IO.Unlift +import Data.Text +import Control.Monad.Reader +import Control.Monad.Logger +import Conduit share [mkPersist sqlSettings, mkSave "entityDefs"] [persistLowerCase| Person name String - age Int + age Int Maybe deriving Show |] +runSqlite' :: (MonadUnliftIO m) => Text -> ReaderT SqlBackend (NoLoggingT (ResourceT m)) a -> m a +runSqlite' = runSqlite + main :: IO () -main = runSqlite ":memory:" $ do - -- this line added: that's it! +main = runSqlite' ":memory:" $ do runMigration $ migrate entityDefs $ entityDef (Nothing :: Maybe Person) - michaelId <- insert $ Person "Michael" 26 + michaelId <- insert $ Person "Michael" $ Just 26 michael <- get michaelId liftIO $ print michael ----- - +---- With this one little code change, Persistent will automatically create your +Person+ table for you. This split between +runMigration+ and +migrate+ allows you to migrate multiple tables simultaneously. From 895e70323995ec9f151f7ed9605745217df2db8e Mon Sep 17 00:00:00 2001 From: RoelofWobben <40333092+RoelofWobben@users.noreply.github.com> Date: Mon, 6 Aug 2018 18:33:51 +0200 Subject: [PATCH 2/2] Update persistent.asciidoc --- book/asciidoc/persistent.asciidoc | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/book/asciidoc/persistent.asciidoc b/book/asciidoc/persistent.asciidoc index d8d5a052..d184e3c0 100644 --- a/book/asciidoc/persistent.asciidoc +++ b/book/asciidoc/persistent.asciidoc @@ -462,11 +462,8 @@ Person deriving Show |] -runSqlite' :: (MonadUnliftIO m) => Text -> ReaderT SqlBackend (NoLoggingT (ResourceT m)) a -> m a -runSqlite' = runSqlite - main :: IO () -main = runSqlite' ":memory:" $ do +main = runSqlite ":memory:" $ do runMigration $ migrate entityDefs $ entityDef (Nothing :: Maybe Person) michaelId <- insert $ Person "Michael" $ Just 26 michael <- get michaelId