Commit 27ab3632 authored by Sophie Herold's avatar Sophie Herold 🌼

Adds support for function inquire

parent bc7594d0
Pipeline #813 passed with stage
in 4 minutes and 9 seconds
......@@ -29,14 +29,15 @@ deployedSchemas = do
where
toSchema (schema, description) = do
tables <- deployedTables schema
functions <- deployedFunctions schema
return
Schema
{ schemaName = schema
, schemaDescription = description
, schemaDependencies = Nothing
, schemaFunctions = Nothing
, schemaFunctions = presetEmpty functions
, schemaFunctionTemplates = Nothing
, schemaTables = Just tables
, schemaTables = presetEmpty tables
, schemaTableTemplates = Nothing
, schemaRoles = Nothing
, schemaSequences = Nothing
......@@ -216,6 +217,66 @@ keyQuery =
GROUP BY tnsp.nspname, trel.relname, irel.relname;
|]
toVariable :: SqlType -> Maybe SqlName -> Maybe Text -> Variable
toVariable varType varName varDefault =
Variable
{ variableName = fromMaybe undefined varName
, variableDescription = Nothing
, variableType = varType
, variableDefault = varDefault
}
deployedFunctions :: SqlName -> SqlT [Function]
deployedFunctions schema = do
funs <- psqlQry qry (Only $ toSqlCode schema)
mapM toFunction funs
where
toFunction (proname, description, prorettype, proargnames, proargtypes, proargdefaults, owner, language, prosecdef, source) = do
return
Function
{ functionName = proname
, functionDescription = fromMaybe "" description
, functionReturns = prorettype
, functionParameters =
let n = length $ fromPGArray proargtypes
def = fromMaybe (replicate n Nothing)
in presetEmpty $
zipWith3
toVariable
(fromPGArray proargtypes)
(def $ fromPGArray <$> proargnames)
(def $ fromPGArray <$> proargdefaults)
, functionTemplates = Nothing
, functionTemplateData = Nothing
, functionReturnsColumns = Nothing
, functionVariables = Nothing
, functionPrivExecute = Nothing
, functionSecurityDefiner = preset False prosecdef
, functionOwner = owner
, functionLanguage = Just language
, functionBody = source
}
qry =
[sql|
SELECT
proname,
pg_catalog.obj_description(p.oid, 'pg_proc')::text AS description,
prorettype::regtype::text,
proargnames,
ARRAY(SELECT UNNEST(proargtypes::regtype[]::text[])),
ARRAY(SELECT pg_get_function_arg_default(p.oid, n)
FROM generate_series(1, pronargs) t(n)),
CASE WHEN proowner<>current_user::regrole
THEN proowner::regrole::text END,
lanname,
prosecdef,
prosrc
FROM pg_catalog.pg_proc AS p
JOIN pg_catalog.pg_language AS l
ON p.prolang = l.oid
WHERE pronamespace::regnamespace = ?::regnamespace
|]
sqlManageSchemaJoin :: Text -> Text
sqlManageSchemaJoin schemaid =
" JOIN pg_namespace AS n " <\> " ON" <-> schemaid <-> "= n.oid AND " <\>
......
......@@ -40,10 +40,10 @@ instance ToSqlStmts (SqlContext (Schema, Function)) where
sqlLanguage (functionLanguage f) <>
"\nSECURITY " <>
sqlSecurity (functionSecurityDefiner f) <>
"\nAS\n$BODY$\n" <>
"\nAS\n$BODY$" <>
sqlBody <>
"\n$BODY$\n"
stmtComment = stmtCommentOn obj $ toSqlCodeString (functionDescription f)
"$BODY$\n"
stmtComment = stmtCommentOn obj $ functionDescription f
sqlSetOwner (Just o) =
newSqlStmt SqlPriv obj $
"ALTER FUNCTION " <> sqlIdCode obj <> "OWNER TO " <>
......
......@@ -3,17 +3,15 @@ module Main where
import Control.Exception.Safe
import Control.Monad.Trans.Reader (runReaderT)
import qualified Data.ByteString as B
--import qualified Data.ByteString as B
import Data.Monoid ((<>))
import qualified Data.Text.Lazy as T
import Data.Maybe (catMaybes, fromMaybe)
import Data.Yaml.Pretty
import Database.PostgreSQL.Simple (Connection)
import System.Exit
import Text.Pretty.Simple
--import System.Directory
import Test.Tasty
import Test.Tasty.HUnit
......@@ -54,15 +52,12 @@ integrationTests =
, "postgresql://postgres@/test1"
]
r @? "Should fail"
--B.putStrLn $ encodePretty defConfig (setupLocal)
--pPrint schemasDb
--B.putStrLn $ encodePretty defConfig (newSetup schemas)
selfTestStmt :: TestTree
selfTestStmt =
testCaseSteps "stmt" $ \step -> do
(schemasDb, setupLocal) <- deploy step "test/setups/self-test.yml"
--B.putStrLn $ encodePretty (setConfDropNull True defConfig) (setupLocal)
mapM_ (doWrite "/tmp/testout" . schemaToDirTree) schemasDb
step "check statement diff"
assertNoDiff
......
---
name: f
description: Function f
language: plpgsql
returns: integer
parameters:
- name: x1
type: character varying
---
BEGIN RETURN 5; END;
---
name: f
description: Function f
language: plpgsql
returns: integer
parameters:
- name: x1
type: integer
- name: x2
type: integer
default: '9'
- name: x3
type: character varying
default: "'mydef,str'::character varying"
- name: x4
type: date
default: now()
---
BEGIN RETURN x1*2 + x2; END;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment