Skip to content

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

Tweak data loading behavior for a test after MySQL 9.6.0 release (#68634)

Timothy S. Dean418d7a031704
diff --git a/test/metabase/query_processor/remapping_test.clj b/test/metabase/query_processor/remapping_test.clj
index 4c8c887f3f8..8560fdab38a 100644
--- a/test/metabase/query_processor/remapping_test.clj
+++ b/test/metabase/query_processor/remapping_test.clj
@@ -14,6 +14,7 @@
[metabase.query-processor.test-util :as qp.test-util]
[metabase.test :as mt]
[metabase.test.data.dataset-definitions :as defs]
+ [metabase.test.data.sql-jdbc.load-data :as load-data]
[metabase.util.date-2 :as u.date]))
(deftest ^:parallel basic-internal-remapping-test
@@ -218,29 +219,32 @@
;; `created_by` column which references the PK column in that same table. This tests that remapping table aliases are
;; handled correctly
(mt/test-drivers (mt/normal-drivers-with-feature :left-join ::self-referencing-fks)
- (mt/dataset test-data-self-referencing-user
- (qp.store/with-metadata-provider (-> (mt/metadata-provider)
- (lib.tu/remap-metadata-provider (mt/id :users :created_by)
- (mt/id :users :name))
+ ;; MySQL 9.6+ enforces FK constraints row-by-row during bulk INSERT, so we need to disable FK checks
+ ;; when loading data for this self-referencing dataset
+ (binding [load-data/*disable-fk-checks* true]
+ (mt/dataset test-data-self-referencing-user
+ (qp.store/with-metadata-provider (-> (mt/metadata-provider)
+ (lib.tu/remap-metadata-provider (mt/id :users :created_by)
+ (mt/id :users :name))
;; simulate this being a real FK so implicit joins work
- (lib.tu/merged-mock-metadata-provider
- {:fields [{:id (mt/id :users :created_by)
- :fk-target-field-id (mt/id :users :id)}]}))
- (let [results (mt/run-mbql-query users
- {:order-by [[:asc $name]]
- :limit 4})]
- (when (= driver/*driver* :h2)
- (is (= ["ID"
- "NAME"
- "LAST_LOGIN"
- "CREATED_BY"
- "USERS__via__CREATED_BY__NAME"] ; <- remapped column
- (map :lib/desired-column-alias (mt/cols results)))))
- (is (= [[14 "Broen Olujimi" "2014-10-03T13:45:00Z" 13 "Dwight Gresham"]
- [7 "Conchúr Tihomir" "2014-08-02T09:30:00Z" 6 "Shad Ferdynand"]
- [13 "Dwight Gresham" "2014-08-01T10:30:00Z" 12 "Kfir Caj"]
- [2 "Felipinho Asklepios" "2014-12-05T15:15:00Z" 1 "Plato Yeshua"]]
- (mt/rows results))))))))
+ (lib.tu/merged-mock-metadata-provider
+ {:fields [{:id (mt/id :users :created_by)
+ :fk-target-field-id (mt/id :users :id)}]}))
+ (let [results (mt/run-mbql-query users
+ {:order-by [[:asc $name]]
+ :limit 4})]
+ (when (= driver/*driver* :h2)
+ (is (= ["ID"
+ "NAME"
+ "LAST_LOGIN"
+ "CREATED_BY"
+ "USERS__via__CREATED_BY__NAME"] ; <- remapped column
+ (map :lib/desired-column-alias (mt/cols results)))))
+ (is (= [[14 "Broen Olujimi" "2014-10-03T13:45:00Z" 13 "Dwight Gresham"]
+ [7 "Conchúr Tihomir" "2014-08-02T09:30:00Z" 6 "Shad Ferdynand"]
+ [13 "Dwight Gresham" "2014-08-01T10:30:00Z" 12 "Kfir Caj"]
+ [2 "Felipinho Asklepios" "2014-12-05T15:15:00Z" 1 "Plato Yeshua"]]
+ (mt/rows results)))))))))
(defn- remappings-with-metadata
[metadata]
diff --git a/test/metabase/test/data/mysql.clj b/test/metabase/test/data/mysql.clj
index 16ee922a11b..4532a53eadc 100644
--- a/test/metabase/test/data/mysql.clj
+++ b/test/metabase/test/data/mysql.clj
@@ -94,6 +94,19 @@
;; load data all at once
nil)
+(defmethod load-data/do-insert! :mysql
+ [driver conn table-identifier rows]
+ (if-not load-data/*disable-fk-checks*
+ ((get-method load-data/do-insert! :sql-jdbc/test-extensions) driver conn table-identifier rows)
+ ;; Disable FK checks during insert to allow self-referencing FK rows in the same batch
+ ;; (MySQL 9.6+ enforces FK constraints row-by-row during bulk INSERT)
+ (do
+ (jdbc/execute! {:connection conn} ["SET FOREIGN_KEY_CHECKS = 0"])
+ (try
+ ((get-method load-data/do-insert! :sql-jdbc/test-extensions) driver conn table-identifier rows)
+ (finally
+ (jdbc/execute! {:connection conn} ["SET FOREIGN_KEY_CHECKS = 1"]))))))
+
(defmethod sql.tx/pk-sql-type :mysql [_] "INTEGER NOT NULL AUTO_INCREMENT")
;;; use one single global lock for all datasets. MySQL needs a global lock to do DDL stuff and blows up if other queries
diff --git a/test/metabase/test/data/sql_jdbc/load_data.clj b/test/metabase/test/data/sql_jdbc/load_data.clj
index 0a103e952d4..12fe5dbcd30 100644
--- a/test/metabase/test/data/sql_jdbc/load_data.clj
+++ b/test/metabase/test/data/sql_jdbc/load_data.clj
@@ -24,6 +24,11 @@
(set! *warn-on-reflection* true)
+(def ^:dynamic *disable-fk-checks*
+ "When bound to `true`, drivers that support it (e.g., MySQL) will disable foreign key checks during data insertion.
+ This is useful for tests that need to insert self-referencing data in a single batch."
+ false)
+
(defmulti row-xform
"Return a transducer that should be applied to each row when loading test data. Default is [[identity]], e.g. apply no
transform, but a few common ones are available, such as [[add-ids-xform]] and [[maybe-add-ids-xform]].