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
Add db fallback for renamed settings (#71470)
Chris Truterfdffe7dc8225
diff --git a/src/metabase/settings/models/setting.clj b/src/metabase/settings/models/setting.cljindex 7a76c855772..a921990c6f2 100644--- a/src/metabase/settings/models/setting.clj+++ b/src/metabase/settings/models/setting.clj@@ -278,8 +278,9 @@;; This is only valid for database-local settings.[:enabled-for-db? [:maybe ifn?]]- ;; A previous name for this setting whose env var (e.g. MB_OLD_NAME) is checked- ;; as a fallback when the primary env var is not set. Logs a warning when used.+ ;; A previous name for this setting whose env var (e.g. MB_OLD_NAME) and database+ ;; key are checked as a fallback when the primary source is not set. Logs a warning+ ;; when the env var fallback is in use.[:deprecated-name [:maybe :keyword]]])(defonce ^{:doc "Map of loaded defsettings"}@@ -524,9 +525,6 @@(binding [*disable-init* true](get setting-definition-or-name)))-(defn- db-value [setting-definition-or-name]- (t2/select-one-fn :value :model/Setting :key (setting-name setting-definition-or-name)))-(def ^:private db-is-set-up-var (atom nil))(defn- db-is-set-up? []@@ -535,23 +533,40 @@(reset! db-is-set-up-var (requiring-resolve 'metabase.app-db.core/db-is-set-up?)))](if f (f) false)))+(defn- db-or-cache-value*+ "Look up a single setting key in the DB or cache. Returns the raw (possibly empty) string, or nil."+ ^String [setting-name-str]+ (if config/*disable-setting-cache*+ (t2/select-one-fn :value :model/Setting :key setting-name-str)+ (do+ ;; gotcha - returns immediately if another process is restoring it, i.e. before it's been populated+ (setting.cache/restore-cache-if-needed!)+ (let [cache (setting.cache/cache)]+ (if (nil? cache)+ ;; nil if we returned early above, and the cache is still being restored - in that case hit the db+ (t2/select-one-fn :value :model/Setting :key setting-name-str)+ (core/get cache setting-name-str))))))++(def ^:dynamic *deprecated-db-key-warned*+ "Set of deprecated DB keys that have already triggered a warning. Dynamic so tests can rebind it."+ (atom #{}))+(defn- db-or-cache-value- "Get the value, if any, of `setting-definition-or-name` from the DB (using / restoring the cache as needed)."+ "Get the value, if any, of `setting-definition-or-name` from the DB (using / restoring the cache as needed).+ When the primary key is absent and the setting has a `:deprecated-name`, the deprecated key is checked as a fallback."^String [setting-definition-or-name]- (let [setting (resolve-setting setting-definition-or-name)]+ (let [setting (resolve-setting setting-definition-or-name)];; cannot use db (and cache populated from db) if db is not set up(when (and (db-is-set-up?) (allows-site-wide-values? setting))- (not-empty- (if config/*disable-setting-cache*- (db-value setting)- (do- ;; gotcha - returns immediately if another process is restoring it, i.e. before it's been populated- (setting.cache/restore-cache-if-needed!)- (let [cache (setting.cache/cache)]- (if (nil? cache)- ;; nil if we returned early above, and the cache is still being restored - in that case hit the db- (db-value setting)- (core/get cache (setting-name setting-definition-or-name))))))))))+ (or (not-empty (db-or-cache-value* (setting-name setting)))+ (when-let [deprecated-name (:deprecated-name setting)]+ (when-let [v (not-empty (db-or-cache-value* (setting-name deprecated-name)))]+ (let [dep-key (setting-name deprecated-name)+ [old-warned _] (swap-vals! *deprecated-db-key-warned* conj dep-key)]+ (when-not (contains? old-warned dep-key)+ (log/warnf "Deprecated setting key %s found in database; rename it to %s."+ dep-key (setting-name setting))))+ v))))))(defonce ^:private ^ReentrantLock init-lock (ReentrantLock.))@@ -834,7 +849,13 @@;; write to DB(cond(nil? new-value)- (t2/delete! (t2/table-name :model/Setting) :key setting-name)+ (do+ (t2/delete! (t2/table-name :model/Setting) :key setting-name)+ ;; also clear the deprecated-name key so that fallback doesn't resurface an old value+ (when-let [deprecated-name (:deprecated-name setting)]+ (let [deprecated-key (core/name deprecated-name)]+ (t2/delete! (t2/table-name :model/Setting) :key deprecated-key)+ (setting.cache/update-cache! deprecated-key nil))));; if there's a value in the cache then the row already exists in the DB; update that(contains? (setting.cache/cache) setting-name)@@ -1140,10 +1161,6 @@(throw (ex-info (tru "Setting {0} uses :enabled-for-db?, but is not limited to only database-local values"setting-name){:setting setting})))- (when (and (:deprecated-name <>) (not (:can-read-from-env? <>)))- (throw (ex-info (tru "Setting {0} uses :deprecated-name but has :can-read-from-env? set to false"- setting-name)- {:setting <>})))(swap! registered-settings assoc setting-name <>))));;; +----------------------------------------------------------------------------------------------------------------+@@ -1351,10 +1368,11 @@###### `:deprecated-name`- A keyword naming a previous version of this setting whose env var should be checked as a fallback when the primary- env var is not set. For example, `:deprecated-name :my-old-setting` means `MB_MY_OLD_SETTING` will be tried when- `MB_MY_NEW_SETTING` is absent. A deprecation warning is logged at startup when the fallback is in use. The setting- must allow env var reading (`:can-read-from-env? true`, the default). (Default: `nil`).+ A keyword naming a previous version of this setting whose env var and database key are checked as a fallback when+ the primary source is not set. For example, `:deprecated-name :my-old-setting` means `MB_MY_OLD_SETTING` will be+ tried when `MB_MY_NEW_SETTING` is absent, and the `my-old-setting` database key will be tried when `my-new-setting`+ has no value. A deprecation warning is logged when a fallback value is found. The env-var fallback only applies+ when `:can-read-from-env?` is true (the default). (Default: `nil`).###### `:deprecated`diff --git a/test/metabase/settings/models/setting_test.clj b/test/metabase/settings/models/setting_test.cljindex d16dd7265ae..e53abe8848d 100644--- a/test/metabase/settings/models/setting_test.clj+++ b/test/metabase/settings/models/setting_test.clj@@ -1851,3 +1851,62 @@(mt/with-temp-env-var-value! [mb-test-setting-with-deprecated-name ""mb-old-test-setting-name "LEGACY"](is (nil? (setting/env-var-value :test-setting-with-deprecated-name)))))++;;; ----------------------------------------- deprecated-name DB fallback -----------------------------------------++(defmacro ^:private with-setting-row-in-db+ "Set up a raw DB row for a setting key (primary or deprecated), refresh the cache, execute body, then clean up.+ Binds a fresh warned-set so warning behaviour can be tested in isolation."+ [[k v] & body]+ `(let [k# (name ~k)]+ (binding [setting/*deprecated-db-key-warned* (atom #{})]+ (try+ (if (t2/select-one :model/Setting :key k#)+ (t2/update! :model/Setting :key k# {:value ~v})+ (t2/insert! :model/Setting {:key k# :value ~v}))+ (setting.cache/restore-cache!)+ ~@body+ (finally+ (t2/delete! (t2/table-name :model/Setting) :key k#)+ (setting.cache/restore-cache!))))))++(deftest deprecated-name-db-fallback-only-legacy-test+ (testing "When only the deprecated key exists in the DB, the new setting reads its value"+ (with-setting-row-in-db [:old-test-setting-name "DB_LEGACY"]+ (is (= "DB_LEGACY" (test-setting-with-deprecated-name))))))++(deftest deprecated-name-db-fallback-primary-takes-precedence-test+ (testing "When both keys exist in the DB, the primary value wins"+ (with-setting-row-in-db [:old-test-setting-name "DB_LEGACY"]+ (mt/with-temporary-setting-values [test-setting-with-deprecated-name "DB_PRIMARY"]+ (is (= "DB_PRIMARY" (test-setting-with-deprecated-name)))))))++(deftest deprecated-name-db-fallback-neither-set-test+ (testing "When neither key exists in the DB, returns nil (or default)"+ (mt/with-temporary-setting-values [test-setting-with-deprecated-name nil]+ (is (nil? (test-setting-with-deprecated-name))))))++(deftest deprecated-name-clearing-primary-does-not-resurface-legacy-test+ (testing "Clearing the primary setting does not cause the deprecated value to resurface"+ (with-setting-row-in-db [:old-test-setting-name "DB_LEGACY"]+ ;; sanity check: deprecated value is visible+ (is (= "DB_LEGACY" (test-setting-with-deprecated-name)))+ ;; now explicitly clear the setting — deprecated value should not resurface+ (test-setting-with-deprecated-name! nil)+ (is (nil? (test-setting-with-deprecated-name))))))++;;; ----------------------------------------- deprecated-name env+DB interaction -----------------------------------------++(deftest deprecated-name-env-var-takes-precedence-over-db-test+ (testing "When the deprecated env var and deprecated DB key are both set, the env var wins (checked first)"+ (mt/with-temp-env-var-value! [mb-old-test-setting-name "ENV_LEGACY"]+ (with-setting-row-in-db [:old-test-setting-name "DB_LEGACY"]+ (is (= "ENV_LEGACY" (test-setting-with-deprecated-name))))))+ (testing "When the deprecated env var is set and primary DB key is also set, env var wins (env > DB)"+ (mt/with-temp-env-var-value! [mb-old-test-setting-name "ENV_LEGACY"]+ (with-setting-row-in-db [:test-setting-with-deprecated-name "DB_PRIMARY"]+ (is (= "ENV_LEGACY" (test-setting-with-deprecated-name))))))+ (testing "When the primary env var is set and deprecated DB key exists, primary env var wins"+ (mt/with-temp-env-var-value! [mb-test-setting-with-deprecated-name "ENV_PRIMARY"]+ (with-setting-row-in-db [:old-test-setting-name "DB_LEGACY"]+ (is (= "ENV_PRIMARY" (test-setting-with-deprecated-name)))))))