Fix #1182, fix subselect view embedding for pg10
This commit is contained in:
committed by
Steve Chávez
parent
b48824bddd
commit
dc834572d6
+1
-2
@@ -5,12 +5,11 @@ module Main where
|
|||||||
|
|
||||||
import PostgREST.App (postgrest)
|
import PostgREST.App (postgrest)
|
||||||
import PostgREST.Config (AppConfig (..),
|
import PostgREST.Config (AppConfig (..),
|
||||||
minimumPgVersion,
|
|
||||||
prettyVersion, readOptions)
|
prettyVersion, readOptions)
|
||||||
import PostgREST.DbStructure (getDbStructure, getPgVersion)
|
import PostgREST.DbStructure (getDbStructure, getPgVersion)
|
||||||
import PostgREST.Error (encodeError)
|
import PostgREST.Error (encodeError)
|
||||||
import PostgREST.OpenAPI (isMalformedProxyUri)
|
import PostgREST.OpenAPI (isMalformedProxyUri)
|
||||||
import PostgREST.Types (DbStructure, Schema, PgVersion(..))
|
import PostgREST.Types (DbStructure, Schema, PgVersion(..), minimumPgVersion)
|
||||||
import Protolude hiding (hPutStrLn, replace)
|
import Protolude hiding (hPutStrLn, replace)
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
+1
-14
@@ -18,9 +18,6 @@ module PostgREST.Config ( prettyVersion
|
|||||||
, docsVersion
|
, docsVersion
|
||||||
, readOptions
|
, readOptions
|
||||||
, corsPolicy
|
, corsPolicy
|
||||||
, minimumPgVersion
|
|
||||||
, pgVersion95
|
|
||||||
, pgVersion96
|
|
||||||
, AppConfig (..)
|
, AppConfig (..)
|
||||||
)
|
)
|
||||||
where
|
where
|
||||||
@@ -52,7 +49,7 @@ import Network.Wai.Middleware.Cors (CorsResourcePolicy (..))
|
|||||||
import Options.Applicative hiding (str)
|
import Options.Applicative hiding (str)
|
||||||
import Paths_postgrest (version)
|
import Paths_postgrest (version)
|
||||||
import PostgREST.Parsers (pRoleClaimKey)
|
import PostgREST.Parsers (pRoleClaimKey)
|
||||||
import PostgREST.Types (PgVersion(..), ApiRequestError(..),
|
import PostgREST.Types (ApiRequestError(..),
|
||||||
JSPath, JSPathExp(..))
|
JSPath, JSPathExp(..))
|
||||||
import Protolude hiding (hPutStrLn, take,
|
import Protolude hiding (hPutStrLn, take,
|
||||||
intercalate, (<>))
|
intercalate, (<>))
|
||||||
@@ -233,13 +230,3 @@ pathParser =
|
|||||||
strArgument $
|
strArgument $
|
||||||
metavar "FILENAME" <>
|
metavar "FILENAME" <>
|
||||||
help "Path to configuration file"
|
help "Path to configuration file"
|
||||||
|
|
||||||
-- | Tells the minimum PostgreSQL version required by this version of PostgREST
|
|
||||||
minimumPgVersion :: PgVersion
|
|
||||||
minimumPgVersion = PgVersion 90400 "9.4"
|
|
||||||
|
|
||||||
pgVersion96 :: PgVersion
|
|
||||||
pgVersion96 = PgVersion 90600 "9.6"
|
|
||||||
|
|
||||||
pgVersion95 :: PgVersion
|
|
||||||
pgVersion95 = PgVersion 90500 "9.5"
|
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ import Data.Text (split, strip,
|
|||||||
import qualified Data.Text as T
|
import qualified Data.Text as T
|
||||||
import qualified Hasql.Session as H
|
import qualified Hasql.Session as H
|
||||||
import PostgREST.Types
|
import PostgREST.Types
|
||||||
import Text.InterpolatedString.Perl6 (q)
|
import Text.InterpolatedString.Perl6 (q, qc)
|
||||||
|
|
||||||
import GHC.Exts (groupWith)
|
import GHC.Exts (groupWith)
|
||||||
import Protolude
|
import Protolude
|
||||||
@@ -36,7 +36,7 @@ getDbStructure :: Schema -> PgVersion -> H.Session DbStructure
|
|||||||
getDbStructure schema pgVer = do
|
getDbStructure schema pgVer = do
|
||||||
tabs <- H.statement () allTables
|
tabs <- H.statement () allTables
|
||||||
cols <- H.statement schema $ allColumns tabs
|
cols <- H.statement schema $ allColumns tabs
|
||||||
syns <- H.statement schema $ allSynonyms cols
|
syns <- H.statement schema $ allSynonyms cols pgVer
|
||||||
childRels <- H.statement () $ allChildRelations tabs cols
|
childRels <- H.statement () $ allChildRelations tabs cols
|
||||||
keys <- H.statement () $ allPrimaryKeys tabs
|
keys <- H.statement () $ allPrimaryKeys tabs
|
||||||
procs <- H.statement schema allProcs
|
procs <- H.statement schema allProcs
|
||||||
@@ -685,11 +685,15 @@ pkFromRow :: [Table] -> (Schema, Text, Text) -> Maybe PrimaryKey
|
|||||||
pkFromRow tabs (s, t, n) = PrimaryKey <$> table <*> pure n
|
pkFromRow tabs (s, t, n) = PrimaryKey <$> table <*> pure n
|
||||||
where table = find (\tbl -> tableSchema tbl == s && tableName tbl == t) tabs
|
where table = find (\tbl -> tableSchema tbl == s && tableName tbl == t) tabs
|
||||||
|
|
||||||
allSynonyms :: [Column] -> H.Statement Schema [Synonym]
|
allSynonyms :: [Column] -> PgVersion -> H.Statement Schema [Synonym]
|
||||||
allSynonyms cols =
|
allSynonyms cols pgVer =
|
||||||
H.Statement sql (HE.param HE.text) (decodeSynonyms cols) True
|
H.Statement sql (HE.param HE.text) (decodeSynonyms cols) True
|
||||||
-- query explanation at https://gist.github.com/steve-chavez/7ee0e6590cddafb532e5f00c46275569
|
-- query explanation at https://gist.github.com/steve-chavez/7ee0e6590cddafb532e5f00c46275569
|
||||||
where sql = [q|
|
where
|
||||||
|
subselectRegex :: Text
|
||||||
|
subselectRegex | pgVer < pgVersion100 = ":subselect {.*?:constraintDeps <>} :location"
|
||||||
|
| otherwise = ":subselect {.*?:stmt_len 0} :location"
|
||||||
|
sql = [qc|
|
||||||
with
|
with
|
||||||
views as (
|
views as (
|
||||||
select
|
select
|
||||||
@@ -704,7 +708,7 @@ allSynonyms cols =
|
|||||||
removed_subselects as(
|
removed_subselects as(
|
||||||
select
|
select
|
||||||
view_schema, view_name,
|
view_schema, view_name,
|
||||||
regexp_replace(view_definition, ':subselect {.*?:constraintDeps <>} :location', '', 'g') as x
|
regexp_replace(view_definition, '{subselectRegex}', '', 'g') as x
|
||||||
from views
|
from views
|
||||||
),
|
),
|
||||||
target_lists as(
|
target_lists as(
|
||||||
@@ -745,8 +749,7 @@ allSynonyms cols =
|
|||||||
join pg_attribute col on col.attrelid = tbl.oid and col.attnum::text = res.resorigcol
|
join pg_attribute col on col.attrelid = tbl.oid and col.attnum::text = res.resorigcol
|
||||||
join pg_namespace sch on sch.oid = tbl.relnamespace
|
join pg_namespace sch on sch.oid = tbl.relnamespace
|
||||||
where resorigtbl <> '0'
|
where resorigtbl <> '0'
|
||||||
order by view_schema, view_name, view_colum_name;
|
order by view_schema, view_name, view_colum_name; |]
|
||||||
|]
|
|
||||||
|
|
||||||
synonymFromRow :: [Column] -> (Text,Text,Text,Text,Text,Text) -> Maybe Synonym
|
synonymFromRow :: [Column] -> (Text,Text,Text,Text,Text,Text) -> Maybe Synonym
|
||||||
synonymFromRow allCols (s1,t1,c1,s2,t2,c2) = (,) <$> col1 <*> col2
|
synonymFromRow allCols (s1,t1,c1,s2,t2,c2) = (,) <$> col1 <*> col2
|
||||||
|
|||||||
@@ -32,7 +32,6 @@ import qualified Hasql.Decoders as HD
|
|||||||
|
|
||||||
import qualified Data.Aeson as JSON
|
import qualified Data.Aeson as JSON
|
||||||
|
|
||||||
import PostgREST.Config (pgVersion96)
|
|
||||||
import PostgREST.RangeQuery (rangeLimit, rangeOffset, allRange)
|
import PostgREST.RangeQuery (rangeLimit, rangeOffset, allRange)
|
||||||
import qualified Data.HashMap.Strict as HM
|
import qualified Data.HashMap.Strict as HM
|
||||||
import Data.Maybe
|
import Data.Maybe
|
||||||
|
|||||||
+17
-1
@@ -317,7 +317,23 @@ toMime (CTOther ct) = ct
|
|||||||
data PgVersion = PgVersion {
|
data PgVersion = PgVersion {
|
||||||
pgvNum :: Int32
|
pgvNum :: Int32
|
||||||
, pgvName :: Text
|
, pgvName :: Text
|
||||||
} deriving (Eq, Ord, Show)
|
} deriving (Eq, Show)
|
||||||
|
|
||||||
|
instance Ord PgVersion where
|
||||||
|
(PgVersion v1 _) `compare` (PgVersion v2 _) = v1 `compare` v2
|
||||||
|
|
||||||
|
-- | Tells the minimum PostgreSQL version required by this version of PostgREST
|
||||||
|
minimumPgVersion :: PgVersion
|
||||||
|
minimumPgVersion = PgVersion 90400 "9.4"
|
||||||
|
|
||||||
|
pgVersion95 :: PgVersion
|
||||||
|
pgVersion95 = PgVersion 90500 "9.5"
|
||||||
|
|
||||||
|
pgVersion96 :: PgVersion
|
||||||
|
pgVersion96 = PgVersion 90600 "9.6"
|
||||||
|
|
||||||
|
pgVersion100 :: PgVersion
|
||||||
|
pgVersion100 = PgVersion 100000 "10"
|
||||||
|
|
||||||
sourceCTEName :: SqlFragment
|
sourceCTEName :: SqlFragment
|
||||||
sourceCTEName = "pg_source"
|
sourceCTEName = "pg_source"
|
||||||
|
|||||||
+1
-2
@@ -6,9 +6,8 @@ import SpecHelper
|
|||||||
import qualified Hasql.Pool as P
|
import qualified Hasql.Pool as P
|
||||||
|
|
||||||
import PostgREST.App (postgrest)
|
import PostgREST.App (postgrest)
|
||||||
import PostgREST.Config (pgVersion95, pgVersion96)
|
|
||||||
import PostgREST.DbStructure (getDbStructure, getPgVersion)
|
import PostgREST.DbStructure (getDbStructure, getPgVersion)
|
||||||
import PostgREST.Types (DbStructure(..))
|
import PostgREST.Types (DbStructure(..), pgVersion95, pgVersion96)
|
||||||
import Control.AutoUpdate (defaultUpdateSettings, mkAutoUpdate, updateAction)
|
import Control.AutoUpdate (defaultUpdateSettings, mkAutoUpdate, updateAction)
|
||||||
import Data.Function (id)
|
import Data.Function (id)
|
||||||
import Data.IORef
|
import Data.IORef
|
||||||
|
|||||||
Reference in New Issue
Block a user