diff --git a/src/metabase/api/search.clj b/src/metabase/api/search.clj index ef34409f0cc95df390ad10ddb18b0df475c688f2..007abbd9d7ac5884107f55e27ccd41c2910cd350 100644 --- a/src/metabase/api/search.clj +++ b/src/metabase/api/search.clj @@ -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" diff --git a/src/metabase/search/config.clj b/src/metabase/search/config.clj index 6be7a6af216c7ccb8e6a73189ac570279a25836a..259b21eb19e01458387a8f11c63ef8e5b1180365 100644 --- a/src/metabase/search/config.clj +++ b/src/metabase/search/config.clj @@ -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" diff --git a/src/metabase/search/scoring.clj b/src/metabase/search/scoring.clj index bca0363cd30b97a452f3a90aed55f2c7231a8c35..2def87f07674c6dc2970c5def46fb2cbeb558dae 100644 --- a/src/metabase/search/scoring.clj +++ b/src/metabase/search/scoring.clj @@ -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 diff --git a/test/metabase/api/search_test.clj b/test/metabase/api/search_test.clj index f8dbe6aca7e07c6f3973878e428fbbc3c1c65800..5048af2757b9fe4e752dd1839f1670018bb03731 100644 --- a/test/metabase/api/search_test.clj +++ b/test/metabase/api/search_test.clj @@ -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")))))))