From 79134b6b1cff7b5bc4c52579e39685766a7f460b Mon Sep 17 00:00:00 2001 From: Walter Leibbrandt <23798+walterl@users.noreply.github.com> Date: Thu, 19 Nov 2020 22:50:53 +0200 Subject: [PATCH] Order fields to dump by ID (#13839) This is to ensure that fields are dumped in ID order, which is necessary to ensure that parent fields are insert _before_ their children. This issue was discovered while investigating a hosted instance for which snapshotting was failing. The failures were caused by the `fk_field_parent_ref_field_id` db constraint in Metabase being violated for a MongoDB database, indicating that child fields were being inserted before parent fields. This was confirmed experimentally, and being strict about the order of the field records were confirmed as the fix. This isn't a common issue, because it only affects fields that can have parent fields, like in MongoDB. --- src/metabase/cmd/dump_to_h2.clj | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/metabase/cmd/dump_to_h2.clj b/src/metabase/cmd/dump_to_h2.clj index a1f7e78630d..9ca8cb535a8 100644 --- a/src/metabase/cmd/dump_to_h2.clj +++ b/src/metabase/cmd/dump_to_h2.clj @@ -77,11 +77,16 @@ (insert-chunk! target-db-conn table-name chunk)) (println-ok)) +(def ^:private table-select-fragments + {"metabase_field" "ORDER BY id ASC"}) ; ensure ID order to ensure that parent fields are inserted before children + (defn- load-data! [target-db-conn] (println "Source db:" (dissoc (mdb/jdbc-spec) :password)) (jdbc/with-db-connection [db-conn (mdb/jdbc-spec)] (doseq [{table-name :table, :as e} entities - :let [rows (jdbc/query db-conn [(str "SELECT * FROM " (name table-name))])] + :let [fragment (table-select-fragments (str/lower-case (name table-name))) + rows (jdbc/query db-conn [(str "SELECT * FROM " (name table-name) + (when fragment (str " " fragment)))])] :when (seq rows)] (insert-entity! target-db-conn e rows)))) -- GitLab