From 5a88161ff7937198fa8068ef39f10c18bc16ff06 Mon Sep 17 00:00:00 2001 From: Wolfgang Walther Date: Sat, 5 Feb 2022 12:43:45 +0100 Subject: [PATCH] fix: Take PG version into account in --dump-schema The PG version is only read by the Connection Worker, which is not used in the case dump-schema. Now, the pg version is read in the schema cache queries directly, avoiding this problem in all cases. Signed-off-by: Wolfgang Walther --- CHANGELOG.md | 7 +++++++ src/PostgREST/CLI.hs | 2 -- src/PostgREST/Config/Database.hs | 8 ++++++-- src/PostgREST/DbStructure.hs | 6 ++++-- src/PostgREST/Workers.hs | 3 +-- test/spec/Main.hs | 6 ++---- 6 files changed, 20 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1a295e11d..6f8ada225 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,13 @@ This project adheres to [Semantic Versioning](http://semver.org/). - #2120, Fix reading database configuration properly when `=` is present in value - @wolfgangwalther - #1771, Fix silently ignoring filter on a non-existent embedded resource - @steve-chavez - #2135, Remove trigger functions from schema cache and OpenAPI output, because they can't be called directly anyway. - @wolfgangwalther + - #2145, Fix accessing json array fields with -> and ->> in ?select= and ?order=. - @wolfgangwalther + - #2153, Fix --dump-schema running with a wrong PG version. - @wolfgangwalther + +### Changed + + - #2001, Return 204 No Content without Content-Type for RPCs returning VOID - @wolfgangwalther + + Previously, those RPCs would return "null" as a body with Content-Type: application/json. ## [9.0.0] - 2021-11-25 diff --git a/src/PostgREST/CLI.hs b/src/PostgREST/CLI.hs index 6cd604dfc..6fddad387 100644 --- a/src/PostgREST/CLI.hs +++ b/src/PostgREST/CLI.hs @@ -53,7 +53,6 @@ main installSignalHandlers runAppWithSocket CLI{cliCommand, cliPath} = do dumpSchema :: AppState -> IO LBS.ByteString dumpSchema appState = do AppConfig{..} <- AppState.getConfig appState - actualPgVersion <- AppState.getPgVersion appState result <- let transaction = if configDbPreparedStatements then SQL.transaction else SQL.unpreparedTransaction in SQL.use (AppState.getPool appState) $ @@ -61,7 +60,6 @@ dumpSchema appState = do queryDbStructure (toList configDbSchemas) configDbExtraSearchPath - actualPgVersion configDbPreparedStatements SQL.release $ AppState.getPool appState case result of diff --git a/src/PostgREST/Config/Database.hs b/src/PostgREST/Config/Database.hs index 51c009de1..9e439ac19 100644 --- a/src/PostgREST/Config/Database.hs +++ b/src/PostgREST/Config/Database.hs @@ -1,7 +1,8 @@ {-# LANGUAGE QuasiQuotes #-} module PostgREST.Config.Database - ( queryDbSettings + ( pgVersionStatement + , queryDbSettings , queryPgVersion ) where @@ -20,7 +21,10 @@ import Text.InterpolatedString.Perl6 (q) import Protolude queryPgVersion :: Session PgVersion -queryPgVersion = statement mempty $ SQL.Statement sql HE.noParams versionRow False +queryPgVersion = statement mempty pgVersionStatement + +pgVersionStatement :: SQL.Statement () PgVersion +pgVersionStatement = SQL.Statement sql HE.noParams versionRow False where sql = "SELECT current_setting('server_version_num')::integer, current_setting('server_version')" versionRow = HD.singleRow $ PgVersion <$> column HD.int4 <*> column HD.text diff --git a/src/PostgREST/DbStructure.hs b/src/PostgREST/DbStructure.hs index ebb872ba6..44199a1ee 100644 --- a/src/PostgREST/DbStructure.hs +++ b/src/PostgREST/DbStructure.hs @@ -41,6 +41,7 @@ import Data.Set as S (fromList) import Data.Text (split) import Text.InterpolatedString.Perl6 (q) +import PostgREST.Config.Database (pgVersionStatement) import PostgREST.Config.PgVersion (PgVersion, pgVersion100) import PostgREST.DbStructure.Identifiers (QualifiedIdentifier (..), Schema, TableName) @@ -83,9 +84,10 @@ type ViewColumn = Column -- | A SQL query that can be executed independently type SqlQuery = ByteString -queryDbStructure :: [Schema] -> [Schema] -> PgVersion -> Bool -> SQL.Transaction DbStructure -queryDbStructure schemas extraSearchPath pgVer prepared = do +queryDbStructure :: [Schema] -> [Schema] -> Bool -> SQL.Transaction DbStructure +queryDbStructure schemas extraSearchPath prepared = do SQL.sql "set local schema ''" -- This voids the search path. The following queries need this for getting the fully qualified name(schema.name) of every db object + pgVer <- SQL.statement mempty pgVersionStatement tabs <- SQL.statement mempty $ allTables pgVer prepared cols <- SQL.statement schemas $ allColumns tabs prepared srcCols <- SQL.statement (schemas, extraSearchPath) $ pfkSourceColumns cols prepared diff --git a/src/PostgREST/Workers.hs b/src/PostgREST/Workers.hs index 9f716e382..0144ef545 100644 --- a/src/PostgREST/Workers.hs +++ b/src/PostgREST/Workers.hs @@ -152,11 +152,10 @@ connectionStatus appState = loadSchemaCache :: AppState -> IO SCacheStatus loadSchemaCache appState = do AppConfig{..} <- AppState.getConfig appState - actualPgVersion <- AppState.getPgVersion appState result <- let transaction = if configDbPreparedStatements then SQL.transaction else SQL.unpreparedTransaction in SQL.use (AppState.getPool appState) . transaction SQL.ReadCommitted SQL.Read $ - queryDbStructure (toList configDbSchemas) configDbExtraSearchPath actualPgVersion configDbPreparedStatements + queryDbStructure (toList configDbSchemas) configDbExtraSearchPath configDbPreparedStatements case result of Left e -> do let diff --git a/test/spec/Main.hs b/test/spec/Main.hs index a292efd6a..9e1e09a41 100644 --- a/test/spec/Main.hs +++ b/test/spec/Main.hs @@ -68,7 +68,6 @@ main = do loadDbStructure pool (configDbSchemas $ testCfg testDbConn) (configDbExtraSearchPath $ testCfg testDbConn) - actualPgVersion let -- For tests that run with the same refDbStructure @@ -88,7 +87,6 @@ main = do loadDbStructure pool (configDbSchemas config) (configDbExtraSearchPath config) - actualPgVersion appState <- AppState.initWithPool pool config AppState.putPgVersion appState actualPgVersion AppState.putDbStructure appState customDbStructure @@ -232,5 +230,5 @@ main = do describe "Feature.RollbackForcedSpec" Feature.RollbackSpec.forced where - loadDbStructure pool schemas extraSearchPath actualPgVersion = - either (panic.show) id <$> P.use pool (HT.transaction HT.ReadCommitted HT.Read $ queryDbStructure (toList schemas) extraSearchPath actualPgVersion True) + loadDbStructure pool schemas extraSearchPath = + either (panic.show) id <$> P.use pool (HT.transaction HT.ReadCommitted HT.Read $ queryDbStructure (toList schemas) extraSearchPath True)