Skip to content

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

Lint a model written with columns in the unmarked-value check (#83163)

Eric Normande713f3471e55
diff --git a/.clj-kondo/src/hooks/metabase/toucan/db_ns.clj b/.clj-kondo/src/hooks/metabase/toucan/db_ns.clj
index 5facc634235..a5cf258d5fa 100644
--- a/.clj-kondo/src/hooks/metabase/toucan/db_ns.clj
+++ b/.clj-kondo/src/hooks/metabase/toucan/db_ns.clj
@@ -203,6 +203,16 @@
(boolean (some #(and (hooks/keyword-node? %) (contains? clause-keys (hooks/sexpr %)))
(take-nth 2 (:children node)))))
+(defn- model-node?
+ "Whether `node` names a literal model: `:model/X`, or `[:model/X & columns]`."
+ [node]
+ (letfn [(model-keyword? [n]
+ (and (hooks/keyword-node? n)
+ (= "model" (namespace (hooks/sexpr n)))))]
+ (or (model-keyword? node)
+ (and (hooks/vector-node? node)
+ (some-> node :children first model-keyword?)))))
+
(def ^:private changes-map-fns
"Conditions-map fns whose arglist also ends in a changes map, so a lone trailing map is changes."
'#{update! update-or-insert!})
@@ -220,10 +230,7 @@
"The values of a conditions map passed to one of [[conditions-map-fns]]."
[f args]
(when (contains? conditions-map-fns (some-> f name symbol))
- (let [after-model (->> args
- (drop-while #(not (and (hooks/keyword-node? %)
- (= "model" (namespace (hooks/sexpr %))))))
- rest)]
+ (let [after-model (rest (drop-while (complement model-node?) args))]
;; Only the map immediately after the model is conditions. A later map is the changes map,
;; whose values are written rather than filtered on, and a written value is never marked: a
;; column's `:in` transform runs on the marker itself and stores it as data.
@@ -252,16 +259,13 @@
`(t2/select :model/X :locale locale)`. Several of these fns take an argument before the model --
`(t2/select-one-fn :value :model/X :key k)` -- so the pairs do not start at a fixed offset; they
- start after the `:model/...` keyword. A call that does not name a literal model is not checked.
+ start after the model. A call that does not name a literal model is not checked.
A trailing query map is not a pair, and its values are reached by [[value-nodes]] instead. A
value that is itself an operator form -- `:id [:in ids]` -- has its own values walked, so the
collection inside is checked rather than the form."
[args]
- (let [after-model (->> args
- (drop-while #(not (and (hooks/keyword-node? %)
- (= "model" (namespace (hooks/sexpr %))))))
- rest)]
+ (let [after-model (rest (drop-while (complement model-node?) args))]
(->> after-model
;; `partition-all` rather than `partition` so a trailing odd argument (a query map, which
;; is not a pair) is still seen and skipped by the keyword-node? test below.
diff --git a/.clj-kondo/test/hooks/metabase/toucan/db_ns_test.clj b/.clj-kondo/test/hooks/metabase/toucan/db_ns_test.clj
index e1cda4d196f..40c99af1c14 100644
--- a/.clj-kondo/test/hooks/metabase/toucan/db_ns_test.clj
+++ b/.clj-kondo/test/hooks/metabase/toucan/db_ns_test.clj
@@ -179,6 +179,17 @@
'(t2/select-one-fn :value :model/Setting :key k)
;; a value inside an operator form is reached, rather than the form being treated as a value
'(t2/select :model/X :id [:in ids])))
+ (testing "GHY-4668: a model written with columns still has its kv-arg values checked"
+ ;; The walker found the model by looking for a `:model/...` keyword, so `[:model/X :col]` hid
+ ;; every pair after it.
+ (are [form] (=? [{:type :metabase/unsafe-app-db-query
+ :message #".*`prefix`.*reaches a SQL value slot unmarked.*"}]
+ (lint-query-call form 'metabase.foo.db))
+ '(t2/select [:model/X :id :public_uuid] :public_uuid_prefix prefix :archived false)
+ '(t2/select-one [:model/X :id] :public_uuid_prefix prefix)))
+ (testing "GHY-4668: a marked value after a model written with columns is not flagged"
+ (is (empty? (lint-query-call '(t2/select [:model/X :id] :public_uuid_prefix [:auto/param prefix])
+ 'metabase.foo.db))))
(testing "a marked or coerced kv-arg value is not flagged"
(are [form] (empty? (lint-query-call form 'metabase.foo.db))
'(t2/select :model/X :locale [:auto/param locale])