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
Better model namespace requiring. (#76571)
Nathan Voxland0de6d6ee2b05
diff --git a/src/metabase/models/resolution.clj b/src/metabase/models/resolution.cljindex b76d315b464..439f80e30d8 100644--- a/src/metabase/models/resolution.clj+++ b/src/metabase/models/resolution.clj@@ -161,13 +161,12 @@"Ensure the namespace for given model is loaded. This is a safety mechanism as we are moving to toucan2 and we don'tneed to require the model namespaces in order to use it."[x]- (when (and (keyword? x)- (= (namespace x) "model")- ;; Don't try to require if it's already registered as a :metabase/model, since that means it has already- ;; been required- (not (isa? x :metabase/model)))- ;; [[classloader/require]] for thread safety- (classloader/require (model->namespace x)))+ (when (keyword? x)+ ;; Always require the model's namespace when we know it. It is fast, and there can be race conditions between+ ;; before side effects like `deftransforms` and `define-before-insert` have run+ (when-let [nspace (get model->namespace x)]+ ;; [[classloader/require]] for thread safety+ (classloader/require nspace)))x)(methodical/defmethod t2.model/resolve-model :around clojure.lang.Symboldiff --git a/test/metabase/models/resolution_test.clj b/test/metabase/models/resolution_test.cljindex 338b31720ed..e27f5a9e8a0 100644--- a/test/metabase/models/resolution_test.clj+++ b/test/metabase/models/resolution_test.clj@@ -46,3 +46,21 @@(deftest ^:parallel symbol-resolution-test(is (= :model/User(t2/resolve-model 'User))))++(deftest resolve-model-requires-namespace-even-when-already-derived-test+ (testing (str "resolve-model must ensure a model's namespace is loaded even when the model is already derived as "+ ":metabase/model. The derivation is established partway through a model namespace's load (before its "+ "deftransforms / define-before-insert side effects), so short-circuiting on it let concurrent "+ "first-access on another thread use the model before its transforms were registered.")+ (let [required (atom [])]+ (with-redefs [classloader/require (fn [& args] (swap! required into args))]+ (t2/resolve-model :model/QueryExecution))+ (is (contains? (set @required) 'metabase.queries.models.query-execution)))))++(deftest resolve-model-ignores-unknown-model-keywords-test+ (testing "resolve-model does not attempt to require a namespace for a `model`-namespaced keyword that is not a known model"+ (let [required (atom [])]+ (with-redefs [classloader/require (fn [& args] (swap! required into args))]+ (t2/resolve-model :model/DefinitelyNotARealModel))+ (is (empty? @required)+ "should not call classloader/require (which would blow up on a nil namespace) for unknown models"))))