Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

deserialization should happen inside a transaction #38442

Merged
merged 1 commit into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
[metabase-enterprise.serialization.v2.ingest :as serdes.ingest]
[metabase.models.serialization :as serdes]
[metabase.util.i18n :refer [trs]]
[metabase.util.log :as log]))
[metabase.util.log :as log]
[toucan2.core :as t2]))

(declare load-one!)

Expand Down Expand Up @@ -85,19 +86,20 @@
[ingestion & {:keys [abort-on-error backfill?]
:or {abort-on-error true
backfill? true}}]
;; We proceed in the arbitrary order of ingest-list, deserializing all the files. Their declared dependencies guide
;; the import, and make sure all containers are imported before contents, etc.
(when backfill?
(serdes.backfill/backfill-ids!))
(let [contents (serdes.ingest/ingest-list ingestion)
ctx {:expanding #{}
:seen #{}
:ingestion ingestion
:from-ids (m/index-by :id contents)
:errors []}
result (reduce (if abort-on-error load-one! try-load-one!) ctx contents)]
(when-let [errors (seq (:errors result))]
(log/error (trs "Errors were encountered during import."))
(doseq [e errors]
(log/error e "Import error details:")))
result))
(t2/with-transaction [_tx]
;; We proceed in the arbitrary order of ingest-list, deserializing all the files. Their declared dependencies
;; guide the import, and make sure all containers are imported before contents, etc.
(when backfill?
(serdes.backfill/backfill-ids!))
(let [contents (serdes.ingest/ingest-list ingestion)
ctx {:expanding #{}
:seen #{}
:ingestion ingestion
:from-ids (m/index-by :id contents)
:errors []}
result (reduce (if abort-on-error load-one! try-load-one!) ctx contents)]
(when-let [errors (seq (:errors result))]
(log/error (trs "Errors were encountered during import."))
(doseq [e errors]
(log/error e "Import error details:")))
result)))
Original file line number Diff line number Diff line change
Expand Up @@ -1195,3 +1195,31 @@
(testing "The extraneous keys do not interfere with loading"
(ts/with-db dest-db
(is (serdes.load/load-metabase! (ingestion-in-memory @serialized))))))))

(deftest tx-test
(mt/with-empty-h2-app-db
(let [coll (ts/create! Collection :name "coll")
card (ts/create! Card :name "card" :collection_id (:id coll))
serialized (atom {})]
(reset! serialized (->> (serdes.extract/extract {:no-settings true
:no-data-model true
:targets [["Collection" (:id coll)]]})
vec))
(testing "Load completes successfully"
(t2/update! Card {:id (:id card)} {:name (str "qwe_" (:name card))})
(is (serdes.load/load-metabase! (ingestion-in-memory @serialized)))
(is (= (:name card)
(t2/select-one-fn :name Card :id (:id card)))))

(testing "Partial load does not change the database"
(t2/update! Collection {:id (:id coll)} {:name (str "qwe_" (:name coll))})
(let [load-update! serdes/load-update!]
(with-redefs [serdes/load-update! (fn [model adjusted local]
;; Collection is loaded first
(if (= model "Card")
(throw (ex-info "oops, error" {}))
(load-update! model adjusted local)))]
(is (thrown? clojure.lang.ExceptionInfo
(serdes.load/load-metabase! (ingestion-in-memory @serialized))))
(is (= (str "qwe_" (:name coll))
(t2/select-one-fn :name Collection :id (:id card))))))))))