Skip to content

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

Return default value if setting is disabled for the given database (#62330)

Chris Truterc3b7b724372c
diff --git a/enterprise/backend/src/metabase_enterprise/database_routing/api.clj b/enterprise/backend/src/metabase_enterprise/database_routing/api.clj
index 4cf197c7e16..a8eb1562224 100644
--- a/enterprise/backend/src/metabase_enterprise/database_routing/api.clj
+++ b/enterprise/backend/src/metabase_enterprise/database_routing/api.clj
@@ -98,7 +98,7 @@
(api/check-404 db)
(api/check-400 (not (:router_database_id db)) "Cannot make a destination database a router database")
(api/check-400 (not (:uploads_enabled db)) "Cannot enable database routing for a database with uploads enabled")
- (setting/with-database-local-values (:settings db)
+ (setting/with-database db
(api/check-400 (not (setting/get :persist-models-enabled)) "Cannot enable database routing for a database with model persistence enabled")
(api/check-400 (not (setting/get :database-enable-actions)) "Cannot enable database routing for a database with actions enabled")))
(if (nil? user_attribute)
diff --git a/src/metabase/actions/actions.clj b/src/metabase/actions/actions.clj
index b1fc686b7fd..3b8a9b652b8 100644
--- a/src/metabase/actions/actions.clj
+++ b/src/metabase/actions/actions.clj
@@ -113,14 +113,14 @@
(defn check-actions-enabled-for-database!
"Throws an appropriate error if actions are unsupported or disabled for a database, otherwise returns nil."
- [{db-settings :settings db-id :id driver :engine db-name :name :as db}]
+ [{db-id :id driver :engine db-name :name :as db}]
(when-not (driver.u/supports? driver :actions db)
(throw (ex-info (i18n/tru "{0} Database {1} does not support actions."
(u/qualified-name driver)
(format "%d %s" db-id (pr-str db-name)))
{:status-code 400, :database-id db-id})))
- (setting/with-database-local-values db-settings
+ (setting/with-database db
(when-not (actions.settings/database-enable-actions)
(throw (ex-info (i18n/tru "Actions are not enabled.")
{:status-code 400, :database-id db-id}))))
@@ -129,14 +129,14 @@
(defn check-data-editing-enabled-for-database!
"Throws an appropriate error if editing is unsupported or disabled for a database, otherwise returns nil."
- [{db-settings :settings db-id :id driver :engine db-name :name :as db}]
+ [{db-id :id driver :engine db-name :name :as db}]
(when-not (driver.u/supports? driver :actions/data-editing db)
(throw (ex-info (i18n/tru "{0} Database {1} does not support data editing."
(u/qualified-name driver)
(format "%d %s" db-id (pr-str db-name)))
{:status-code 400, :database-id db-id})))
- (setting/with-database-local-values db-settings
+ (setting/with-database db
(when-not (actions.settings/database-enable-table-editing)
(throw (ex-info (i18n/tru "Data editing is not enabled.")
{:status-code 400, :database-id db-id}))))
diff --git a/src/metabase/lib_be/metadata/jvm.clj b/src/metabase/lib_be/metadata/jvm.clj
index b3eb99a9ec2..3783b8dab2e 100644
--- a/src/metabase/lib_be/metadata/jvm.clj
+++ b/src/metabase/lib_be/metadata/jvm.clj
@@ -54,7 +54,8 @@
(update-keys memoized-kebab-key)
(assoc :lib/type metadata-type)
normalize
- u.snake-hating-map/snake-hating-map)))
+ u.snake-hating-map/snake-hating-map
+ (vary-meta assoc :metabase/toucan-instance instance))))
;;;
;;; Database
diff --git a/src/metabase/query_processor/setup.clj b/src/metabase/query_processor/setup.clj
index 914cee3651c..abf7c53f204 100644
--- a/src/metabase/query_processor/setup.clj
+++ b/src/metabase/query_processor/setup.clj
@@ -176,8 +176,8 @@
(f query)
:else
- (let [{:keys [settings]} (lib.metadata/database (qp.store/metadata-provider))]
- (setting/with-database-local-values (or settings {})
+ (let [db (lib.metadata/database (qp.store/metadata-provider))]
+ (setting/with-database db
(f query))))))
(mu/defn- do-with-canceled-chan :- fn?
diff --git a/src/metabase/settings/core.clj b/src/metabase/settings/core.clj
index 75e4c202b49..9b9399fe75d 100644
--- a/src/metabase/settings/core.clj
+++ b/src/metabase/settings/core.clj
@@ -133,10 +133,11 @@
[]
metabase.settings.models.setting/*database-local-values*)
-(defmacro with-database-local-values
+(defmacro with-database
"Execute `body` with Database-local Setting values bound to `new-values`."
- [new-values & body]
- `(binding [metabase.settings.models.setting/*database-local-values* ~new-values]
+ [new-db & body]
+ `(binding [metabase.settings.models.setting/*database* (:metabase/toucan-instance (meta ~new-db))
+ metabase.settings.models.setting/*database-local-values* (or (:settings ~new-db) {})]
~@body))
(defn user-local-values
diff --git a/src/metabase/settings/models/setting.clj b/src/metabase/settings/models/setting.clj
index 0e64828b444..8cd0ccb18cb 100644
--- a/src/metabase/settings/models/setting.clj
+++ b/src/metabase/settings/models/setting.clj
@@ -31,14 +31,22 @@
(java.util.concurrent TimeUnit)
(java.util.concurrent.locks ReentrantLock)))
+(def ^:dynamic *database*
+ "The database upon which we are operating, from which [[*database-local-values*]] are taken.
+ This is used to do a just-in-time check whether a given setting is enabled for the given database, so that we can
+ revert to the default value even if there is a vestigial value saved against it.
+
+ This is normally bound automatically in Query Processor context by [[metabase.query-processor.setup/do-with-database]].
+ You may need to manually bind it in other places where you want to use Database-local values."
+ nil)
+
(def ^:dynamic *database-local-values*
"Database-local Settings values (as a map of Setting name -> already-deserialized value). This comes from the value of
`Database.settings` in the application DB. When bound, any Setting that *can* be Database-local will have a value
from this map returned preferentially to the site-wide value.
- This is normally bound automatically in Query Processor context
- by [[metabase.query-processor.setup/do-with-database-local-settings]]. You may need to manually bind it in other
- places where you want to use Database-local values."
+ This is normally bound automatically in Query Processor context by [[metabase.query-processor.setup/do-with-database]].
+ You may need to manually bind it in other places where you want to use Database-local values."
nil)
(def ^:dynamic *user-local-values*
@@ -147,6 +155,23 @@
(.getCanonicalName ^Class klass))
{:tag klass}))))))
+(defn disabled-for-db-reasons
+ "Return the reasons, if any, for the given setting being disabled."
+ [setting-def database]
+ (when-let [f (:enabled-for-db? setting-def)]
+ (try (when-not (f database)
+ [{:key :disabled-for-db
+ :message "This database does not support this setting"}])
+ (catch ExceptionInfo e
+ (or (:setting/disabled-reasons (ex-data e))
+ (throw e))))))
+
+(defn custom-disabled-reasons!
+ "Expose custom reasons for a setting being disabled to the admin panel."
+ [reasons]
+ (assert (seq reasons) "At least one reason must be given")
+ (throw (ex-info "Setting is not enabled for this database" {:setting/disabled-reasons reasons})))
+
;; This is called `LocalOption` rather than `DatabaseLocalOption` or something like that because we intend to also add
;; User-Local Settings at some point in the future. The will use the same options
(def ^:private LocalOption
@@ -639,6 +664,11 @@
(defn- default-getter-for-type [setting-type]
(partial get-value-of-type (keyword setting-type)))
+(defn- throw-or-log
+ "Given an error that should never happen, throw it for us, log it for customers."
+ [e]
+ (if config/is-prod? (log/warn e) (throw e)))
+
(defn get
"Fetch the value of `setting-definition-or-name`. What this means depends on the Setting's `:getter`; by default, this
looks for first for a corresponding env var, then checks the cache, then returns the default value of the Setting,
@@ -647,11 +677,28 @@
Note: If the setting has an initializer, and this is the first time accessing, a value will be generated and saved
unless *disable-init* has been bound to a truthy value."
[setting-definition-or-name]
- (let [{:keys [cache? getter enabled? default feature]} (resolve-setting setting-definition-or-name)
- disable-cache? (or config/*disable-setting-cache* (not cache?))]
+ (let [setting-def (resolve-setting setting-definition-or-name)
+ {:keys [getter enabled? feature]} setting-def
+ disable-cache? (or config/*disable-setting-cache* (not (:cache? setting-def)))]
+
+ ;; Reading database-local settings is failure prone, so catch easy mistakes.
+ (when (= :only (:database-local setting-def))
+ (cond
+ (not *database-local-values*)
+ (throw-or-log
+ (ex-info (format "Trying to read database-local setting %s without surrounding [[setting/with-database]] call."
+ (:name setting-def))
+ {:setting setting-def}))
+
+ ;; This likely means we're in a test using hand-crafted metadata. Just stub the instance too.
+ (and (:enabled-for-db? setting-def) (not *database*))
+ (log/warnf "Skipping enabled-for-db? check for %s as we don't have the underlying toucan2 db instance."
+ (:name setting-def))))
+
(if (or (and feature (not (has-feature? feature)))
- (and enabled? (not (enabled?))))
- default
+ (and enabled? (not (enabled?)))
+ (and *database* (disabled-for-db-reasons setting-def *database*)))
+ (:default setting-def)
(if (= config/*disable-setting-cache* disable-cache?) ;; Optimization: only bind dynvar if necessary.
(getter)
(binding [config/*disable-setting-cache* disable-cache?]
@@ -888,22 +935,6 @@
(when (= setter :none)
(throw (UnsupportedOperationException. (tru "You cannot set {0}; it is a read-only setting." s-name))))))))
-(defn disabled-for-db-reasons
- "Return the reasons, if any, for the given setting being disabled."
- [setting-def database]
- (when-let [f (:enabled-for-db? setting-def)]
- (try (when-not (f database)
- [{:key :disabled-for-db
- :message "This database does not support this setting"}])
- (catch ExceptionInfo e
- (or (:setting/disabled-reasons (ex-data e))
- (throw e))))))
-
-(defn custom-disabled-reasons!
- "Expose custom reasons for a setting being disabled to the admin panel."
- [reasons]
- (throw (ex-info "Setting is not enabled for this database" {:setting/disabled-reasons reasons})))
-
(defn validate-settable-for-db!
"Check whether the given setting can be set for the given database."
[setting-definition-or-name database driver-supports?]
diff --git a/test/metabase/settings/models/setting_test.clj b/test/metabase/settings/models/setting_test.clj
index ea36c47e5d7..e9613f57ef4 100644
--- a/test/metabase/settings/models/setting_test.clj
+++ b/test/metabase/settings/models/setting_test.clj
@@ -933,8 +933,9 @@
(test-database-local-only-setting! 2))))
(testing "Default values should be allowed for Database-local-only Settings"
- (is (= "DEFAULT"
- (test-database-local-only-setting-with-default)))
+ (binding [setting/*database-local-values* {}]
+ (is (= "DEFAULT"
+ (test-database-local-only-setting-with-default))))
(binding [setting/*database-local-values* {:test-database-local-only-setting-with-default "WOW"}]
(is (= "WOW"
(test-database-local-only-setting-with-default))))))