Skip to content

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

fix: correct filtering for internal collections (#64065)

Edward Pagetb2939990fc05
diff --git a/resources/sample-content.edn b/resources/sample-content.edn
index 44de76d75f8..0a3904ed7f1 100644
--- a/resources/sample-content.edn
+++ b/resources/sample-content.edn
@@ -4606,7 +4606,7 @@
:name "E-commerce",
:personal_owner_id nil,
:type nil,
- :is_sample false,
+ :is_sample true,
:id 3,
:archived_directly true,
:entity_id "54O0IbgCzm290Nnxf1wRH",
diff --git a/src/metabase/analytics/stats.clj b/src/metabase/analytics/stats.clj
index 2b8498c3fd9..24ffd86dd74 100644
--- a/src/metabase/analytics/stats.clj
+++ b/src/metabase/analytics/stats.clj
@@ -300,9 +300,10 @@
(defn- collection-metrics
"Get metrics on Collection usage."
[]
- (let [collections (t2/select :model/Collection {:where (mi/exclude-internal-content-hsql :model/Collection)})
- cards (t2/select [:model/Card :collection_id :card_schema] {:where (mi/exclude-internal-content-hsql :model/Card)})]
- {:collections (count collections)
+ (let [collections (t2/count :model/Collection {:where (mi/exclude-internal-content-hsql :model/Collection)})
+ cards (t2/select [:model/Card :collection_id :card_schema] {:where
+ [:and (mi/exclude-internal-content-hsql :model/Card)]})]
+ {:collections collections
:cards_in_collections (count (filter :collection_id cards))
:cards_not_in_collections (count (remove :collection_id cards))
:num_cards_per_collection (medium-histogram cards :collection_id)}))
diff --git a/src/metabase/collections/models/collection.clj b/src/metabase/collections/models/collection.clj
index 5111f25eef3..de88ff11b00 100644
--- a/src/metabase/collections/models/collection.clj
+++ b/src/metabase/collections/models/collection.clj
@@ -1423,8 +1423,12 @@
[_model & {:keys [table-alias]}]
(let [maybe-alias #(h2x/identifier :field (some-> table-alias name) %)]
[:and
- [:not= (maybe-alias :type) [:inline instance-analytics-collection-type]]
- [:not= (maybe-alias :type) [:inline trash-collection-type]]
+ [:or [:= (maybe-alias :type) nil]
+ [:and
+ [:not= (maybe-alias :type) [:inline instance-analytics-collection-type]]
+ [:not= (maybe-alias :type) [:inline trash-collection-type]]]]
+ [:or [:= (maybe-alias :namespace) nil]
+ [:not= (maybe-alias :namespace) [:inline "analytics"]]]
[:not (maybe-alias :is_sample)]]))
(defn- parent-identity-hash [coll]
diff --git a/test/metabase/collections/models/collection_test.clj b/test/metabase/collections/models/collection_test.clj
index b359f88f88e..b635597025a 100644
--- a/test/metabase/collections/models/collection_test.clj
+++ b/test/metabase/collections/models/collection_test.clj
@@ -1772,3 +1772,32 @@
;; The count should be the same since personal collections don't get permission entries
(is (= initial-perms-count final-perms-count)
"No new permissions should be created for collections inside personal collections")))))))
+
+(deftest exclude-internal-content-hsql-test
+ (testing "The exclude-internal-content-hsql multimethod returns correct HoneySQL expressions"
+ (testing "filters exclude internal collections in practice"
+ (mt/with-temp [:model/Collection regular-collection {:name "Regular Collection"}
+ :model/Collection trash-collection {:name "Trash Collection" :type "trash"}
+ :model/Collection analytics-collection {:name "Analytics Collection" :type "instance-analytics"}
+ :model/Collection sample-collection {:name "Sample Collection" :is_sample true}]
+ (testing "regular collection is included"
+ (is (some #(= (:id regular-collection) (:id %))
+ (t2/select :model/Collection :id (:id regular-collection)))))
+
+ (testing "trash collection would be excluded by filter"
+ (let [hsql-clause (mi/exclude-internal-content-hsql :model/Collection)
+ query (t2/select :model/Collection
+ {:where [:and [:= :id (:id trash-collection)] hsql-clause]})]
+ (is (empty? query))))
+
+ (testing "analytics collection would be excluded by filter"
+ (let [hsql-clause (mi/exclude-internal-content-hsql :model/Collection)
+ query (t2/select :model/Collection
+ {:where [:and [:= :id (:id analytics-collection)] hsql-clause]})]
+ (is (empty? query))))
+
+ (testing "sample collection would be excluded by filter"
+ (let [hsql-clause (mi/exclude-internal-content-hsql :model/Collection)
+ query (t2/select :model/Collection
+ {:where [:and [:= :id (:id sample-collection)] hsql-clause]})]
+ (is (empty? query))))))))