Skip to content
Snippets Groups Projects
Unverified Commit 94f43d5b authored by metamben's avatar metamben Committed by GitHub
Browse files

Hydrate app_id on collections in the search endpoint (#25118)

Hydrate app_id on collections in the search endpoint
parent 2627bdf6
No related branches found
No related tags found
No related merge requests found
......@@ -8,7 +8,7 @@
[medley.core :as m]
[metabase.api.common :as api]
[metabase.db :as mdb]
[metabase.models :refer [Database]]
[metabase.models :refer [App Database]]
[metabase.models.bookmark :refer [CardBookmark CollectionBookmark DashboardBookmark]]
[metabase.models.collection :as collection :refer [Collection]]
[metabase.models.interface :as mi]
......@@ -74,6 +74,7 @@
:archived :boolean
;; returned for Card, Dashboard, Pulse, and Collection
:collection_id :integer
:collection_app_id :integer
:collection_name :text
:collection_authority_level :text
;; returned for Card and Dashboard
......@@ -85,6 +86,8 @@
:dashboardcard_count :integer
:dataset_query :text
:moderated_status :text
;; returned for Collection only
:app_id :integer
;; returned for Metric and Segment
:table_id :integer
:database_id :integer
......@@ -232,7 +235,9 @@
(cond-> honeysql-query
(not= collection-id-column :collection.id)
(hh/merge-left-join [Collection :collection]
[:= collection-id-column :collection.id]))))
[:= collection-id-column :collection.id]
[App :collection_app]
[:= :collection.id :collection_app.collection_id]))))
(s/defn ^:private add-table-db-id-clause
"Add a WHERE clause to only return tables with the given DB id.
......@@ -284,7 +289,9 @@
(hh/left-join [CollectionBookmark :bookmark]
[:and
[:= :bookmark.collection_id :collection.id]
[:= :bookmark.user_id api/*current-user-id*]])
[:= :bookmark.user_id api/*current-user-id*]]
[App :app]
[:= :app.collection_id :collection.id])
(add-collection-join-and-where-clauses :collection.id search-ctx)))
(s/defmethod search-query-for-model "database"
......
......@@ -122,6 +122,7 @@
(defmethod columns-for-model "card"
[_]
(conj default-columns :collection_id :collection_position :dataset_query
[:collection_app.collection_id :collection_app_id]
[:collection.name :collection_name]
[:collection.authority_level :collection_authority_level]
[{:select [:status]
......@@ -140,6 +141,7 @@
(defmethod columns-for-model "dashboard"
[_]
(conj default-columns :collection_id :collection_position bookmark-col
[:collection_app.collection_id :collection_app_id]
[:collection.name :collection_name]
[:collection.authority_level :collection_authority_level]))
......@@ -149,12 +151,18 @@
(defmethod columns-for-model "pulse"
[_]
[:id :name :collection_id [:collection.name :collection_name]])
[:id :name :collection_id
[:collection_app.collection_id :collection_app_id]
[:collection.name :collection_name]])
(defmethod columns-for-model "collection"
[_]
(conj (remove #{:updated_at} default-columns) [:collection.id :collection_id] [:name :collection_name]
(conj (remove #{:updated_at} default-columns)
[:collection.id :collection_id]
[:name :collection_name]
[:authority_level :collection_authority_level]
[:app.id :app_id]
[:app.id :collection_app_id]
bookmark-col))
(defmethod columns-for-model "segment"
......
......@@ -219,7 +219,7 @@
"Massage the raw result from the DB and match data into something more useful for the client"
[result {:keys [column match-context-thunk]} scores]
(let [{:keys [name display_name
collection_id collection_name collection_authority_level]} result]
collection_id collection_name collection_authority_level collection_app_id]} result]
(-> result
(assoc
:name (if (or (= column :name)
......@@ -231,12 +231,14 @@
(match-context-thunk))
:collection {:id collection_id
:name collection_name
:authority_level collection_authority_level}
:authority_level collection_authority_level
:app_id collection_app_id}
:scores scores)
(update :dataset_query #(some-> % json/parse-string mbql.normalize/normalize))
(dissoc
:collection_id
:collection_name
:collection_app_id
:display_name))))
(defn weights-and-scores
......
......@@ -6,7 +6,7 @@
[metabase.api.search :as api.search]
[metabase.models
:refer
[Card CardBookmark Collection Dashboard DashboardBookmark DashboardCard
[App Card CardBookmark Collection Dashboard DashboardBookmark DashboardCard
Database Metric PermissionsGroup PermissionsGroupMembership Pulse PulseCard
Segment Table]]
[metabase.models.permissions :as perms]
......@@ -22,9 +22,10 @@
{:id true
:description nil
:archived false
:collection {:id false :name nil :authority_level nil}
:collection {:id false :name nil :authority_level nil :app_id false}
:collection_authority_level nil
:collection_position nil
:app_id false
:moderated_status nil
:context nil
:dashboardcard_count nil
......@@ -54,15 +55,13 @@
(defn- make-result
[name & kvs]
(merge
default-search-row
{:name name}
(apply array-map kvs)))
(apply assoc default-search-row :name name kvs))
(def ^:private test-collection (make-result "collection test collection"
:bookmark false
:model "collection"
:collection {:id true, :name true :authority_level nil}
:collection {:id true, :name true :authority_level nil
:app_id false}
:updated_at false))
(defn- default-search-results []
......@@ -95,7 +94,7 @@
(defn- default-results-with-collection []
(on-search-types #{"dashboard" "pulse" "card" "dataset"}
#(assoc % :collection {:id true, :name true :authority_level nil})
#(assoc % :collection {:id true, :name true :authority_level nil :app_id false})
(default-search-results)))
(defn- do-with-search-items [search-string in-root-collection? f]
......@@ -610,3 +609,15 @@
(is (= ["Another SQL query"]
(->> (search-request-data :rasta :q "aggregation")
(map :name)))))))
(deftest app-test
(testing "App collections should come with app_id set"
(with-search-items-in-collection {:keys [collection]} "test"
(mt/with-temp App [_app {:collection_id (:id collection)}]
(is (= (mapv
(fn [result]
(cond-> result
(not (#{"metric" "segment"} (:model result))) (assoc-in [:collection :app_id] true)
(= (:model result) "collection") (assoc :app_id true)))
(default-results-with-collection))
(search-request-data :rasta :q "test")))))))
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