Skip to content

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

Test to prove #28060 is fixed :wrench: (#69865)

Cam Saul71606de98c40
diff --git a/src/metabase/driver.clj b/src/metabase/driver.clj
index 218d572c8ed..c36354d062c 100644
--- a/src/metabase/driver.clj
+++ b/src/metabase/driver.clj
@@ -374,6 +374,8 @@
Takes optional keyword arguments to narrow down the results to a set of `schema-names`
and `table-names`.
+ `database` should be a Lib-style `:metadata/database` (i.e., should use kebab-case keys).
+
Results match [[metabase.sync.interface/FKMetadataEntry]].
Results are optionally filtered by `schema-names` and `table-names` provided.
Results are ordered by `fk-table-schema` and `fk-table-name` in ascending order.
diff --git a/src/metabase/driver/mysql.clj b/src/metabase/driver/mysql.clj
index 418a6815fed..33a6b7d8dc8 100644
--- a/src/metabase/driver/mysql.clj
+++ b/src/metabase/driver/mysql.clj
@@ -1,6 +1,6 @@
(ns metabase.driver.mysql
"MySQL driver. Builds off of the SQL-JDBC driver."
- (:refer-clojure :exclude [some not-empty])
+ (:refer-clojure :exclude [get-in some not-empty])
(:require
[clojure.java.io :as jio]
[clojure.java.jdbc :as jdbc]
@@ -27,28 +27,18 @@
[metabase.driver.sql.query-processor.util :as sql.qp.u]
[metabase.driver.sql.util :as sql.u]
[metabase.driver.util :as driver.u]
+ [metabase.lib.schema.common :as lib.schema.common]
[metabase.util :as u]
[metabase.util.date-2 :as u.date]
[metabase.util.honey-sql-2 :as h2x]
[metabase.util.i18n :refer [deferred-tru]]
[metabase.util.log :as log]
- [metabase.util.performance :as perf :refer [some not-empty]])
+ [metabase.util.malli :as mu]
+ [metabase.util.performance :as perf :refer [get-in not-empty some]])
(:import
(java.io File)
- (java.sql
- Connection
- DatabaseMetaData
- ResultSet
- ResultSetMetaData
- SQLException
- Statement
- Types)
- (java.time
- LocalDateTime
- OffsetDateTime
- OffsetTime
- ZoneOffset
- ZonedDateTime)
+ (java.sql Connection DatabaseMetaData ResultSet ResultSetMetaData SQLException Statement Types)
+ (java.time LocalDateTime OffsetDateTime OffsetTime ZonedDateTime ZoneOffset)
(java.time.format DateTimeFormatter)))
(set! *warn-on-reflection* true)
@@ -121,10 +111,23 @@
;; Only supported for MySQL right now. Revise when a child driver is added.
(= driver :mysql)))
+(mu/defn- database-flavor :- [:maybe :string]
+ ^String [database :- [:maybe :map]]
+ ;; avoid trying `:dbms_version` if `:dbms-version` is present but `nil`; this will cause snake-hating-map warnings
+ (when-let [k (some #(when (contains? database %)
+ %)
+ [:dbms-version
+ :dbms_version])]
+ (get-in database [k :flavor])))
+
+(mu/defn- connection-flavor :- :string
+ ^String [^Connection conn :- (lib.schema.common/instance-of-class Connection)]
+ (.. conn getMetaData getDatabaseProductName))
+
(defn mariadb?
"Returns true if the database is MariaDB. Assumes the database has been synced so `:dbms_version` is present."
[database]
- (-> database :dbms_version :flavor (= "MariaDB")))
+ (= (database-flavor database) "MariaDB"))
(defn- mysql?
"Returns true if the database is MySQL (not MariaDB).
@@ -132,13 +135,13 @@
[db]
(= "MySQL"
(if-let [conn (:connection db)]
- (->> ^java.sql.Connection conn .getMetaData .getDatabaseProductName)
- (-> db :dbms_version :flavor))))
+ (connection-flavor conn)
+ (database-flavor db))))
-(defn mariadb-connection?
+(mu/defn- mariadb-connection? :- :boolean
"Returns true if the database is MariaDB."
- [driver conn]
- (->> conn (sql-jdbc.sync/dbms-version driver) :flavor (= "MariaDB")))
+ [conn :- (lib.schema.common/instance-of-class Connection)]
+ (= (connection-flavor conn) "MariaDB"))
(defn- partial-revokes-enabled?
[driver db]
@@ -219,14 +222,14 @@
(declare privilege-grants-for-user)
(defmethod sql-jdbc.sync/current-user-table-privileges :mysql
- [driver conn & {:as _options}]
+ [_driver conn-spec & {:as _options}]
;; MariaDB doesn't allow users to query the privileges of roles a user might have (unless they have select privileges
;; for the mysql database), so we can't query the full privileges of the current user.
- (when-not (mariadb-connection? driver conn)
- (let [sql->tuples (fn [sql] (drop 1 (jdbc/query conn sql {:as-arrays? true})))
+ (when-not (some-> conn-spec :connection mariadb-connection?)
+ (let [sql->tuples (fn [sql] (drop 1 (jdbc/query conn-spec sql {:as-arrays? true})))
db-name (ffirst (sql->tuples "SELECT DATABASE()"))
table-names (map first (sql->tuples "SHOW TABLES"))]
- (for [[table-name privileges] (table-names->privileges (privilege-grants-for-user conn "CURRENT_USER()")
+ (for [[table-name privileges] (table-names->privileges (privilege-grants-for-user conn-spec "CURRENT_USER()")
db-name
table-names)]
{:role nil
diff --git a/test/metabase/driver/mysql_test.clj b/test/metabase/driver/mysql_test.clj
index 8166ce94b30..efb94f70c69 100644
--- a/test/metabase/driver/mysql_test.clj
+++ b/test/metabase/driver/mysql_test.clj
@@ -21,6 +21,7 @@
[metabase.driver.sql-jdbc.sync.describe-database :as sql-jdbc.describe-database]
[metabase.driver.sql-jdbc.sync.interface :as sql-jdbc.sync.interface]
[metabase.driver.sql.query-processor :as sql.qp]
+ [metabase.lib-be.core :as lib-be]
[metabase.lib.card :as lib.card]
[metabase.lib.core :as lib]
[metabase.lib.metadata :as lib.metadata]
@@ -74,6 +75,38 @@
(mt/rows
(mt/run-mbql-query exciting-moments-in-history))))))))))
+(deftest sync-fks-test
+ (testing "Make sure we sync table FKs correctly for MySQL (#28060)"
+ (mt/test-driver :mysql
+ (tx/drop-if-exists-and-create-db! driver/*driver* "test_28060")
+ (let [details (tx/dbdef->connection-details :mysql :db {:database-name "test_28060"})
+ spec (sql-jdbc.conn/connection-details->spec :mysql details)]
+ ;; according to #28060 this synced incorrectly (nil column and table names) and triggered an exception; this
+ ;; test is to prove that the issue is fixed
+ (doseq [lines [["CREATE TABLE IF NOT EXISTS foo ("
+ " id BIGINT(20) UNSIGNED,"
+ " PRIMARY KEY (id)"
+ ");"]
+ ["CREATE TABLE IF NOT EXISTS bar ("
+ " id BIGINT(20) UNSIGNED,"
+ " foo_id BIGINT(20) UNSIGNED,"
+ " FOREIGN KEY (foo_id) REFERENCES foo(id)"
+ ");"]]
+ :let [sql (str/join "\n" lines)]]
+ (jdbc/execute! spec [sql]))
+ (mt/with-temp [:model/Database database {:engine "mysql", :details details}]
+ (sync/sync-database! database)
+ (is (= [{:pk-table-schema nil
+ :pk-table-name "foo"
+ :pk-column-name "id"
+ :fk-table-schema nil
+ :fk-table-name "bar"
+ :fk-column-name "foo_id"}]
+ (into []
+ (driver/describe-fks driver/*driver*
+ (lib-be/instance->metadata database :metadata/database)
+ {:table-names #{"foo" "bar"}})))))))))
+
(deftest multiple-schema-test
(testing "Make sure that we filter databases (schema) with :db or :dbname (#50072)"
(mt/test-driver :mysql