The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
Transforms with parameters (#69592)
William347f3f11b4fb
diff --git a/.clj-kondo/config/modules/config.edn b/.clj-kondo/config/modules/config.ednindex 6923c670f38..64db37ed7c5 100644--- a/.clj-kondo/config/modules/config.edn+++ b/.clj-kondo/config/modules/config.edn@@ -1183,6 +1183,7 @@metabase.query-processor.middleware.add-remapsmetabase.query-processor.middleware.annotatemetabase.query-processor.middleware.cache-backend.db+ metabase.query-processor.middleware.catch-exceptionsmetabase.query-processor.middleware.constraintsmetabase.query-processor.middleware.limitmetabase.query-processor.middleware.permissionsdiff --git a/src/metabase/transforms/api/transform.clj b/src/metabase/transforms/api/transform.cljindex 2eb6f0b8021..fc9c4db224b 100644--- a/src/metabase/transforms/api/transform.clj+++ b/src/metabase/transforms/api/transform.clj@@ -177,6 +177,13 @@(api/check (transforms.util/check-feature-enabled transform)[402 (deferred-tru "Premium features required for this transform type are not enabled.")]))+(defn- validate-transform-query!+ [transform]+ (when-let [error (transforms.util/validate-transform-query transform)]+ (throw (ex-info (:error error)+ (assoc error+ :status-code 400)))))+(defn get-transforms"Get a list of transforms."[& {:keys [last_run_start_time last_run_statuses tag_ids]}]@@ -290,6 +297,8 @@([body](create-transform! body nil))([body creator-id]+ (when (transforms.util/query-transform? body)+ (validate-transform-query! body))(let [creator-id (or creator-id api/*current-user-id*)transform (t2/with-transaction [_](let [tag-ids (:tag_ids body)@@ -449,6 +458,7 @@(check-database-feature new)(validate-incremental-column-type! new)(when (transforms.util/query-transform? old)+ (validate-transform-query! new)(when-let [{:keys [cycle-str]} (transforms.ordering/get-transform-cycle new)](throw (ex-info (str "Cyclic transform definitions detected: " cycle-str){:status-code 400}))))diff --git a/src/metabase/transforms/util.clj b/src/metabase/transforms/util.cljindex 5b1e18bd76b..c20f7d51f1d 100644--- a/src/metabase/transforms/util.clj+++ b/src/metabase/transforms/util.clj@@ -21,8 +21,10 @@[metabase.query-processor :as qp][metabase.query-processor.compile :as qp.compile][metabase.query-processor.middleware.add-remaps :as remap]+ [metabase.query-processor.middleware.catch-exceptions :as qp.catch-exceptions][metabase.query-processor.parameters.dates :as params.dates][metabase.query-processor.pipeline :as qp.pipeline]+ [metabase.query-processor.preprocess :as qp.preprocess][metabase.sync.core :as sync][metabase.transforms.canceling :as canceling][metabase.transforms.feature-gating :as transforms.gating]@@ -411,6 +413,17 @@(update outer-query :query wrap-query))outer-query)))+(mu/defn validate-transform-query :- [:maybe [:map [:error :string]]]+ "Verifies that a query transform's query can actually be run as is. Returns nil on success and an error map on failure."+ [{:keys [source]}]+ (case (keyword (:type source))+ :query+ (try+ (qp.preprocess/preprocess (:query source))+ nil+ (catch Exception e+ (qp.catch-exceptions/exception-response e)))))+(defn compile-source"Compile the source query of a transform to SQL, applying incremental filtering if required."[{:keys [source] :as transform}]diff --git a/test/metabase/transforms/api/transform_test.clj b/test/metabase/transforms/api/transform_test.cljindex ce5e0d7772b..6a7ca537100 100644--- a/test/metabase/transforms/api/transform_test.clj+++ b/test/metabase/transforms/api/transform_test.clj@@ -92,6 +92,73 @@(is (map? (:owner response)))(is (= lucky-id (get-in response [:owner :id]))))))))))))+(deftest create-transform-with-param-test+ (mt/with-premium-features #{}+ (mt/test-drivers (mt/normal-drivers-with-feature :transforms/table)+ (mt/dataset transforms-dataset/transforms-test+ (mt/with-data-analyst-role! (mt/user->id :lucky)+ (mt/with-db-perm-for-group! (perms-group/all-users) (mt/id) :perms/transforms :yes+ (with-transform-cleanup! [table-name "gadget_products"]+ (testing "Can create a transform with a param"+ (let [query (lib/native-query (mt/metadata-provider) "select * from foo [[where {{id}} = id]]")+ schema (get-test-schema)+ response (mt/user-http-request :lucky :post 200 "transform"+ {:name "Gadget Products"+ :source {:type "query"+ :query query}+ :target {:type "table"+ :schema schema+ :name table-name}})+ transform-id (:id response)]+ (is (some? transform-id)))))))))))++(deftest create-transform-with-required-param-test+ (mt/with-premium-features #{}+ (mt/test-drivers (mt/normal-drivers-with-feature :transforms/table)+ (mt/dataset transforms-dataset/transforms-test+ (mt/with-data-analyst-role! (mt/user->id :lucky)+ (mt/with-db-perm-for-group! (perms-group/all-users) (mt/id) :perms/transforms :yes+ (with-transform-cleanup! [table-name "gadget_products"]+ (testing "Cannot create a transform with a required param"+ (let [base-query (lib/native-query (mt/metadata-provider) "select * from foo where {{id}} = id")+ tag (get (lib/template-tags base-query) "id")+ query (lib/with-template-tags base-query+ {"id" (assoc tag :required true)})+ schema (get-test-schema)+ response (mt/user-http-request :lucky :post 400 "transform"+ {:name "Gadget Products"+ :source {:type "query"+ :query query}+ :target {:type "table"+ :schema schema+ :name table-name}})+ transform-id (:id response)]+ (is (nil? transform-id))+ (is (= "You'll need to pick a value for 'ID' before this query can run."+ (:message response))))))))))))++(deftest create-transform-with-unofficial-required-param-test+ (mt/with-premium-features #{}+ (mt/test-drivers (mt/normal-drivers-with-feature :transforms/table)+ (mt/dataset transforms-dataset/transforms-test+ (mt/with-data-analyst-role! (mt/user->id :lucky)+ (mt/with-db-perm-for-group! (perms-group/all-users) (mt/id) :perms/transforms :yes+ (with-transform-cleanup! [table-name "gadget_products"]+ (testing "Cannot create a transform with a param that is necessary but not marked as required"+ (let [query (lib/native-query (mt/metadata-provider) "select * from foo where {{id}} = id")+ schema (get-test-schema)+ response (mt/user-http-request :lucky :post 400 "transform"+ {:name "Gadget Products"+ :source {:type "query"+ :query query}+ :target {:type "table"+ :schema schema+ :name table-name}})+ transform-id (:id response)]+ (is (nil? transform-id))+ (is (= "Cannot run the query: missing required parameters: #{\"id\"}"+ (:message response))))))))))))+(deftest create-transform-with-owner-test(mt/with-premium-features #{}(mt/test-drivers (mt/normal-drivers-with-feature :transforms/table)@@ -182,6 +249,79 @@(is (nil? (:owner_email updated)))(is (nil? (:owner updated)))))))))))+(deftest update-transform-query-with-param-test+ (mt/with-premium-features #{}+ (mt/test-drivers (mt/normal-drivers-with-feature :transforms/table)+ (mt/dataset transforms-dataset/transforms-test+ (with-transform-cleanup! [table-name "update_owner_test"]+ (let [query (make-query "Gadget")+ schema (get-test-schema)+ created (mt/user-http-request :crowberto :post 200 "transform"+ {:name "Transform for owner update"+ :source {:type "query"+ :query query}+ :target {:type "table"+ :schema schema+ :name table-name}})+ transform-id (:id created)+ new-query (lib/native-query (mt/metadata-provider) "select * from foo [[where {{id}} = id]]")]+ (testing "Update query to a query with an optional param"+ (mt/user-http-request :crowberto :put 200+ (format "transform/%s" transform-id)+ {:source {:type "query"+ :query new-query}}))))))))++(deftest update-transform-query-with-required-param-test+ (mt/with-premium-features #{}+ (mt/test-drivers (mt/normal-drivers-with-feature :transforms/table)+ (mt/dataset transforms-dataset/transforms-test+ (with-transform-cleanup! [table-name "update_owner_test"]+ (testing "Update query to a query with a required param"+ (let [query (make-query "Gadget")+ schema (get-test-schema)+ created (mt/user-http-request :crowberto :post 200 "transform"+ {:name "Transform for owner update"+ :source {:type "query"+ :query query}+ :target {:type "table"+ :schema schema+ :name table-name}})+ transform-id (:id created)+ base-query (lib/native-query (mt/metadata-provider) "select * from foo where {{id}} = id")+ tag (get (lib/template-tags base-query) "id")+ new-query (lib/with-template-tags base-query+ {"id" (assoc tag :required true)})+ response (mt/user-http-request :crowberto :put 400+ (format "transform/%s" transform-id)+ {:source {:type "query"+ :query new-query}})]+ (is (= "You'll need to pick a value for 'ID' before this query can run."+ (:message response))))))))))++(deftest update-transform-query-with-unofficial-required-param-test+ (mt/with-premium-features #{}+ (mt/test-drivers (mt/normal-drivers-with-feature :transforms/table)+ (mt/dataset transforms-dataset/transforms-test+ (with-transform-cleanup! [table-name "update_owner_test"]+ (testing "Update query to a query with a param that is necessary but not marked as required"+ (let [query (make-query "Gadget")+ schema (get-test-schema)+ created (mt/user-http-request :crowberto :post 200 "transform"+ {:name "Transform for owner update"+ :source {:type "query"+ :query query}+ :target {:type "table"+ :schema schema+ :name table-name}})+ transform-id (:id created)+ new-query (lib/native-query (mt/metadata-provider) "select * from foo where {{id}} = id")+ response (mt/user-http-request :crowberto :put 400+ (format "transform/%s" transform-id)+ {:source {:type "query"+ :query new-query}})]+ (is (= "Cannot run the query: missing required parameters: #{\"id\"}"+ (:message response))))))))))+(deftest transform-type-detection-test(mt/with-premium-features #{}(testing "Transform type is automatically detected and set based on source"