Skip to content

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

Disable table-data-editing if database has routing (#62071)

Chris Truter116a1195f2e8
diff --git a/.clj-kondo/config/modules/config.edn b/.clj-kondo/config/modules/config.edn
index 8e090a1464f..6cbe6a69e18 100644
--- a/.clj-kondo/config/modules/config.edn
+++ b/.clj-kondo/config/modules/config.edn
@@ -68,7 +68,8 @@
query-processor
search
settings
- util}}
+ util
+ warehouses}}
activity-feed
{:team "UX West"
diff --git a/src/metabase/actions/settings.clj b/src/metabase/actions/settings.clj
index fd258121524..1ccd9ee647e 100644
--- a/src/metabase/actions/settings.clj
+++ b/src/metabase/actions/settings.clj
@@ -1,22 +1,24 @@
(ns metabase.actions.settings
(:require
[metabase.settings.core :as setting]
- [metabase.util.i18n :as i18n]))
+ [metabase.util.i18n :as i18n]
+ [metabase.warehouses.models.database :as database]))
(setting/defsetting database-enable-actions
(i18n/deferred-tru "Whether to enable Actions for a specific Database.")
- :default false
- :driver-feature :actions
- :type :boolean
- :visibility :public
- :database-local :only)
+ :default false
+ :driver-feature :actions
+ :type :boolean
+ :visibility :public
+ :database-local :only)
(setting/defsetting database-enable-table-editing
(i18n/deferred-tru "Whether to enable table data editing for a specific Database.")
- :default false
- :feature :table-data-editing
- :driver-feature :actions/data-editing
- :type :boolean
- :visibility :public
- :database-local :only
- :export? true)
+ :default false
+ :feature :table-data-editing
+ :driver-feature :actions/data-editing
+ :enabled-for-db? (complement database/is-destination?)
+ :type :boolean
+ :visibility :public
+ :database-local :only
+ :export? true)
diff --git a/src/metabase/settings/core.clj b/src/metabase/settings/core.clj
index 347becb574c..75e4c202b49 100644
--- a/src/metabase/settings/core.clj
+++ b/src/metabase/settings/core.clj
@@ -90,7 +90,9 @@
admin-writable-site-wide-settings
can-read-setting?
current-user-readable-visibilities
+ custom-disabled-reasons!
defsetting
+ disabled-for-db-reasons
env-var-value
export?
get
diff --git a/src/metabase/settings/models/setting.clj b/src/metabase/settings/models/setting.clj
index 1a846b66084..0e64828b444 100644
--- a/src/metabase/settings/models/setting.clj
+++ b/src/metabase/settings/models/setting.clj
@@ -23,7 +23,7 @@
[methodical.core :as methodical]
[toucan2.core :as t2])
(:import
- (clojure.lang Keyword)
+ (clojure.lang ExceptionInfo Keyword)
(com.fasterxml.jackson.core JsonParseException)
(com.fasterxml.jackson.core.io JsonEOFException)
(java.io StringWriter)
@@ -230,7 +230,17 @@
;; If non-nil, determines the database driver feature required for this setting. This is only valid for database-local
;; settings. If the database driver doesn't support the required feature, setting this will throw an exception.
- [:driver-feature [:maybe :keyword]]])
+ [:driver-feature [:maybe :keyword]]
+
+ ;; Function that takes a database and returns true if this setting should be enabled for that specific database.
+ ;; If the function returns false, attempting to set this will fail.
+ ;;
+ ;; In cases where the admin panel should display specific reasons to the user, [[custom-disabled-reasons!]] can be
+ ;; used to throw a custom exception. This supports multiple reasons, allowing the frontend to be authoritative over
+ ;; display rules - for example, giving certain reasons higher precedence.
+ ;;
+ ;; This is only valid for database-local settings.
+ [:enabled-for-db? [:maybe ifn?]]])
(defonce ^{:doc "Map of loaded defsettings"}
registered-settings
@@ -878,6 +888,22 @@
(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?]
@@ -888,7 +914,12 @@
(throw (ex-info (tru "Setting {0} requires driver feature {1}, but the database does not support it" s-name driver-feature)
{:setting s-name
:required-feature driver-feature
- :database-id (:id database)})))))
+ :database-id (:id database)})))
+ (when-let [reasons (disabled-for-db-reasons setting database)]
+ (throw (ex-info (tru "Setting {0} is not enabled for this database" s-name)
+ {:setting s-name
+ :database-id (:id database)
+ :reasons reasons})))))
(defn set!
"Set the value of `setting-definition-or-name`. What this means depends on the Setting's `:setter`; by default, this
@@ -974,7 +1005,8 @@
:feature nil
:database-local :never
:user-local :never
- :driver-feature nil
+ :driver-feature nil
+ :enabled-for-db? nil
:deprecated nil
:enabled? nil
:can-read-from-env? true
@@ -1018,7 +1050,11 @@
setting-name)
{:setting setting})))
(when (and (:driver-feature setting) (not (database-local-only? setting)))
- (throw (ex-info (tru "Setting {0} requires a :driver-feature, but is not limited to only database-local values."
+ (throw (ex-info (tru "Setting {0} requires a driver feature, but is not limited to only database-local values"
+ setting-name)
+ {:setting setting})))
+ (when (and (:enabled-for-db? setting) (not (database-local-only? setting)))
+ (throw (ex-info (tru "Setting {0} uses :enabled-for-db?, but is not limited to only database-local values"
setting-name)
{:setting setting})))
(swap! registered-settings assoc setting-name <>))))
@@ -1208,6 +1244,18 @@
settings (those with `:database-local` set to `:only`). When setting a database-local setting, the system will check
if the database driver supports the specified feature. If not, an exception will be thrown.
+ ###### `:enabled-for-db?`
+
+ Function which takes a specific database, and returns true if this setting should be enabled for it.
+ This is only valid for database-local settings. When setting a database-local setting, the system will call this
+ function with the database. If it returns false, an exception will be thrown.
+
+ Useful for disabling settings based on database-specific conditions like routing configuration.
+
+ The [[custom-disabled-reasons!]] method can be used to override this exception with explicit reasons, in the case that
+ we want to communicate them within the admin panel. This supports multiple reasons so that the frontend can be
+ authoritative over their \n precedence.
+
###### `:user-local`
Whether this Setting is /User-local/. Valid values are `:only`, `:allowed`, and `:never`. Default: `:never`. See
diff --git a/src/metabase/warehouses/api.clj b/src/metabase/warehouses/api.clj
index 30b4e8638aa..e187e987103 100644
--- a/src/metabase/warehouses/api.clj
+++ b/src/metabase/warehouses/api.clj
@@ -36,6 +36,7 @@
[metabase.util.i18n :refer [deferred-tru trs tru]]
[metabase.util.log :as log]
[metabase.util.malli :as mu]
+ [metabase.util.malli.registry :as mr]
[metabase.util.malli.schema :as ms]
[metabase.util.quick-task :as quick-task]
[metabase.warehouse-schema.models.field :refer [readable-fields-only]]
@@ -1279,3 +1280,53 @@
[:= :collection_id nil]
[:in :collection_id (api/check-404 (not-empty (t2/select-pks-set :model/Collection :name schema)))])])
(map schema.table/card->virtual-table))))
+
+;;; -------------------------------- GET /api/database/:id/settings-available ------------------------------------
+
+(defn- database-local-settings
+ "Return a sorted map of all database-local settings with their enabled status for the given database.
+ Settings that require unavailable premium features are omitted entirely."
+ [database]
+ (let [settings @setting/registered-settings
+ driver (driver.u/database->driver database)
+ driver-supports? (fn [feature] (driver.u/supports? driver feature database))]
+ (into (sorted-map)
+ (for [[setting-name {:keys [feature database-local driver-feature enabled?] :as setting-def}] settings
+ :when (and (not= :never database-local)
+ (or (nil? feature) (premium-features/has-feature? feature)))]
+ [setting-name
+ (let [reasons (cond-> []
+ (and enabled? (not (enabled?)))
+ (conj {:key :setting-disabled
+ :message "This setting is disabled for all databases."})
+
+ (and driver-feature (not (driver-supports? driver-feature)))
+ (conj {:key :driver-feature-missing
+ :message (format "The %s driver does not support the `%s` feature"
+ (driver/display-name driver)
+ (name driver-feature))})
+
+ true
+ (into (setting/disabled-for-db-reasons setting-def database)))]
+ (if (empty? reasons)
+ {:enabled true}
+ {:enabled false, :reasons reasons}))]))))
+
+(mr/def ::available-settings
+ [:map-of
+ :keyword
+ [:multi {:dispatch :enabled}
+ [true [:map [:enabled [:= true]]]]
+ [false [:map
+ [:enabled [:= false]]
+ [:reasons
+ [:sequential [:map
+ [:key :keyword]
+ [:message :string]]]]]]]])
+
+(api.macros/defendpoint :get "/:id/settings-available" :- [:map [:settings ::available-settings]]
+ "Get all database-local settings and their availability for the given database."
+ [{:keys [id]} :- [:map
+ [:id ms/PositiveInt]]]
+ (let [database (api/read-check (get-database id))]
+ {:settings (database-local-settings database)}))
diff --git a/src/metabase/warehouses/models/database.clj b/src/metabase/warehouses/models/database.clj
index 7cea91ca33f..6fb06d0364e 100644
--- a/src/metabase/warehouses/models/database.clj
+++ b/src/metabase/warehouses/models/database.clj
@@ -151,7 +151,7 @@
;; so we just manually nullify it here
(assoc database :cache_field_values_schedule nil)))
-(defn- is-destination?
+(defn is-destination?
"Is this database a destination database for some router database?"
[db]
(boolean (:router_database_id db)))
diff --git a/test/metabase/settings/models/setting_test.clj b/test/metabase/settings/models/setting_test.clj
index 26651207223..ea36c47e5d7 100644
--- a/test/metabase/settings/models/setting_test.clj
+++ b/test/metabase/settings/models/setting_test.clj
@@ -1035,7 +1035,7 @@
:database-local :allowed
:encryption :when-encryption-key-set)))))
-;;; ------------------------------------------------ Database-feature Settings ----------------------------------------
+;;; ------------------------------------------------- Driver-feature Settings -----------------------------------------
(defsetting ^:private test-driver-feature-only-setting
(deferred-tru "test Setting")
@@ -1047,16 +1047,16 @@
(testing "A setting with :driver-feature must be :database-local"
(is (thrown-with-msg?
Throwable
- #"Setting :test-driver-feature-non-local-setting requires a :driver-feature, but is not limited to only database-local values."
+ #"Setting :test-driver-feature-non-local-setting requires a driver feature, but is not limited to only database-local values"
(defsetting test-driver-feature-non-local-setting
(deferred-tru "test Setting")
:driver-feature :actions
:encryption :when-encryption-key-set))))
- (testing "Having :database-local :allowed is not enough"
+ (testing "Having :database-local :allowed is not enough to use :driver-feature"
(is (thrown-with-msg?
Throwable
- #"Setting :test-driver-feature-allowed-setting requires a :driver-feature, but is not limited to only database-local values."
+ #"Setting :test-driver-feature-allowed-setting requires a driver feature, but is not limited to only database-local values"
(defsetting test-driver-feature-allowed-setting
(deferred-tru "test Setting 2")
:database-local :allowed
@@ -1086,6 +1086,60 @@
(testing "should succeed for settings without driver-feature requirement"
(is (nil? (setting/validate-settable-for-db! setting-without-driver-feature database driver-supports-nothing?)))))))
+;;; ------------------------------------------------ Database-enabled Settings ----------------------------------------
+
+(defsetting ^:private test-enabled-for-db-setting
+ (deferred-tru "test Setting")
+ :database-local :only
+ :enabled-for-db? (fn [db] (not (:router_database_id db)))
+ :encryption :when-encryption-key-set)
+
+(deftest enabled-for-db-validation-test
+ (testing ":enabled-for-db? must only be used with database-local settings"
+ (is (thrown-with-msg?
+ ExceptionInfo
+ #"Setting .* uses :enabled-for-db\?, but is not limited to only database-local values"
+ (defsetting test-enabled-for-db-non-local-setting
+ (deferred-tru "test Setting")
+ :enabled-for-db? (constantly true)
+ :encryption :when-encryption-key-set))))
+
+ (testing "Having :database-local :allowed is not enough to use :enabled-for-db?"
+ (is (thrown-with-msg?
+ ExceptionInfo
+ #"Setting .* uses :enabled-for-db\?, but is not limited to only database-local values"
+ (defsetting test-enabled-for-db-allowed-setting
+ (deferred-tru "test Setting")
+ :database-local :allowed
+ :enabled-for-db? (constantly true)
+ :encryption :when-encryption-key-set))))
+
+ (testing "A setting with :enabled-for-db? and :database-local :only should be valid"
+ (is (some? test-enabled-for-db-setting))))
+
+(deftest validate-settable-for-db-enabled-test
+ (testing "validate-settable-for-db! validates database-specific enablement"
+ (let [regular-database {:id 1 :engine :h2}
+ routed-database {:id 2 :engine :h2 :router_database_id 3}]
+
+ (testing "should succeed when database passes enabled-for-db? predicate"
+ (is (nil? (setting/validate-settable-for-db! :test-enabled-for-db-setting
+ regular-database
+ (constantly true)))))
+
+ (testing "should throw when database fails enabled-for-db? predicate"
+ (is (thrown-with-msg?
+ ExceptionInfo
+ #"Setting test-enabled-for-db-setting is not enabled for this database"
+ (setting/validate-settable-for-db! :test-enabled-for-db-setting
+ routed-database
+ (constantly true)))))
+
+ (testing "should succeed for settings without enabled-for-db? requirement"
+ (is (nil? (setting/validate-settable-for-db! :test-database-local-allowed-setting
+ routed-database
+ (constantly true))))))))
+
(deftest identity-hash-test
(testing "Settings are hashed based on the key"
(mt/with-temporary-setting-values [test-setting-1 "123"
diff --git a/test/metabase/warehouses/api_test.clj b/test/metabase/warehouses/api_test.clj
index 0d8e783209b..00c3b3724ab 100644
--- a/test/metabase/warehouses/api_test.clj
+++ b/test/metabase/warehouses/api_test.clj
@@ -2327,3 +2327,77 @@
(is (= {:status "error"
:message "Failed to connect to Database"}
(mt/user-http-request :crowberto :get 200 (str "database/" id "/healthcheck")))))))))
+
+(setting/defsetting api-test-missing-premium-feature
+ "A feature used for testing /settings-available (1)"
+ :type :boolean
+ :database-local :only
+ :feature :forever-withheld-feature)
+
+(setting/defsetting api-test-missing-driver-feature
+ "A feature used for testing /settings-available (2)"
+ :type :boolean
+ :database-local :only
+ ;; Something h2 will never support
+ :driver-feature :test/jvm-timezone-setting)
+
+(setting/defsetting api-test-disabled-for-database
+ "A feature used for testing /settings-available (3)"
+ :type :boolean
+ :database-local :only
+ :enabled-for-db? (constantly false))
+
+(setting/defsetting api-test-disabled-for-custom-reasons
+ "A feature used for testing /settings-available (4)"
+ :type :boolean
+ :database-local :only
+ :enabled-for-db? (fn [_]
+ (setting/custom-disabled-reasons! [{:key :custom/one, :message "Because..."}
+ {:key :custom/two, :message "Also..."}])))
+
+(setting/defsetting api-test-disabled-for-multiple-reasons
+ "A feature used for testing /settings-available (5)"
+ :type :boolean
+ :database-local :only
+ ;; Something h2 will never support
+ :driver-feature :test/jvm-timezone-setting
+ :enabled-for-db? (fn [_]
+ (setting/custom-disabled-reasons! [{:key :custom/three, :message "Never"}])))
+
+(deftest settings-available-test
+ (testing "GET /api/database/:id/settings-available"
+ (mt/with-premium-features #{:table-data-editing}
+ (mt/with-temp [:model/Database {id :id} {:engine :h2}]
+ (testing "returns database-local settings with correct business logic"
+ (let [settings (:settings (mt/user-http-request :rasta :get 200 (str "database/" id "/settings-available")))]
+ (is (= {:unaggregated-query-row-limit
+ {:enabled true}
+
+ :api-test-missing-driver-feature
+ {:enabled false
+ :reasons [{:key "driver-feature-missing"
+ :message "The H2 driver does not support the `jvm-timezone-setting` feature"}]}
+
+ :api-test-disabled-for-database
+ {:enabled false
+ :reasons [{:key "disabled-for-db"
+ :message "This database does not support this setting"}]}
+
+ :api-test-disabled-for-custom-reasons
+ {:enabled false
+ :reasons [{:key "custom/one", :message "Because..."}
+ {:key "custom/two", :message "Also..."}]}
+
+ :api-test-disabled-for-multiple-reasons
+ {:enabled false
+ :reasons [{:key "driver-feature-missing"
+ :message "The H2 driver does not support the `jvm-timezone-setting` feature"}
+ {:key "custom/three"
+ :message "Never"}]}}
+
+ (select-keys settings [:unaggregated-query-row-limit
+ :api-test-missing-premium-feature
+ :api-test-missing-driver-feature
+ :api-test-disabled-for-database
+ :api-test-disabled-for-custom-reasons
+ :api-test-disabled-for-multiple-reasons])))))))))