Groundhog usage in Snap Applications

82 views
Skip to first unread message

Cody Goodman

unread,
Dec 8, 2014, 2:36:25 AM12/8/14
to snap_fr...@googlegroups.com
I'm just wondering how/where I should do migrations with groundhog in a snap application. I'm also wondering if I could get a small example of using groundhog with a simple snap application, but if that's asking too much the first will do.

I believe I can construct the small example myself after figuring out where to/how to do migrations. I remember hearing that groundhog doesn't require using a snaplet, but things like persistent do. Why is that?

Thanks!

MightyByte

unread,
Dec 8, 2014, 10:48:13 AM12/8/14
to snap_fr...@googlegroups.com
Call something like the following in your initializer.

runMigration defaultMigrationLogger $ do
migrate (undefined :: BlogPost)
migrate (undefined :: BlogComment)
...

You can find more info on groundhog's migration functions here
http://hackage.haskell.org/package/groundhog-0.7.0/docs/Database-Groundhog.html#g:3

Technically none of the other DBs require a snaplet either. But in
practice, there is some boilerplate involved in using them because of
the way their APIs are designed. Groundhog's API (the
ConnectionManager type class in particular) requires less boilerplate
than persistent and postgresql-simple, so the amount of boilerplate is
small enough that having a snaplet for it doesn't really make sense.
The boilerplate ends up looking something like this:

data App = App
{ _heist :: Snaplet (Heist App)
, _sess :: Snaplet SessionManager
, _gh :: Pool Postgresql
...
}

instance ConnectionManager App Postgresql where
withConn f app = withConn f (_gh app)
withConnNoTransaction f app = withConnNoTransaction f (_gh app)

runGH :: ConnectionManager b conn
=> DbPersist conn (NoLoggingT IO) a
-> Handler b v a
runGH f = withTop' id $ do
cm <- ask
liftIO $ runNoLoggingT (withConn (runDbPersist f) cm)

In snap 1.0 this will be even less because the impedance mismatch
between MonadCatchIO-transformers and monad-control will be gone.
IIRC this will make the runGH function unnecessary.
> --
>
> ---
> You received this message because you are subscribed to the Google Groups
> "Snap Framework" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to snap_framewor...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Cody Goodman

unread,
Dec 13, 2014, 8:29:55 PM12/13/14
to snap_fr...@googlegroups.com
Thanks again for your help! I eagerly await Snap 1.0 :)
Reply all
Reply to author
Forward
0 new messages