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

Return card__<id> for non-model cards and FK targets for tables (#38378)

Fixes #38312.
parent b5af8b9b
Branches
Tags
No related merge requests found
......@@ -112,6 +112,14 @@
_
(lib.metadata.calculation/display-name query stage-number filter-clause))))
(defn- foreign-key-items
[columns]
(for [column columns
:let [fk-target (:fk-target-field-id column)]
:when (and (integer? fk-target)
(lib.types.isa/foreign-key? column))]
{:type :field, :id fk-target}))
(defn- query-dependents
[metadata-providerable query-or-join]
(let [base-stage (first (:stages query-or-join))
......@@ -130,17 +138,16 @@
;; cf. frontend/src/metabase-lib/Question.ts and frontend/src/metabase-lib/queries/StructuredQuery.ts
(when-let [card-id (:source-card base-stage)]
(let [card-metadata (lib.metadata/card metadata-providerable card-id)]
(cond-> (for [column (:result-metadata card-metadata)
:let [column (u/normalize-map column)
fk-target (:fk-target-field-id column)]
:when (and (integer? fk-target)
(lib.types.isa/foreign-key? column))]
{:type :field, :id fk-target})
;; the FE code mentions this, but #36974 doesn't
#_#_:always (conj {:type :question, :id card-id})
(:dataset card-metadata) (conj {:type :table, :id (str "card__" card-id)}))))
(->> (:result-metadata card-metadata)
(map u/normalize-map)
foreign-key-items
;; the FE code mentions this, but #36974 doesn't
#_(cons {:type :question, :id card-id})
(cons {:type :table, :id (str "card__" card-id)}))))
(when-let [table-id (:source-table base-stage)]
[{:type :table, :id table-id}])
(->> (lib.metadata/fields metadata-providerable table-id)
foreign-key-items
(cons {:type :table, :id table-id})))
(for [stage (:stages query-or-join)
join (:joins stage)
dependent (query-dependents metadata-providerable join)]
......
......@@ -152,7 +152,8 @@
(testing "simple query"
(are [query] (=? [{:type :database, :id (meta/id)}
{:type :schema, :id (meta/id)}
{:type :table, :id (meta/id :venues)}]
{:type :table, :id (meta/id :venues)}
{:type :field, :id (meta/id :categories :id)}]
(lib/dependent-metadata query))
lib.tu/venues-query
(lib/append-stage lib.tu/venues-query)))
......@@ -160,19 +161,22 @@
(are [query] (=? [{:type :database, :id (meta/id)}
{:type :schema, :id (meta/id)}
{:type :table, :id (meta/id :venues)}
{:type :field, :id (meta/id :categories :id)}
{:type :table, :id (meta/id :categories)}]
(lib/dependent-metadata query))
lib.tu/query-with-join
(lib/append-stage lib.tu/query-with-join)))
(testing "source card based query"
(are [query] (=? [{:type :database, :id (meta/id)}
{:type :schema, :id (meta/id)}]
{:type :schema, :id (meta/id)}
{:type :table, :id "card__1"}]
(lib/dependent-metadata query))
lib.tu/query-with-source-card
(lib/append-stage lib.tu/query-with-source-card)))
(testing "source card based query with result metadata"
(are [query] (=? [{:type :database, :id (meta/id)}
{:type :schema, :id (meta/id)}
{:type :table, :id "card__1"}
{:type :field, :id (meta/id :users :id)}]
(lib/dependent-metadata query))
lib.tu/query-with-source-card-with-result-metadata
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment