Skip to content
Snippets Groups Projects
Unverified Commit 200541d5 authored by Ngoc Khuat's avatar Ngoc Khuat Committed by GitHub
Browse files

Fix sync indexes mark all fields with same name as indexed if one of them is (#46313)

parent e2086b2f
No related branches found
No related tags found
No related merge requests found
......@@ -265,6 +265,50 @@
(finally
(t2/delete! :model/Database (mt/id)))))))
(deftest sync-indexes-top-level-and-nested-column-with-same-name-test
(mt/test-driver :mongo
(testing "when a table has fields at the top level and nested level with the same name
we shouldn't mistakenly mark both of them as indexed if one is(#46312)"
(mt/dataset (mt/dataset-definition "index-duplicate-name"
["top-level-indexed"
[{:field-name "name" :indexed? true :base-type :type/Text}
{:field-name "class" :indexed? false :base-type :type/Text}]
[["Metabase" {"name" "Physics"}]]]
["nested-indexed"
[{:field-name "name" :indexed? false :base-type :type/Text}
{:field-name "class" :indexed? false :base-type :type/Text}]
[["Metabase" {"name" "Physics"}]]])
(mongo.connection/with-mongo-database [db (mt/db)]
(mongo.util/create-index (mongo.util/collection db "nested-indexed") (array-map "class.name" 1)))
(sync/sync-database! (mt/db) {:scan :schema})
(testing "top level indexed, nested not"
(let [name-fields (t2/select [:model/Field :name :parent_id :database_indexed]
:table_id (mt/id :top-level-indexed) :name "name")]
(testing "sanity check that we have 2 `name` fields"
(is (= 2 (count name-fields))))
(testing "only the top level field is indexed"
(is (=? [{:name "name"
:parent_id nil
:database_indexed true}
{:name "name"
:parent_id (mt/malli=? int?)
:database_indexed false}]
(sort-by :parent_id name-fields))))))
(testing "nested field indexed, top level not"
(let [name-fields (t2/select [:model/Field :name :parent_id :database_indexed]
:table_id (mt/id :nested-indexed) :name "name")]
(testing "sanity check that we have 2 `name` fields"
(is (= 2 (count name-fields))))
(testing "only the nested field is indexed"
(is (=? [{:name "name"
:parent_id nil
:database_indexed false}
{:name "name"
:parent_id (mt/malli=? int?)
:database_indexed true}]
(sort-by :parent_id name-fields))))))))))
(deftest describe-table-indexes-test
(mt/test-driver :mongo
(mt/dataset (mt/dataset-definition "indexing"
......@@ -561,8 +605,8 @@
(testing "make sure x-rays don't use features that the driver doesn't support"
(is (empty?
(lib.util.match/match-one (->> (magic/automagic-analysis (t2/select-one Field :id (mt/id :venues :price)) {})
:dashcards
(mapcat (comp :breakout :query :dataset_query :card)))
:dashcards
(mapcat (comp :breakout :query :dataset_query :card)))
[:field _ (_ :guard :binning)]))))))
(deftest no-values-test
......
......@@ -19,7 +19,7 @@
(let [normal-indexes (->> indexes (filter #(= (:type %) :normal-column-index)) (map :value))
nested-indexes (->> indexes (filter #(= (:type %) :nested-column-index)) (map :value))
normal-indexes-field-ids (when (seq normal-indexes)
(t2/select-pks-vec :model/Field :name [:in normal-indexes] :table_id table-id))
(t2/select-pks-vec :model/Field :name [:in normal-indexes] :table_id table-id :parent_id nil))
nested-indexes-field-ids (remove nil? (map #(field/nested-field-names->field-id table-id %) nested-indexes))]
(set (filter some? (concat normal-indexes-field-ids nested-indexes-field-ids))))))
......
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