Skip to content
Snippets Groups Projects
Unverified Commit b701c337 authored by Cam Saul's avatar Cam Saul
Browse files

MySQL fixes :wrench:

parent 3d85a8d6
Branches
Tags
No related merge requests found
......@@ -39,12 +39,12 @@
[environ.core :as env]
[honeysql.core :as hsql]
[metabase
[db :as mdb]
[events :as events]
[util :as u]]
[metabase.util.honeysql-extensions :as hx]
[puppetlabs.i18n.core :refer [trs tru]]
[schema.core :as s]
[metabase.db :as mdb]
[toucan
[db :as db]
[models :as models]]))
......@@ -124,8 +124,7 @@
(let [current-timestamp-as-string-honeysql (hx/cast (if (= (mdb/db-type) :mysql) :char :text)
(hsql/raw "current_timestamp"))]
;; attempt to UPDATE the existing row. If no row exists, `update-where!` will return false...
(or (db/debug-print-queries ; NOCOMMIT
(db/update-where! Setting {:key settings-last-updated-key} :value current-timestamp-as-string-honeysql))
(or (db/update-where! Setting {:key settings-last-updated-key} :value current-timestamp-as-string-honeysql)
;; ...at which point we will try to INSERT a new row. Note that it is entirely possible two instances can both
;; try to INSERT it at the same time; one instance would fail because it would violate the PK constraint on
;; `key`, and throw a SQLException. As long as one instance updates the value, we are fine, so we can go ahead
......@@ -157,11 +156,11 @@
;; if not, get the cached value of `settings-last-updated`, and if it exists...
(when-let [last-known-update (core/get @cache settings-last-updated-key)]
;; compare it to the value in the DB. This is done be seeing whether a row exists
;; WHERE CAST(value AS TIMESTAMP) > CAST(<local-value> AS TIMESTAMP)
;; WHERE value > <local-value>
(db/select-one Setting
{:where [:and
[:= :key settings-last-updated-key]
[:> (hx/cast :timestamp :value) (hx/cast :timestamp last-known-update)]]})))))
[:> :value last-known-update]]})))))
(def ^:private cache-update-check-interval-ms
"How often we should check whether the Settings cache is out of date (which requires a DB call)?"
......
......@@ -2,13 +2,14 @@
(:require [clojure.core.memoize :as memoize]
[expectations :refer :all]
[honeysql.core :as hsql]
[metabase
[db :as mdb]
[util :as u]]
[metabase.models.setting :as setting :refer [defsetting Setting]]
[metabase.test.util :refer :all]
[metabase.util :as u]
[metabase.util
[encryption :as encryption]
[encryption-test :as encryption-test]
[honeysql-extensions :as hx]]
[encryption-test :as encryption-test]]
[puppetlabs.i18n.core :refer [tru]]
[toucan.db :as db]))
......@@ -342,7 +343,13 @@
"Simulate a different instance updating the value of `settings-last-updated` in the DB by updating its value without
updating our locally cached value.."
[]
(db/update-where! Setting {:key settings-last-updated-key} :value (hx/cast :text (hsql/raw "current_timestamp"))))
(db/update-where! Setting {:key settings-last-updated-key}
:value (hsql/raw (case (mdb/db-type)
;; make it one second in the future so we don't end up getting an exact match when we try to test
;; to see if things update below
:h2 "cast(dateadd('second', 1, current_timestamp) AS text)"
:mysql "cast((current_timestamp + interval 1 second) AS char)"
:postgres "cast((current_timestamp + interval '1 second') AS text)"))))
(defn- simulate-another-instance-updating-setting! [setting-name new-value]
(db/update-where! Setting {:key (name setting-name)} :value new-value)
......@@ -374,6 +381,9 @@
(clear-settings-last-updated-value-in-db!)
(toucan-name "Bird Can")
(let [first-value (settings-last-updated-value-in-db)]
;; MySQL only has the resolution of one second on the timestamps here so we should wait that long to make sure
;; the second-value actually ends up being greater than the first
(Thread/sleep 1200)
(toucan-name "Bird Can")
(let [second-value (settings-last-updated-value-in-db)]
;; first & second values should be different, and first value should be "less than" the second value
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment