persistent-database-url (empty) → 1.0.0
raw patch · 5 files changed
+114/−0 lines, 5 filesdep +basedep +bytestringdep +failsetup-changed
Dependencies added: base, bytestring, fail, hspec, persistent-database-url, persistent-postgresql, uri-bytestring
Files
- LICENSE +19/−0
- Setup.hs +2/−0
- persistent-database-url.cabal +43/−0
- src/Database/Persist/URL.hs +49/−0
- test/Spec.hs +1/−0
+ LICENSE view
@@ -0,0 +1,19 @@+Copyright (c) 2014 thoughtbot <hello@thoughtbot.com>++Permission is hereby granted, free of charge, to any person obtaining a copy+of this software and associated documentation files (the "Software"), to deal+in the Software without restriction, including without limitation the rights+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell+copies of the Software, and to permit persons to whom the Software is+furnished to do so, subject to the following conditions:++The above copyright notice and this permission notice shall be included in+all copies or substantial portions of the Software.++THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN+THE SOFTWARE.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ persistent-database-url.cabal view
@@ -0,0 +1,43 @@+name: persistent-database-url+version: 1.0.0+author: Joe Ferris <jferris@thoughtbot.com>+maintainer: Joe Ferris <jferris@thoughtbot.com>+license: MIT+license-File: LICENSE+synopsis: Parse DATABASE_URL into configuration types for Persistent+description:+ Converts the parameters parsed from a database url to the concrete+ configuration types required by persistent.+ .+ Currently, only persistent-postgresql's PostgresConf is provided.+ .++cabal-Version: >= 1.10+build-Type: Simple+category: Database++library+ default-language: Haskell2010+ hs-source-dirs: src+ ghc-options: -Wall+ exposed-modules: Database.Persist.URL+ build-depends: base >= 4 && < 5+ , bytestring+ , persistent-postgresql >= 1.0.0+ , uri-bytestring+ , fail++test-suite spec+ type: exitcode-stdio-1.0+ default-language: Haskell2010+ hs-source-dirs: test+ ghc-options: -Wall+ main-is: Spec.hs+ build-depends: base >= 4.7.0.0+ , hspec+ , persistent-database-url+ , persistent-postgresql++source-repository head+ type: git+ location: https://github.com/thoughtbot/persistent-database-url
+ src/Database/Persist/URL.hs view
@@ -0,0 +1,49 @@+{-# LANGUAGE OverloadedStrings #-}+module Database.Persist.URL+ ( fromDatabaseUrl+ ) where++import Control.Monad (unless)+import Control.Monad.Fail (MonadFail)+import Data.ByteString (ByteString, uncons)+import Data.Monoid ((<>))+import Database.Persist.Postgresql (PostgresConf(..))+import URI.ByteString+ ( Authority(..)+ , Host(..)+ , Port(..)+ , URI(..)+ , UserInfo(..)+ , Scheme(..)+ , parseURI+ , strictURIParserOptions+ )++import qualified Data.ByteString.Char8 as Char8++-- | Build a @'PostgresConf'@ by parsing a database URL String+fromDatabaseUrl :: MonadFail m => Int -> ByteString -> m PostgresConf+fromDatabaseUrl size url = do+ uri <- abortLeft $ parseURI strictURIParserOptions url+ auth <- abortNothing "authority" $ uriAuthority uri+ userInfo <- abortNothing "user info" $ authorityUserInfo auth+ port <- abortNothing "port" $ authorityPort auth+ dbName <- abortNothing "path" $ snd <$> uncons (uriPath uri)+ unless (schemeBS (uriScheme uri) == "postgres") $+ fail "DATABASE_URL has unknown scheme"++ return PostgresConf+ { pgConnStr =+ "user=" <> uiUsername userInfo+ <> " password=" <> uiPassword userInfo+ <> " host=" <> hostBS (authorityHost auth)+ <> " port=" <> Char8.pack (show $ portNumber port)+ <> " dbname=" <> dbName+ , pgPoolSize = size+ }++abortLeft :: (MonadFail m, Show e) => Either e b -> m b+abortLeft = either (fail . ("DATABASE_URL failed to parse: " <>) . show) return++abortNothing :: MonadFail m => String -> Maybe a -> m a+abortNothing s = maybe (fail $ "DATABASE_URL is missing " <> s) return
+ test/Spec.hs view
@@ -0,0 +1,1 @@+{-# OPTIONS_GHC -F -pgmF hspec-discover #-}