-}
-{-# LANGUAGE ConstraintKinds #-}
+{-# LANGUAGE ConstraintKinds, ScopedTypeVariables #-}
module Gargantext.Database.Prelude where
import Control.Exception
import Control.Lens (Getter, view)
import Control.Monad.Except
+--import Control.Monad.Logger (MonadLogger)
import Control.Monad.Random
import Control.Monad.Reader
import Control.Monad.Trans.Control (MonadBaseControl)
import Data.Either.Extra (Either)
import Data.Pool (Pool, withResource)
import Data.Profunctor.Product.Default (Default)
-import Data.Text (unpack, Text)
+import Data.Text (pack, unpack, Text)
import Data.Word (Word16)
import Database.PostgreSQL.Simple (Connection, connect)
import Database.PostgreSQL.Simple.FromField ( Conversion, ResultError(ConversionFailed), fromField, returnError)
import Database.PostgreSQL.Simple.Internal (Field)
+import Database.PostgreSQL.Simple.Types (Query(..))
import Gargantext.Core.Mail.Types (HasMail)
import Gargantext.Prelude
import Gargantext.Prelude.Config (readIniFile', val)
import Opaleye.Aggregate (countRows)
import System.IO (FilePath)
import System.IO (stderr)
-import Text.Read (read)
+import Text.Read (readMaybe)
import qualified Data.ByteString as DB
import qualified Data.List as DL
import qualified Database.PostgreSQL.Simple as PGS
, MonadError err m
, MonadBaseControl IO m
, MonadRandom m
+ --, MonadLogger m
)
type CmdM' env err m =
( MonadReader env m
, MonadError err m
, MonadBaseControl IO m
+ --, MonadLogger m
-- , MonadRandom m
)
runPGSQuery_ q = mkCmd $ \conn -> catch (PGS.query_ conn q) printError
where
printError (SomeException e) = do
- printDebug "[G.D.P.runPGSQuery_]" ("TODO: format query error" :: Text)
+ hPutStrLn stderr (fromQuery q)
throw (SomeException e)
-
execPGSQuery :: PGS.ToRow a => PGS.Query -> a -> Cmd err Int64
execPGSQuery q a = mkCmd $ \conn -> PGS.execute conn q a
databaseParameters fp = do
ini <- readIniFile' fp
let val' key = unpack $ val ini "database" key
+ let dbPortRaw = val' "DB_PORT"
+ let dbPort = case (readMaybe dbPortRaw :: Maybe Word16) of
+ Nothing -> panic $ "DB_PORT incorrect: " <> (pack dbPortRaw)
+ Just d -> d
pure $ PGS.ConnectInfo { PGS.connectHost = val' "DB_HOST"
- , PGS.connectPort = read (val' "DB_PORT") :: Word16
+ , PGS.connectPort = dbPort
, PGS.connectUser = val' "DB_USER"
, PGS.connectPassword = val' "DB_PASS"
, PGS.connectDatabase = val' "DB_NAME"
printSqlOpa :: Default Unpackspec a a => Select a -> IO ()
printSqlOpa = putStrLn . maybe "Empty query" identity . showSql
+dbCheck :: CmdM env err m => m Bool
+dbCheck = do
+ r :: [PGS.Only Text] <- runPGSQuery_ "select username from public.auth_user"
+ case r of
+ [] -> return False
+ _ -> return True