Compare commits

...

1 Commits

Author SHA1 Message Date
lbrdnk
b80328c23b Post sync hook stub for implicit joins testing stub 2024-06-21 22:39:05 +02:00
4 changed files with 60 additions and 1 deletions

View File

@@ -303,6 +303,14 @@
[_driver _feature _db]
true)
;; We say Mongo supports foreign keys so that the front end can use implicit
;; joins. In reality, Mongo doesn't support foreign keys.
;; Only define an implementation for `:foreign-keys` if none exists already.
;; In test extensions we define an alternate implementation, and we don't want
;; to stomp over that if it was loaded already.
(when-not (get (methods driver/database-supports?) [:mongo :foreign-keys])
(defmethod driver/database-supports? [:mongo :foreign-keys] [_driver _feature _db] true))
(defmethod driver/mbql->native :mongo
[_ query]
(mongo.qp/mbql->native query))

View File

@@ -9,7 +9,8 @@
[metabase.driver.ddl.interface :as ddl.i]
[metabase.driver.mongo.connection :as mongo.connection]
[metabase.driver.mongo.util :as mongo.util]
[metabase.test.data.interface :as tx])
[metabase.test.data.interface :as tx]
[toucan2.core :as t2])
(:import
(com.fasterxml.jackson.core JsonGenerator)
(com.mongodb.client MongoDatabase)))
@@ -134,3 +135,42 @@
(merge ((get-method tx/aggregate-column-info ::tx/test-extensions) driver ag-type column-metadata)
(when (= ag-type :cum-count)
{:base_type :type/Integer}))))
(defn dbdef->fk-field-infos
[dbdef db]
(let [table-field-fk (loop [[table-def & table-defs] (:table-definitions dbdef)
result []]
(if (nil? table-def)
result
(recur table-defs
(into result
(comp (filter (comp some? :fk))
(map #(vector (:table-name table-def)
(:field-name %)
(name (:fk %)))))
(:field-definitions table-def)))))
tables (t2/select :model/Table :db_id (:id db))
fields (t2/select :model/Field {:where [:in :table_id (map :id tables)]})
table-id->table (m/index-by :id tables)
table-name->field-name->field (-> (group-by :table_id fields)
(update-keys (comp :name table-id->table))
(update-vals (partial m/index-by :name)))
table-name->pk-field (-> (group-by :table_id fields)
(update-keys (comp :name table-id->table))
(update-vals (partial some #(when (= (:semantic_type %) :type/PK) %))))]
(map (fn [[table-name field-name target-table-name]]
{:id (get-in table-name->field-name->field [table-name field-name :id])
:fk-target-field-id (get-in table-name->pk-field [target-table-name :id])})
table-field-fk)))
(defn- add-foreign-keys!
"This is necessary for implicit joins tests to work."
[dbdef db]
(let [fk-field-infos (dbdef->fk-field-infos dbdef db)]
(doseq [{:keys [id fk-target-field-id]} fk-field-infos]
(t2/update! :model/Field :id id {:semantic_type :type/FK
:fk_target_field_id fk-target-field-id}))))
(defmethod tx/post-sync-hook! :mongo
[_driver dbdef db]
(add-foreign-keys! dbdef db))

View File

@@ -165,6 +165,7 @@
:details connection-details})))]
(sync-newly-created-database! driver database-definition connection-details db)
(set-test-db-permissions! (u/the-id db))
(tx/post-sync-hook! driver database-definition db)
;; make sure we're returing an up-to-date copy of the DB
(t2/select-one Database :id (u/the-id db)))
(catch Throwable e

View File

@@ -793,3 +793,13 @@
;; Following cyclic dependency by that requiring resolve.
((requiring-resolve 'metabase.test.data.impl/resolve-dataset-definition)
'metabase.test.data.dataset-definitions 'test-data))
(defmulti post-sync-hook!
"WIP"
{:arglists '([driver db-def db])}
dispatch-on-driver-with-test-extensions
:hierarchy #'driver/hierarchy)
(defmethod post-sync-hook! ::driver/driver
[_driver _db-def _db]
nil)