Skip to content
Snippets Groups Projects
Unverified Commit 79134b6b authored by Walter Leibbrandt's avatar Walter Leibbrandt Committed by GitHub
Browse files

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.
parent c607ce23
No related branches found
No related tags found
No related merge requests found
......@@ -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))))
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment