Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

2.5 KB
Raw Blame
(ns metabase-enterprise.transform-testing.executor
  "The one place warehouse I/O lives. Given a single test-run connection (from
  `driver/do-with-test-connection`), it creates temp tables from compiled queries, drops them, and
  runs read-back queries. It knows nothing about transform tests, expectations, or judgment — it takes
  compiled queries and table names and talks to the connection.

  Kept separate from the runner (orchestration) and the compiler (pure SQL) so the module's I/O is
  in one auditable namespace. No plan object is marshaled between stages — callers pass ordinary
  arguments (driver, conn, compiled-query, table name)."
  (:require
   [metabase-enterprise.transform-testing.compile :as transform-testing.compile]
   [metabase-enterprise.transform-testing.schema :as transform-testing.schema]
   [metabase.driver :as driver]
   [metabase.lib.schema.common :as lib.schema.common]
   [metabase.util.log :as log]
   [metabase.util.malli :as mu]))

(mu/defn create-temp-table!
  "Create the temp table `table` from the compiled `query` on `conn`."
  [driver :- :keyword
   conn   :- ::transform-testing.schema/connection
   table  :- :string
   query  :- ::transform-testing.compile/compiled-query]
  (driver/execute-on-connection! driver conn (driver/compile-create-temp-table driver {:table table :query query})))

(mu/defn drop-temp-table!
  "Drop the temp table `table` on `conn` if it exists, logging a failure instead of throwing it."
  [driver :- :keyword
   conn   :- ::transform-testing.schema/connection
   table  :- :string]
  (try
    (driver/execute-on-connection! driver conn (driver/compile-drop-temp-table driver table))
    (catch Exception e
      (log/warnf "Failed to drop transform test temp table %s: %s" table (ex-message e)))))

(mu/defn table-columns :- [:sequential ::transform-testing.schema/result-column]
  "The columns of the temp table `table` on `conn`, in order, fetching no rows."
  [driver :- :keyword
   conn   :- ::transform-testing.schema/connection
   table  :- :string]
  (let [{:keys [query params]} (transform-testing.compile/columns-query driver table)]
    (:columns (driver/query-on-connection driver conn [query params] {:max-rows 1}))))

(mu/defn run-query
  "Run the compiled `[sql params]` `query` on `conn`, returning `{:rows :columns}`."
  [driver   :- :keyword
   conn     :- ::transform-testing.schema/connection
   query    :- [:tuple :string [:maybe [:sequential ::lib.schema.common/field-value]]]
   max-rows :- :int]
  (driver/query-on-connection driver conn query {:max-rows max-rows}))