From 60bd355a99cfd8123be56ae636fc6f389aa6312e Mon Sep 17 00:00:00 2001 From: Gil Mizrahi Date: Thu, 2 Mar 2023 17:28:56 +0200 Subject: [PATCH] test: logimo validation prepared statement is deallocated PR-URL: https://github.com/hasura/graphql-engine-mono/pull/8175 GitOrigin-RevId: 35ec7e69b616ebd7a4a30760b1698bd379e07d74 --- .../Test/API/Metadata/LogicalModelsSpec.hs | 48 +++++++++++++++++++ .../Postgres/Instances/LogicalModels.hs | 43 +++++++++-------- 2 files changed, 72 insertions(+), 19 deletions(-) diff --git a/server/lib/api-tests/src/Test/API/Metadata/LogicalModelsSpec.hs b/server/lib/api-tests/src/Test/API/Metadata/LogicalModelsSpec.hs index 1564e35c08a..bd22ba3902c 100644 --- a/server/lib/api-tests/src/Test/API/Metadata/LogicalModelsSpec.hs +++ b/server/lib/api-tests/src/Test/API/Metadata/LogicalModelsSpec.hs @@ -521,3 +521,51 @@ tests opts = do statement: "PREPARE _logimo_vali_divided_stuff AS SELECT thing / $1 AS divided FROM does_not_exist WHERE date = $2" path: "$.args" |] + + describe "Validation succeeds" do + it "when tracking then untracking then re-tracking a logical model" $ + \testEnv -> do + shouldReturnYaml + opts + ( GraphqlEngine.postMetadata + testEnv + [yaml| + type: bulk + args: + - type: pg_track_logical_model + args: + type: query + source: postgres + root_field_name: divided_stuff2 + code: *simpleQuery + arguments: + denominator: int + target_date: date + returns: + columns: + divided: + type: integer + - type: pg_untrack_logical_model + args: + root_field_name: divided_stuff2 + source: postgres + - type: pg_track_logical_model + args: + type: query + source: postgres + root_field_name: divided_stuff2 + code: *simpleQuery + arguments: + denominator: int + target_date: date + returns: + columns: + divided: + type: integer + |] + ) + [yaml| + - message: success + - message: success + - message: success + |] diff --git a/server/src-lib/Hasura/Backends/Postgres/Instances/LogicalModels.hs b/server/src-lib/Hasura/Backends/Postgres/Instances/LogicalModels.hs index f89b0812e2d..779ae635f10 100644 --- a/server/src-lib/Hasura/Backends/Postgres/Instances/LogicalModels.hs +++ b/server/src-lib/Hasura/Backends/Postgres/Instances/LogicalModels.hs @@ -32,22 +32,27 @@ validateLogicalModel env connConf model = do i <- get modify (+ 1) pure $ "$" <> tshow i - result <- - liftIO $ - withPostgresDB env connConf $ - PG.rawQE - ( \e -> - (err400 ValidationFailed "Failed to validate query") - { qeInternal = Just $ ExtraInternal $ toJSON e - } - ) - (PG.fromText $ "PREPARE _logimo_vali_" <> toTxt name <> " AS " <> code) - [] - False - case result of - -- running the query failed - Left err -> - throwError err - -- running the query succeeded - Right () -> - pure () + runRaw :: (MonadIO m, MonadError QErr m) => PG.Query -> m () + runRaw stmt = + liftEither + =<< liftIO + ( withPostgresDB + env + connConf + ( PG.rawQE + ( \e -> + (err400 ValidationFailed "Failed to validate query") + { qeInternal = Just $ ExtraInternal $ toJSON e + } + ) + stmt + [] + False + ) + ) + prepname = "_logimo_vali_" <> toTxt name + + -- We don't need to deallocate because 'withPostgresDB' opens a connection, + -- runs a statement, and then closes the connection. Since a prepared statement only + -- lasts the duration of the session, once it closes, it is deallocated as well. + runRaw (PG.fromText $ "PREPARE " <> prepname <> " AS " <> code)