Merge pull request #46 from isaactovsky/parse-connection-postgres-uri

Parse connection postgres uri
This commit is contained in:
paluh 2020-05-21 18:20:36 +02:00 committed by GitHub
commit fd465da8ac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 90 additions and 41 deletions

View File

@ -166,8 +166,6 @@ To run suite please:
Till we are hosted on the github platform let's just use github releasing model for tagging new versions and `github-release-notes` to generate CHANGELOG.md from it: Till we are hosted on the github platform let's just use github releasing model for tagging new versions and `github-release-notes` to generate CHANGELOG.md from it:
```bash `$ # This only requires repo access`
$ # This only requires repo access `$ export GREN_GITHUB_TOKEN=...`
$ export GREN_GITHUB_TOKEN=... `$ github-release-notes changelog --override`
$ github-release-notes changelog --override
```

View File

@ -2,40 +2,38 @@
Welcome to a Spago project! Welcome to a Spago project!
You can edit this file as you like. You can edit this file as you like.
-} -}
{ name = { name = "postgresql-client"
"postgresql-client"
, license = "BSD-3-Clause" , license = "BSD-3-Clause"
, dependencies = , dependencies =
[ "aff" [ "aff"
, "arrays" , "argonaut"
, "argonaut" , "arrays"
, "assert" , "assert"
, "bifunctors" , "bifunctors"
, "bytestrings" , "bytestrings"
, "console" , "console"
, "datetime" , "datetime"
, "decimals" , "decimals"
, "effect" , "effect"
, "either" , "either"
, "exceptions" , "exceptions"
, "foldable-traversable" , "foldable-traversable"
, "foreign" , "foreign"
, "foreign-generic" , "foreign-generic"
, "foreign-object" , "foreign-object"
, "js-date" , "js-date"
, "lists" , "lists"
, "maybe" , "maybe"
, "newtype" , "newtype"
, "nullable" , "nullable"
, "prelude" , "prelude"
, "psci-support" , "psci-support"
, "test-unit" , "string-parsers"
, "transformers" , "test-unit"
, "tuples" , "transformers"
] , "tuples"
, packages = ]
./packages.dhall , packages = ./packages.dhall
, repository = "https://github.com/rightfold/purescript-postgresql-client.git" , repository = "https://github.com/rightfold/purescript-postgresql-client.git"
, sources = , sources = [ "src/**/*.purs", "test/**/*.purs" ]
[ "src/**/*.purs", "test/**/*.purs" ]
} }

View File

@ -9,11 +9,13 @@ module Database.PostgreSQL
, Connection , Connection
, ConnectResult , ConnectResult
, Query(..) , Query(..)
, PgConnectionUri
, newPool , newPool
, connect , connect
, withConnection , withConnection
, withTransaction , withTransaction
, defaultPoolConfiguration , defaultPoolConfiguration
, getDefaultPoolConfigurationByUri
, command , command
, execute , execute
, query , query
@ -28,12 +30,14 @@ import Data.Bifunctor (lmap)
import Data.Either (Either(..), either, hush) import Data.Either (Either(..), either, hush)
import Data.Generic.Rep (class Generic) import Data.Generic.Rep (class Generic)
import Data.Generic.Rep.Show (genericShow) import Data.Generic.Rep.Show (genericShow)
import Data.Int (fromString)
import Data.Maybe (Maybe(..), maybe) import Data.Maybe (Maybe(..), maybe)
import Data.Newtype (class Newtype) import Data.Newtype (class Newtype)
import Data.Nullable (Nullable, toMaybe, toNullable) import Data.Nullable (Nullable, toMaybe, toNullable)
import Data.String (Pattern(..)) import Data.String (Pattern(..))
import Data.String as String import Data.String as String
import Data.Traversable (traverse) import Data.String.CodeUnits (singleton)
import Data.Traversable (foldMap, traverse)
import Database.PostgreSQL.Row (class FromSQLRow, class ToSQLRow, Row0(..), Row1(..), Row10(..), Row11(..), Row12(..), Row13(..), Row14(..), Row15(..), Row16(..), Row17(..), Row18(..), Row19(..), Row2(..), Row3(..), Row4(..), Row5(..), Row6(..), Row7(..), Row8(..), Row9(..), fromSQLRow, toSQLRow) as Row import Database.PostgreSQL.Row (class FromSQLRow, class ToSQLRow, Row0(..), Row1(..), Row10(..), Row11(..), Row12(..), Row13(..), Row14(..), Row15(..), Row16(..), Row17(..), Row18(..), Row19(..), Row2(..), Row3(..), Row4(..), Row5(..), Row6(..), Row7(..), Row8(..), Row9(..), fromSQLRow, toSQLRow) as Row
import Database.PostgreSQL.Row (class FromSQLRow, class ToSQLRow, Row0(..), Row1(..), fromSQLRow, toSQLRow) import Database.PostgreSQL.Row (class FromSQLRow, class ToSQLRow, Row0(..), Row1(..), fromSQLRow, toSQLRow)
import Database.PostgreSQL.Value (class FromSQLValue) import Database.PostgreSQL.Value (class FromSQLValue)
@ -44,6 +48,9 @@ import Effect.Aff.Compat (EffectFnAff, fromEffectFnAff)
import Effect.Class (liftEffect) import Effect.Class (liftEffect)
import Effect.Exception (Error) import Effect.Exception (Error)
import Foreign (Foreign) import Foreign (Foreign)
import Text.Parsing.StringParser (runParser)
import Text.Parsing.StringParser.CodePoints (anyChar, char, string)
import Text.Parsing.StringParser.Combinators (many, manyTill)
type Database = String type Database = String
@ -69,6 +76,28 @@ defaultPoolConfiguration database =
, user: Nothing , user: Nothing
} }
type PgConnectionUri = String
-- | Get the default pool configuration from postgres connection uri
getDefaultPoolConfigurationByUri :: PgConnectionUri -> Maybe PoolConfiguration
getDefaultPoolConfigurationByUri uri = hush $ flip runParser uri do
_ <- string "postgres://"
user <- tillChar (char ':')
password <- tillChar (char '@')
host <- tillChar (char ':')
port <- tillChar (char '/')
database <- many anyChar
pure { database: toStr database
, host: Just $ toStr host
, idleTimeoutMillis: Nothing
, max: Nothing
, password: Just $ toStr password
, port: fromString $ toStr port
, user: Just $ toStr user
}
where tillChar = manyTill anyChar
toStr = foldMap singleton
-- | PostgreSQL connection pool. -- | PostgreSQL connection pool.
foreign import data Pool :: Type foreign import data Pool :: Type

View File

@ -22,13 +22,14 @@ import Data.Maybe (Maybe(..), fromJust)
import Data.Newtype (unwrap) import Data.Newtype (unwrap)
import Data.Tuple (Tuple(..)) import Data.Tuple (Tuple(..))
import Data.Tuple.Nested ((/\)) import Data.Tuple.Nested ((/\))
import Database.PostgreSQL (PgConnectionUri, getDefaultPoolConfigurationByUri)
import Database.PostgreSQL.PG (Connection, PGError(..), Pool, PoolConfiguration, Query(Query), Row0(Row0), Row1(Row1), Row2(Row2), Row3(Row3), Row9(Row9), command, execute, newPool, onIntegrityError, query, scalar) import Database.PostgreSQL.PG (Connection, PGError(..), Pool, PoolConfiguration, Query(Query), Row0(Row0), Row1(Row1), Row2(Row2), Row3(Row3), Row9(Row9), command, execute, newPool, onIntegrityError, query, scalar)
import Effect (Effect) import Effect (Effect)
import Effect.Aff (Aff, error, launchAff) import Effect.Aff (Aff, error, launchAff)
import Effect.Class (liftEffect) import Effect.Class (liftEffect)
import Effect.Exception (message) import Effect.Exception (message)
import Foreign.Object (fromFoldable) as Object
import Foreign.Object (Object) import Foreign.Object (Object)
import Foreign.Object (fromFoldable) as Object
import Global.Unsafe (unsafeStringify) import Global.Unsafe (unsafeStringify)
import Math ((%)) import Math ((%))
import Partial.Unsafe (unsafePartial) import Partial.Unsafe (unsafePartial)
@ -357,6 +358,11 @@ main = do
Left (ProgrammingError { code, message }) -> equal code "3D000" Left (ProgrammingError { code, message }) -> equal code "3D000"
_ -> Test.Unit.failure "PostgreSQL error was expected" _ -> Test.Unit.failure "PostgreSQL error was expected"
Test.Unit.test "get pool configuration from postgres uri" do
equal (getDefaultPoolConfigurationByUri validUriToPoolConfigs.uri) (Just validUriToPoolConfigs.poolConfig)
equal (getDefaultPoolConfigurationByUri notValidConnUri) Nothing
config :: PoolConfiguration config :: PoolConfiguration
config = config =
{ user: Nothing { user: Nothing
@ -374,4 +380,22 @@ noSuchDatabaseConfig =
cannotConnectConfig :: PoolConfiguration cannotConnectConfig :: PoolConfiguration
cannotConnectConfig = cannotConnectConfig =
config { host = Just "127.0.0.1", port = Just 45287 } config { host = Just "127.0.0.1"
, port = Just 45287
}
validUriToPoolConfigs :: { uri :: PgConnectionUri
, poolConfig :: PoolConfiguration }
validUriToPoolConfigs = { uri: "postgres://urllgqrivcyako:c52275a95b7f177e2850c49de9bfa8bedc457ce860ccca664cb15db973554969@ec2-79-124-25-231.eu-west-1.compute.amazonaws.com:5432/e7cecg4nirunpo"
, poolConfig: { database: "e7cecg4nirunpo"
, host: Just "ec2-79-124-25-231.eu-west-1.compute.amazonaws.com"
, idleTimeoutMillis: Nothing
, max: Nothing
, password: Just "c52275a95b7f177e2850c49de9bfa8bedc457ce860ccca664cb15db973554969"
, port: Just 5432
, user: Just "urllgqrivcyako"
}
}
notValidConnUri :: PgConnectionUri
notValidConnUri = "postgres://urllgqrivcyakoc52275a95b7f177e2850c49de9bfa8bedc457ce860ccca664cb15db973554969@ec2-79-124-25-231.eu-west-1.compute.amazonaws.com:5432/e7cecg4nirunpo"