Skip to content
Snippets Groups Projects
Unverified Commit 2f61b2d7 authored by Alexander Solovyov's avatar Alexander Solovyov Committed by GitHub
Browse files

serialization: log actual model when couldn't generate entity id (#38814)

parent e3901481
Branches
Tags
No related merge requests found
......@@ -73,9 +73,9 @@
(set entity-id-models)))
(defn- seed-entity-id-for-instance! [model instance]
(try
(let [primary-key (first (t2/primary-keys model))
pk-value (get instance primary-key)]
(let [primary-key (first (t2/primary-keys model))
pk-value (get instance primary-key)]
(try
(when-not (some? pk-value)
(throw (ex-info (format "Missing value for primary key column %s" (pr-str primary-key))
{:model (name model)
......@@ -84,10 +84,12 @@
(let [new-hash (serdes/identity-hash instance)]
(log/infof "Update %s %s entity ID => %s" (name model) (pr-str pk-value) (pr-str new-hash))
(t2/update! model pk-value {:entity_id new-hash}))
{:update-count 1})
(catch Throwable e
(log/errorf e "Error updating entity ID: %s" (ex-message e))
{:error-count 1})))
{:update-count 1}
(catch Throwable e
(let [data (ex-data e)]
(log/errorf e "Error updating entity ID for %s %s: %s %s" (name model) (pr-str pk-value) (ex-message e)
(or (some-> data pr-str) "")))
{:error-count 1}))))
(defn- seed-entity-ids-for-model! [model]
(log/infof "Seeding Entity IDs for model %s" (name model))
......
......@@ -48,7 +48,16 @@
(is (= false
(v2.entity-ids/seed-entity-ids!))))
(is (= nil
(entity-id))))))))))))
(entity-id)))))))
(testing "Cannot create entity ID error"
(mt/with-temp [Collection c3 {:name "error collection"
:entity_id nil}]
;; update table directly so checks for collection existence don't trigger
(t2/update! :collection (:id c3) {:location "/13371338/"})
(is (=? [[:error Throwable #"Error updating entity ID for \w+ \d+:.*is an orphan.*\{:parent-id \d+\}"]]
(mt/with-log-messages-for-level ['metabase-enterprise :error]
(is (= false
(v2.entity-ids/seed-entity-ids!)))))))))))))
(deftest drop-entity-ids-test
(mt/with-empty-h2-app-db
......
......@@ -943,10 +943,12 @@
(defn- parent-identity-hash [coll]
(let [parent-id (-> coll
(t2/hydrate :parent_id)
:parent_id)]
(if parent-id
(serdes/identity-hash (t2/select-one Collection :id parent-id))
"ROOT")))
:parent_id)
parent (when parent-id (t2/select-one Collection :id parent-id))]
(cond
(not parent-id) "ROOT"
(not parent) (throw (ex-info (format "Collection %s is an orphan" (:id coll)) {:parent-id parent-id}))
:else (serdes/identity-hash parent))))
(defmethod serdes/hash-fields Collection
[_collection]
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment