Skip to content

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

Fix `batch-upsert!` method for heterogeneous entries (#65177)

John Swanson4eda82325296
diff --git a/src/metabase/search/appdb/index.clj b/src/metabase/search/appdb/index.clj
index 5e659bab09f..574078d4178 100644
--- a/src/metabase/search/appdb/index.clj
+++ b/src/metabase/search/appdb/index.clj
@@ -1,5 +1,6 @@
(ns metabase.search.appdb.index
(:require
+ [clojure.set :as set]
[clojure.string :as str]
[honey.sql :as sql]
[honey.sql.helpers :as sql.helpers]
@@ -249,17 +250,14 @@
(defn- document->entry [entity]
(-> entity
- (select-keys
- ;; remove attrs that get explicitly aliased below
- (remove #{:id :created_at :updated_at :native_query}
- (conj search.spec/attr-columns :model :display_data :legacy_input)))
+ (select-keys (conj search.spec/attr-columns :model :display_data :legacy_input))
+ (set/rename-keys {:id :model_id
+ :created_at :model_created_at
+ :updated_at :model_updated_at})
+ (assoc :updated_at :%now)
(update :display_data json/encode)
(update :legacy_input json/encode)
- (assoc
- :updated_at :%now
- :model_id (:id entity)
- :model_created_at (:created_at entity)
- :model_updated_at (:updated_at entity))
+ (dissoc :native_query)
(merge (specialization/extra-entry-fields entity))))
(defn- table-not-found-exception? [e]
diff --git a/src/metabase/search/appdb/specialization/postgres.clj b/src/metabase/search/appdb/specialization/postgres.clj
index dbc76d04fb7..fa9a0e2e216 100644
--- a/src/metabase/search/appdb/specialization/postgres.clj
+++ b/src/metabase/search/appdb/specialization/postgres.clj
@@ -26,8 +26,10 @@
(defmethod specialization/batch-upsert! :postgres [table entries]
(when (seq entries)
(t2/query
- ;; The cost of dynamically calculating these keys should be small compared to the IO cost, so unoptimized.
- (let [update-keys (vec (disj (set (keys (first entries))) :id :model :model_id))
+ ;; The cost of dynamically calculating these keys should be small compared to the IO cost, so unoptimized. Note
+ ;; that the entries are not guaranteed to be homogeneous - some may be missing nullable columns. So we need to
+ ;; get the keys from *all* the entries.
+ (let [update-keys (vec (disj (set (mapcat keys entries)) :id :model :model_id))
excluded-kw (fn [column] (keyword (str "excluded." (name column))))]
{:insert-into table
:values entries
diff --git a/test/metabase/search/appdb/specializations/postgres_test.clj b/test/metabase/search/appdb/specializations/postgres_test.clj
index c7ccdd3e093..8bbcf565b2f 100644
--- a/test/metabase/search/appdb/specializations/postgres_test.clj
+++ b/test/metabase/search/appdb/specializations/postgres_test.clj
@@ -1,3 +1,18 @@
(ns metabase.search.appdb.specializations.postgres-test
(:require
- [clojure.test :refer :all]))
+ [clojure.test :refer :all]
+ [metabase.app-db.core :as mdb]
+ [metabase.search.appdb.specialization.api :as specialization]
+ [toucan2.core :as t2]))
+
+(use-fixtures :each (fn [t]
+ (when (= :postgres (mdb/db-type))
+ (t))))
+
+(deftest batch-upsert-test
+ (with-redefs [t2/query identity]
+ (let [query (specialization/batch-upsert! :some-table
+ [{:a :b}
+ {:b :c}])]
+ (is (= [{:a :b} {:b :c}] (:values query)))
+ (is (= {:a :excluded.a :b :excluded.b} (:do-update-set query))))))