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
Don't fail data apps typed schema generation on bad models (#82071)
Timofey Kachalov5529e9aeef91
diff --git a/src/metabase/typed_schemas/build.clj b/src/metabase/typed_schemas/build.cljindex fc222a4b5ab..ab9c86c5ae0 100644--- a/src/metabase/typed_schemas/build.clj+++ b/src/metabase/typed_schemas/build.clj@@ -8,6 +8,7 @@docstring for the pipeline shape and the separation rules."(:require[clojure.set :as set]+ [medley.core :as m][metabase.system.core :as system][metabase.typed-schemas.common :as common][metabase.typed-schemas.source :as source]@@ -55,11 +56,13 @@"Fetched schema entities, ready for pure assembly by [[create-schema]].Each entry holds entity maps shaped by the `metabase.typed-schemas.schema.*`- builders; `:key` on each entity seeds the generated object keys."+ builders; `:key` on each entity seeds the generated object keys. `:errors`+ describes entities that could not be built (currently only models)."[:map {:closed true}[:models [:sequential :map]][:tables [:sequential :map]]- [:metrics [:sequential :map]]])+ [:metrics [:sequential :map]]+ [:errors [:sequential :map]]])(defn- invalid-options![message]@@ -90,12 +93,13 @@:metrics metrics}))(defn- models-for-scope- "Returns model schemas scoped to `database-ids`, or all readable models when requested without a database scope."+ "Returns `{:models [...] :errors [...]}` scoped to `database-ids`, or all readable+ models when requested without a database scope."[source database-ids include-models?](conddatabase-ids (source/models source database-ids)include-models? (source/models source nil)- :else []))+ :else {:models [] :errors []}))(defn fetch-items"Fetches the schema entities selected by [[SemanticSchemaOptions]].@@ -124,17 +128,20 @@:include-data-library? include-data-library?:include-metric-library? include-metric-library?})database-ids (source/database-ids source database)- models (models-for-scope source database-ids include-models?)]+ {model-schemas :models+ model-errors :errors} (models-for-scope source database-ids include-models?)](if (or library-scope(and include-models? (nil? database-ids)))(let [{:keys [tables metrics]} (when library-scope(library-items source library-scope))]- {:models (vec models)+ {:models (vec model-schemas):tables (vec tables)- :metrics (vec metrics)})- {:models (vec models)+ :metrics (vec metrics)+ :errors (vec model-errors)})+ {:models (vec model-schemas):tables (source/tables source database-ids nil)- :metrics (source/metrics source database-ids nil)})))))+ :metrics (source/metrics source database-ids nil)+ :errors (vec model-errors)})))))(defn create-schema"Assembles fetched [[Items]] into the semantic schema value rendered by@@ -144,11 +151,14 @@to the current time and the configured site URL."([items](create-schema items nil))- ([{:keys [models tables metrics]} {:keys [generated-at instance-url]}]- (array-map- :schemaVersion 2- :generatedAt (str (or generated-at (Instant/now)))- :metabase {:instanceUrl (or instance-url (system/site-url))}- :models (common/keyed-model-map models)- :tables (common/keyed-map tables)- :metrics (common/keyed-map metrics))))+ ([{:keys [models tables metrics errors]} {:keys [generated-at instance-url]}]+ (m/assoc-some+ (array-map+ :schemaVersion 2+ :generatedAt (str (or generated-at (Instant/now)))+ :metabase {:instanceUrl (or instance-url (system/site-url))}+ :models (common/keyed-model-map models)+ :tables (common/keyed-map tables)+ :metrics (common/keyed-map metrics))+ ;; Omit when empty so healthy responses carry no `errors` key.+ :errors (not-empty (vec errors)))))diff --git a/src/metabase/typed_schemas/schema/model.clj b/src/metabase/typed_schemas/schema/model.cljindex 5444ee5b3c1..71937f050e7 100644--- a/src/metabase/typed_schemas/schema/model.clj+++ b/src/metabase/typed_schemas/schema/model.clj@@ -242,19 +242,73 @@:keyDisambiguator id:actions (common/keyed-map action-schemas)})))+(defn- interrupted-exception?+ "Returns true when `exception`, or one of its causes, is an InterruptedException."+ [exception]+ (or (instance? InterruptedException exception)+ (some-> (ex-cause exception) interrupted-exception?)))++(defn- rethrow-if-interrupted!+ "Rethrows `exception` when interrupted, so cancellations are not swallowed as errors."+ [exception]+ (when (interrupted-exception? exception)+ (throw exception)))++(defn- model-error-entry+ "Returns a schema error entry for a model that could not be built."+ [model exception]+ (m/assoc-some+ {:type "modelError"+ :modelId (:id model)+ :message (or (ex-message exception) "unknown error")}+ :modelName (:name model)))++(defn- bulk-action-schema-builder+ "Returns a `model -> action-schemas` function backed by one bulk action lookup,+ or nil when that lookup fails so [[model-schemas]] resolves each model on its own."+ [models model-ids]+ (try+ (let [action-rows-by-model-id (group-by :model_id (action-rows model-ids))+ action-details-by-model-id (group-by :model_id (resolved-action-details-for-models models))]+ (fn [model]+ (model-action-schemas model+ (get action-rows-by-model-id (:id model))+ (get action-details-by-model-id (:id model)))))+ ;; Broad on purpose: the per-model fallback recomputes the same result, so degrading masks nothing.+ (catch Exception exception+ (rethrow-if-interrupted! exception)+ nil)))++(defn- collect-model-schema+ "Reduces one model into `{:models [...] :errors [...]}`.++ Only the structured `ExceptionInfo` a bad model raises becomes an error entry;+ anything else is an unexpected bug and propagates instead of masking it."+ [acc build-action-schemas model]+ (let [{:keys [schema error]}+ (try+ {:schema (model-schema model (build-action-schemas model))}+ (catch clojure.lang.ExceptionInfo exception+ (rethrow-if-interrupted! exception)+ {:error (model-error-entry model exception)}))]+ (cond-> acc+ schema (update :models conj schema)+ error (update :errors conj error))))+(defn model-schemas- "Returns model schemas, with optional database and collection scopes."+ "Returns `{:models [...] :errors [...]}`, with optional database and collection scopes.++ A model that cannot be built becomes an `:errors` entry instead of failing the rest."[database-ids collection-ids](let [models (schema.common/select-schema-cards :model database-ids collection-ids)](if (seq models)- (let [model-ids (set (map :id models))- action-rows-by-model-id (group-by :model_id (action-rows model-ids))- action-details-by-model-id (group-by :model_id (resolved-action-details-for-models models))]- (for [model models- :let [action-schemas (model-action-schemas model- (get action-rows-by-model-id (:id model))- (get action-details-by-model-id (:id model)))- schema (model-schema model action-schemas)]- :when schema]- schema))- [])))+ (let [model-ids (set (map :id models))+ ;; The bulk lookup avoids N+1s; when a broken model makes it throw we+ ;; fall back to resolving each model's actions on its own.+ build-action-schemas (or (bulk-action-schema-builder models model-ids)+ model-action-schemas)]+ (reduce (fn [acc model]+ (collect-model-schema acc build-action-schemas model))+ {:models [] :errors []}+ models))+ {:models [] :errors []})))diff --git a/src/metabase/typed_schemas/source.clj b/src/metabase/typed_schemas/source.cljindex fe1c313cee7..986fc5cc2d7 100644--- a/src/metabase/typed_schemas/source.clj+++ b/src/metabase/typed_schemas/source.clj@@ -17,7 +17,8 @@All methods return the shaped schema entities produced by the`metabase.typed-schemas.schema.*` builders, except [[library-tables]], which- returns raw table rows (only `:id` is consumed).+ returns raw table rows (only `:id` is consumed), and [[models]], which returns+ `{:models [...] :errors [...]}` so broken models surface as data.When the pipeline needs to read something new, add a protocol method and its[[app-db-source]] implementation here — do not call `t2`/`metabot` directly@@ -49,7 +50,8 @@(questions [source database-ids collection-ids]"Question schema entities.")(models [source database-ids]- "Model schema entities that have executable actions.")+ "Model schemas as `{:models [...] :errors [...]}`: entities for models with+ executable actions, plus errors for models that could not be built.")(metrics [source database-ids collection-ids]"Metric schema entities.")(tables [source database-ids table-ids]@@ -69,7 +71,7 @@(questions [_ database-ids collection-ids](vec (schema.question/question-schemas database-ids collection-ids)))(models [_ database-ids]- (vec (schema.model/model-schemas database-ids nil)))+ (schema.model/model-schemas database-ids nil))(metrics [_ database-ids collection-ids](vec (schema.metric/metric-schemas database-ids collection-ids)))(tables [_ database-ids table-ids]diff --git a/test/metabase/typed_schemas/core_test.clj b/test/metabase/typed_schemas/core_test.cljindex a4b6eba48fc..20a88738dad 100644--- a/test/metabase/typed_schemas/core_test.clj+++ b/test/metabase/typed_schemas/core_test.clj@@ -70,13 +70,13 @@"A [[source/SchemaSource]] over literal values. `tables` are filtered by therequested table ids so tests can see which tables fetching asked for."[{:keys [database-ids collection-ids library-scope library-tables- questions models metrics tables]}]+ questions models model-errors metrics tables]}](reify source/SchemaSource(database-ids [_ _] database-ids)(collection-ids [_ _] collection-ids)(library-scope [_ _] library-scope)(questions [_ _ _] (vec questions))- (models [_ _] (vec models))+ (models [_ _] {:models (vec models) :errors (vec model-errors)})(metrics [_ _ _] (vec metrics))(tables [_ _ table-ids] (cond->> (vec tables)table-ids (filterv #(contains? table-ids (:id %)))))@@ -96,7 +96,8 @@(is (= {:models []:tables [{:id 10, :type "table", :key "publishedTable"}{:id 42, :type "table", :key "mappedTable"}]- :metrics [{:type "metric", :key "revenue", :id 1, :mappedTableIds [42]}]}+ :metrics [{:type "metric", :key "revenue", :id 1, :mappedTableIds [42]}]+ :errors []}(build/fetch-items {:include-data-library? true} source)))))(deftest options-reject-question-collection-refs-test@@ -104,6 +105,31 @@#"Invalid semantic schema options\."(build/fetch-items {:question-collection-refs [{:id 1}]}))))+(deftest fetch-items-threads-model-errors-test+ (testing "model errors from the source land in Items :errors alongside the built models"+ (let [source (literal-source+ {:models [{:key "orders", :name "Orders"+ :actions {"create" {:kind "action", :id 9}}}]+ :model-errors [{:type "modelError", :modelId 7, :modelName "Broken"+ :message "Failed to build action schemas for model \"Broken\" (card 7): boom"}]})]+ (is (= {:models [{:key "orders", :name "Orders"+ :actions {"create" {:kind "action", :id 9}}}]+ :tables []+ :metrics []+ :errors [{:type "modelError", :modelId 7, :modelName "Broken"+ :message "Failed to build action schemas for model \"Broken\" (card 7): boom"}]}+ (build/fetch-items {:include-models? true} source))))))++(deftest create-schema-includes-errors-when-present-test+ (testing "errors are omitted from a healthy schema and included when present"+ (let [base-items {:models [], :tables [], :metrics []}]+ (is (not (contains? (build/create-schema base-items test-info) :errors)))+ (is (= [{:type "modelError", :modelId 7, :modelName "Broken", :message "boom"}]+ (:errors (build/create-schema+ (assoc base-items :errors [{:type "modelError", :modelId 7+ :modelName "Broken", :message "boom"}])+ test-info)))))))+;; One end-to-end test over the real test-data dataset: cards for every entity;; kind on real synced tables, run through the whole pipeline to TypeScript.;; Everything above tests the stages with cheap literal data; this proves thediff --git a/test/metabase/typed_schemas/render_test.clj b/test/metabase/typed_schemas/render_test.cljindex 7c9772ad629..929d650b118 100644--- a/test/metabase/typed_schemas/render_test.clj+++ b/test/metabase/typed_schemas/render_test.clj@@ -156,6 +156,28 @@(is (= [:lit "string"] (obj-entry field-node :jsType)))(is (nil? (obj-entry field-node :displayName))))))+(def ^:private schema-with-errors+ {:schemaVersion 2+ :metabase {:instanceUrl "https://metabase.example.com"}+ :questions {}+ :models {}+ :tables {}+ :metrics {}+ :errors [{:type "modelError"+ :modelId 7+ :modelName "Broken model"+ :message "Failed to build action schemas for model \"Broken model\" (card 7): boom"}]})++(deftest typescript-renderer-emits-model-errors-test+ (let [body (typed-schemas/render-typescript schema-with-errors)]+ ;; Broken models surface as runtime data so agents can tell users which+ ;; models are bad instead of the whole response failing.+ (is (str/includes? body "errors: ["))+ (is (str/includes? body "type: \"modelError\""))+ (is (str/includes? body "modelId: 7"))+ (is (str/includes? body "modelName: \"Broken model\""))+ (is (str/includes? body "message: \"Failed to build action schemas"))))+(deftest typescript-renderer-omits-pick-fields-helper-for-raw-dimensions-test(let [body (typed-schemas/render-typescript raw-dimensions-schema)];; Dimensions that cannot be resolved to table fields stay as raw fields, sodiff --git a/test/metabase/typed_schemas/schema/model_test.clj b/test/metabase/typed_schemas/schema/model_test.cljindex 48d2bf09394..799c4dd4fa7 100644--- a/test/metabase/typed_schemas/schema/model_test.clj+++ b/test/metabase/typed_schemas/schema/model_test.clj@@ -32,7 +32,7 @@:parameters []}])];; only model 42 has an action, so model 43 is omitted(is (= ["model42"]- (map :key (schema.model/model-schemas #{1} nil))))))+ (map :key (:models (schema.model/model-schemas #{1} nil))))))); Ensures we are not doing N+1 queries for action rows and details(deftest model-schemas-bulk-loads-actions-test@@ -47,11 +47,93 @@actions/select-actions-non-http-for-models (fn [known-models model-ids](swap! action-details-calls conj [known-models model-ids])[])]- (is (= [] (vec (schema.model/model-schemas #{1} nil))))+ (is (= {:models [] :errors []} (schema.model/model-schemas #{1} nil)))(is (= [#{42 43}] @action-rows-calls))(is (= [[models #{42 43}]]@action-details-calls)))))+(deftest model-schemas-collects-broken-model-errors-test+ (testing "a broken model becomes an :errors entry while healthy models still build"+ (with-redefs [schema.common/select-schema-cards+ (constantly [{:id 42 :name "Model 42"}+ {:id 43 :name "Broken model"}])+ schema.model/action-rows+ (constantly [{:id 5 :model_id 42 :name "Create" :type :query}+ ;; model 43's action row resolves to no action details+ {:id 6 :model_id 43 :name "Broken action" :type :broken}])+ actions/select-actions-non-http-for-models+ (constantly [{:id 5+ :model_id 42+ :name "Create"+ :type :query+ :parameters []}])]+ (let [{:keys [models errors]} (schema.model/model-schemas #{1} nil)]+ (is (= ["model42"] (map :key models)))+ (is (=? [{:type "modelError"+ :modelId 43+ :modelName "Broken model"+ :message #".*Broken model.*could not be resolved.*"}]+ errors))))))++(deftest model-schemas-falls-back-when-bulk-lookup-fails-test+ (testing "a broken model poisoning the bulk action lookup does not hide other models"+ (with-redefs [schema.common/select-schema-cards+ (constantly [{:id 42 :name "Model 42"}+ {:id 43 :name "Broken model"}])+ ;; bulk lookup blows up for the whole batch+ actions/select-actions-non-http-for-models+ (fn [& _] (throw (ex-info "bulk lookup exploded" {})))+ ;; per-model fallback: model 42 resolves, model 43 still fails+ schema.model/action-rows+ (fn [model-ids]+ (if (contains? model-ids 42)+ [{:id 5 :model_id 42 :name "Create" :type :query}]+ []))+ actions/select-actions+ (fn [_ & {:keys [model_id]}]+ (if (= model_id 42)+ [{:id 5 :model_id 42 :name "Create" :type :query :parameters []}]+ (throw (ex-info "action lookup failed" {:status-code 500}))))]+ (let [{:keys [models errors]} (schema.model/model-schemas #{1} nil)]+ (is (= ["model42"] (map :key models)))+ (is (=? [{:type "modelError"+ :modelId 43+ :modelName "Broken model"+ :message #".*Broken model.*action lookup failed.*"}]+ errors))))))++(deftest model-schemas-does-not-swallow-interruption-test+ (testing "an interruption while bulk-resolving actions propagates instead of collecting an error"+ (with-redefs [schema.common/select-schema-cards+ (constantly [{:id 42 :name "Model 42"}])+ schema.model/action-rows (constantly [])+ actions/select-actions-non-http-for-models+ (fn [& _] (throw (InterruptedException. "cancelled")))]+ (let [thrown (is (thrown? clojure.lang.ExceptionInfo+ (schema.model/model-schemas #{1} nil)))]+ (is (instance? InterruptedException (ex-cause thrown))))))+ (testing "an interruption wrapped in the structured error propagates instead of collecting an error"+ (with-redefs [schema.common/select-schema-cards+ (constantly [{:id 42 :name "Model 42"}])+ schema.model/action-rows (constantly [])+ actions/select-actions-non-http-for-models (constantly [])+ schema.model/model-action-schemas+ (fn [& _] (throw (ex-info "cancelled" {} (InterruptedException. "cancelled"))))]+ (let [thrown (is (thrown? clojure.lang.ExceptionInfo+ (schema.model/model-schemas #{1} nil)))]+ (is (instance? InterruptedException (ex-cause thrown)))))))++(deftest model-schemas-propagates-unexpected-errors-test+ (testing "an unexpected, unstructured failure propagates instead of downgrading to partial data"+ (with-redefs [schema.common/select-schema-cards+ (constantly [{:id 42 :name "Model 42"}])+ schema.model/action-rows (constantly [])+ actions/select-actions-non-http-for-models (constantly [])+ schema.model/model-action-schemas+ (fn [& _] (throw (NullPointerException. "boom")))]+ (is (thrown? NullPointerException+ (schema.model/model-schemas #{1} nil))))))+(deftest model-schema-surfaces-action-selection-errors-test(with-redefs [schema.model/action-rows (constantly [])actions/select-actions (fn [& _]