Skip to content

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

Show upstream archived on inactive dependencies (#73186)

Alexander Polyankin9c76f5c6297c
diff --git a/.clj-kondo/config/modules/config.edn b/.clj-kondo/config/modules/config.edn
index f24792e35dd..409ee858184 100644
--- a/.clj-kondo/config/modules/config.edn
+++ b/.clj-kondo/config/modules/config.edn
@@ -3016,8 +3016,7 @@
metabase-enterprise.dependencies.core
metabase-enterprise.dependencies.init
metabase-enterprise.dependencies.models.dependency}
- :uses #{analyze
- api
+ :uses #{api
app-db
collections
database-routing
@@ -3028,7 +3027,6 @@
lib
lib-be
models
- native-query-snippets
permissions
premium-features
queries
diff --git a/.gitignore b/.gitignore
index 90da23ba921..570cad1100d 100644
--- a/.gitignore
+++ b/.gitignore
@@ -172,6 +172,7 @@ llm-payloads
/config.yml
CLAUDE.local.md
.claude/settings.local.json
+.claude/agent-memory/
.eslintcache
diff --git a/e2e/test/scenarios/data-studio/transforms/template-tags.cy.spec.ts b/e2e/test/scenarios/data-studio/transforms/template-tags.cy.spec.ts
index 54bc5d8e412..ac1d2487762 100644
--- a/e2e/test/scenarios/data-studio/transforms/template-tags.cy.spec.ts
+++ b/e2e/test/scenarios/data-studio/transforms/template-tags.cy.spec.ts
@@ -25,9 +25,6 @@ describe("scenarios > admin > transforms", () => {
cy.intercept("POST", "/api/transform-tag").as("createTag");
cy.intercept("PUT", "/api/transform-tag/*").as("updateTag");
cy.intercept("DELETE", "/api/transform-tag/*").as("deleteTag");
- cy.intercept("POST", "/api/ee/dependencies/check_transform").as(
- "checkTransformDependencies",
- );
});
it("should be able to use the data reference and snippets when writing a SQL transform", () => {
diff --git a/e2e/test/scenarios/data-studio/transforms/transforms-incremental.cy.spec.ts b/e2e/test/scenarios/data-studio/transforms/transforms-incremental.cy.spec.ts
index eea8f49cd63..cebc882d8da 100644
--- a/e2e/test/scenarios/data-studio/transforms/transforms-incremental.cy.spec.ts
+++ b/e2e/test/scenarios/data-studio/transforms/transforms-incremental.cy.spec.ts
@@ -25,9 +25,6 @@ describe("scenarios > admin > transforms incremental", () => {
cy.intercept("POST", "/api/transform-tag").as("createTag");
cy.intercept("PUT", "/api/transform-tag/*").as("updateTag");
cy.intercept("DELETE", "/api/transform-tag/*").as("deleteTag");
- cy.intercept("POST", "/api/ee/dependencies/check-transform").as(
- "checkTransformDependencies",
- );
cy.intercept("POST", "/api/transform/*/reset-checkpoint").as(
"resetCheckpoint",
);
diff --git a/e2e/test/scenarios/data-studio/transforms/transforms.cy.spec.ts b/e2e/test/scenarios/data-studio/transforms/transforms.cy.spec.ts
index c216f5dee7c..fe1cd63ec07 100644
--- a/e2e/test/scenarios/data-studio/transforms/transforms.cy.spec.ts
+++ b/e2e/test/scenarios/data-studio/transforms/transforms.cy.spec.ts
@@ -50,9 +50,6 @@ describe("scenarios > admin > transforms", { tags: ["@external"] }, () => {
cy.intercept("POST", "/api/transform-tag").as("createTag");
cy.intercept("PUT", "/api/transform-tag/*").as("updateTag");
cy.intercept("DELETE", "/api/transform-tag/*").as("deleteTag");
- cy.intercept("POST", "/api/ee/dependencies/check-transform").as(
- "checkTransformDependencies",
- );
});
afterEach(() => {
diff --git a/enterprise/backend/src/metabase_enterprise/dependencies/api.clj b/enterprise/backend/src/metabase_enterprise/dependencies/api.clj
index 1bc3f30c6e4..eaf0a13fe74 100644
--- a/enterprise/backend/src/metabase_enterprise/dependencies/api.clj
+++ b/enterprise/backend/src/metabase_enterprise/dependencies/api.clj
@@ -2,13 +2,10 @@
(:require
[clojure.set :as set]
[clojure.string :as str]
- [medley.core :as m]
- [metabase-enterprise.dependencies.core :as dependencies]
[metabase-enterprise.dependencies.dependency-types :as deps.dependency-types]
[metabase-enterprise.dependencies.models.analysis-finding-error :as analysis-finding-error]
[metabase-enterprise.dependencies.models.dependency :as dependency]
[metabase-enterprise.dependencies.models.dependency-status :as deps.dependency-status]
- [metabase.analyze.core :as analyze]
[metabase.api.common :as api]
[metabase.api.macros :as api.macros]
[metabase.api.routes.common :refer [+auth]]
@@ -19,147 +16,19 @@
[metabase.documents.schema :as documents.schema]
[metabase.graph.core :as graph]
[metabase.lib-be.core :as lib-be]
- [metabase.lib.metadata :as lib.metadata]
[metabase.lib.schema.id :as lib.schema.id]
[metabase.lib.schema.metadata :as lib.schema.metadata]
[metabase.models.interface :as mi]
- [metabase.native-query-snippets.core :as native-query-snippets]
[metabase.permissions.core :as perms]
- [metabase.queries.schema :as queries.schema]
[metabase.request.core :as request]
[metabase.revisions.core :as revisions]
- [metabase.transforms.schema :as transforms.schema]
[metabase.util :as u]
- [metabase.util.i18n :refer [tru]]
[metabase.util.malli :as mu]
[metabase.util.malli.registry :as mr]
[metabase.util.malli.schema :as ms]
[toucan2.core :as t2]
[toucan2.util :as t2.util]))
-(mr/def ::card-body
- [:merge
- ::queries.schema/card
- ;; TODO (Cam 10/1/25) -- merge this into `::queries.schema/card` itself
- [:map
- [:result_metadata {:optional true} [:maybe analyze/ResultsMetadata]]]])
-
-(mr/def ::broken-cards-response
- [:map
- [:success :boolean]
- [:bad_cards {:optional true} [:sequential ::queries.schema/card]]
- [:bad_transforms {:optional true} [:sequential ::transforms.schema/transform]]])
-
-(mu/defn- broken-cards-response :- ::broken-cards-response
- "Build the `check-*` response from a breakage map.
-
- `bad_cards` and `bad_transforms` are sorted to ensure the stable response order."
- [{:keys [card transform]}]
- (let [broken-card-ids (keys card)
- broken-cards (when (seq broken-card-ids)
- (-> (t2/select :model/Card :id [:in broken-card-ids])
- (t2/hydrate [:collection :effective_ancestors] :dashboard :document)))
- broken-transform-ids (keys transform)
- broken-transforms (when (seq broken-transform-ids)
- (t2/select :model/Transform :id [:in broken-transform-ids]))]
- {:success (and (empty? broken-card-ids)
- (empty? broken-transform-ids))
- :bad_cards (->> broken-cards
- (filter mi/can-read?)
- (map (fn [card]
- (-> card
- collection.root/hydrate-root-collection
- (update :dashboard #(some-> % (select-keys [:id :name])))
- (update :document #(some-> % (select-keys [:id :name]))))))
- (sort-by (juxt :created_at :id))
- vec)
- :bad_transforms (->> broken-transforms
- (filter mi/can-read?)
- (sort-by (juxt :created_at :id))
- vec)}))
-
-(api.macros/defendpoint :post "/check-card" :- ::broken-cards-response
- "Check a proposed edit to a card, and return the cards/transforms this edit will break.
-
- `broken-cards-response` ensures the stable response order."
- [_route-params
- _query-params
- body :- ::card-body]
- (api/read-check :model/Card (:id body))
- (let [database-id (-> body :dataset_query :database)
- base-provider (lib-be/application-database-metadata-provider database-id)
- original (lib.metadata/card base-provider (:id body))
- card (-> original
- (assoc :dataset-query (:dataset_query body)
- :type (:type body (:type original)))
- ;; Remove the old `:result-metadata` from the card, it's likely wrong now.
- (dissoc :result-metadata)
- ;; But if the request includes `:result_metadata`, use that. It may be from a native card
- ;; that's been run before saving the card.
- (cond-> #_card
- (:result_metadata body) (assoc :result-metadata (:result_metadata body))))
- edits {:card [card]}
- breakages (dependencies/errors-from-proposed-edits edits
- :base-provider base-provider
- :include-native? true)]
- (broken-cards-response breakages)))
-
-(mr/def ::transform-body
- [:map
- [:id {:optional false} ::lib.schema.id/transform]
- [:name {:optional true} :string]
- ;; TODO (Cam 10/8/25) -- no idea what the correct schema for these is supposed to be -- it was just `map` before --
- ;; this is my attempt to guess it
- [:source {:optional true} [:maybe [:map
- [:type {:optional true} :keyword]
- [:query {:optional true} ::queries.schema/query]]]]
- [:target {:optional true} [:maybe ms/Map]]])
-
-(api.macros/defendpoint :post "/check-transform" :- ::broken-cards-response
- "Check a proposed edit to a transform, and return the card, transform, etc. IDs for things that will break."
- [_route-params
- _query-params
- {:keys [id source target]} :- ::transform-body]
- (api/read-check :model/Transform id)
- (if (= (keyword (:type source)) :query)
- (let [database-id (-> source :query :database)
- base-provider (lib-be/application-database-metadata-provider database-id)
- original (lib.metadata/transform base-provider id)
- transform (-> original
- (cond-> #_transform source (assoc :source source))
- (cond-> #_transform target (assoc :target target)))
- edits {:transform [transform]}
- breakages (dependencies/errors-from-proposed-edits edits
- :base-provider base-provider
- :include-native? true)]
- (broken-cards-response breakages))
- ;; if this isn't a sql query, just claim it works
- {:success true}))
-
-(api.macros/defendpoint :post "/check-snippet" :- ::broken-cards-response
- "Check a proposed edit to a native snippet, and return the cards, etc. which will be broken."
- [_route-params
- _query-params
- {:keys [id content], snippet-name :name}
- :- [:map
- [:id {:optional false} ::lib.schema.id/snippet]
- [:name {:optional true} native-query-snippets/NativeQuerySnippetName]
- [:content {:optional true} :string]]]
- (api/read-check :model/NativeQuerySnippet id)
- (let [original (t2/select-one :model/NativeQuerySnippet id)
- _ (when (and snippet-name
- (not= snippet-name (:name original))
- (t2/exists? :model/NativeQuerySnippet :name snippet-name))
- (throw (ex-info (tru "A snippet with that name already exists. Please pick a different name.")
- {:status-code 400})))
- snippet (cond-> (m/assoc-some original
- :lib/type :metadata/native-query-snippet
- :name snippet-name
- :content content)
- content native-query-snippets/add-template-tags)
- breakages (dependencies/errors-from-proposed-edits {:snippet [snippet]} :include-native? true)]
- (broken-cards-response breakages)))
-
(def ^:private entity-keys
{:table [:name :description :display_name :db_id :db :schema :fields :transform
:owner :owner_user_id :owner_email]
@@ -621,27 +490,20 @@
(api.macros/defendpoint :get "/graph" :- ::graph-response
"This endpoint takes an :id and a supported entity :type, and returns a graph of all its upstream dependencies.
The graph is represented by a list of :nodes and a list of :edges. Each node has an :id, :type, :data (which
- depends on the node type), and a map of :dependent_counts per entity type. Each edge is a :model/Dependency.
-
- Optional :archived parameter controls whether entities in archived collections are included:
- - false (default): Excludes entities in archived collections
- - true: Includes entities in archived collections"
+ depends on the node type), and a map of :dependent_counts per entity type. Each edge is a :model/Dependency."
[_route-params
- {:keys [id type archived]} :- [:map
- [:id {:optional true} ms/PositiveInt]
- [:type {:optional true} ::deps.dependency-types/dependency-types]
- [:archived {:optional true} :boolean]]]
+ {:keys [id type]} :- [:map
+ [:id {:optional true} ms/PositiveInt]
+ [:type {:optional true} ::deps.dependency-types/dependency-types]]]
(api/read-check (deps.dependency-types/dependency-type->model type) id)
(lib-be/with-metadata-provider-cache
- (let [graph-opts {:include-archived-items (if archived :all :exclude)}
- starting-nodes [[type id]]
- upstream-graph (readable-graph-dependencies graph-opts)
- ;; cache the downstream graph specifically, because between calculating transitive children and calculating
- ;; edges, we'll call this multiple times on the same nodes.
- downstream-graph (graph/cached-graph (readable-graph-dependents graph-opts))
+ (let [starting-nodes [[type id]]
+ upstream-graph (readable-graph-dependencies {:include-archived-items :all})
+ downstream-graph (graph/cached-graph (readable-graph-dependents))
+ edge-graph (graph/cached-graph (readable-graph-dependents {:include-archived-items :all}))
nodes (into (set starting-nodes)
(graph/transitive upstream-graph starting-nodes))
- edges (graph/edges-between downstream-graph nodes)]
+ edges (graph/edges-between edge-graph nodes)]
{:nodes (expanded-nodes downstream-graph nodes {:include-errors? false})
:edges edges})))
@@ -717,7 +579,6 @@
[:or
(ms/enum-decode-keyword lib.schema.metadata/card-types)
[:sequential (ms/enum-decode-keyword lib.schema.metadata/card-types)]]]
- [:archived {:optional true} :boolean]
[:broken {:optional true} :boolean]
[:query {:optional true} :string]
[:include-personal-collections {:optional true} :boolean]
@@ -736,23 +597,20 @@
If not provided, returns all types. Example: ?dependent-types=card&dependent-types=dashboard
- `dependent-card-types`: Card types to filter by when dependent-types includes :card.
Ignored if dependent-types doesn't include :card. Example: ?dependent-card-types=question&dependent-card-types=model
- - `archived`: Include entities in archived collections (default: false)
- `broken`: Return only broken entities (default: false)
- `query`: Search string to filter results by name or location (case-insensitive)
- `include-personal-collections`: Include items in personal collections (default: false)
- `sort-column`: Column to sort by - name, location, or view-count (default: name)
- `sort-direction`: Sort direction - asc or desc (default: asc)"
[_route-params
- {:keys [id type dependent-types dependent-card-types archived broken
+ {:keys [id type dependent-types dependent-card-types broken
query include-personal-collections sort-column sort-direction]
:or {include-personal-collections false
sort-column :name
sort-direction :asc}} :- dependents-args]
(api/read-check (deps.dependency-types/dependency-type->model type) id)
(lib-be/with-metadata-provider-cache
- (let [graph-opts {:include-archived-items (if archived :all :exclude)
- :broken broken}
- downstream-graph (graph/cached-graph (readable-graph-dependents graph-opts))
+ (let [downstream-graph (graph/cached-graph (readable-graph-dependents {:broken broken}))
nodes (-> (graph/children-of downstream-graph [[type id]])
(get [type id]))
dep-types-set (cond
@@ -815,15 +673,14 @@
(:segment :measure) :table.display_name)}))
(defn- query-type-join-and-filter
- [query-type entity-type {:keys [include-archived-items]}]
+ [query-type entity-type]
(case query-type
:unreferenced {:join [:dependency [:and
[:= :dependency.to_entity_id :entity.id]
[:= :dependency.to_entity_type (name entity-type)]
(visible-entities-filter-clause
:dependency.from_entity_type
- :dependency.from_entity_id
- {:include-archived-items include-archived-items})]]
+ :dependency.from_entity_id)]]
:join-filter [:= :dependency.id nil]}
:broken {:join [:analysis_finding [:and
[:= :analysis_finding.analyzed_entity_id :entity.id]
@@ -834,8 +691,7 @@
[:= :analysis_finding_error.source_entity_type (name entity-type)]
(visible-entities-filter-clause
:analysis_finding_error.analyzed_entity_type
- :analysis_finding_error.analyzed_entity_id
- {:include-archived-items include-archived-items})]]
+ :analysis_finding_error.analyzed_entity_id)]]
:join-filter [:!= :analysis_finding_error.id nil]}))
(defn- location-joins-for-entity
@@ -849,7 +705,7 @@
#{}))
(defn- build-optional-filters
- [{:keys [query-type entity-type card-types query include-archived-items include-personal-collections]}
+ [{:keys [query-type entity-type card-types query include-personal-collections]}
{:keys [name-column location-column]}]
(let [card-type-filter (when (and (= entity-type :card)
(seq card-types))
@@ -874,7 +730,7 @@
nil))]
{:filter (mi/exclude-internal-content-hsql model :table-alias :entity)
:filter-joins #{}})
- archived-filter (when (= include-archived-items :exclude)
+ archived-filter (when-not (= query-type :breaking)
{:filter (case entity-type
(:card :dashboard :document :snippet :segment :measure)
[:= :entity.archived false]
@@ -944,14 +800,14 @@
(:table joins) (conj [:metabase_table :table] [:= :entity.table_id :table.id])))
(defn- dependency-items-query
- [{:keys [query-type entity-type sort-column include-archived-items] :as params}]
+ [{:keys [query-type entity-type sort-column] :as params}]
(let [{:keys [table-name name-column location-column] :as config} (entity-type-config entity-type)
- {:keys [join join-filter]} (query-type-join-and-filter query-type entity-type
- {:include-archived-items include-archived-items})
+ {:keys [join join-filter]} (query-type-join-and-filter query-type entity-type)
{:keys [filters filter-joins]} (build-optional-filters params config)
{:keys [sort-column sort-joins]} (sort-key-cols-and-joins sort-column entity-type name-column location-column)
visible-filter (visible-entities-filter-clause (name entity-type) :entity.id
- {:include-archived-items include-archived-items})
+ (when (= query-type :breaking)
+ {:include-archived-items :all}))
all-required-joins (set/union filter-joins sort-joins)
select-clause [[[:inline (name entity-type)] :entity_type]
[:entity.id :entity_id]
@@ -974,7 +830,6 @@
(ms/enum-decode-keyword lib.schema.metadata/card-types)
[:sequential (ms/enum-decode-keyword lib.schema.metadata/card-types)]]]
[:query {:optional true} :string]
- [:archived {:optional true} :boolean]
[:include-personal-collections {:optional true} :boolean]
[:sort-column {:optional true} (ms/enum-decode-keyword breaking-items-sort-columns)]
[:sort-direction {:optional true} (ms/enum-decode-keyword sort-directions)]])
@@ -994,7 +849,6 @@
- `types`: List of entity types to include (e.g., [:card :transform :snippet :dashboard])
- `card-types`: List of card types to include when filtering cards (e.g., [:question :model :metric])
- `query`: Search string to filter by name or location
- - `archived`: Controls whether archived entities are included
- `include-personal-collections`: Controls whether items in personal collections are included (default: false)
- `sort-column`: Sort column - `:name`, `:location`, `:dependents-errors`, or `:dependents-with-errors` (default: `:name`)
- `sort-direction`: Sort direction - `:asc` or `:desc` (default: `:asc`)
@@ -1007,7 +861,7 @@
- `offset`: Applied offset
- `limit`: Applied limit"
[_route-params
- {:keys [types card-types query archived include-personal-collections sort-column sort-direction]
+ {:keys [types card-types query include-personal-collections sort-column sort-direction]
:or {types (vec deps.dependency-types/dependency-types)
card-types (vec lib.schema.metadata/card-types)
include-personal-collections false
@@ -1015,8 +869,6 @@
sort-direction :asc}} :- dependency-items-args]
(let [offset (or (request/offset) 0)
limit (or (request/limit) 50)
- include-archived-items (if archived :all :exclude)
- graph-opts {:include-archived-items include-archived-items}
selected-types (cond->> (if (sequential? types) types [types])
;; Sandboxes don't support query filtering, so exclude them when a query is provided
query (remove #{:sandbox}))
@@ -1025,7 +877,6 @@
:entity-type %
:card-types card-types
:query query
- :include-archived-items include-archived-items
:include-personal-collections include-personal-collections
:sort-column sort-column})
selected-types)
@@ -1036,7 +887,7 @@
:limit limit))
(map (fn [{:keys [entity_id entity_type]}]
[(keyword entity_type) entity_id])))
- downstream-graph (graph/cached-graph (readable-graph-dependents graph-opts))
+ downstream-graph (graph/cached-graph (readable-graph-dependents))
total (-> (t2/query {:select [[:%count.* :total]]
:from [[union-query :subquery]]})
first
@@ -1055,7 +906,6 @@
- `types`: List of source entity types - only `:card` or `:table` (default: both)
- `card-types`: List of card types to include when filtering cards (e.g., `[:question :model :metric]`)
- `query`: Search string to filter by name or location
- - `archived`: Controls whether archived entities are included
- `include-personal-collections`: Controls whether items in personal collections are included (default: false)
- `sort-column`: Sort column - `:name`, `:location`, `:dependents-errors`, or `:dependents-with-errors` (default: `:name`)
- `sort-direction`: Sort direction - `:asc` or `:desc` (default: `:asc`)
@@ -1068,7 +918,7 @@
- `offset`: Applied offset
- `limit`: Applied limit"
[_route-params
- {:keys [types card-types query archived include-personal-collections sort-column sort-direction]
+ {:keys [types card-types query include-personal-collections sort-column sort-direction]
:or {types [:card :table]
card-types (vec lib.schema.metadata/card-types)
include-personal-collections false
@@ -1076,8 +926,6 @@
sort-direction :asc}} :- dependency-items-args]
(let [offset (or (request/offset) 0)
limit (or (request/limit) 50)
- include-archived-items (if archived :all :exclude)
- graph-opts {:include-archived-items include-archived-items}
selected-types (cond->> (if (sequential? types) types [types])
;; Sandboxes don't support query filtering, so exclude them when a query is provided
query (remove #{:sandbox}))
@@ -1086,7 +934,6 @@
:entity-type %
:card-types card-types
:query query
- :include-archived-items include-archived-items
:include-personal-collections include-personal-collections
:sort-column sort-column})
selected-types)
@@ -1097,7 +944,7 @@
:limit limit))
(map (fn [{:keys [entity_id entity_type]}]
[(keyword entity_type) entity_id])))
- downstream-graph (graph/cached-graph (readable-graph-dependents graph-opts))
+ downstream-graph (graph/cached-graph (readable-graph-dependents))
nodes-by-type (u/group-by first second all-ids)
downstream-errors (node-downstream-errors nodes-by-type)
total (-> (t2/query {:select [[:%count.* :total]]
diff --git a/enterprise/backend/src/metabase_enterprise/dependencies/dependency_types.clj b/enterprise/backend/src/metabase_enterprise/dependencies/dependency_types.clj
index 458a06534de..7f20139b543 100644
--- a/enterprise/backend/src/metabase_enterprise/dependencies/dependency_types.clj
+++ b/enterprise/backend/src/metabase_enterprise/dependencies/dependency_types.clj
@@ -24,6 +24,11 @@
"The set of all dependency types."
(-> model->dependency-type vals set))
+(def backfillable-dependency-types
+ "Dependency types that the backfill job computes dependencies for. Excludes `:table`: tables aren't
+ backfilled directly; links involving tables are found via analysis of the other side of the relation."
+ (disj dependency-types :table))
+
(def models
"The set of all models that are handled by dependencies."
(-> model->dependency-type keys set))
diff --git a/enterprise/backend/src/metabase_enterprise/dependencies/models/analysis_finding.clj b/enterprise/backend/src/metabase_enterprise/dependencies/models/analysis_finding.clj
index 9adbe4997ad..d6961728a32 100644
--- a/enterprise/backend/src/metabase_enterprise/dependencies/models/analysis_finding.clj
+++ b/enterprise/backend/src/metabase_enterprise/dependencies/models/analysis_finding.clj
@@ -26,8 +26,9 @@
- 4: Disable naive sql validation
- 5: Added source entity tracking in analysis_finding_error table
- 6: Removed validate prefix from error_type in analysis_finding_error
- - 7: Only mark inactive (not missing) field refs in :fields as soft (GHY-3157)"
- 7)
+ - 7: Only mark inactive (not missing) field refs in :fields as soft (GHY-3157)
+ - 8: Flag MBQL queries whose source-table is missing/inactive (e.g. dropped during sync)"
+ 8)
(defn- error->finding-error-row
"Convert an error from lib/find-bad-refs-with-source to a row for analysis_finding_error table.
diff --git a/enterprise/backend/src/metabase_enterprise/dependencies/models/dependency_status.clj b/enterprise/backend/src/metabase_enterprise/dependencies/models/dependency_status.clj
index ade8ddfa90a..46dfea30e68 100644
--- a/enterprise/backend/src/metabase_enterprise/dependencies/models/dependency_status.clj
+++ b/enterprise/backend/src/metabase_enterprise/dependencies/models/dependency_status.clj
@@ -56,7 +56,7 @@
(defn instances-for-dependency-calculation
"Find a batch of instances of type `entity-type` and maximum size `batch-size` that need
- dependency calculation: stale=true OR version < current.
+ dependency calculation: no status row yet, stale=true, OR version < current.
Excludes terminal entities and entities whose retry delay hasn't elapsed.
Returns full entity objects. Prioritizes stale over outdated.
Uses Java time (not DB time) so tests with [[mt/with-clock]] work correctly."
@@ -69,21 +69,24 @@
(t2/select model
{:select [table-wildcard]
:from table-name
- :inner-join [:dependency_status [:and
- [:= :dependency_status.entity_id id-field]
- [:= :dependency_status.entity_type (name entity-type)]]]
- :where [:and
- ;; Needs processing: stale or version outdated
- [:or
- [:= :dependency_status.stale true]
- [:< :dependency_status.dependency_analysis_version
- models.dependency/current-dependency-analysis-version]]
- ;; Not terminally broken
- [:= :dependency_status.terminal false]
- ;; Retry delay has elapsed (or no delay set)
- [:or
- [:is :dependency_status.next_retry_at nil]
- [:<= :dependency_status.next_retry_at now]]]
+ :left-join [:dependency_status [:and
+ [:= :dependency_status.entity_id id-field]
+ [:= :dependency_status.entity_type (name entity-type)]]]
+ :where [:or
+ ;; No status row yet — needs initial processing.
+ [:= :dependency_status.entity_id nil]
+ [:and
+ ;; Needs processing: stale or version outdated
+ [:or
+ [:= :dependency_status.stale true]
+ [:< :dependency_status.dependency_analysis_version
+ models.dependency/current-dependency-analysis-version]]
+ ;; Not terminally broken
+ [:= :dependency_status.terminal false]
+ ;; Retry delay has elapsed (or no delay set)
+ [:or
+ [:is :dependency_status.next_retry_at nil]
+ [:<= :dependency_status.next_retry_at now]]]]
:order-by [[[:case [:= :dependency_status.stale true] [:inline 0] :else [:inline 1]]]]
:limit batch-size})))
@@ -95,20 +98,14 @@
:next_retry_at [:not= nil]))
(defn has-stale-or-outdated?
- "Returns true if there are any entities needing dependency calculation:
- stale=true OR version < current, not terminal, and retry delay elapsed."
+ "Returns true if there are any entities needing dependency calculation: no status row yet,
+ stale=true, OR version < current (not terminal, retry delay elapsed). Defined in terms of
+ [[instances-for-dependency-calculation]] so it stays consistent with what the backfill processes."
[]
- (let [now (t/offset-date-time)]
- (t2/exists? :model/DependencyStatus
- {:where [:and
- [:or
- [:= :stale true]
- [:< :dependency_analysis_version
- models.dependency/current-dependency-analysis-version]]
- [:= :terminal false]
- [:or
- [:is :next_retry_at nil]
- [:<= :next_retry_at now]]]})))
+ (boolean
+ (some (fn [entity-type]
+ (seq (instances-for-dependency-calculation entity-type 1)))
+ deps.dependency-types/backfillable-dependency-types)))
(defn record-failure!
"Record a failed dependency calculation attempt for an entity.
diff --git a/enterprise/backend/src/metabase_enterprise/dependencies/task/backfill.clj b/enterprise/backend/src/metabase_enterprise/dependencies/task/backfill.clj
index 54b9b45acd4..0818142cc07 100644
--- a/enterprise/backend/src/metabase_enterprise/dependencies/task/backfill.clj
+++ b/enterprise/backend/src/metabase_enterprise/dependencies/task/backfill.clj
@@ -12,6 +12,7 @@
[clojurewerkz.quartzite.triggers :as triggers]
[java-time.api :as t]
[metabase-enterprise.dependencies.calculation :as deps.calculation]
+ [metabase-enterprise.dependencies.dependency-types :as deps.dependency-types]
[metabase-enterprise.dependencies.models.dependency :as models.dependency]
[metabase-enterprise.dependencies.models.dependency-status :as deps.dependency-status]
[metabase-enterprise.dependencies.settings :as deps.settings]
@@ -32,7 +33,7 @@
This is not the same as deps.dependency-types/dependency-types, because tables shouldn't be backfilled. Instead, links
involving tables are found via analysis of the other side of the relation."
- [:card :transform :snippet :dashboard :document :sandbox :segment :measure])
+ (vec deps.dependency-types/backfillable-dependency-types))
(def ^:private max-retries 5)
diff --git a/enterprise/backend/test/metabase_enterprise/dependencies/api_test.clj b/enterprise/backend/test/metabase_enterprise/dependencies/api_test.clj
index 2eb98bbbe5b..5245c0694d8 100644
--- a/enterprise/backend/test/metabase_enterprise/dependencies/api_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/dependencies/api_test.clj
@@ -4,7 +4,6 @@
[medley.core :as m]
[metabase-enterprise.dependencies.api :as deps.api]
[metabase-enterprise.dependencies.async :as dependencies.async]
- [metabase-enterprise.dependencies.core :as dependencies]
[metabase-enterprise.dependencies.events]
[metabase-enterprise.dependencies.findings :as dependencies.findings]
[metabase-enterprise.dependencies.test-util :as deps.test]
@@ -15,7 +14,6 @@
[metabase.lib-be.core :as lib-be]
[metabase.lib.core :as lib]
[metabase.lib.metadata :as lib.metadata]
- [metabase.native-query-snippets.models.native-query-snippet.permissions :as snippet.perms]
[metabase.permissions.core :as perms]
[metabase.permissions.models.permissions-group :as perms-group]
[metabase.queries.models.card :as card]
@@ -77,13 +75,6 @@
(lib/with-join-fields :all))]
(card-with-query "Card with join" (lib/join base-query join-clause))))
-(defn broken-mbql-query
- "Construct a broken MBQL query that references a field from a wrong table."
- []
- (let [mp (mt/metadata-provider)]
- (-> (lib/query mp (lib.metadata/table mp (mt/id :products)))
- (lib/aggregate (lib/sum (lib.metadata/field mp (mt/id :orders :total)))))))
-
(defn- create-model-card!
"Create a model card with the given name and optional collection-id.
Returns the created card."
@@ -117,6 +108,15 @@
collection-id (assoc :collection_id collection-id))
user)))
+(defn- edge-present?
+ "Returns true if `edges` contains an edge from (`from-type` `from-id`) to (`to-type` `to-id`)."
+ [edges from-type from-id to-type to-id]
+ (boolean (some #(and (= (:from_entity_type %) from-type)
+ (= (:from_entity_id %) from-id)
+ (= (:to_entity_type %) to-type)
+ (= (:to_entity_id %) to-id))
+ edges)))
+
(defn- break-model-card!
"Update a model card to query the products table instead of orders.
This breaks any downstream cards that depend on columns only in the orders table (like TOTAL)."
@@ -145,277 +145,6 @@
;; work completes before model cleanup deletes cards, avoiding lock timeouts.
@(dependencies.async/submit! (fn [] nil))))))
-(deftest check-card-test
- (testing "POST /api/ee/dependencies/check-card"
- (mt/with-premium-features #{:dependencies}
- (mt/with-temp [:model/User user {:email "[email protected]"}]
- (mt/with-model-cleanup [:model/Card :model/Dependency :model/DependencyStatus]
- (let [card (card/create-card! (basic-card) user)
- _ (deps.test/synchronously-run-backfill!)
- response (mt/user-http-request :rasta :post 200 "ee/dependencies/check-card"
- (assoc (card/create-card! (basic-card "Product question" :products)
- user)
- :id (:id card)))]
- (is (= {:bad_cards [], :bad_transforms [], :success true}
- response))))))))
-
-(deftest check-card-hydrates-dashboard-and-document-test
- (testing "POST /api/ee/dependencies/check-card hydrates dashboard and document for cards"
- (mt/dataset test-data
- (mt/with-premium-features #{:dependencies}
- (mt/with-current-user (mt/user->id :rasta)
- (mt/with-temp [:model/User user {:email "[email protected]"}
- :model/Dashboard dashboard {}
- :model/Document document {}]
- (mt/with-model-cleanup [:model/Card :model/Dependency :model/DependencyStatus]
- (let [metadata-provider (mt/metadata-provider)
- ;; Create base card querying real orders table
- base-card (card/create-card! (basic-card) user)
- ;; Create dependent cards that filter on TOTAL
- base-card-meta (lib.metadata/card metadata-provider (:id base-card))
- dependent-query (let [q (lib/query metadata-provider base-card-meta)
- cols (lib/filterable-columns q)
- total-col (m/find-first #(= (:id %) (mt/id :orders :total)) cols)]
- (lib/filter q (lib/> total-col 100)))
- dashboard-card (card/create-card!
- (merge (card-with-query "Dashboard card" dependent-query) {:dashboard_id (:id dashboard)})
- user)
- document-card (card/create-card!
- (merge (card-with-query "Document card" dependent-query) {:document_id (:id document)})
- user)
- ;; Propose changing to products table (doesn't have TOTAL column, breaks downstream)
- proposed-query (lib/query metadata-provider (lib.metadata/table metadata-provider (mt/id :products)))
- proposed-card {:id (:id base-card)
- :type :question
- :dataset_query proposed-query
- :result_metadata nil}]
- (deps.test/synchronously-run-backfill!)
- (is (=? {:success false
- :bad_cards [{:id (:id dashboard-card)
- :dashboard_id (:id dashboard)
- :dashboard (select-keys dashboard [:id :name])}
- {:id (:id document-card)
- :document_id (:id document)
- :document (select-keys document [:id :name])}]
- :bad_transforms []}
- (mt/user-http-request :rasta :post 200 "ee/dependencies/check-card" proposed-card)))))))))))
-
-(deftest check-card-removing-column-breaks-downstream-test
- (testing "POST /api/ee/dependencies/check-card detects when removing a column breaks downstream cards"
- (mt/dataset test-data
- (mt/with-premium-features #{:dependencies}
- (mt/with-temp [:model/User user {:email "[email protected]"}]
- (mt/with-model-cleanup [:model/Card :model/Dependency :model/DependencyStatus]
- (let [mp (mt/metadata-provider)
- ;; Create base card querying real orders table
- base-card (card/create-card! (basic-card) user)
- ;; Create dependent card that filters on TOTAL
- base-card-meta (lib.metadata/card mp (:id base-card))
- dependent-query (let [q (lib/query mp base-card-meta)
- cols (lib/filterable-columns q)
- total-col (m/find-first #(= (:id %) (mt/id :orders :total)) cols)]
- (lib/filter q (lib/> total-col 100)))
- dependent-card (card/create-card!
- (card-with-query "Dependent Card filtering on Total" dependent-query)
- user)
- ;; Propose changing to products table (doesn't have TOTAL column, breaks downstream)
- proposed-query (lib/query mp (lib.metadata/table mp (mt/id :products)))
- proposed-card {:id (:id base-card)
- :type :question
- :dataset_query proposed-query
- :result_metadata nil}
- _ (deps.test/synchronously-run-backfill!)
- response (mt/user-http-request :rasta :post 200 "ee/dependencies/check-card" proposed-card)]
- (is (=? {:success false
- :bad_cards [{:id (:id dependent-card)}]
- :bad_transforms []}
- response)))))))))
-
-(deftest check-card-renaming-expression-breaks-downstream-test
- (testing "POST /api/ee/dependencies/check-card detects when renaming an expression breaks downstream cards"
- (mt/dataset test-data
- (mt/with-premium-features #{:dependencies}
- (mt/with-temp [:model/User user {:email "[email protected]"}]
- (mt/with-model-cleanup [:model/Card :model/Dependency :model/DependencyStatus]
- (let [mp (mt/metadata-provider)
- orders-query (lib/query mp (lib.metadata/table mp (mt/id :orders)))
- base-query (-> orders-query
- (lib/expression "Tax Rate"
- (lib// (lib.metadata/field mp (mt/id :orders :tax))
- (lib.metadata/field mp (mt/id :orders :subtotal)))))
- base-card (card/create-card!
- (card-with-query "Base Card with Tax Rate" base-query)
- user)
- dependent-query (let [q (lib/query mp (lib.metadata/card mp (:id base-card)))
- cols (lib/filterable-columns q)
- tax-rate-col (m/find-first #(= (:lib/deduplicated-name %) "Tax Rate") cols)]
- (lib/filter q (lib/> tax-rate-col 0.06)))
- dependent-card (card/create-card!
- (card-with-query "Dependent Card filtering on Tax Rate" dependent-query)
- user)
- ;; renaming "Tax Rate" to "Sales Tax" should break downstream reference
- proposed-query (-> orders-query
- (lib/expression "Sales Tax"
- (lib// (lib.metadata/field mp (mt/id :orders :tax))
- (lib.metadata/field mp (mt/id :orders :subtotal)))))
- proposed-card {:id (:id base-card)
- :type :question
- :dataset_query proposed-query
- :result_metadata nil}
- _ (deps.test/synchronously-run-backfill!)
- response (mt/user-http-request :rasta :post 200 "ee/dependencies/check-card"
- proposed-card)]
- (is (=? {:success false
- :bad_cards [{:id (:id dependent-card)}]
- :bad_transforms []}
- response)))))))))
-
-(deftest check-card-breaks-multiple-downstream-cards-test
- (testing "POST /api/ee/dependencies/check-card detects when one change breaks multiple downstream cards"
- (mt/dataset test-data
- (mt/with-premium-features #{:dependencies}
- (mt/with-temp [:model/User user {:email "[email protected]"}]
- (mt/with-model-cleanup [:model/Card :model/Dependency :model/DependencyStatus]
- (let [mp (mt/metadata-provider)
- ;; Create base card querying real orders table
- base-card (card/create-card! (basic-card) user)
- ;; Create first dependent card filtering on TOTAL
- base-card-meta (lib.metadata/card mp (:id base-card))
- dependent-query-1 (let [q (lib/query mp base-card-meta)
- total-col (m/find-first #(= (:id %) (mt/id :orders :total))
- (lib/filterable-columns q))]
- (lib/filter q (lib/> total-col 100)))
- dependent-card-1 (card/create-card!
- (card-with-query "First Dependent Card" dependent-query-1)
- user)
- ;; Create second dependent card also using TOTAL
- dependent-query-2 (let [q (lib/query mp base-card-meta)
- total-col (m/find-first #(= (:id %) (mt/id :orders :total))
- (lib/filterable-columns q))]
- (lib/filter q (lib/< total-col 50)))
- dependent-card-2 (card/create-card!
- (card-with-query "Second Dependent Card" dependent-query-2)
- user)
- ;; Propose changing to products table (no TOTAL, breaks both downstream cards)
- proposed-query (lib/query mp (lib.metadata/table mp (mt/id :products)))
- proposed-card {:id (:id base-card)
- :type :question
- :dataset_query proposed-query
- :result_metadata nil}
- _ (deps.test/synchronously-run-backfill!)
- response (mt/user-http-request :rasta :post 200 "ee/dependencies/check-card" proposed-card)]
- (is (=? {:success false
- :bad_cards #{(:id dependent-card-1) (:id dependent-card-2)}
- :bad_transforms []}
- (update response :bad_cards #(into #{} (map :id) %)))))))))))
-
-(deftest check-card-skips-native-cards-test
- (testing "POST /api/ee/dependencies/check-card does not validate native cards"
- (mt/dataset test-data
- (mt/with-premium-features #{:dependencies}
- (mt/with-temp [:model/User user {:email "[email protected]"}]
- (mt/with-model-cleanup [:model/Card :model/Dependency :model/DependencyStatus]
- (let [mp (mt/metadata-provider)
- ;; Create base card querying real orders table
- base-card (card/create-card! (basic-card) user)
- ;; Create dependent card that filters on TOTAL
- dependent-query (lib/native-query mp (str "select * from {{#"
- (:id base-card)
- "}} orders where total > 100"))
- dependent-card (card/create-card!
- (card-with-query "Dependent Card filtering on Total" dependent-query)
- user)
- ;; Propose changing to products table (doesn't have TOTAL column, breaks downstream)
- proposed-query (lib/query mp (lib.metadata/table mp (mt/id :products)))
- proposed-card {:id (:id base-card)
- :type :question
- :dataset_query proposed-query
- :result_metadata nil}
- _ (deps.test/synchronously-run-backfill!)
- response (mt/user-http-request :rasta :post 200 "ee/dependencies/check-card" proposed-card)]
- (is (=? {:success false
- :bad_cards [{:id (:id dependent-card)}]
- :bad_transforms []}
- response)))))))))
-
-(deftest check-card-native-explicit-columns-not-broken-test
- (testing "POST /api/ee/dependencies/check-card does not report a native card (or its consumer) as broken when an
- edit switches SELECT * to an explicit, valid column list yielding the same columns (GHY-3682)"
- (mt/dataset test-data
- (mt/with-premium-features #{:dependencies}
- (mt/with-temp [:model/User user {:email "[email protected]"}]
- (mt/with-model-cleanup [:model/Card :model/Dependency :model/DependencyStatus]
- (let [mp (mt/metadata-provider)
- ;; Native source card originally selecting all columns.
- source-card (card/create-card!
- (card-with-query "Products source"
- (lib/native-query mp "SELECT * FROM products"))
- user)
- ;; A downstream question that consumes the source card.
- consumer-query (lib/native-query mp (str "select * from {{#" (:id source-card) "}} src"))
- consumer-card (card/create-card!
- (card-with-query "Consumer" consumer-query)
- user)
- ;; Propose editing the source to an explicit column list that yields the same columns.
- proposed-card {:id (:id source-card)
- :type :question
- :dataset_query (lib/native-query mp "SELECT id, category, price FROM products")
- :result_metadata nil}
- _ (deps.test/synchronously-run-backfill!)
- response (mt/user-http-request :rasta :post 200 "ee/dependencies/check-card" proposed-card)]
- (is (some? consumer-card))
- (is (= {:success true :bad_cards [] :bad_transforms []}
- response)
- "Switching a native card to explicit valid columns must not report the card itself or its consumer as broken"))))))))
-
-(deftest check-transform-test
- (testing "POST /api/ee/dependencies/check-transform"
- (mt/with-premium-features #{:dependencies :transforms-basic}
- (mt/with-temp [:model/Transform {_transform-id :id :as transform} {}]
- (let [response (mt/user-http-request :crowberto :post 200 "ee/dependencies/check-transform" transform)]
- (is (= {:bad_cards [], :bad_transforms [], :success true}
- response)))))))
-
-(deftest check-snippet-test
- (testing "POST /api/ee/dependencies/check-snippet"
- (mt/with-premium-features #{:dependencies}
- (mt/with-temp [:model/NativeQuerySnippet {_snippet-id :id :as snippet} {:name "test-snippet"
- :content "WHERE ID > 10"}]
- (let [response (mt/user-http-request :crowberto :post 200 "ee/dependencies/check-snippet" snippet)]
- (is (= {:bad_cards [], :bad_transforms [], :success true}
- response)))))))
-
-(deftest check-snippet-content-change-doest-break-cards-test
- (testing "POST /api/ee/dependencies/check-snippet catches when a change would break a card"
- (mt/dataset test-data
- (mt/with-premium-features #{:dependencies}
- (mt/with-temp [:model/User user {:email "[email protected]"}
- :model/NativeQuerySnippet {snippet-id :id snippet-name :name} {:name "filter-snippet"
- :content "WHERE SUBTOTAL > 100"}]
- (mt/with-model-cleanup [:model/Card :model/Dependency :model/DependencyStatus]
- (let [tag-name (str "snippet: " snippet-name)
- mp (mt/metadata-provider)
- native-query (-> (lib/native-query mp (format "SELECT * FROM ORDERS %s" (str "{{" tag-name "}}")))
- (lib/with-template-tags {tag-name {:name tag-name
- :display-name (str "Snippet: " snippet-name)
- :type :snippet
- :snippet-name snippet-name
- :snippet-id snippet-id}}))
- card (card/create-card! {:name "Card using snippet"
- :dataset_query native-query
- :display :table
- :visualization_settings {}}
- user)
- _ (deps.test/synchronously-run-backfill!)
- proposed-content "WHERE NONEXISTENT_COLUMN > 100"
- response (mt/user-http-request :rasta :post 200 "ee/dependencies/check-snippet"
- {:id snippet-id
- :content proposed-content})]
- (is (=? {:success false
- :bad_cards [{:id (:id card)}]
- :bad_transforms []}
- response)))))))))
-
(deftest graph-test
(testing "GET /api/ee/dependencies/graph"
(mt/with-premium-features #{:dependencies}
@@ -609,7 +338,7 @@
(is (every? #(= "dashboard" (:type %)) response))))))))))
(deftest graph-archived-card-test
- (testing "GET /api/ee/dependencies/graph with archived parameter"
+ (testing "GET /api/ee/dependencies/graph always includes archived upstream entities"
(mt/with-premium-features #{:dependencies}
(mt/with-model-cleanup [:model/Card :model/Dependency :model/DependencyStatus]
(mt/with-temp [:model/User user {:email "[email protected]"}]
@@ -618,25 +347,43 @@
(card/update-card! {:card-before-update base-card
:card-updates {:archived true}})
(deps.test/synchronously-run-backfill!)
- (testing "archived=false (default) excludes archived card from dependencies"
- (let [response (mt/user-http-request :rasta :get 200 "ee/dependencies/graph"
- :id (:id dependent-card)
- :type "card")
- node-ids (set (map :id (:nodes response)))]
- (is (contains? node-ids (:id dependent-card)))
- (is (not (contains? node-ids (:id base-card))))))
- (testing "archived=true includes archived card in dependencies"
- (let [response (mt/user-http-request :rasta :get 200 "ee/dependencies/graph"
- :id (:id dependent-card)
- :type "card"
- :archived true)
- node-ids (set (map :id (:nodes response)))]
- (is (contains? node-ids (:id dependent-card)))
+ (let [response (mt/user-http-request :rasta :get 200 "ee/dependencies/graph"
+ :id (:id dependent-card)
+ :type "card")
+ node-ids (set (map :id (:nodes response)))]
+ (is (contains? node-ids (:id dependent-card)))
+ (is (contains? node-ids (:id base-card)))
+ (is (contains? node-ids (mt/id :orders))))))))))
+
+(deftest graph-archived-upstream-edges-test
+ (testing "GET /api/ee/dependencies/graph CONNECTS archived upstream entities with edges, not just nodes"
+ ;; Chain: dependent-card (active) -> base-card (archived) -> orders (table).
+ ;; The archived base-card is the *dependent* side of the base-card->orders edge, so a
+ ;; downstream graph built with :include-archived-items :exclude drops that edge, leaving
+ ;; the orders node present but disconnected. Asserts both edges survive.
+ (mt/with-premium-features #{:dependencies}
+ (mt/with-model-cleanup [:model/Card :model/Dependency :model/DependencyStatus]
+ (mt/with-temp [:model/User user {:email "[email protected]"}]
+ (let [base-card (card/create-card! (basic-card "Archived Base Card") user)
+ dependent-card (card/create-card! (wrap-card base-card) user)]
+ (card/update-card! {:card-before-update base-card
+ :card-updates {:archived true}})
+ (deps.test/synchronously-run-backfill!)
+ (let [response (mt/user-http-request :rasta :get 200 "ee/dependencies/graph"
+ :id (:id dependent-card)
+ :type "card")
+ node-ids (set (map :id (:nodes response)))
+ edges (:edges response)]
+ (testing "archived upstream nodes are present"
(is (contains? node-ids (:id base-card)))
- (is (contains? node-ids (mt/id :orders)))))))))))
+ (is (contains? node-ids (mt/id :orders))))
+ (testing "edge from active dependent to archived base card"
+ (is (edge-present? edges "card" (:id dependent-card) "card" (:id base-card))))
+ (testing "edge from archived base card to its upstream table is NOT dropped"
+ (is (edge-present? edges "card" (:id base-card) "table" (mt/id :orders)))))))))))
(deftest dependents-archived-card-test
- (testing "GET /api/ee/dependencies/graph/dependents with archived parameter"
+ (testing "GET /api/ee/dependencies/graph/dependents excludes archived dependents"
(mt/with-premium-features #{:dependencies}
(mt/with-model-cleanup [:model/Card :model/Dependency :model/DependencyStatus]
(mt/with-temp [:model/User user {:email "[email protected]"}]
@@ -645,21 +392,11 @@
(card/update-card! {:card-before-update dependent-card
:card-updates {:archived true}})
(deps.test/synchronously-run-backfill!)
- (testing "archived=false (default) excludes archived dependent"
- (let [response (mt/user-http-request :rasta :get 200 "ee/dependencies/graph/dependents"
- :id (:id base-card)
- :type "card"
- :dependent-types "card")]
- (is (empty? response))))
- (testing "archived=true includes archived dependent"
- (let [response (mt/user-http-request :rasta :get 200 "ee/dependencies/graph/dependents"
- :id (:id base-card)
- :type "card"
- :dependent-types "card"
- :dependent-card-types "question"
- :archived true)
- dependent-ids (set (map :id response))]
- (is (contains? dependent-ids (:id dependent-card)))))))))))
+ (let [response (mt/user-http-request :rasta :get 200 "ee/dependencies/graph/dependents"
+ :id (:id base-card)
+ :type "card"
+ :dependent-types "card")]
+ (is (empty? response)))))))))
(deftest ^:sequential dependents-broken-parameter-test
(testing "GET /api/ee/dependencies/graph/dependents?broken=true - only returns entities that are broken"
@@ -685,88 +422,6 @@
(is (= #{(:id dependent-card) (:id next-card)} (set (map :id response2)))
"There should two dependents total"))))))))))
-(deftest check-card-permissions-test
- (testing "POST /api/ee/dependencies/check-card requires read permissions on the input card"
- (mt/with-premium-features #{:dependencies}
- (mt/with-non-admin-groups-no-root-collection-perms
- (mt/with-temp [:model/Collection collection {}
- :model/User user {:email "[email protected]"}]
- (mt/with-model-cleanup [:model/Card :model/Dependency :model/DependencyStatus]
- (let [card (card/create-card! (assoc (basic-card) :collection_id (u/the-id collection)) user)]
- (deps.test/synchronously-run-backfill!)
- (testing "Returns 403 when user lacks read permissions"
- (is (= "You don't have permissions to do that."
- (mt/user-http-request :rasta :post 403 "ee/dependencies/check-card"
- (assoc card :name "Modified name")))))
- (testing "Returns 200 when user has read permissions"
- (perms/grant-collection-read-permissions! (perms/all-users-group) collection)
- (is (=? {:success true}
- (mt/user-http-request :rasta :post 200 "ee/dependencies/check-card"
- (assoc card :name "Modified name"))))))))))))
-
-(deftest check-snippet-permissions-test
- (testing "POST /api/ee/dependencies/check-snippet requires native query execution permissions"
- (mt/with-premium-features #{:dependencies}
- (mt/with-non-admin-groups-no-root-collection-perms
- (mt/with-temp [:model/NativeQuerySnippet snippet {:name "test snippet"
- :content "SELECT 1"
- :creator_id (mt/user->id :crowberto)}]
- ;; remove native permissions
- (mt/with-dynamic-fn-redefs [snippet.perms/has-any-native-permissions? (constantly false)]
- (testing "Returns 403 when user lacks native query execution permissions"
- (is (= "You don't have permissions to do that."
- (mt/user-http-request :rasta :post 403 "ee/dependencies/check-snippet"
- {:id (:id snippet)
- :content "SELECT 2"})))))
- ;; Grant native query permissions
- (testing "Returns 200 when user has native query execution permissions"
- (mt/user-http-request :rasta :post 200 "ee/dependencies/check-snippet"
- {:id (:id snippet)
- :content "SELECT 2"})))))))
-
-(deftest check-transform-permissions-test
- (testing "POST /api/ee/dependencies/check-transform requires read permissions on the input transform"
- (mt/with-premium-features #{:dependencies :transforms-basic}
- (mt/with-temp [:model/Transform transform {:name "test transform"}]
- (testing "Returns 403 when user is not an admin (only admins can read transforms)"
- (is (= "You don't have permissions to do that."
- (mt/user-http-request :rasta :post 403 "ee/dependencies/check-transform"
- {:id (:id transform)
- :source (:source transform)
- :target (:target transform)}))))
- (testing "Returns 200 when user is an admin"
- (is (=? {:success true}
- (mt/user-http-request :crowberto :post 200 "ee/dependencies/check-transform"
- {:id (:id transform)
- :source (:source transform)
- :target (:target transform)}))))))))
-
-(deftest check-card-bad-transforms-filtered-by-can-read-test
- (testing "POST /api/ee/dependencies/check-card filters bad_transforms by mi/can-read?"
- (mt/dataset test-data
- (mt/with-premium-features #{:dependencies :transforms-basic}
- (mt/with-temp [:model/User user {:email "[email protected]"}
- :model/Transform transform {}]
- (mt/with-model-cleanup [:model/Card :model/Dependency :model/DependencyStatus]
- (let [base-card (card/create-card! (basic-card) user)
- proposed-card {:id (:id base-card)
- :type :question
- :dataset_query (:dataset_query base-card)}]
- (deps.test/synchronously-run-backfill!)
- ;; Mock errors-from-proposed-edits to report the transform as broken
- (mt/with-dynamic-fn-redefs [dependencies/errors-from-proposed-edits
- (constantly {:transform {(:id transform) #{:some-error}}})]
- (testing "Admin sees broken transforms in response"
- (let [response (mt/user-http-request :crowberto :post 200 "ee/dependencies/check-card"
- proposed-card)]
- (is (false? (:success response)))
- (is (=? [{:id (:id transform)}]
- (:bad_transforms response)))))
- (testing "Non-admin user cannot see broken transforms they lack read permission for"
- (let [response (mt/user-http-request :rasta :post 200 "ee/dependencies/check-card"
- proposed-card)]
- (is (= [] (:bad_transforms response)))))))))))))
-
(deftest graph-permissions-test
(testing "GET /api/ee/dependencies/graph requires read permissions on the starting entity"
(mt/with-premium-features #{:dependencies}
@@ -1562,16 +1217,10 @@
:archived true
:dataset_query (lib/query mp products)}]
(deps.test/synchronously-run-backfill!)
- (testing "archived=false (default) excludes archived card"
- (let [response (mt/user-http-request :crowberto :get 200 "ee/dependencies/graph/unreferenced?types=card&card-types=question&query=archivedtest")
- card-ids (set (map :id (:data response)))]
- (is (contains? card-ids unreffed-card-id))
- (is (not (contains? card-ids archived-card-id)))))
- (testing "archived=true includes archived card"
- (let [response (mt/user-http-request :crowberto :get 200 "ee/dependencies/graph/unreferenced?types=card&card-types=question&query=archivedtest&archived=true")
- card-ids (set (map :id (:data response)))]
- (is (contains? card-ids unreffed-card-id))
- (is (contains? card-ids archived-card-id)))))))))
+ (let [response (mt/user-http-request :crowberto :get 200 "ee/dependencies/graph/unreferenced?types=card&card-types=question&query=archivedtest")
+ card-ids (set (map :id (:data response)))]
+ (is (contains? card-ids unreffed-card-id))
+ (is (not (contains? card-ids archived-card-id)))))))))
(deftest ^:sequential unreferenced-archived-dashboard-test
(testing "GET /api/ee/dependencies/graph/unreferenced with archived parameter for dashboards"
@@ -1580,16 +1229,10 @@
:model/Dashboard {archived-dashboard-id :id} {:name "Archived Unreferenced Dashboard - archivedtest"
:archived true}]
(deps.test/synchronously-run-backfill!)
- (testing "archived=false (default) excludes archived dashboard"
- (let [response (mt/user-http-request :crowberto :get 200 "ee/dependencies/graph/unreferenced?types=dashboard&query=archivedtest")
- dashboard-ids (set (map :id (:data response)))]
- (is (contains? dashboard-ids unreffed-dashboard-id))
- (is (not (contains? dashboard-ids archived-dashboard-id)))))
- (testing "archived=true includes archived dashboard"
- (let [response (mt/user-http-request :crowberto :get 200 "ee/dependencies/graph/unreferenced?types=dashboard&query=archivedtest&archived=true")
- dashboard-ids (set (map :id (:data response)))]
- (is (contains? dashboard-ids unreffed-dashboard-id))
- (is (contains? dashboard-ids archived-dashboard-id))))))))
+ (let [response (mt/user-http-request :crowberto :get 200 "ee/dependencies/graph/unreferenced?types=dashboard&query=archivedtest")
+ dashboard-ids (set (map :id (:data response)))]
+ (is (contains? dashboard-ids unreffed-dashboard-id))
+ (is (not (contains? dashboard-ids archived-dashboard-id))))))))
(deftest ^:sequential unreferenced-archived-document-test
(testing "GET /api/ee/dependencies/graph/unreferenced with archived parameter for documents"
@@ -1598,16 +1241,10 @@
:model/Document {archived-document-id :id} {:name "Archived Unreferenced Document - archivedtest"
:archived true}]
(deps.test/synchronously-run-backfill!)
- (testing "archived=false (default) excludes archived document"
- (let [response (mt/user-http-request :crowberto :get 200 "ee/dependencies/graph/unreferenced?types=document&query=archivedtest")
- document-ids (set (map :id (:data response)))]
- (is (contains? document-ids unreffed-document-id))
- (is (not (contains? document-ids archived-document-id)))))
- (testing "archived=true includes archived document"
- (let [response (mt/user-http-request :crowberto :get 200 "ee/dependencies/graph/unreferenced?types=document&query=archivedtest&archived=true")
- document-ids (set (map :id (:data response)))]
- (is (contains? document-ids unreffed-document-id))
- (is (contains? document-ids archived-document-id))))))))
+ (let [response (mt/user-http-request :crowberto :get 200 "ee/dependencies/graph/unreferenced?types=document&query=archivedtest")
+ document-ids (set (map :id (:data response)))]
+ (is (contains? document-ids unreffed-document-id))
+ (is (not (contains? document-ids archived-document-id))))))))
(deftest ^:sequential unreferenced-archived-snippet-test
(testing "GET /api/ee/dependencies/graph/unreferenced with archived parameter for snippets"
@@ -1618,16 +1255,10 @@
:content "WHERE ID > 20"
:archived true}]
(deps.test/synchronously-run-backfill!)
- (testing "archived=false (default) excludes archived snippet"
- (let [response (mt/user-http-request :crowberto :get 200 "ee/dependencies/graph/unreferenced?types=snippet&query=archivedtest")
- snippet-ids (set (map :id (:data response)))]
- (is (contains? snippet-ids unreffed-snippet-id))
- (is (not (contains? snippet-ids archived-snippet-id)))))
- (testing "archived=true includes archived snippet"
- (let [response (mt/user-http-request :crowberto :get 200 "ee/dependencies/graph/unreferenced?types=snippet&query=archivedtest&archived=true")
- snippet-ids (set (map :id (:data response)))]
- (is (contains? snippet-ids unreffed-snippet-id))
- (is (contains? snippet-ids archived-snippet-id))))))))
+ (let [response (mt/user-http-request :crowberto :get 200 "ee/dependencies/graph/unreferenced?types=snippet&query=archivedtest")
+ snippet-ids (set (map :id (:data response)))]
+ (is (contains? snippet-ids unreffed-snippet-id))
+ (is (not (contains? snippet-ids archived-snippet-id))))))))
(deftest ^:sequential unreferenced-excludes-internal-content-test
(testing "GET /api/ee/dependencies/graph/unreferenced excludes system-managed (internal-user) content"
@@ -1695,16 +1326,10 @@
(events/publish-event! :event/segment-create {:object unreffed-segment :user-id (mt/user->id :crowberto)})
(events/publish-event! :event/segment-create {:object archived-segment :user-id (mt/user->id :crowberto)})
(deps.test/synchronously-run-backfill!)
- (testing "archived=false (default) excludes archived segment"
- (let [response (mt/user-http-request :crowberto :get 200 "ee/dependencies/graph/unreferenced?types=segment&query=archivedtest")
- segment-ids (set (map :id (:data response)))]
- (is (contains? segment-ids unreffed-segment-id))
- (is (not (contains? segment-ids archived-segment-id)))))
- (testing "archived=true includes archived segment"
- (let [response (mt/user-http-request :crowberto :get 200 "ee/dependencies/graph/unreferenced?types=segment&query=archivedtest&archived=true")
- segment-ids (set (map :id (:data response)))]
- (is (contains? segment-ids unreffed-segment-id))
- (is (contains? segment-ids archived-segment-id)))))))))
+ (let [response (mt/user-http-request :crowberto :get 200 "ee/dependencies/graph/unreferenced?types=segment&query=archivedtest")
+ segment-ids (set (map :id (:data response)))]
+ (is (contains? segment-ids unreffed-segment-id))
+ (is (not (contains? segment-ids archived-segment-id)))))))))
(deftest ^:sequential unreferenced-archived-table-test
(testing "GET /api/ee/dependencies/graph/unreferenced with archived parameter for tables"
@@ -1720,18 +1345,11 @@
:active true
:visibility_type "hidden"}]
(deps.test/synchronously-run-backfill!)
- (testing "archived=false (default) excludes inactive and hidden tables"
- (let [response (mt/user-http-request :crowberto :get 200 "ee/dependencies/graph/unreferenced?types=table&query=archivedtest")
- table-ids (set (map :id (:data response)))]
- (is (contains? table-ids active-table-id))
- (is (not (contains? table-ids inactive-table-id)))
- (is (not (contains? table-ids hidden-table-id)))))
- (testing "archived=true includes inactive and hidden tables"
- (let [response (mt/user-http-request :crowberto :get 200 "ee/dependencies/graph/unreferenced?types=table&query=archivedtest&archived=true")
- table-ids (set (map :id (:data response)))]
- (is (contains? table-ids active-table-id))
- (is (contains? table-ids inactive-table-id))
- (is (contains? table-ids hidden-table-id))))))))
+ (let [response (mt/user-http-request :crowberto :get 200 "ee/dependencies/graph/unreferenced?types=table&query=archivedtest")
+ table-ids (set (map :id (:data response)))]
+ (is (contains? table-ids active-table-id))
+ (is (not (contains? table-ids inactive-table-id)))
+ (is (not (contains? table-ids hidden-table-id))))))))
(deftest ^:sequential unreferenced-pagination-test
(testing "GET /api/ee/dependencies/unreferenced - should paginate results"
@@ -1789,16 +1407,10 @@
(events/publish-event! :event/measure-create {:object unreffed-measure :user-id (mt/user->id :crowberto)})
(events/publish-event! :event/measure-create {:object archived-measure :user-id (mt/user->id :crowberto)})
(deps.test/synchronously-run-backfill!)
- (testing "archived=false (default) excludes archived measure"
- (let [response (mt/user-http-request :crowberto :get 200 "ee/dependencies/graph/unreferenced?types=measure&query=archivedtest")
- measure-ids (set (map :id (:data response)))]
- (is (contains? measure-ids unreffed-measure-id))
- (is (not (contains? measure-ids archived-measure-id)))))
- (testing "archived=true includes archived measure"
- (let [response (mt/user-http-request :crowberto :get 200 "ee/dependencies/graph/unreferenced?types=measure&query=archivedtest&archived=true")
- measure-ids (set (map :id (:data response)))]
- (is (contains? measure-ids unreffed-measure-id))
- (is (contains? measure-ids archived-measure-id)))))))))
+ (let [response (mt/user-http-request :crowberto :get 200 "ee/dependencies/graph/unreferenced?types=measure&query=archivedtest")
+ measure-ids (set (map :id (:data response)))]
+ (is (contains? measure-ids unreffed-measure-id))
+ (is (not (contains? measure-ids archived-measure-id)))))))))
(deftest ^:sequential unreferenced-personal-collection-card-test
(testing "GET /api/ee/dependencies/graph/unreferenced with include-personal-collections parameter for cards"
@@ -2034,7 +1646,7 @@
(is (empty? ids) "No tables should be in results")))))))))
(deftest ^:sequential breaking-entities-archived-card-test
- (testing "GET /api/ee/dependencies/graph/breaking with archived parameter for source cards"
+ (testing "GET /api/ee/dependencies/graph/breaking includes archived source cards that break a non-archived dependent"
(mt/with-premium-features #{:dependencies}
(mt/with-temp [:model/User user {:email "[email protected]"}]
(mt/with-model-cleanup [:model/Card :model/Dependency :model/DependencyStatus :model/AnalysisFinding :model/AnalysisFindingError]
@@ -2058,16 +1670,46 @@
(deps.test/synchronously-run-backfill!)
(run-analysis-for-card! (:id dependent-card-1))
(run-analysis-for-card! (:id dependent-card-2)))
- (testing "archived=false (default) excludes archived source card"
- (let [response (mt/user-http-request :crowberto :get 200 "ee/dependencies/graph/breaking?types=card&query=archivedbrokentestcard")
- card-ids (set (map :id (:data response)))]
- (is (contains? card-ids (:id active-model)))
- (is (not (contains? card-ids (:id archived-model))))))
- (testing "archived=true includes archived source card"
- (let [response (mt/user-http-request :crowberto :get 200 "ee/dependencies/graph/breaking?types=card&query=archivedbrokentestcard&archived=true")
- card-ids (set (map :id (:data response)))]
- (is (contains? card-ids (:id active-model)))
- (is (contains? card-ids (:id archived-model)))))))))))
+ (let [response (mt/user-http-request :crowberto :get 200 "ee/dependencies/graph/breaking?types=card&query=archivedbrokentestcard")
+ card-ids (set (map :id (:data response)))]
+ (is (contains? card-ids (:id active-model)))
+ ;; The archived model is itself a breaking source: its non-archived dependent
+ ;; (dependent-card-2) has an error traced back to it, so it should be surfaced.
+ (is (contains? card-ids (:id archived-model))))))))))
+
+(deftest ^:sequential breaking-entities-inactive-table-test
+ (testing "GET /api/ee/dependencies/graph/breaking?types=table includes an inactive table that breaks a non-archived dependent"
+ (mt/with-premium-features #{:dependencies}
+ (mt/with-temp [:model/Database db {:engine :h2, :name "inactive-table-breaking-test"}
+ :model/Table table {:db_id (:id db), :name "RETIRED_TABLE", :active true}
+ :model/Field _field {:table_id (:id table), :name "ID"
+ :base_type :type/Integer, :database_type "INTEGER"}
+ :model/User user {:email "[email protected]"}]
+ (mt/with-model-cleanup [:model/Card :model/Dependency :model/DependencyStatus
+ :model/AnalysisFinding :model/AnalysisFindingError]
+ (let [query (lib-be/with-metadata-provider-cache
+ (let [mp (lib-be/application-database-metadata-provider (:id db))]
+ (lib/query mp (lib.metadata/table mp (:id table)))))]
+ (mt/with-temp [:model/Card card {:database_id (:id db)
+ :dataset_query query
+ :type :question
+ :name "Dependent of retired table"
+ :creator_id (:id user)}
+ :model/Dependency _dep {:from_entity_type "card", :from_entity_id (:id card)
+ :to_entity_type "table", :to_entity_id (:id table)}]
+ (t2/update! :model/Table (:id table) {:active false})
+ (lib-be/with-metadata-provider-cache
+ (run-analysis-for-card! (:id card)))
+ (testing "the dependent card's analysis is now broken, attributing the inactive table as the source"
+ (is (false? (t2/select-one-fn :result :model/AnalysisFinding
+ :analyzed_entity_type "card" :analyzed_entity_id (:id card))))
+ (is (t2/exists? :model/AnalysisFindingError
+ :source_entity_type "table" :source_entity_id (:id table)
+ :analyzed_entity_type "card" :analyzed_entity_id (:id card))))
+ (testing "the inactive table is surfaced as a breaking source"
+ (let [response (mt/user-http-request :crowberto :get 200 "ee/dependencies/graph/breaking?types=table")
+ table-ids (set (map :id (:data response)))]
+ (is (contains? table-ids (:id table))))))))))))
(deftest ^:sequential breaking-entities-multiple-dependents-test
(testing "GET /api/ee/dependencies/graph/breaking - model breaking multiple dependents appears once"
diff --git a/enterprise/backend/test/metabase_enterprise/dependencies/task/backfill_test.clj b/enterprise/backend/test/metabase_enterprise/dependencies/task/backfill_test.clj
index 56383996845..6b25e030965 100644
--- a/enterprise/backend/test/metabase_enterprise/dependencies/task/backfill_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/dependencies/task/backfill_test.clj
@@ -402,3 +402,35 @@
(assert-processed :card card-id)
(is (t2/exists? :model/Dependency :from_entity_type :card :from_entity_id card-id
:to_entity_type :table :to_entity_id (mt/id :orders)))))))
+
+(deftest ^:sequential backfill-no-status-row-test
+ (testing "Entities with no dependency_status row yet get picked up and processed by the backfill"
+ (backfill-all-existing-entities!)
+ (mt/with-premium-features #{}
+ (mt/with-temp [:model/Card {card-id :id} {:dataset_query (mt/mbql-query orders)}]
+ ;; Precondition: the temp card was inserted directly (no :event/card-create), so it has no
+ ;; status row at all. This is the case the left-join in instances-for-dependency-calculation
+ ;; must catch — guard it so the test can't silently pass without exercising that branch.
+ (is (not (t2/exists? :model/DependencyStatus :entity_type :card :entity_id card-id))
+ "Expected the temp card to start with no dependency_status row")
+ (backfill-dependencies-single-trigger!)
+ (assert-processed :card card-id)
+ (is (t2/exists? :model/Dependency :from_entity_type :card :from_entity_id card-id
+ :to_entity_type :table :to_entity_id (mt/id :orders)))))))
+
+(deftest ^:sequential has-stale-or-outdated?-counts-no-status-row-test
+ (testing "has-stale-or-outdated? (backing /backfill-status) stays consistent with what the backfill processes"
+ (backfill-all-existing-entities!)
+ (mt/with-premium-features #{}
+ ;; Clean slate: nothing left to process.
+ (is (false? (deps.dependency-status/has-stale-or-outdated?))
+ "Expected no pending work after backfilling all existing entities")
+ (mt/with-temp [:model/Card {card-id :id} {:dataset_query (mt/mbql-query orders)}]
+ ;; A card with no status row is pending work, even though no DependencyStatus row exists.
+ (is (not (t2/exists? :model/DependencyStatus :entity_type :card :entity_id card-id)))
+ (is (true? (deps.dependency-status/has-stale-or-outdated?))
+ "Expected pending work: a card with no status row still needs calculation")
+ (backfill-dependencies-single-trigger!)
+ (assert-processed :card card-id)
+ (is (false? (deps.dependency-status/has-stale-or-outdated?))
+ "Expected no pending work once the card has been processed")))))
diff --git a/enterprise/backend/test/metabase_enterprise/replacement/api_test.clj b/enterprise/backend/test/metabase_enterprise/replacement/api_test.clj
index 1330d11d375..8f7174e8b53 100644
--- a/enterprise/backend/test/metabase_enterprise/replacement/api_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/replacement/api_test.clj
@@ -172,14 +172,19 @@
(let [src (t2/select-one-fn :source :model/Transform :id transform-id)]
(is (= new-id (lib/primary-source-card-id (:query src))))))
(testing "Dependencies point to new model"
- (let [deps-to-old (t2/select :model/Dependency
- :to_entity_type :card
- :to_entity_id old-id)
- deps-to-new (t2/select :model/Dependency
- :to_entity_type :card
- :to_entity_id new-id)]
- (is (empty? deps-to-old))
- (is (seq deps-to-new)))))))))))
+ (deps.test/synchronously-run-backfill!)
+ (let [edges-to (fn [card-id]
+ (t2/select-fn-set (juxt :from_entity_type :from_entity_id)
+ :model/Dependency
+ :to_entity_type :card
+ :to_entity_id card-id))]
+ (is (= #{[:dashboard dashboard-id]}
+ (edges-to old-id)))
+ (is (= #{[:card mbql-child-1-id]
+ [:card mbql-child-2-id]
+ [:card native-child-id]
+ [:transform transform-id]}
+ (edges-to new-id))))))))))))
(deftest concurrent-run-returns-409-test
(testing "POST /replace-source — returns 409 when another run is already active"
diff --git a/enterprise/frontend/src/metabase-enterprise/api/dependencies.ts b/enterprise/frontend/src/metabase-enterprise/api/dependencies.ts
index 3fd94ee0571..a97f46b8bdd 100644
--- a/enterprise/frontend/src/metabase-enterprise/api/dependencies.ts
+++ b/enterprise/frontend/src/metabase-enterprise/api/dependencies.ts
@@ -1,8 +1,4 @@
import type {
- CheckCardDependenciesRequest,
- CheckDependenciesResponse,
- CheckSnippetDependenciesRequest,
- CheckTransformDependenciesRequest,
DependencyGraph,
DependencyNode,
GetDependencyGraphRequest,
@@ -81,36 +77,6 @@ export const dependencyApi = EnterpriseApi.injectEndpoints({
providesTags: (response) =>
response ? provideDependencyNodeListTags(response.data) : [],
}),
- checkCardDependencies: builder.query<
- CheckDependenciesResponse,
- CheckCardDependenciesRequest
- >({
- query: (body) => ({
- method: "POST",
- url: "/api/ee/dependencies/check-card",
- body,
- }),
- }),
- checkSnippetDependencies: builder.query<
- CheckDependenciesResponse,
- CheckSnippetDependenciesRequest
- >({
- query: (body) => ({
- method: "POST",
- url: "/api/ee/dependencies/check-snippet",
- body,
- }),
- }),
- checkTransformDependencies: builder.query<
- CheckDependenciesResponse,
- CheckTransformDependenciesRequest
- >({
- query: (body) => ({
- method: "POST",
- url: "/api/ee/dependencies/check-transform",
- body,
- }),
- }),
}),
});
@@ -120,7 +86,4 @@ export const {
useListBreakingGraphNodesQuery,
useListBrokenGraphNodesQuery,
useListUnreferencedGraphNodesQuery,
- useLazyCheckCardDependenciesQuery,
- useLazyCheckSnippetDependenciesQuery,
- useLazyCheckTransformDependenciesQuery,
} = dependencyApi;
diff --git a/enterprise/frontend/src/metabase-enterprise/dependencies/components/CheckDependenciesForm/CheckDependenciesForm.module.css b/enterprise/frontend/src/metabase-enterprise/dependencies/components/CheckDependenciesForm/CheckDependenciesForm.module.css
deleted file mode 100644
index efc2e2a8fa4..00000000000
--- a/enterprise/frontend/src/metabase-enterprise/dependencies/components/CheckDependenciesForm/CheckDependenciesForm.module.css
+++ /dev/null
@@ -1,12 +0,0 @@
-.card {
- flex-shrink: 0;
-
- &:hover {
- background-color: var(--mb-color-background-secondary);
- }
-}
-
-.list {
- overflow: auto;
- max-height: 60vh;
-}
diff --git a/enterprise/frontend/src/metabase-enterprise/dependencies/components/CheckDependenciesForm/CheckDependenciesForm.tsx b/enterprise/frontend/src/metabase-enterprise/dependencies/components/CheckDependenciesForm/CheckDependenciesForm.tsx
deleted file mode 100644
index 4d33432a73e..00000000000
--- a/enterprise/frontend/src/metabase-enterprise/dependencies/components/CheckDependenciesForm/CheckDependenciesForm.tsx
+++ /dev/null
@@ -1,262 +0,0 @@
-import { Fragment } from "react";
-import { Link } from "react-router";
-import { t } from "ttag";
-
-import CS from "metabase/css/core/index.css";
-import {
- Form,
- FormErrorMessage,
- FormProvider,
- FormSubmitButton,
-} from "metabase/forms";
-import type { CheckDependenciesFormProps } from "metabase/plugins";
-import {
- Anchor,
- Box,
- Button,
- Card,
- FixedSizeIcon,
- Group,
- Stack,
- Text,
-} from "metabase/ui";
-import * as Urls from "metabase/urls";
-import visualizations from "metabase/visualizations";
-import type {
- Card as ApiCard,
- CheckDependenciesResponse,
- IconName,
- Transform,
-} from "metabase-types/api";
-
-import S from "./CheckDependenciesForm.module.css";
-
-type DependencyItem = CardDependencyItem | TransformDependencyItem;
-
-type CardDependencyItem = {
- type: "card";
- card: ApiCard;
-};
-
-type TransformDependencyItem = {
- type: "transform";
- transform: Transform;
-};
-
-export function CheckDependenciesForm({
- checkData,
- onSave,
- onCancel,
-}: CheckDependenciesFormProps) {
- const items = getDependencyItems(checkData);
-
- return (
- <FormProvider initialValues={{}} onSubmit={onSave}>
- <Form>
- <Text mb="md" px="xl">
- {t`The items below will break because of these changes:`}
- </Text>
- <Stack className={S.list} mb="xl" px="xl">
- {items.map((item, index) => (
- <DependencyItemCard key={index} item={item} />
- ))}
- </Stack>
- <Group px="xl" wrap="nowrap">
- <Box flex={1}>
- <FormErrorMessage />
- </Box>
- <Button onClick={onCancel}>{t`Cancel`}</Button>
- <FormSubmitButton label={t`Save anyway`} variant="filled" />
- </Group>
- </Form>
- </FormProvider>
- );
-}
-
-function getDependencyItems({
- bad_cards = [],
- bad_transforms = [],
-}: CheckDependenciesResponse): DependencyItem[] {
- return [
- ...bad_transforms.map(
- (transform): TransformDependencyItem => ({
- type: "transform",
- transform,
- }),
- ),
- ...bad_cards.map(
- (card): CardDependencyItem => ({
- type: "card",
- card,
- }),
- ),
- ];
-}
-
-type DependencyItemCardProps = {
- item: DependencyItem;
-};
-
-function DependencyItemCard({ item }: DependencyItemCardProps) {
- return (
- <Card
- className={S.card}
- component={Link}
- to={getItemLink(item)}
- p="md"
- shadow="none"
- withBorder
- >
- <Stack className={CS.textWrap} gap="xs">
- <Group c="brand" gap="sm" wrap="nowrap">
- <FixedSizeIcon name={getItemIcon(item)} />
- <Box fw="bold" lh="h4">
- {getItemName(item)}
- </Box>
- </Group>
- {getItemDescription(item)}
- </Stack>
- </Card>
- );
-}
-
-function getItemIcon(item: DependencyItem): IconName {
- if (item.type === "card") {
- const { card } = item;
-
- switch (card.type) {
- case "question":
- return visualizations.get(card.display)?.iconName ?? "table2";
- case "model":
- return "model";
- case "metric":
- return "metric";
- }
- }
-
- if (item.type === "transform") {
- return "transform";
- }
-
- return "unknown";
-}
-
-function getItemName(item: DependencyItem) {
- switch (item.type) {
- case "card":
- return item.card.name;
- case "transform":
- return item.transform.name;
- default:
- return null;
- }
-}
-
-function getItemLink(item: DependencyItem) {
- switch (item.type) {
- case "card":
- return Urls.card(item.card);
- default:
- return Urls.transform(item.transform.id);
- }
-}
-
-function getItemDescription(item: DependencyItem) {
- if (item.type === "card") {
- return (
- <Group gap="sm" wrap="nowrap">
- <FixedSizeIcon
- c="text-secondary"
- name={getCardLinkIcon(item)}
- flex="0 0 auto"
- />
- <BreadcrumbList items={getCardBreadcrumbs(item)} />
- </Group>
- );
- }
-
- if (item.type === "transform") {
- return (
- <Box fz="sm" lh="h5">
- {t`Transform`}
- </Box>
- );
- }
-
- return null;
-}
-
-function getCardBreadcrumbs(item: CardDependencyItem) {
- const { collection, dashboard, document } = item.card;
- if (collection == null) {
- return [];
- }
-
- const ancestors = collection.effective_ancestors ?? [];
- const breadcrumbs = ancestors.map((ancestor) => ({
- title: ancestor.name,
- to: Urls.collection(ancestor),
- }));
- breadcrumbs.push({ title: collection.name, to: Urls.collection(collection) });
- if (dashboard != null) {
- breadcrumbs.push({ title: dashboard.name, to: Urls.dashboard(dashboard) });
- } else if (document != null) {
- breadcrumbs.push({ title: document.name, to: Urls.document(document) });
- }
-
- return breadcrumbs;
-}
-
-function getCardLinkIcon(item: CardDependencyItem): IconName {
- const { dashboard, document } = item.card;
- if (dashboard != null) {
- return "dashboard";
- }
- if (document != null) {
- return "document";
- }
- return "collection";
-}
-
-type BreadcrumbItem = {
- title: string;
- to: string;
-};
-
-type BreadcrumbProps = {
- item: BreadcrumbItem;
-};
-
-function Breadcrumb({ item }: BreadcrumbProps) {
- return (
- <Anchor
- component={Link}
- className={CS.textWrap}
- to={item.to}
- c="text-secondary"
- fz="sm"
- lh="h5"
- >
- {item.title}
- </Anchor>
- );
-}
-
-type BreadcrumbListProps = {
- items: BreadcrumbItem[];
-};
-
-function BreadcrumbList({ items }: BreadcrumbListProps) {
- return (
- <Group align="center" gap="xs">
- {items.map((item, itemIndex) => (
- <Fragment key={itemIndex}>
- {itemIndex > 0 && (
- <FixedSizeIcon name="chevronright" c="text-secondary" size={8} />
- )}
- <Breadcrumb item={item} />
- </Fragment>
- ))}
- </Group>
- );
-}
diff --git a/enterprise/frontend/src/metabase-enterprise/dependencies/components/CheckDependenciesForm/CheckDependenciesForm.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/dependencies/components/CheckDependenciesForm/CheckDependenciesForm.unit.spec.tsx
deleted file mode 100644
index 0c596f2e61b..00000000000
--- a/enterprise/frontend/src/metabase-enterprise/dependencies/components/CheckDependenciesForm/CheckDependenciesForm.unit.spec.tsx
+++ /dev/null
@@ -1,230 +0,0 @@
-import { Route } from "react-router";
-
-import { getIcon, renderWithProviders, screen } from "__support__/ui";
-import registerVisualizations from "metabase/visualizations/register";
-import type {
- CardDisplayType,
- CardType,
- CheckDependenciesResponse,
- IconName,
-} from "metabase-types/api";
-import {
- createMockCard,
- createMockCheckDependenciesResponse,
- createMockCollection,
- createMockTransform,
-} from "metabase-types/api/mocks";
-
-import { CheckDependenciesForm } from "./CheckDependenciesForm";
-
-type SetupOpts = {
- checkData?: CheckDependenciesResponse;
-};
-
-function setup({
- checkData = createMockCheckDependenciesResponse(),
-}: SetupOpts) {
- const onSave = jest.fn();
- const onCancel = jest.fn();
- renderWithProviders(
- <Route
- path="/"
- component={() => (
- <CheckDependenciesForm
- checkData={checkData}
- onSave={onSave}
- onCancel={onCancel}
- />
- )}
- />,
- { withRouter: true, initialRoute: "/" },
- );
- return { onSave, onCancel };
-}
-
-registerVisualizations();
-
-describe("CheckDependenciesForm", () => {
- describe.each<CardType>(["question", "model", "metric"])("%s", (type) => {
- it("should display broken cards", () => {
- setup({
- checkData: createMockCheckDependenciesResponse({
- success: false,
- bad_cards: [
- createMockCard({
- name: "Card",
- type,
- }),
- ],
- }),
- });
- expect(screen.getByText("Card")).toBeInTheDocument();
- expect(screen.getByRole("link", { name: /Card/ })).toHaveAttribute(
- "href",
- `/${type}/1-card`,
- );
- });
-
- it("should display the root collection", () => {
- setup({
- checkData: createMockCheckDependenciesResponse({
- success: false,
- bad_cards: [
- createMockCard({
- name: "Card",
- type,
- collection: createMockCollection({
- id: "root",
- name: "Our analytics",
- }),
- }),
- ],
- }),
- });
- expect(
- screen.getByRole("link", { name: "Our analytics" }),
- ).toHaveAttribute("href", "/collection/root");
- });
-
- it("should display the nested collection with parent collections", () => {
- setup({
- checkData: createMockCheckDependenciesResponse({
- success: false,
- bad_cards: [
- createMockCard({
- name: "Card",
- type,
- collection: createMockCollection({
- id: 2,
- name: "Second collection",
- effective_ancestors: [
- createMockCollection({ id: 1, name: "First collection" }),
- createMockCollection({ id: "root", name: "Our analytics" }),
- ],
- }),
- }),
- ],
- }),
- });
- expect(
- screen.getByRole("link", { name: "Our analytics" }),
- ).toHaveAttribute("href", "/collection/root");
- expect(
- screen.getByRole("link", { name: "First collection" }),
- ).toHaveAttribute("href", "/collection/1-first-collection");
- expect(
- screen.getByRole("link", { name: "Second collection" }),
- ).toHaveAttribute("href", "/collection/2-second-collection");
- });
- });
-
- it.each<{ display: CardDisplayType; icon: IconName }>([
- { display: "line", icon: "line" },
- { display: "pie", icon: "pie" },
- ])(
- "should display the icon that corresponds to the display type",
- ({ display, icon }) => {
- setup({
- checkData: createMockCheckDependenciesResponse({
- success: false,
- bad_cards: [
- createMockCard({
- type: "question",
- display,
- }),
- ],
- }),
- });
- expect(getIcon(icon)).toBeInTheDocument();
- },
- );
-
- it("should display the dashboard that the question belongs to", () => {
- setup({
- checkData: createMockCheckDependenciesResponse({
- success: false,
- bad_cards: [
- createMockCard({
- name: "Card",
- type: "question",
- collection: createMockCollection({
- id: 1,
- name: "First collection",
- effective_ancestors: [
- createMockCollection({ id: "root", name: "Our analytics" }),
- ],
- }),
- dashboard: {
- id: 2,
- name: "Card dashboard",
- },
- }),
- ],
- }),
- });
- expect(screen.getByRole("link", { name: "Our analytics" })).toHaveAttribute(
- "href",
- "/collection/root",
- );
- expect(
- screen.getByRole("link", { name: "First collection" }),
- ).toHaveAttribute("href", "/collection/1-first-collection");
- expect(
- screen.getByRole("link", { name: "Card dashboard" }),
- ).toHaveAttribute("href", "/dashboard/2-card-dashboard");
- });
-
- it("should display the document that the question belongs to", () => {
- setup({
- checkData: createMockCheckDependenciesResponse({
- success: false,
- bad_cards: [
- createMockCard({
- name: "Card",
- type: "question",
- collection: createMockCollection({
- id: 1,
- name: "First collection",
- effective_ancestors: [
- createMockCollection({ id: "root", name: "Our analytics" }),
- ],
- }),
- document: {
- id: 3,
- name: "Card document",
- },
- }),
- ],
- }),
- });
- expect(screen.getByRole("link", { name: "Our analytics" })).toHaveAttribute(
- "href",
- "/collection/root",
- );
- expect(
- screen.getByRole("link", { name: "First collection" }),
- ).toHaveAttribute("href", "/collection/1-first-collection");
- expect(screen.getByRole("link", { name: "Card document" })).toHaveAttribute(
- "href",
- "/document/3",
- );
- });
-
- it("should display transforms", () => {
- setup({
- checkData: createMockCheckDependenciesResponse({
- success: false,
- bad_transforms: [
- createMockTransform({
- id: 1,
- name: "SQL transform",
- }),
- ],
- }),
- });
- expect(screen.getByRole("link", { name: /SQL transform/ })).toHaveAttribute(
- "href",
- "/data-studio/transforms/1",
- );
- });
-});
diff --git a/enterprise/frontend/src/metabase-enterprise/dependencies/components/CheckDependenciesForm/index.ts b/enterprise/frontend/src/metabase-enterprise/dependencies/components/CheckDependenciesForm/index.ts
deleted file mode 100644
index 420134c7ca9..00000000000
--- a/enterprise/frontend/src/metabase-enterprise/dependencies/components/CheckDependenciesForm/index.ts
+++ /dev/null
@@ -1 +0,0 @@
-export * from "./CheckDependenciesForm";
diff --git a/enterprise/frontend/src/metabase-enterprise/dependencies/components/CheckDependenciesModal/CheckDependenciesModal.tsx b/enterprise/frontend/src/metabase-enterprise/dependencies/components/CheckDependenciesModal/CheckDependenciesModal.tsx
deleted file mode 100644
index bff78b63a82..00000000000
--- a/enterprise/frontend/src/metabase-enterprise/dependencies/components/CheckDependenciesModal/CheckDependenciesModal.tsx
+++ /dev/null
@@ -1,35 +0,0 @@
-import type { CheckDependenciesModalProps } from "metabase/plugins";
-import { Flex, Modal } from "metabase/ui";
-
-import { CheckDependenciesForm } from "../CheckDependenciesForm";
-import { CheckDependenciesTitle } from "../CheckDependenciesTitle";
-
-export function CheckDependenciesModal({
- checkData,
- opened,
- onSave,
- onClose,
-}: CheckDependenciesModalProps) {
- return (
- <Modal.Root size="xl" padding="xl" opened={opened} onClose={onClose}>
- <Modal.Overlay />
- <Modal.Content>
- <Modal.Header px="xl">
- <Modal.Title>
- <CheckDependenciesTitle />
- </Modal.Title>
- <Flex justify="flex-end">
- <Modal.CloseButton />
- </Flex>
- </Modal.Header>
- <Modal.Body px={0}>
- <CheckDependenciesForm
- checkData={checkData}
- onSave={onSave}
- onCancel={onClose}
- />
- </Modal.Body>
- </Modal.Content>
- </Modal.Root>
- );
-}
diff --git a/enterprise/frontend/src/metabase-enterprise/dependencies/components/CheckDependenciesModal/index.ts b/enterprise/frontend/src/metabase-enterprise/dependencies/components/CheckDependenciesModal/index.ts
deleted file mode 100644
index 6f83f37598d..00000000000
--- a/enterprise/frontend/src/metabase-enterprise/dependencies/components/CheckDependenciesModal/index.ts
+++ /dev/null
@@ -1 +0,0 @@
-export * from "./CheckDependenciesModal";
diff --git a/enterprise/frontend/src/metabase-enterprise/dependencies/components/CheckDependenciesTitle/CheckDependenciesTitle.tsx b/enterprise/frontend/src/metabase-enterprise/dependencies/components/CheckDependenciesTitle/CheckDependenciesTitle.tsx
deleted file mode 100644
index 82f20ab86d3..00000000000
--- a/enterprise/frontend/src/metabase-enterprise/dependencies/components/CheckDependenciesTitle/CheckDependenciesTitle.tsx
+++ /dev/null
@@ -1,11 +0,0 @@
-import { t } from "ttag";
-
-import { Box } from "metabase/ui";
-
-export function CheckDependenciesTitle() {
- return (
- <Box fz="h3" lh="h3">
- {t`These changes will break some other things. Save anyway?`}
- </Box>
- );
-}
diff --git a/enterprise/frontend/src/metabase-enterprise/dependencies/components/CheckDependenciesTitle/index.ts b/enterprise/frontend/src/metabase-enterprise/dependencies/components/CheckDependenciesTitle/index.ts
deleted file mode 100644
index 815b05cb08e..00000000000
--- a/enterprise/frontend/src/metabase-enterprise/dependencies/components/CheckDependenciesTitle/index.ts
+++ /dev/null
@@ -1 +0,0 @@
-export * from "./CheckDependenciesTitle";
diff --git a/enterprise/frontend/src/metabase-enterprise/dependencies/hooks/use-check-card-dependencies/index.ts b/enterprise/frontend/src/metabase-enterprise/dependencies/hooks/use-check-card-dependencies/index.ts
deleted file mode 100644
index bb1a1c1d203..00000000000
--- a/enterprise/frontend/src/metabase-enterprise/dependencies/hooks/use-check-card-dependencies/index.ts
+++ /dev/null
@@ -1 +0,0 @@
-export * from "./use-check-card-dependencies";
diff --git a/enterprise/frontend/src/metabase-enterprise/dependencies/hooks/use-check-card-dependencies/use-check-card-dependencies.ts b/enterprise/frontend/src/metabase-enterprise/dependencies/hooks/use-check-card-dependencies/use-check-card-dependencies.ts
deleted file mode 100644
index 90e81476748..00000000000
--- a/enterprise/frontend/src/metabase-enterprise/dependencies/hooks/use-check-card-dependencies/use-check-card-dependencies.ts
+++ /dev/null
@@ -1,38 +0,0 @@
-import { useCallback } from "react";
-
-import type {
- UseCheckDependenciesProps,
- UseCheckDependenciesResult,
-} from "metabase/plugins";
-import { getSubmittableQuestion } from "metabase/query_builder/selectors";
-import { useStore } from "metabase/redux";
-import { useLazyCheckCardDependenciesQuery } from "metabase-enterprise/api";
-import type Question from "metabase-lib/v1/Question";
-import type { CheckCardDependenciesRequest } from "metabase-types/api";
-
-import { useCheckDependencies } from "../use-check-dependencies";
-
-export function useCheckCardDependencies({
- onSave,
-}: UseCheckDependenciesProps<Question>): UseCheckDependenciesResult<Question> {
- const store = useStore();
-
- const getCheckDependenciesRequest = useCallback(
- (question: Question): CheckCardDependenciesRequest => {
- const submittableQuestion = getSubmittableQuestion(
- store.getState(),
- question,
- );
- const { id, type, dataset_query, result_metadata } =
- submittableQuestion.card();
- return { id, type, dataset_query, result_metadata };
- },
- [store],
- );
-
- return useCheckDependencies({
- getCheckDependenciesRequest,
- useLazyCheckDependenciesQuery: useLazyCheckCardDependenciesQuery,
- onSave,
- });
-}
diff --git a/enterprise/frontend/src/metabase-enterprise/dependencies/hooks/use-check-card-dependencies/use-check-card-dependencies.unit.spec.ts b/enterprise/frontend/src/metabase-enterprise/dependencies/hooks/use-check-card-dependencies/use-check-card-dependencies.unit.spec.ts
deleted file mode 100644
index 362f4f99b36..00000000000
--- a/enterprise/frontend/src/metabase-enterprise/dependencies/hooks/use-check-card-dependencies/use-check-card-dependencies.unit.spec.ts
+++ /dev/null
@@ -1,94 +0,0 @@
-import {
- setupCheckCardDependenciesEndpoint,
- setupCheckCardDependenciesEndpointError,
-} from "__support__/server-mocks/dependencies";
-import { act, renderHookWithProviders } from "__support__/ui";
-import { SAMPLE_METADATA } from "metabase-lib/test-helpers";
-import Question from "metabase-lib/v1/Question";
-import type { CheckDependenciesResponse } from "metabase-types/api";
-import { createMockCheckDependenciesResponse } from "metabase-types/api/mocks";
-
-import { useCheckCardDependencies } from "./use-check-card-dependencies";
-
-type SetupOpts = {
- response?: CheckDependenciesResponse;
- hasError?: boolean;
-};
-
-function setup({
- response = createMockCheckDependenciesResponse(),
- hasError = false,
-}: SetupOpts = {}) {
- if (hasError) {
- setupCheckCardDependenciesEndpointError();
- } else {
- setupCheckCardDependenciesEndpoint(response);
- }
-
- const onSave = jest.fn();
- const { result } = renderHookWithProviders(
- () => useCheckCardDependencies({ onSave }),
- {},
- );
-
- return { result, onSave };
-}
-
-describe("useCheckCardDependencies", () => {
- it("should save if there are no broken dependencies", async () => {
- const question = Question.create({ metadata: SAMPLE_METADATA });
- const response = createMockCheckDependenciesResponse({ success: true });
- const { result, onSave } = setup({ response });
-
- await act(() => result.current.handleInitialSave(question));
-
- expect(onSave).toHaveBeenCalledWith(question);
- });
-
- it("should not save if there are broken dependencies", async () => {
- const question = Question.create({ metadata: SAMPLE_METADATA });
- const response = createMockCheckDependenciesResponse({ success: false });
- const { result, onSave } = setup({ response });
-
- await act(() => result.current.handleInitialSave(question));
-
- expect(result.current.checkData).toEqual(response);
- expect(result.current.isConfirmationShown).toBe(true);
- expect(onSave).not.toHaveBeenCalled();
- });
-
- it("should allow to confirm saving", async () => {
- const question = Question.create({ metadata: SAMPLE_METADATA });
- const response = createMockCheckDependenciesResponse({ success: false });
- const { result, onSave } = setup({ response });
-
- await act(() => result.current.handleInitialSave(question));
- await act(() => result.current.handleSaveAfterConfirmation());
-
- expect(result.current.checkData).toEqual(response);
- expect(result.current.isConfirmationShown).toBe(false);
- expect(onSave).toHaveBeenCalledWith(question);
- });
-
- it("should allow to cancel the confirmation screen", async () => {
- const question = Question.create({ metadata: SAMPLE_METADATA });
- const response = createMockCheckDependenciesResponse({ success: false });
- const { result, onSave } = setup({ response });
-
- await act(() => result.current.handleInitialSave(question));
- await act(() => result.current.handleCloseConfirmation());
-
- expect(result.current.checkData).toEqual(response);
- expect(result.current.isConfirmationShown).toBe(false);
- expect(onSave).not.toHaveBeenCalled();
- });
-
- it("should save if there is a backend error when checking dependencies", async () => {
- const question = Question.create({ metadata: SAMPLE_METADATA });
- const { result, onSave } = setup({ hasError: true });
-
- await act(() => result.current.handleInitialSave(question));
-
- expect(onSave).toHaveBeenCalledWith(question);
- });
-});
diff --git a/enterprise/frontend/src/metabase-enterprise/dependencies/hooks/use-check-dependencies/index.ts b/enterprise/frontend/src/metabase-enterprise/dependencies/hooks/use-check-dependencies/index.ts
deleted file mode 100644
index db8905bf067..00000000000
--- a/enterprise/frontend/src/metabase-enterprise/dependencies/hooks/use-check-dependencies/index.ts
+++ /dev/null
@@ -1 +0,0 @@
-export * from "./use-check-dependencies";
diff --git a/enterprise/frontend/src/metabase-enterprise/dependencies/hooks/use-check-dependencies/use-check-dependencies.ts b/enterprise/frontend/src/metabase-enterprise/dependencies/hooks/use-check-dependencies/use-check-dependencies.ts
deleted file mode 100644
index b38fb7eb658..00000000000
--- a/enterprise/frontend/src/metabase-enterprise/dependencies/hooks/use-check-dependencies/use-check-dependencies.ts
+++ /dev/null
@@ -1,79 +0,0 @@
-import { useCallback, useState } from "react";
-
-import type { UseCheckDependenciesResult } from "metabase/plugins";
-import type { CheckDependenciesResponse } from "metabase-types/api";
-
-type UseCheckDependenciesQueryData = {
- data?: CheckDependenciesResponse;
- error?: unknown;
- isFetching?: boolean;
-};
-
-type UseCheckDependenciesArgsData<TRequest> = {
- lastArg?: TRequest;
-};
-
-type UseCheckDependenciesQueryResult<TRequest> = [
- (request: TRequest) => Promise<UseCheckDependenciesQueryData>,
- UseCheckDependenciesQueryData,
- UseCheckDependenciesArgsData<TRequest>,
-];
-
-type UseCheckDependenciesProps<TChange, TRequest> = {
- getCheckDependenciesRequest: (change: TChange) => TRequest;
- useLazyCheckDependenciesQuery: () => UseCheckDependenciesQueryResult<TRequest>;
- onSave: (change: TChange) => Promise<void>;
-};
-
-export function useCheckDependencies<TChange, TRequest>({
- getCheckDependenciesRequest,
- useLazyCheckDependenciesQuery,
- onSave,
-}: UseCheckDependenciesProps<
- TChange,
- TRequest
->): UseCheckDependenciesResult<TChange> {
- const [change, setChange] = useState<TChange | null>(null);
- const [isConfirmationShown, setIsConfirmationShown] = useState(false);
- const [checkDependencies, { data, isFetching = false }] =
- useLazyCheckDependenciesQuery();
-
- const handleInitialSave = useCallback(
- async (change: TChange) => {
- const { data, error } = await checkDependencies(
- getCheckDependenciesRequest(change),
- );
- if (error != null) {
- console.error("Error when checking dependencies.", error);
- await onSave(change);
- } else if (data != null && !data.success) {
- setChange(change);
- setIsConfirmationShown(true);
- } else {
- await onSave(change);
- }
- },
- [getCheckDependenciesRequest, checkDependencies, onSave],
- );
-
- const handleCloseConfirmation = useCallback(() => {
- setChange(null);
- setIsConfirmationShown(false);
- }, []);
-
- const handleSaveAfterConfirmation = useCallback(async () => {
- if (change != null) {
- await onSave(change);
- handleCloseConfirmation();
- }
- }, [change, onSave, handleCloseConfirmation]);
-
- return {
- checkData: data,
- isCheckingDependencies: isFetching,
- isConfirmationShown,
- handleInitialSave,
- handleSaveAfterConfirmation,
- handleCloseConfirmation,
- };
-}
diff --git a/enterprise/frontend/src/metabase-enterprise/dependencies/hooks/use-check-snippet-dependencies/index.ts b/enterprise/frontend/src/metabase-enterprise/dependencies/hooks/use-check-snippet-dependencies/index.ts
deleted file mode 100644
index d100a598a59..00000000000
--- a/enterprise/frontend/src/metabase-enterprise/dependencies/hooks/use-check-snippet-dependencies/index.ts
+++ /dev/null
@@ -1 +0,0 @@
-export * from "./use-check-snippet-dependencies";
diff --git a/enterprise/frontend/src/metabase-enterprise/dependencies/hooks/use-check-snippet-dependencies/use-check-snippet-dependencies.ts b/enterprise/frontend/src/metabase-enterprise/dependencies/hooks/use-check-snippet-dependencies/use-check-snippet-dependencies.ts
deleted file mode 100644
index e7c740a329a..00000000000
--- a/enterprise/frontend/src/metabase-enterprise/dependencies/hooks/use-check-snippet-dependencies/use-check-snippet-dependencies.ts
+++ /dev/null
@@ -1,29 +0,0 @@
-import type {
- UseCheckDependenciesProps,
- UseCheckDependenciesResult,
-} from "metabase/plugins";
-import { useLazyCheckSnippetDependenciesQuery } from "metabase-enterprise/api";
-import type {
- CheckSnippetDependenciesRequest,
- UpdateSnippetRequest,
-} from "metabase-types/api";
-
-import { useCheckDependencies } from "../use-check-dependencies";
-
-export function useCheckSnippetDependencies({
- onSave,
-}: UseCheckDependenciesProps<UpdateSnippetRequest>): UseCheckDependenciesResult<UpdateSnippetRequest> {
- return useCheckDependencies({
- getCheckDependenciesRequest,
- useLazyCheckDependenciesQuery: useLazyCheckSnippetDependenciesQuery,
- onSave,
- });
-}
-
-function getCheckDependenciesRequest({
- id,
- name,
- content,
-}: UpdateSnippetRequest): CheckSnippetDependenciesRequest {
- return { id, name, content };
-}
diff --git a/enterprise/frontend/src/metabase-enterprise/dependencies/hooks/use-check-snippet-dependencies/use-check-snippet-dependencies.unit.spec.ts b/enterprise/frontend/src/metabase-enterprise/dependencies/hooks/use-check-snippet-dependencies/use-check-snippet-dependencies.unit.spec.ts
deleted file mode 100644
index 5476df8e34a..00000000000
--- a/enterprise/frontend/src/metabase-enterprise/dependencies/hooks/use-check-snippet-dependencies/use-check-snippet-dependencies.unit.spec.ts
+++ /dev/null
@@ -1,51 +0,0 @@
-import { setupCheckSnippetDependenciesEndpoint } from "__support__/server-mocks/dependencies";
-import { act, renderHookWithProviders } from "__support__/ui";
-import type { CheckDependenciesResponse } from "metabase-types/api";
-import {
- createMockCheckDependenciesResponse,
- createMockUpdateSnippetRequest,
-} from "metabase-types/api/mocks";
-
-import { useCheckSnippetDependencies } from "./use-check-snippet-dependencies";
-
-type SetupOpts = {
- response?: CheckDependenciesResponse;
-};
-
-function setup({
- response = createMockCheckDependenciesResponse(),
-}: SetupOpts = {}) {
- setupCheckSnippetDependenciesEndpoint(response);
-
- const onSave = jest.fn();
- const { result } = renderHookWithProviders(
- () => useCheckSnippetDependencies({ onSave }),
- {},
- );
-
- return { result, onSave };
-}
-
-describe("useCheckSnippetDependencies", () => {
- it("should save if there are no broken dependencies", async () => {
- const request = createMockUpdateSnippetRequest();
- const response = createMockCheckDependenciesResponse({ success: true });
- const { result, onSave } = setup({ response });
-
- await act(() => result.current.handleInitialSave(request));
-
- expect(onSave).toHaveBeenCalledWith(request);
- });
-
- it("should not save if there are broken dependencies", async () => {
- const request = createMockUpdateSnippetRequest();
- const response = createMockCheckDependenciesResponse({ success: false });
- const { result, onSave } = setup({ response });
-
- await act(() => result.current.handleInitialSave(request));
-
- expect(result.current.checkData).toEqual(response);
- expect(result.current.isConfirmationShown).toBe(true);
- expect(onSave).not.toHaveBeenCalled();
- });
-});
diff --git a/enterprise/frontend/src/metabase-enterprise/dependencies/hooks/use-check-transform-dependencies/index.ts b/enterprise/frontend/src/metabase-enterprise/dependencies/hooks/use-check-transform-dependencies/index.ts
deleted file mode 100644
index 8b2fd65fcaa..00000000000
--- a/enterprise/frontend/src/metabase-enterprise/dependencies/hooks/use-check-transform-dependencies/index.ts
+++ /dev/null
@@ -1 +0,0 @@
-export * from "./use-check-transform-dependencies";
diff --git a/enterprise/frontend/src/metabase-enterprise/dependencies/hooks/use-check-transform-dependencies/use-check-transform-dependencies.ts b/enterprise/frontend/src/metabase-enterprise/dependencies/hooks/use-check-transform-dependencies/use-check-transform-dependencies.ts
deleted file mode 100644
index 57f3e416021..00000000000
--- a/enterprise/frontend/src/metabase-enterprise/dependencies/hooks/use-check-transform-dependencies/use-check-transform-dependencies.ts
+++ /dev/null
@@ -1,28 +0,0 @@
-import type {
- UseCheckDependenciesProps,
- UseCheckDependenciesResult,
-} from "metabase/plugins";
-import { useLazyCheckTransformDependenciesQuery } from "metabase-enterprise/api";
-import type {
- CheckTransformDependenciesRequest,
- UpdateTransformRequest,
-} from "metabase-types/api";
-
-import { useCheckDependencies } from "../use-check-dependencies";
-
-export function useCheckTransformDependencies({
- onSave,
-}: UseCheckDependenciesProps<UpdateTransformRequest>): UseCheckDependenciesResult<UpdateTransformRequest> {
- return useCheckDependencies({
- getCheckDependenciesRequest,
- useLazyCheckDependenciesQuery: useLazyCheckTransformDependenciesQuery,
- onSave,
- });
-}
-
-function getCheckDependenciesRequest({
- id,
- source,
-}: UpdateTransformRequest): CheckTransformDependenciesRequest {
- return { id, source };
-}
diff --git a/enterprise/frontend/src/metabase-enterprise/dependencies/hooks/use-check-transform-dependencies/use-check-transform-dependencies.unit.spec.ts b/enterprise/frontend/src/metabase-enterprise/dependencies/hooks/use-check-transform-dependencies/use-check-transform-dependencies.unit.spec.ts
deleted file mode 100644
index 3a277df266b..00000000000
--- a/enterprise/frontend/src/metabase-enterprise/dependencies/hooks/use-check-transform-dependencies/use-check-transform-dependencies.unit.spec.ts
+++ /dev/null
@@ -1,51 +0,0 @@
-import { setupCheckTransformDependenciesEndpoint } from "__support__/server-mocks/dependencies";
-import { act, renderHookWithProviders } from "__support__/ui";
-import type { CheckDependenciesResponse } from "metabase-types/api";
-import {
- createMockCheckDependenciesResponse,
- createMockUpdateTransformRequest,
-} from "metabase-types/api/mocks";
-
-import { useCheckTransformDependencies } from "./use-check-transform-dependencies";
-
-type SetupOpts = {
- response?: CheckDependenciesResponse;
-};
-
-function setup({
- response = createMockCheckDependenciesResponse(),
-}: SetupOpts = {}) {
- setupCheckTransformDependenciesEndpoint(response);
-
- const onSave = jest.fn();
- const { result } = renderHookWithProviders(
- () => useCheckTransformDependencies({ onSave }),
- {},
- );
-
- return { result, onSave };
-}
-
-describe("useCheckTransformDependencies", () => {
- it("should save if there are no broken dependencies", async () => {
- const request = createMockUpdateTransformRequest();
- const response = createMockCheckDependenciesResponse({ success: true });
- const { result, onSave } = setup({ response });
-
- await act(() => result.current.handleInitialSave(request));
-
- expect(onSave).toHaveBeenCalledWith(request);
- });
-
- it("should not save if there are broken dependencies", async () => {
- const request = createMockUpdateTransformRequest();
- const response = createMockCheckDependenciesResponse({ success: false });
- const { result, onSave } = setup({ response });
-
- await act(() => result.current.handleInitialSave(request));
-
- expect(result.current.checkData).toEqual(response);
- expect(result.current.isConfirmationShown).toBe(true);
- expect(onSave).not.toHaveBeenCalled();
- });
-});
diff --git a/enterprise/frontend/src/metabase-enterprise/dependencies/index.ts b/enterprise/frontend/src/metabase-enterprise/dependencies/index.ts
index a685305cd17..e9bf016f346 100644
--- a/enterprise/frontend/src/metabase-enterprise/dependencies/index.ts
+++ b/enterprise/frontend/src/metabase-enterprise/dependencies/index.ts
@@ -1,9 +1,6 @@
import { PLUGIN_DEPENDENCIES } from "metabase/plugins";
import { hasPremiumFeature } from "metabase-enterprise/settings";
-import { CheckDependenciesForm } from "./components/CheckDependenciesForm";
-import { CheckDependenciesModal } from "./components/CheckDependenciesModal";
-import { CheckDependenciesTitle } from "./components/CheckDependenciesTitle";
import { useGetDependenciesCount } from "./hooks/use-get-dependencies-count";
import { DependencyGraphPage } from "./pages/DependencyGraphPage";
import {
@@ -22,9 +19,6 @@ export function initializePlugin() {
PLUGIN_DEPENDENCIES.getDataStudioDependencyDiagnosticsRoutes =
getDataStudioDependencyDiagnosticsRoutes;
PLUGIN_DEPENDENCIES.DependencyGraphPage = DependencyGraphPage;
- PLUGIN_DEPENDENCIES.CheckDependenciesForm = CheckDependenciesForm;
- PLUGIN_DEPENDENCIES.CheckDependenciesModal = CheckDependenciesModal;
- PLUGIN_DEPENDENCIES.CheckDependenciesTitle = CheckDependenciesTitle;
PLUGIN_DEPENDENCIES.useGetDependenciesCount = useGetDependenciesCount;
}
}
diff --git a/enterprise/frontend/src/metabase-enterprise/dependencies/utils.ts b/enterprise/frontend/src/metabase-enterprise/dependencies/utils.ts
index 9879e59a51e..bb2f60ea119 100644
--- a/enterprise/frontend/src/metabase-enterprise/dependencies/utils.ts
+++ b/enterprise/frontend/src/metabase-enterprise/dependencies/utils.ts
@@ -730,6 +730,10 @@ export function getErrorTypeLabel(
count = 0,
): string {
switch (type) {
+ case "missing-table":
+ return count === 1 ? t`Missing table` : t`Missing tables`;
+ case "missing-card":
+ return count === 1 ? t`Missing question` : t`Missing questions`;
case "missing-column":
return count === 1 ? t`Missing column` : t`Missing columns`;
case "missing-table-alias":
@@ -748,6 +752,18 @@ export function getErrorTypeLabelWithCount(
count = 0,
): string {
switch (type) {
+ case "missing-table":
+ return ngettext(
+ msgid`${count} missing table`,
+ `${count} missing tables`,
+ count,
+ );
+ case "missing-card":
+ return ngettext(
+ msgid`${count} missing question`,
+ `${count} missing questions`,
+ count,
+ );
case "missing-column":
return ngettext(
msgid`${count} missing column`,
diff --git a/frontend/src/metabase-types/api/dependencies.ts b/frontend/src/metabase-types/api/dependencies.ts
index e2981cb8959..b57c91c357d 100644
--- a/frontend/src/metabase-types/api/dependencies.ts
+++ b/frontend/src/metabase-types/api/dependencies.ts
@@ -211,6 +211,8 @@ export type DependencyNode =
export type AnalysisFindingErrorId = number;
export const ANALYSIS_FINDING_ERROR_TYPES = [
+ "missing-table",
+ "missing-card",
"missing-column",
"missing-table-alias",
"duplicate-column",
@@ -259,21 +261,6 @@ export type ListNodeDependentsRequest = {
"sort-direction"?: SortDirection;
};
-export type CheckDependenciesResponse = {
- success: boolean;
- bad_cards?: Card[];
- bad_transforms?: Transform[];
-};
-
-export type CheckCardDependenciesRequest = Pick<Card, "id"> &
- Partial<Pick<Card, "type" | "dataset_query" | "result_metadata">>;
-
-export type CheckSnippetDependenciesRequest = Pick<NativeQuerySnippet, "id"> &
- Partial<Pick<NativeQuerySnippet, "name" | "content">>;
-
-export type CheckTransformDependenciesRequest = Pick<Transform, "id"> &
- Partial<Pick<Transform, "source">>;
-
export const DEPENDENCY_SORT_COLUMNS = [
"name",
"location",
diff --git a/frontend/src/metabase-types/api/mocks/dependencies.ts b/frontend/src/metabase-types/api/mocks/dependencies.ts
index 935bf74f146..12c080130ef 100644
--- a/frontend/src/metabase-types/api/mocks/dependencies.ts
+++ b/frontend/src/metabase-types/api/mocks/dependencies.ts
@@ -2,10 +2,6 @@ import type {
AnalysisFindingError,
CardDependencyNode,
CardDependencyNodeData,
- CheckCardDependenciesRequest,
- CheckDependenciesResponse,
- CheckSnippetDependenciesRequest,
- CheckTransformDependenciesRequest,
DashboardDependencyNode,
DashboardDependencyNodeData,
DependencyEdge,
@@ -203,42 +199,6 @@ export function createMockSegmentDependencyNode(
};
}
-export function createMockCheckCardDependenciesRequest(
- opts?: Partial<CheckCardDependenciesRequest>,
-): CheckCardDependenciesRequest {
- return {
- id: 1,
- ...opts,
- };
-}
-
-export function createMockCheckSnippetDependenciesRequest(
- opts?: Partial<CheckSnippetDependenciesRequest>,
-): CheckSnippetDependenciesRequest {
- return {
- id: 1,
- ...opts,
- };
-}
-
-export function createMockCheckTransformDependenciesRequest(
- opts?: Partial<CheckTransformDependenciesRequest>,
-): CheckTransformDependenciesRequest {
- return {
- id: 1,
- ...opts,
- };
-}
-
-export function createMockCheckDependenciesResponse(
- opts?: Partial<CheckDependenciesResponse>,
-): CheckDependenciesResponse {
- return {
- success: true,
- ...opts,
- };
-}
-
export function createMockDashboardDependencyNodeData(
opts?: Partial<DashboardDependencyNodeData>,
): DashboardDependencyNodeData {
diff --git a/frontend/src/metabase/common/components/SaveQuestionModal/SaveQuestionModal.tsx b/frontend/src/metabase/common/components/SaveQuestionModal/SaveQuestionModal.tsx
index 78f3d6a4179..e88722309e5 100644
--- a/frontend/src/metabase/common/components/SaveQuestionModal/SaveQuestionModal.tsx
+++ b/frontend/src/metabase/common/components/SaveQuestionModal/SaveQuestionModal.tsx
@@ -5,7 +5,6 @@ import {
import { SaveQuestionProvider } from "metabase/common/components/SaveQuestionForm/context";
import type { SaveQuestionProps } from "metabase/common/components/SaveQuestionForm/types";
import { useEscapeToCloseModal } from "metabase/common/hooks/use-escape-to-close-modal";
-import { PLUGIN_DEPENDENCIES } from "metabase/plugins";
import { Flex, Modal, type ModalProps } from "metabase/ui";
type SaveQuestionModalProps = Omit<SaveQuestionProps, "initialDashboardTabId"> &
@@ -23,14 +22,6 @@ export const SaveQuestionModal = ({
targetCollection,
...modalProps
}: SaveQuestionModalProps) => {
- const {
- checkData,
- isConfirmationShown,
- handleInitialSave,
- handleSaveAfterConfirmation,
- } = PLUGIN_DEPENDENCIES.useCheckCardDependencies({
- onSave,
- });
useEscapeToCloseModal(onClose);
return (
@@ -46,7 +37,7 @@ export const SaveQuestionModal = ({
return newQuestion;
}}
- onSave={handleInitialSave}
+ onSave={onSave}
onCancel={onClose}
multiStep={multiStep}
initialCollectionId={initialCollectionId}
@@ -55,37 +46,24 @@ export const SaveQuestionModal = ({
<Modal.Root
padding="xl"
{...modalProps}
- size={isConfirmationShown ? "xl" : undefined}
closeOnEscape={false}
onClose={onClose}
>
<Modal.Overlay />
<Modal.Content data-testid="save-question-modal">
- <Modal.Header px={isConfirmationShown ? "xl" : undefined}>
+ <Modal.Header>
<Modal.Title>
- {isConfirmationShown ? (
- <PLUGIN_DEPENDENCIES.CheckDependenciesTitle />
- ) : (
- <SaveQuestionTitle />
- )}
+ <SaveQuestionTitle />
</Modal.Title>
<Flex justify="flex-end">
<Modal.CloseButton />
</Flex>
</Modal.Header>
- <Modal.Body px={isConfirmationShown ? 0 : undefined}>
- {checkData != null && isConfirmationShown ? (
- <PLUGIN_DEPENDENCIES.CheckDependenciesForm
- checkData={checkData}
- onSave={handleSaveAfterConfirmation}
- onCancel={onClose}
- />
- ) : (
- <SaveQuestionForm
- onSaveSuccess={() => closeOnSuccess && onClose()}
- onCancel={onClose}
- />
- )}
+ <Modal.Body>
+ <SaveQuestionForm
+ onSaveSuccess={() => closeOnSuccess && onClose()}
+ onCancel={onClose}
+ />
</Modal.Body>
</Modal.Content>
</Modal.Root>
diff --git a/frontend/src/metabase/metrics/pages/MetricQueryPage/MetricQueryPage.tsx b/frontend/src/metabase/metrics/pages/MetricQueryPage/MetricQueryPage.tsx
index c89910e4a7b..76bc6ff616c 100644
--- a/frontend/src/metabase/metrics/pages/MetricQueryPage/MetricQueryPage.tsx
+++ b/frontend/src/metabase/metrics/pages/MetricQueryPage/MetricQueryPage.tsx
@@ -9,7 +9,6 @@ import { PageContainer } from "metabase/data-studio/common/components/PageContai
import { PaneHeaderActions } from "metabase/data-studio/common/components/PaneHeader";
import { getResultMetadata } from "metabase/data-studio/common/utils";
import { useMetadataToasts } from "metabase/metadata/hooks";
-import { PLUGIN_DEPENDENCIES } from "metabase/plugins";
import { getInitialUiState } from "metabase/querying/editor/components/QueryEditor";
import { useSelector } from "metabase/redux";
import { getMetadata } from "metabase/selectors/metadata";
@@ -96,37 +95,29 @@ function MetricQueryPageBody({
return !Lib.areLegacyQueriesEqual(datasetQuery, card.dataset_query);
}, [datasetQuery, card.dataset_query]);
- const {
- checkData,
- isCheckingDependencies,
- isConfirmationShown,
- handleInitialSave,
- handleSaveAfterConfirmation,
- handleCloseConfirmation,
- } = PLUGIN_DEPENDENCIES.useCheckCardDependencies({
- onSave: async (question) => {
- const { display, settings } = Lib.defaultDisplay(question.query());
- const { error } = await updateCard({
- id: card.id,
- dataset_query: question.datasetQuery(),
- display,
- visualization_settings: settings,
- result_metadata: resultMetadata,
- });
- if (error) {
- sendErrorToast(t`Failed to update metric query`);
- } else {
- sendSuccessToast(t`Metric query updated`);
- }
- },
- });
-
const handleChangeQuery = (query: Lib.Query) => {
setDatasetQuery(Lib.toJsQuery(query));
};
- const handleSave = () => {
- handleInitialSave(question.setResultsMetadata({ columns: resultMetadata }));
+ const handleSave = async () => {
+ const questionWithMetadata = question.setResultsMetadata({
+ columns: resultMetadata,
+ });
+ const { display, settings } = Lib.defaultDisplay(
+ questionWithMetadata.query(),
+ );
+ const { error } = await updateCard({
+ id: card.id,
+ dataset_query: questionWithMetadata.datasetQuery(),
+ display,
+ visualization_settings: settings,
+ result_metadata: resultMetadata,
+ });
+ if (error) {
+ sendErrorToast(t`Failed to update metric query`);
+ } else {
+ sendSuccessToast(t`Metric query updated`);
+ }
};
const handleCancel = () => {
@@ -156,7 +147,7 @@ function MetricQueryPageBody({
errorMessage={validationResult.errorMessage}
isValid={validationResult.isValid}
isDirty={isDirty}
- isSaving={isSaving || isCheckingDependencies}
+ isSaving={isSaving}
onSave={handleSave}
onCancel={handleCancel}
/>
@@ -172,18 +163,7 @@ function MetricQueryPageBody({
/>
</Card>
</PageContainer>
- {isConfirmationShown && checkData != null && (
- <PLUGIN_DEPENDENCIES.CheckDependenciesModal
- checkData={checkData}
- opened
- onSave={handleSaveAfterConfirmation}
- onClose={handleCloseConfirmation}
- />
- )}
- <LeaveRouteConfirmModal
- route={route}
- isEnabled={isDirty && !isSaving && !isCheckingDependencies}
- />
+ <LeaveRouteConfirmModal route={route} isEnabled={isDirty && !isSaving} />
</>
);
}
diff --git a/frontend/src/metabase/plugins/index.ts b/frontend/src/metabase/plugins/index.ts
index 8521c80fdb3..cf51eab3cac 100644
--- a/frontend/src/metabase/plugins/index.ts
+++ b/frontend/src/metabase/plugins/index.ts
@@ -151,10 +151,6 @@ export {
export {
PLUGIN_DEPENDENCIES,
type DependencyGraphPageContextType,
- type CheckDependenciesFormProps,
- type CheckDependenciesModalProps,
- type UseCheckDependenciesProps,
- type UseCheckDependenciesResult,
} from "./oss/dependencies";
export { PLUGIN_UPLOAD_MANAGEMENT } from "./oss/upload-management";
export { PLUGIN_WHITELABEL } from "./oss/whitelabel";
diff --git a/frontend/src/metabase/plugins/oss/dependencies.ts b/frontend/src/metabase/plugins/oss/dependencies.ts
index c2ee5318881..cdd4f03efde 100644
--- a/frontend/src/metabase/plugins/oss/dependencies.ts
+++ b/frontend/src/metabase/plugins/oss/dependencies.ts
@@ -2,13 +2,7 @@ import type { ComponentType, Context, ReactNode } from "react";
import { createContext } from "react";
import { PluginPlaceholder } from "metabase/plugins/components/PluginPlaceholder";
-import type Question from "metabase-lib/v1/Question";
-import type {
- CheckDependenciesResponse,
- GetDependencyGraphRequest,
- UpdateSnippetRequest,
- UpdateTransformRequest,
-} from "metabase-types/api";
+import type { GetDependencyGraphRequest } from "metabase-types/api";
// Types
export type DependencyGraphPageContextType = {
@@ -16,62 +10,12 @@ export type DependencyGraphPageContextType = {
defaultEntry?: any;
};
-export type CheckDependenciesFormProps = {
- checkData: CheckDependenciesResponse;
- onSave: () => void | Promise<void>;
- onCancel: () => void;
-};
-
-export type CheckDependenciesModalProps = {
- checkData: CheckDependenciesResponse;
- opened: boolean;
- onSave: () => void | Promise<void>;
- onClose: () => void;
-};
-
-export type UseCheckDependenciesProps<TChange> = {
- onSave: (change: TChange) => Promise<void>;
-};
-
-export type UseCheckDependenciesResult<TChange> = {
- checkData?: CheckDependenciesResponse;
- isCheckingDependencies: boolean;
- isConfirmationShown: boolean;
- handleInitialSave: (change: TChange) => Promise<void>;
- handleSaveAfterConfirmation: () => Promise<void>;
- handleCloseConfirmation: () => void;
-};
-
-function useCheckDependencies<TChange>({
- onSave,
-}: UseCheckDependenciesProps<TChange>): UseCheckDependenciesResult<TChange> {
- return {
- isConfirmationShown: false,
- isCheckingDependencies: false,
- handleInitialSave: onSave,
- handleSaveAfterConfirmation: () => Promise.resolve(),
- handleCloseConfirmation: () => undefined,
- };
-}
-
type DependenciesPlugin = {
isEnabled: boolean;
getDataStudioDependencyRoutes: () => ReactNode;
getDataStudioDependencyDiagnosticsRoutes: () => ReactNode;
DependencyGraphPage: ComponentType;
DependencyGraphPageContext: Context<DependencyGraphPageContextType>;
- CheckDependenciesForm: ComponentType<CheckDependenciesFormProps>;
- CheckDependenciesModal: ComponentType<CheckDependenciesModalProps>;
- CheckDependenciesTitle: ComponentType;
- useCheckCardDependencies: (
- props: UseCheckDependenciesProps<Question>,
- ) => UseCheckDependenciesResult<Question>;
- useCheckSnippetDependencies: (
- props: UseCheckDependenciesProps<UpdateSnippetRequest>,
- ) => UseCheckDependenciesResult<UpdateSnippetRequest>;
- useCheckTransformDependencies: (
- props: UseCheckDependenciesProps<UpdateTransformRequest>,
- ) => UseCheckDependenciesResult<UpdateTransformRequest>;
useGetDependenciesCount: (args: GetDependencyGraphRequest) => {
dependenciesCount: number;
dependentsCount: number;
@@ -84,12 +28,6 @@ const getDefaultPluginDependencies = (): DependenciesPlugin => ({
getDataStudioDependencyDiagnosticsRoutes: () => null,
DependencyGraphPage: PluginPlaceholder,
DependencyGraphPageContext: createContext({}),
- CheckDependenciesForm: PluginPlaceholder,
- CheckDependenciesModal: PluginPlaceholder,
- CheckDependenciesTitle: PluginPlaceholder,
- useCheckCardDependencies: useCheckDependencies,
- useCheckSnippetDependencies: useCheckDependencies,
- useCheckTransformDependencies: useCheckDependencies,
useGetDependenciesCount: () => ({
dependenciesCount: 0,
dependentsCount: 0,
diff --git a/frontend/src/metabase/query_builder/components/DatasetEditor/DatasetEditorInner.tsx b/frontend/src/metabase/query_builder/components/DatasetEditor/DatasetEditorInner.tsx
index 85f22546637..4a18a36f90e 100644
--- a/frontend/src/metabase/query_builder/components/DatasetEditor/DatasetEditorInner.tsx
+++ b/frontend/src/metabase/query_builder/components/DatasetEditor/DatasetEditorInner.tsx
@@ -5,7 +5,6 @@ import {
type ReactNode,
useCallback,
useEffect,
- useLayoutEffect,
useMemo,
useRef,
useState,
@@ -25,7 +24,6 @@ import { LeaveConfirmModal } from "metabase/common/components/LeaveConfirmModal"
import { getSemanticTypeIcon } from "metabase/common/utils/fields";
import ButtonsS from "metabase/css/components/buttons.module.css";
import CS from "metabase/css/core/index.css";
-import { PLUGIN_DEPENDENCIES } from "metabase/plugins";
import {
setDatasetEditorTab,
setTemplateTagConfig,
@@ -487,23 +485,14 @@ const DatasetEditorInnerView = (props: DatasetEditorInnerProps) => {
};
const saveButtonRef = useRef<ActionButtonHandle>(null);
- const {
- checkData,
- isConfirmationShown,
- handleInitialSave,
- handleSaveAfterConfirmation,
- handleCloseConfirmation,
- } = PLUGIN_DEPENDENCIES.useCheckCardDependencies({
- onSave: async (question) => {
+ const handleInitialSave = useCallback(
+ async (question: Question) => {
await onSave(question, { rerunQuery: true });
await setQueryBuilderMode("view");
runQuestionQuery();
},
- });
-
- useLayoutEffect(() => {
- saveButtonRef.current?.resetState();
- }, [isConfirmationShown]);
+ [onSave, setQueryBuilderMode, runQuestionQuery],
+ );
const handleSave = useCallback(async () => {
const canBeDataset = checkCanBeModel(question);
@@ -782,15 +771,6 @@ const DatasetEditorInnerView = (props: DatasetEditorInnerProps) => {
onConfirm={handleCancelEdit}
onClose={closeModal}
/>
-
- {isConfirmationShown && checkData != null && (
- <PLUGIN_DEPENDENCIES.CheckDependenciesModal
- checkData={checkData}
- opened
- onSave={handleSaveAfterConfirmation}
- onClose={handleCloseConfirmation}
- />
- )}
</>
);
};
diff --git a/frontend/src/metabase/querying/components/NativeQueryEditor/SnippetFormModal/SnippetFormModal.tsx b/frontend/src/metabase/querying/components/NativeQueryEditor/SnippetFormModal/SnippetFormModal.tsx
index 48b40cec11b..9e304a5c419 100644
--- a/frontend/src/metabase/querying/components/NativeQueryEditor/SnippetFormModal/SnippetFormModal.tsx
+++ b/frontend/src/metabase/querying/components/NativeQueryEditor/SnippetFormModal/SnippetFormModal.tsx
@@ -5,7 +5,6 @@ import {
useCreateSnippetMutation,
useUpdateSnippetMutation,
} from "metabase/api";
-import { PLUGIN_DEPENDENCIES } from "metabase/plugins";
import { Flex, Modal } from "metabase/ui";
import type {
CreateSnippetRequest,
@@ -71,26 +70,16 @@ export function SnippetFormModal({
onClose?.();
}, [snippet, updateSnippet, onClose]);
- const {
- checkData,
- isConfirmationShown,
- handleInitialSave,
- handleSaveAfterConfirmation,
- handleCloseConfirmation,
- } = PLUGIN_DEPENDENCIES.useCheckSnippetDependencies({
- onSave: handleUpdate,
- });
-
const handleSubmit = useCallback(
async (values: SnippetFormValues) => {
if (isSavedSnippet(snippet)) {
setSnippet({ ...snippet, ...values });
- await handleInitialSave({ ...values, id: snippet.id });
+ await handleUpdate({ ...values, id: snippet.id });
} else {
await handleCreate(values);
}
},
- [snippet, handleCreate, handleInitialSave],
+ [snippet, handleCreate, handleUpdate],
);
return (
@@ -98,34 +87,19 @@ export function SnippetFormModal({
<Modal.Overlay />
<Modal.Content>
<Modal.Header px="xl">
- <Modal.Title>
- {isConfirmationShown ? (
- <PLUGIN_DEPENDENCIES.CheckDependenciesTitle />
- ) : (
- modalTitle
- )}
- </Modal.Title>
+ <Modal.Title>{modalTitle}</Modal.Title>
<Flex justify="flex-end">
<Modal.CloseButton />
</Flex>
</Modal.Header>
- <Modal.Body px={isConfirmationShown ? 0 : "xl"}>
- {isConfirmationShown && checkData != null ? (
- <PLUGIN_DEPENDENCIES.CheckDependenciesForm
- checkData={checkData}
- onSave={handleSaveAfterConfirmation}
- onCancel={handleCloseConfirmation}
- />
- ) : (
- <SnippetForm
- snippet={snippet}
- isEditing={isEditing}
- isDirty={checkData != null}
- onSubmit={handleSubmit}
- onArchive={handleArchive}
- onCancel={onClose}
- />
- )}
+ <Modal.Body px="xl">
+ <SnippetForm
+ snippet={snippet}
+ isEditing={isEditing}
+ onSubmit={handleSubmit}
+ onArchive={handleArchive}
+ onCancel={onClose}
+ />
</Modal.Body>
</Modal.Content>
</Modal.Root>
diff --git a/frontend/src/metabase/querying/metrics/components/MetricEditor/MetricEditor.tsx b/frontend/src/metabase/querying/metrics/components/MetricEditor/MetricEditor.tsx
index fec580638a5..4ac4bebce97 100644
--- a/frontend/src/metabase/querying/metrics/components/MetricEditor/MetricEditor.tsx
+++ b/frontend/src/metabase/querying/metrics/components/MetricEditor/MetricEditor.tsx
@@ -2,7 +2,6 @@ import { forwardRef, useState } from "react";
import { LeaveConfirmModal } from "metabase/common/components/LeaveConfirmModal";
import { SaveQuestionModal } from "metabase/common/components/SaveQuestionModal";
-import { PLUGIN_DEPENDENCIES } from "metabase/plugins";
import { Flex } from "metabase/ui";
import * as Lib from "metabase-lib";
import type Question from "metabase-lib/v1/Question";
@@ -50,15 +49,6 @@ export const MetricEditor = forwardRef<HTMLDivElement, MetricEditorProps>(
) {
const [modalType, setModalType] = useState<MetricModalType>();
const isRunnable = Lib.canRun(question.query(), "metric");
- const {
- checkData,
- isConfirmationShown,
- handleInitialSave,
- handleSaveAfterConfirmation,
- handleCloseConfirmation,
- } = PLUGIN_DEPENDENCIES.useCheckCardDependencies({
- onSave,
- });
const handleCreate = (question: Question) => {
return onCreate(question.setDefaultDisplay());
@@ -69,7 +59,7 @@ export const MetricEditor = forwardRef<HTMLDivElement, MetricEditorProps>(
};
const handleSave = async (question: Question) => {
- await handleInitialSave(question.setDefaultDisplay());
+ await onSave(question.setDefaultDisplay());
};
const handleConfirmCancel = () => {
@@ -94,7 +84,6 @@ export const MetricEditor = forwardRef<HTMLDivElement, MetricEditorProps>(
question={question}
isDirty={isDirty}
isRunnable={isRunnable}
- isConfirmationShown={isConfirmationShown}
onCreate={handleCreateStart}
onSave={handleSave}
onCancel={handleCancelStart}
@@ -135,14 +124,6 @@ export const MetricEditor = forwardRef<HTMLDivElement, MetricEditorProps>(
onClose={handleModalClose}
/>
)}
- {isConfirmationShown && checkData != null && (
- <PLUGIN_DEPENDENCIES.CheckDependenciesModal
- checkData={checkData}
- opened
- onSave={handleSaveAfterConfirmation}
- onClose={handleCloseConfirmation}
- />
- )}
</Flex>
);
},
diff --git a/frontend/src/metabase/querying/metrics/components/MetricEditor/MetricEditorHeader/MetricEditorHeader.tsx b/frontend/src/metabase/querying/metrics/components/MetricEditor/MetricEditorHeader/MetricEditorHeader.tsx
index ba24a78ab03..835a181e047 100644
--- a/frontend/src/metabase/querying/metrics/components/MetricEditor/MetricEditorHeader/MetricEditorHeader.tsx
+++ b/frontend/src/metabase/querying/metrics/components/MetricEditor/MetricEditorHeader/MetricEditorHeader.tsx
@@ -1,5 +1,5 @@
import cx from "classnames";
-import { useLayoutEffect, useRef } from "react";
+import { useRef } from "react";
import { t } from "ttag";
import {
@@ -17,7 +17,6 @@ type MetricEditorHeaderProps = {
question: Question;
isDirty: boolean;
isRunnable: boolean;
- isConfirmationShown: boolean;
onCreate: (question: Question) => void;
onSave: (question: Question) => Promise<void>;
onCancel: () => void;
@@ -27,7 +26,6 @@ export function MetricEditorHeader({
question,
isDirty,
isRunnable,
- isConfirmationShown,
onCreate,
onSave,
onCancel,
@@ -36,10 +34,6 @@ export function MetricEditorHeader({
const handleCreate = () => onCreate(question);
const handleSave = () => onSave(question);
- useLayoutEffect(() => {
- saveButtonRef.current?.resetState();
- }, [isConfirmationShown]);
-
return (
<EditBar
className={S.root}
diff --git a/frontend/src/metabase/transforms/pages/TransformQueryPage/TransformQueryPage.tsx b/frontend/src/metabase/transforms/pages/TransformQueryPage/TransformQueryPage.tsx
index 8daa0919694..5320d0a19c7 100644
--- a/frontend/src/metabase/transforms/pages/TransformQueryPage/TransformQueryPage.tsx
+++ b/frontend/src/metabase/transforms/pages/TransformQueryPage/TransformQueryPage.tsx
@@ -17,11 +17,7 @@ import { LeaveRouteConfirmModal } from "metabase/common/components/LeaveConfirmM
import { LoadingAndErrorWrapper } from "metabase/common/components/LoadingAndErrorWrapper";
import { PageContainer } from "metabase/data-studio/common/components/PageContainer";
import { useMetadataToasts } from "metabase/metadata/hooks";
-import {
- PLUGIN_DEPENDENCIES,
- PLUGIN_REMOTE_SYNC,
- PLUGIN_TRANSFORMS_PYTHON,
-} from "metabase/plugins";
+import { PLUGIN_REMOTE_SYNC, PLUGIN_TRANSFORMS_PYTHON } from "metabase/plugins";
import { getInitialUiState } from "metabase/querying/editor/components/QueryEditor";
import { useDispatch, useSelector } from "metabase/redux";
import { getMetadata } from "metabase/selectors/metadata";
@@ -34,6 +30,7 @@ import type {
DatasetQuery,
DraftTransformSource,
Transform,
+ UpdateTransformRequest,
} from "metabase-types/api";
import {
@@ -148,33 +145,6 @@ function TransformQueryPageBody({
dryRunError ?? lastRunError,
);
- const {
- checkData,
- isCheckingDependencies,
- isConfirmationShown,
- handleInitialSave,
- handleSaveAfterConfirmation,
- handleCloseConfirmation,
- } = PLUGIN_DEPENDENCIES.useCheckTransformDependencies({
- onSave: async (request) => {
- const { error } = await updateTransform(request);
- if (error) {
- const message = getErrorMessage(error);
- sendErrorToast(
- message
- ? t`Failed to update transform query: ${message}`
- : t`Failed to update transform query`,
- );
- } else {
- sendSuccessToast(t`Transform query updated`);
-
- if (isEditMode) {
- dispatch(push(Urls.transform(transform.id)));
- }
- }
- },
- });
-
const handleResetRef = useLatest(() => {
setSource(transform.source);
setUiState(getInitialUiState());
@@ -197,7 +167,25 @@ function TransformQueryPageBody({
}
}, [isRemoteSyncReadOnly, isEditMode, dispatch, transform.id]);
- const handleSave = async () => {
+ const handleSave = async (request: UpdateTransformRequest) => {
+ const { error } = await updateTransform(request);
+ if (error) {
+ const message = getErrorMessage(error);
+ sendErrorToast(
+ message
+ ? t`Failed to update transform query: ${message}`
+ : t`Failed to update transform query`,
+ );
+ } else {
+ sendSuccessToast(t`Transform query updated`);
+
+ if (isEditMode) {
+ dispatch(push(Urls.transform(transform.id)));
+ }
+ }
+ };
+
+ const handleSaveAttempt = async () => {
if (!isCompleteSource(source)) {
return;
}
@@ -208,7 +196,7 @@ function TransformQueryPageBody({
openTurnOffIncremental();
return;
}
- await handleInitialSave({ id: transform.id, source });
+ await handleSave({ id: transform.id, source });
};
const handleConfirmTurnOffIncremental = async () => {
@@ -217,7 +205,7 @@ function TransformQueryPageBody({
}
closeTurnOffIncremental();
const values = getInitialValues({ incremental: false });
- await handleInitialSave({
+ await handleSave({
id: transform.id,
source: buildIncrementalSource(source, values),
target: buildIncrementalTarget(transform.target, values),
@@ -241,7 +229,7 @@ function TransformQueryPageBody({
source={source}
isSaving={isSaving}
isDirty={isDirty}
- handleSave={handleSave}
+ handleSave={handleSaveAttempt}
handleCancel={handleCancel}
transform={transform}
readOnly={readOnly}
@@ -305,14 +293,6 @@ function TransformQueryPageBody({
)}
</Box>
</PageContainer>
- {isConfirmationShown && checkData != null && (
- <PLUGIN_DEPENDENCIES.CheckDependenciesModal
- checkData={checkData}
- opened
- onSave={handleSaveAfterConfirmation}
- onClose={handleCloseConfirmation}
- />
- )}
<ConfirmModal
opened={isTurnOffIncrementalShown}
title={t`Turn off incremental processing?`}
@@ -323,7 +303,7 @@ function TransformQueryPageBody({
/>
<LeaveRouteConfirmModal
route={route as Route}
- isEnabled={isDirty && !isSaving && !isCheckingDependencies}
+ isEnabled={isDirty && !isSaving}
onConfirm={rejectProposed}
/>
</>
diff --git a/frontend/test/__support__/server-mocks/dependencies.ts b/frontend/test/__support__/server-mocks/dependencies.ts
index 6c062616833..512df991fb4 100644
--- a/frontend/test/__support__/server-mocks/dependencies.ts
+++ b/frontend/test/__support__/server-mocks/dependencies.ts
@@ -1,35 +1,12 @@
import fetchMock from "fetch-mock";
import type {
- CheckDependenciesResponse,
DependencyGraph,
DependencyNode,
ListBreakingGraphNodesResponse,
ListUnreferencedGraphNodesResponse,
} from "metabase-types/api";
-export function setupCheckCardDependenciesEndpoint(
- response: CheckDependenciesResponse,
-) {
- fetchMock.post("path:/api/ee/dependencies/check-card", response);
-}
-
-export function setupCheckCardDependenciesEndpointError() {
- fetchMock.post("path:/api/ee/dependencies/check-card", { status: 500 });
-}
-
-export function setupCheckSnippetDependenciesEndpoint(
- response: CheckDependenciesResponse,
-) {
- fetchMock.post("path:/api/ee/dependencies/check-snippet", response);
-}
-
-export function setupCheckTransformDependenciesEndpoint(
- response: CheckDependenciesResponse,
-) {
- fetchMock.post("path:/api/ee/dependencies/check-transform", response);
-}
-
export function setupListGraphNodeDependentsEndpoint(nodes: DependencyNode[]) {
fetchMock.get("path:/api/ee/dependencies/graph/dependents", nodes);
}
diff --git a/src/metabase/lib/core.cljc b/src/metabase/lib/core.cljc
index 841d85837e8..d5bf5c012e7 100644
--- a/src/metabase/lib/core.cljc
+++ b/src/metabase/lib/core.cljc
@@ -1625,8 +1625,10 @@
duplicate-column-error
find-bad-refs
find-bad-refs-with-source
+ missing-card-error
missing-column-error
missing-table-alias-error
+ missing-table-error
syntax-error
validation-exception-error]
[metabase.lib.walk.util
diff --git a/src/metabase/lib/schema/validate.cljc b/src/metabase/lib/schema/validate.cljc
index 5fa910e5e3c..c28468ec735 100644
--- a/src/metabase/lib/schema/validate.cljc
+++ b/src/metabase/lib/schema/validate.cljc
@@ -13,6 +13,18 @@
[:type [:= :missing-table-alias]]
[:name :string]])
+(mr/def ::missing-table-error
+ "A stage's `:source-table` references a table that is missing or inactive (e.g. dropped during sync).
+ The offending table is identified by the `:source-entity-*` keys, so no `:name` is carried."
+ [:map
+ [:type [:= :missing-table]]])
+
+(mr/def ::missing-card-error
+ "A stage's `:source-card` references a card that has been deleted.
+ The offending card is identified by the `:source-entity-*` keys, so no `:name` is carried."
+ [:map
+ [:type [:= :missing-card]]])
+
(mr/def ::duplicate-column-error
[:map
[:type [:= :duplicate-column]]
@@ -28,7 +40,8 @@
[:message :string]])
(mr/def ::validate-error-type
- [:enum :missing-column :missing-table-alias :duplicate-column :syntax-error :validation-exception-error])
+ [:enum :missing-column :missing-table-alias :missing-table :missing-card :duplicate-column :syntax-error
+ :validation-exception-error])
(mr/def ::error
[:and
@@ -36,6 +49,8 @@
[:multi {:dispatch #(-> % :type keyword)}
[:missing-column [:ref ::missing-column-error]]
[:missing-table-alias [:ref ::missing-table-alias-error]]
+ [:missing-table [:ref ::missing-table-error]]
+ [:missing-card [:ref ::missing-card-error]]
[:duplicate-column [:ref ::duplicate-column-error]]
[:syntax-error [:ref ::syntax-error]]
[:validation-exception-error [:ref ::validation-exception-error]]]])
@@ -61,6 +76,8 @@
[:multi {:dispatch #(-> % :type keyword)}
[:missing-column [:merge [:ref ::missing-column-error] [:ref ::source-entity]]]
[:missing-table-alias [:merge [:ref ::missing-table-alias-error] [:ref ::source-entity]]]
+ [:missing-table [:merge [:ref ::missing-table-error] [:ref ::source-entity]]]
+ [:missing-card [:merge [:ref ::missing-card-error] [:ref ::source-entity]]]
[:duplicate-column [:merge [:ref ::duplicate-column-error] [:ref ::source-entity]]]
[:syntax-error [:merge [:ref ::syntax-error] [:ref ::source-entity]]]
[:validation-exception-error [:merge [:ref ::validation-exception-error] [:ref ::source-entity]]]]])
diff --git a/src/metabase/lib/validate.cljc b/src/metabase/lib/validate.cljc
index 5bcb5ff3f21..8844ebae3bf 100644
--- a/src/metabase/lib/validate.cljc
+++ b/src/metabase/lib/validate.cljc
@@ -13,6 +13,18 @@
[metabase.util.malli :as mu]
[metabase.util.performance :as perf]))
+(mu/defn missing-table-error :- [:ref ::lib.schema.validate/missing-table-error]
+ "Create a missing-table lib validation error. The offending table is carried via the `:source-entity-*`
+ keys, so this error has no `:name`."
+ []
+ {:type :missing-table})
+
+(mu/defn missing-card-error :- [:ref ::lib.schema.validate/missing-card-error]
+ "Create a missing-card lib validation error. The offending card is carried via the `:source-entity-*`
+ keys, so this error has no `:name`."
+ []
+ {:type :missing-card})
+
(mu/defn missing-column-error :- [:ref ::lib.schema.validate/missing-column-error]
"Create a missing-column lib validation error"
[col-name :- :string]
@@ -112,14 +124,51 @@
nil))
@bad-fields))
-(mu/defn find-bad-refs-with-source :- [:set ::lib.schema.validate/error-with-source]
- "Like [[find-bad-refs]] but includes source entity information for each error.
- Returns a set of error maps, each containing:
- - `:type` - the error type (e.g., `:missing-column`)
- - `:name` - the column name (for missing-column errors)
- - `:source-entity-type` - optional, `:table` or `:card` if source could be determined
- - `:source-entity-id` - optional, the ID of the source entity
- - `:soft?` - optional, true when the ref could be dropped (eg. from a `:fields` list) without breaking the query"
+(mu/defn- find-bad-source-refs :- [:set ::lib.schema.validate/error-with-source]
+ "Returns errors for any stage (including join stages) whose `:source-table`/`:source-card` references an
+ entity that no longer backs a valid query: a table that is missing or inactive (e.g. dropped from the
+ warehouse and retired during sync), or a source card that has been deleted.
+
+ The field-ref walk in [[find-bad-field-refs-with-source]] can't catch this: a `SELECT *`-style query has
+ no `:field` clauses to inspect, and retiring a table doesn't deactivate its fields. So we resolve each
+ stage source directly, reporting a `:missing-table`/`:missing-card` error carrying the offending entity as
+ the source, so dependency analysis attributes the breakage to it.
+
+ An inactive table is still returned by [[lib.metadata/table]] (it's fetched by id), so we catch it via the
+ `:active` flag; a truly deleted table id makes [[lib.metadata/table]] throw, so we treat a throw as a
+ deleted table and flag it too rather than letting it abort the analysis. A deleted source card resolves to
+ nil via [[lib.metadata/card]] (and a throw is likewise treated as unresolvable); an *archived* card still
+ resolves and can back a query, so it is not flagged."
+ [query :- ::lib.schema/query]
+ (let [mp (lib.metadata/->metadata-provider query)
+ bad-sources (volatile! #{})
+ flag! (fn [error entity-type entity-id]
+ (vswap! bad-sources conj
+ (assoc error
+ :source-entity-type entity-type
+ :source-entity-id entity-id)))]
+ (lib.walk/walk-stages
+ query
+ (fn [_query _path stage]
+ (when-let [table-id (:source-table stage)]
+ (let [table (try
+ (lib.metadata/table mp table-id)
+ (catch #?(:clj Throwable :cljs :default) _
+ nil))]
+ (when (or (nil? table) (not (:active table)))
+ (flag! (missing-table-error) :table table-id))))
+ (when-let [card-id (:source-card stage)]
+ (when (nil? (try
+ (lib.metadata/card mp card-id)
+ (catch #?(:clj Throwable :cljs :default) _
+ nil)))
+ (flag! (missing-card-error) :card card-id)))
+ nil))
+ @bad-sources))
+
+(mu/defn- find-bad-field-refs-with-source :- [:set ::lib.schema.validate/error-with-source]
+ "Returns source-annotated errors for any `:field` ref in the query that can't be resolved to an active
+ column. See [[find-bad-refs-with-source]] for the shape of each error map."
[query :- ::lib.schema/query]
(let [bad-fields (volatile! #{})]
(lib.walk/walk-clauses
@@ -145,6 +194,20 @@
nil))
@bad-fields))
+(mu/defn find-bad-refs-with-source :- [:set ::lib.schema.validate/error-with-source]
+ "Like [[find-bad-refs]] but includes source entity information for each error.
+ Returns a set of error maps, each containing:
+ - `:type` - the error type (e.g., `:missing-column`)
+ - `:name` - the column name (for missing-column errors)
+ - `:source-entity-type` - optional, `:table` or `:card` if source could be determined
+ - `:source-entity-id` - optional, the ID of the source entity
+ - `:soft?` - optional, true when the ref could be dropped (eg. from a `:fields` list) without breaking the query
+
+ Catches both unresolvable `:field` refs and stages whose `:source-table`/`:source-card` is missing or inactive."
+ [query :- ::lib.schema/query]
+ (into (find-bad-source-refs query)
+ (find-bad-field-refs-with-source query)))
+
(comment
(require '[metabase.lib.core :as lib])
(require '[metabase.lib.test-metadata :as meta])
diff --git a/test/metabase/lib/validate_test.cljc b/test/metabase/lib/validate_test.cljc
index 4a6d6838ead..c02da590521 100644
--- a/test/metabase/lib/validate_test.cljc
+++ b/test/metabase/lib/validate_test.cljc
@@ -174,6 +174,46 @@
:name "missingcol2"}}
(set errors))))))
+(deftest ^:parallel find-bad-refs-with-source-inactive-source-table-test
+ (testing "a query whose `:source-table` is inactive returns a missing-table error attributing the table"
+ (let [orders (meta/table-metadata :orders)
+ query (-> (lib.tu/mock-metadata-provider
+ meta/metadata-provider
+ {:tables [(assoc orders :active false)]})
+ (lib/query orders))]
+ (is (= #{{:type :missing-table
+ :source-entity-type :table
+ :source-entity-id (meta/id :orders)}}
+ (lib/find-bad-refs-with-source query)))))
+ (testing "a query on an active source table has no source-table errors"
+ (let [query (lib/query meta/metadata-provider (meta/table-metadata :orders))]
+ (is (empty? (lib/find-bad-refs-with-source query)))))
+ (testing "a query whose `:source-table` can't be resolved (deleted table) is flagged, not thrown"
+ (let [orders-query (lib/query meta/metadata-provider (meta/table-metadata :orders))
+ query-on-deleted-table (-> (lib.tu/mock-metadata-provider
+ {:database (lib.metadata/database meta/metadata-provider)
+ :tables [(meta/table-metadata :products)]})
+ (lib/query orders-query))]
+ (is (= #{{:type :missing-table
+ :source-entity-type :table
+ :source-entity-id (meta/id :orders)}}
+ (lib/find-bad-refs-with-source query-on-deleted-table))))))
+
+(deftest ^:parallel find-bad-refs-with-source-deleted-source-card-test
+ (let [card-id 101
+ products-query (lib/query meta/metadata-provider (meta/table-metadata :products))
+ card-query (as-> (lib.tu/metadata-provider-with-card-from-query
+ meta/metadata-provider card-id products-query) provider
+ (lib/query provider (lib.metadata/card provider card-id)))]
+ (testing "a query on a resolvable source card has no source-card errors"
+ (is (empty? (lib/find-bad-refs-with-source card-query))))
+ (testing "a query whose `:source-card` resolves to nil (deleted card) returns a missing-card error"
+ (let [query-on-deleted-card (lib/query meta/metadata-provider card-query)]
+ (is (= #{{:type :missing-card
+ :source-entity-type :card
+ :source-entity-id card-id}}
+ (lib/find-bad-refs-with-source query-on-deleted-card)))))))
+
(deftest ^:parallel find-bad-refs-with-source-multi-stage-test
(testing "invalid query referencing previous stage has no source"
(let [mp meta/metadata-provider