Skip to content

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

Fix token check errors on startup (#70789)

John Swanson7319599ceba5
diff --git a/src/metabase/premium_features/token_check.clj b/src/metabase/premium_features/token_check.clj
index c5ab6ec4c66..64062e42ea6 100644
--- a/src/metabase/premium_features/token_check.clj
+++ b/src/metabase/premium_features/token_check.clj
@@ -424,47 +424,47 @@
result))]
(reify TokenChecker
(-check-token [_ token]
- (let [token-hash (hash-token token)
- local-entry (get @local-cache token-hash)
- db-row (read-cache-from-db token-hash)
- now (t/instant)]
- (if (and local-entry
- db-row
- (= (:result-hash local-entry) (:token_status_hash db-row)))
- ;; Local cache matches DB hash — check TTLs
- (let [age-start (:updated-at local-entry)]
- (cond
- ;; Fresh (< soft-ttl): return local value
- (t/before? now (t/plus age-start soft-ttl))
- (:result local-entry)
-
- ;; Stale (soft..hard): return local + async refresh
- (t/before? now (t/plus age-start hard-ttl))
- (do
- (when (compare-and-set! refresh-in-progress? false true)
- (future
- (try
- (do-refresh! token token-hash)
- (catch Exception e
- (log/error e "Background premium features refresh failed"))
- (finally
- (reset! refresh-in-progress? false)
- (when-let [after *testing-only-call-after-refresh*]
- (after))))))
- (:result local-entry))
-
- ;; Expired (> hard-ttl): synchronous refresh
- :else
- (do-refresh! token token-hash)))
-
- ;; No local cache, no DB row, or hash mismatch: synchronous fetch
- (do-refresh! token token-hash))))
+ (if-not (app-db/db-is-set-up?)
+ (-check-token token-checker token)
+ (let [token-hash (hash-token token)
+ local-entry (get @local-cache token-hash)
+ db-row (read-cache-from-db token-hash)
+ now (t/instant)]
+ (if (and local-entry
+ db-row
+ (= (:result-hash local-entry) (:token_status_hash db-row)))
+ ;; Local cache matches DB hash — check TTLs
+ (let [age-start (:updated-at local-entry)]
+ (cond
+ ;; Fresh (< soft-ttl): return local value
+ (t/before? now (t/plus age-start soft-ttl))
+ (:result local-entry)
+
+ ;; Stale (soft..hard): return local + async refresh
+ (t/before? now (t/plus age-start hard-ttl))
+ (do
+ (when (compare-and-set! refresh-in-progress? false true)
+ (future
+ (try
+ (do-refresh! token token-hash)
+ (catch Exception e
+ (log/error e "Background premium features refresh failed"))
+ (finally
+ (reset! refresh-in-progress? false)
+ (when-let [after *testing-only-call-after-refresh*]
+ (after))))))
+ (:result local-entry))
+
+ ;; Expired (> hard-ttl): synchronous refresh
+ :else
+ (do-refresh! token token-hash)))
+
+ ;; No local cache, no DB row, or hash mismatch: synchronous fetch
+ (do-refresh! token token-hash)))))
(-clear-cache! [_]
(reset! local-cache {})
- (try
- (clear-db-cache!)
- (catch Exception e
- (log/warn e "Failed to clear premium features cache table")))
+ (when (app-db/db-is-set-up?)
+ (clear-db-cache!))
(-clear-cache! token-checker))))))
(defn local-cached-token-checker
diff --git a/test/metabase/premium_features/token_check_test.clj b/test/metabase/premium_features/token_check_test.clj
index 4f2615d3ade..36709d7a0e3 100644
--- a/test/metabase/premium_features/token_check_test.clj
+++ b/test/metabase/premium_features/token_check_test.clj
@@ -609,3 +609,23 @@
(is (nil? (t2/select-one :model/PremiumFeaturesCache :token_hash token-hash))))
(finally
(token-check/-clear-cache! checker))))))
+
+(deftest db-hash-aware-token-checker-db-not-set-up-test
+ (testing "When DB is not set up, delegates directly to inner checker without touching the DB"
+ (let [call-count (atom 0)
+ good-response {:valid true :status "OK" :features ["sandboxes"]}
+ checker (make-db-hash-aware-checker
+ (fn [_token]
+ (swap! call-count inc)
+ good-response)
+ {:soft-ttl (t/minutes 1) :hard-ttl (t/minutes 2)})
+ token (tu/random-token)
+ bomb (fn [& _] (throw (ex-info "DB should not be touched" {})))]
+ (with-redefs [mdb/db-is-set-up? (constantly false)
+ token-check/read-cache-from-db bomb
+ token-check/write-cache-to-db! bomb
+ token-check/clear-db-cache! bomb]
+ (is (= good-response (token-check/-check-token checker token)))
+ (is (= 1 @call-count) "inner checker was called exactly once")
+ ;; clear-cache! should also skip DB without error
+ (token-check/-clear-cache! checker)))))