The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
source replacement (#69257)
William8ceeb1fce484
diff --git a/.clj-kondo/config/modules/config.edn b/.clj-kondo/config/modules/config.ednindex 7a0f857a5f2..435922b0f49 100644--- a/.clj-kondo/config/modules/config.edn+++ b/.clj-kondo/config/modules/config.edn@@ -775,6 +775,7 @@:friends #{legacy-mbql ;; Reuses internal schemas and a few helper functions. Deeply coupled to MBQL.lib-be ;; BE-specific extension of lib; tight coupling is acceptable.query-processor ;; Necessarily manipulates MBQL; better to pull most logic into a new module.+ source-swap ;; Manipulates MBQL queries; tight coupling with lib is expected.enterprise/sandbox ;; Really part of the QP, though its lives in the enterprise/ tree.}}@@ -1454,6 +1455,15 @@systemutil}}+ source-swap+ {:team "Graphy"+ :api #{metabase.source-swap.core+ metabase.source-swap.schema}+ :uses #{driver+ lib+ sql-tools+ util}}+sql-parsing{:team "DevEx":api #{metabase.sql-parsing.core}@@ -2159,7 +2169,8 @@{:team "Graphy":api #{metabase-enterprise.dependencies.apimetabase-enterprise.dependencies.core- metabase-enterprise.dependencies.init}+ metabase-enterprise.dependencies.init+ metabase-enterprise.dependencies.models.dependency}:uses #{analyzeapiapp-db@@ -2342,6 +2353,21 @@enterprise/serializationenterprise/transforms-python}}+ enterprise/replacement+ {:team "Graphy"+ :api #{metabase-enterprise.replacement.api}+ :uses #{api+ app-db+ events+ lib+ lib-be+ models+ queries+ source-swap+ task+ util+ enterprise/dependencies}}+enterprise/sandbox{:team "UX West":api #{metabase-enterprise.sandbox.api.routesdiff --git a/dev/src/dev/coverage.clj b/dev/src/dev/coverage.cljindex 140fb6159a0..629598ac6a5 100644--- a/dev/src/dev/coverage.clj+++ b/dev/src/dev/coverage.clj@@ -133,7 +133,9 @@(let [;; Try different possible file pathspossible-paths [(str "src/" file)file- (str "test/" file)]+ (str "test/" file)+ (str "enterprise/backend/src/" file)+ (str "enterprise/backend/test/" file)]source-file (first (filter #(and % (.exists (java.io.File. ^String %))) possible-paths))](when-not source-file(throw (ex-info (str "Could not find source file for: " file)diff --git a/dev/src/dev/mutation_testing.clj b/dev/src/dev/mutation_testing.cljindex 72d3c51674c..da2942e0068 100644--- a/dev/src/dev/mutation_testing.clj+++ b/dev/src/dev/mutation_testing.clj@@ -230,8 +230,13 @@(str "mutation-testing-lib-" short-ns "-" fn-name)))(defn create-branch!- "Create and checkout a new branch from the base branch for mutation testing a function."+ "Create and checkout a new branch from the base branch for mutation testing a function.+ Fails early if there are uncommitted changes."[target-ns fn-name]+ (let [status (:out (sh "git" "status" "--porcelain" "-uno"))]+ (when-not (str/blank? status)+ (throw (ex-info (str "Uncommitted changes detected. Commit or stash before running.\n" status)+ {:status status}))))(let [branch (branch-name target-ns fn-name)](sh "git" "checkout" (base-branch))(sh "git" "pull")@@ -441,19 +446,22 @@test-path-base (-> test-ns(str/replace "." "/")(str/replace "-" "_"))- source-ext (cond- (.exists (io/file (str "src/" ns-path ".cljc"))) ".cljc"- (.exists (io/file (str "src/" ns-path ".clj"))) ".clj"- :else ".cljc")- test-ext (cond- (.exists (io/file (str "test/" test-path-base ".cljc"))) ".cljc"- (.exists (io/file (str "test/" test-path-base ".clj"))) ".clj"- :else ".cljc")]+ source-prefixes ["src/" "enterprise/backend/src/"]+ test-prefixes ["test/" "enterprise/backend/test/"]+ find-file (fn [prefixes base exts]+ (first (for [prefix prefixes, ext exts+ :let [path (str prefix base ext)]+ :when (.exists (io/file path))]+ {:prefix prefix :ext ext :path path})))+ source-match (or (find-file source-prefixes ns-path [".cljc" ".clj"])+ {:prefix "src/" :ext ".cljc" :path (str "src/" ns-path ".cljc")})+ test-match (or (find-file test-prefixes test-path-base [".cljc" ".clj"])+ {:prefix "test/" :ext ".cljc" :path (str "test/" test-path-base ".cljc")})]{:target-ns (symbol target-ns):test-ns (symbol test-ns):short-name short-name- :source-path (str "src/" ns-path source-ext)- :test-path (str "test/" test-path-base test-ext)+ :source-path (:path source-match)+ :test-path (:path test-match):report-path (str "mutation-testing-report." target-ns ".before.md")}))(defn group-functions@@ -717,6 +725,70 @@(println " " (:primary-fn r) "-" (:error r))))(println "========================================")))+(defn run-one!+ "Run mutation testing for a single function. No branching or PR creation.+ Generates a report, invokes Claude to write tests, and verifies mutations are killed.++ Arguments:+ - fq-fn-sym — fully-qualified function symbol (e.g. 'my.ns/my-fn)+ - test-ns-sym — test namespace symbol (e.g. 'my.ns-test)++ Options:+ - :max-retries — max Claude retries for surviving mutations (default 2)"+ ([fq-fn-sym test-ns-sym] (run-one! fq-fn-sym test-ns-sym {}))+ ([fq-fn-sym test-ns-sym opts]+ (let [fq-fn-name (symbol fq-fn-sym)+ target-ns (symbol (namespace fq-fn-name))+ test-ns (symbol (str test-ns-sym))+ parsed (parse-namespace target-ns)+ {:keys [source-path test-path report-path]} parsed+ max-retries (get opts :max-retries 2)]++ ;; 1. Generate report+ (println "Generating mutation testing report for" (str target-ns) "...")+ (coverage/generate-report target-ns [test-ns] report-path)+ (println "Report written to" report-path)++ ;; 2. Run coverage for the single function+ (println "Running coverage for" (str fq-fn-name) "...")+ (let [coverage-results (coverage/test-namespace target-ns [test-ns])+ fn-data (get coverage-results fq-fn-name)]+ (when-not fn-data+ (throw (ex-info (str "Function not found in coverage results: " fq-fn-name)+ {:available (keys coverage-results)})))+ (let [mutations (:survived fn-data)]+ (if (empty? mutations)+ (do (println "No surviving mutations for" (str fq-fn-name) "— nothing to do.")+ {:status :success :killed [] :survived []})++ (do+ (println (count mutations) "surviving mutations")++ ;; 3. Invoke Claude+ (println "Invoking Claude to write tests...")+ (let [prompt (build-test-prompt {:target-ns target-ns+ :test-ns test-ns+ :source-path source-path+ :test-path test-path+ :fn-names [fq-fn-name]+ :mutations mutations})]+ (invoke-claude! prompt))++ ;; 4. Verify and retry+ (println "Verifying mutations killed...")+ (let [result (verify-and-retry! {:fn-names [fq-fn-name]+ :test-ns test-ns+ :test-path test-path+ :max-retries max-retries})]+ (println "\nResult:" (:status result))+ (println " Killed:" (count (:killed result)))+ (println " Survived:" (count (:survived result)))+ (when (seq (:survived result))+ (println "\n Surviving mutations:")+ (doseq [{:keys [description]} (:survived result)]+ (println " -" description)))+ result))))))))+(defn run!"Main entry point. Runs the full mutation testing workflow for a namespace.@@ -735,6 +807,11 @@(set-config! {:base-branch bb}))(when-let [pid (:project-id opts)](set-config! {:project-id pid}))+ ;; 0. Fail early if working tree is dirty+ (let [status (:out (sh "git" "status" "--porcelain" "-uno"))]+ (when-not (str/blank? status)+ (throw (ex-info (str "Uncommitted changes detected. Commit or stash before running.\n" status)+ {:status status}))))(let [parsed (parse-namespace target-ns-sym){:keys [target-ns test-ns report-path]} parsed];; 1. Generate report@@ -767,7 +844,15 @@(try(process-group! parsed group)(catch Exception e- (println "ERROR:" (.getMessage e))+ (let [log-file (str "mutation-testing-error-"+ (name (:primary-fn group))+ ".log")]+ (with-open [w (io/writer log-file)]+ (.write w (str "Error processing group: " (:primary-fn group) "\n"))+ (.write w (str "Message: " (.getMessage e) "\n\n"))+ (.printStackTrace e (java.io.PrintWriter. w)))+ (println "ERROR:" (.getMessage e))+ (println " Full stack trace logged to" log-file))(try (return-to-base!) (catch Exception _)){:error (.getMessage e):primary-fn (:primary-fn group)})))))]diff --git a/enterprise/backend/src/metabase_enterprise/api_routes/routes.clj b/enterprise/backend/src/metabase_enterprise/api_routes/routes.cljindex 9f6c5ebf8f8..ef03a3d773f 100644--- a/enterprise/backend/src/metabase_enterprise/api_routes/routes.clj+++ b/enterprise/backend/src/metabase_enterprise/api_routes/routes.clj@@ -32,6 +32,7 @@[metabase-enterprise.metabot-v3.tools.api][metabase-enterprise.permission-debug.api][metabase-enterprise.remote-sync.api]+ [metabase-enterprise.replacement.api][metabase-enterprise.sandbox.api.routes][metabase-enterprise.scim.routes][metabase-enterprise.semantic-search.api]@@ -121,6 +122,7 @@"/dependencies" (premium-handler metabase-enterprise.dependencies.api/routes :dependencies)"/email" (premium-handler metabase-enterprise.email.api/routes :cloud-custom-smtp)"/remote-sync" (premium-handler metabase-enterprise.remote-sync.api/routes :remote-sync)+ "/replacement" (premium-handler metabase-enterprise.replacement.api/routes :dependencies)"/embedding-hub" (premium-handler metabase-enterprise.embedding-hub.api/routes :embedding)"/gsheets" (-> gsheets.api/routes ;; gsheets requires both features.(premium-handler :attached-dwh)diff --git a/enterprise/backend/src/metabase_enterprise/dependencies/models/analysis_finding.clj b/enterprise/backend/src/metabase_enterprise/dependencies/models/analysis_finding.cljindex 499aeefd6cf..037b224a9b3 100644--- a/enterprise/backend/src/metabase_enterprise/dependencies/models/analysis_finding.clj+++ b/enterprise/backend/src/metabase_enterprise/dependencies/models/analysis_finding.clj@@ -3,6 +3,7 @@[metabase-enterprise.dependencies.dependency-types :as deps.dependency-types][metabase-enterprise.dependencies.models.analysis-finding-error :as deps.analysis-finding-error][metabase.models.interface :as mi]+ [metabase.util :as u][methodical.core :as methodical][toucan2.core :as t2]))@@ -32,7 +33,8 @@We use `:message` for the error detail if there was an `validation-exception-error`."[error]{:error-type (:type error)- :error-detail (or (:name error) (:message error))+ ;; error-detail cannot be longer than 254+ :error-detail (some-> (or (:name error) (:message error)) (u/truncate 254)):source-entity-type (:source-entity-type error):source-entity-id (:source-entity-id error)})diff --git a/enterprise/backend/src/metabase_enterprise/dependencies/models/dependency.clj b/enterprise/backend/src/metabase_enterprise/dependencies/models/dependency.cljindex 7595423cd4c..3dfc3d65747 100644--- a/enterprise/backend/src/metabase_enterprise/dependencies/models/dependency.clj+++ b/enterprise/backend/src/metabase_enterprise/dependencies/models/dependency.clj@@ -75,6 +75,11 @@:destination-filter-fn destination-filter-fn:source-filter-fn source-filter-fn})))+(defn direct-dependents+ "Returns the direct dependents for the given entity key sequence."+ [key-seq]+ (key-dependents key-seq))+(defn- key-dependencies"Get the dependency entity keys for the entity keys in `key-seq`.@@ -263,3 +268,28 @@(t2/delete! :model/Dependency :id [:in to-remove]))(when (seq to-add)(t2/insert! :model/Dependency to-add)))))++(defn swap-dependency!+ "Efficiently swap a dependency from old-source to new-source during replacement operations.+ This is more efficient than full dependency analysis since we know exactly what changed.++ If the new dependency already exists, we just delete the old one (to avoid duplicate key violation). If the old+ dependency doesn't exist, this is a no-op.++ Parameters:+ - entity-type: The type of the entity whose dependency is changing (e.g., :card)+ - entity-id: The ID of the entity+ - old-source: The source being replaced, as [source-type source-id] (e.g., [:card 783])+ - new-source: The new source, as [source-type source-id] (e.g., [:table 164])"+ [entity-type entity-id [old-source-type old-source-id] [new-source-type new-source-id]]+ (let [already-present? (t2/exists? :model/Dependency+ :from_entity_type entity-type :from_entity_id entity-id+ :to_entity_type new-source-type :to_entity_id new-source-id)]+ (if already-present?+ (t2/delete! :model/Dependency+ :from_entity_type entity-type :from_entity_id entity-id+ :to_entity_type old-source-type :to_entity_id old-source-id)+ (t2/update! :model/Dependency+ {:from_entity_type entity-type :from_entity_id entity-id+ :to_entity_type old-source-type :to_entity_id old-source-id}+ {:to_entity_type new-source-type :to_entity_id new-source-id}))))diff --git a/enterprise/backend/src/metabase_enterprise/replacement/api.clj b/enterprise/backend/src/metabase_enterprise/replacement/api.cljnew file mode 100644index 00000000000..7037bd6e44b--- /dev/null+++ b/enterprise/backend/src/metabase_enterprise/replacement/api.clj@@ -0,0 +1,89 @@+(ns metabase-enterprise.replacement.api+ "`/api/ee/replacement/` routes"+ (:require+ [metabase-enterprise.replacement.execute :as replacement.execute]+ [metabase-enterprise.replacement.models.replacement-run :as replacement-run]+ [metabase-enterprise.replacement.runner :as replacement.runner]+ [metabase-enterprise.replacement.schema :as replacement.schema]+ [metabase-enterprise.replacement.source-check :as replacement.source-check]+ [metabase.api.common :as api]+ [metabase.api.macros :as api.macros]+ [metabase.api.routes.common :refer [+auth]]+ [ring.util.response :as response]+ [toucan2.core :as t2]))++(set! *warn-on-reflection* true)++(api.macros/defendpoint :post "/check-replace-source" :- ::replacement.schema/check-replace-source-response+ "Check whether a source entity can be replaced by a target entity. Returns compatibility+ errors describing column mismatches, type mismatches, primary key mismatches, and foreign+ key mismatches."+ [_route-params+ _query-params+ {:keys [source_entity_id source_entity_type target_entity_id target_entity_type]}+ :- [:map+ [:source_entity_id ::replacement.schema/source-entity-id]+ [:source_entity_type ::replacement.schema/source-entity-type]+ [:target_entity_id ::replacement.schema/source-entity-id]+ [:target_entity_type ::replacement.schema/source-entity-type]]]+ (api/check-superuser)+ (replacement.source-check/check-replace-source+ [source_entity_type source_entity_id]+ [target_entity_type target_entity_id]))++(api.macros/defendpoint :post "/replace-source" :- [:map+ [:status [:= 202]] ;; throws to return 409+ [:body [:map {:closed true}+ [:run_id ::replacement.schema/run-id]]]]+ "Replace all usages of a source entity with a target entity asynchronously.+ Returns 202 with a run_id for polling. Returns 409 if a replacement is already running."+ [_route-params+ _query-params+ {:keys [source_entity_id source_entity_type target_entity_id target_entity_type]}+ :- [:map+ [:source_entity_id ::replacement.schema/source-entity-id]+ [:source_entity_type ::replacement.schema/source-entity-type]+ [:target_entity_id ::replacement.schema/source-entity-id]+ [:target_entity_type ::replacement.schema/source-entity-type]]]+ (api/check-superuser)+ (let [result (replacement.source-check/check-replace-source+ [source_entity_type source_entity_id]+ [target_entity_type target_entity_id])]+ (when-not (:success result)+ (throw (ex-info "Sources are not replaceable" {:status-code 400+ :errors (:errors result)}))))+ (let [work-fn (fn [progress]+ (replacement.runner/run-swap+ [source_entity_type source_entity_id]+ [target_entity_type target_entity_id]+ progress))+ job-row (replacement-run/create-run!+ source_entity_type source_entity_id+ target_entity_type target_entity_id api/*current-user-id*)+ progress (replacement-run/run-row->progress job-row)]+ (replacement.execute/execute-async! work-fn progress)+ (-> (response/response {:run_id (:id job-row)})+ (assoc :status 202))))++(api.macros/defendpoint :get "/runs/:id" :- ::replacement.schema/run+ "Get the status of a source replacement run."+ [{:keys [id]} :- [:map [:id ::replacement.schema/run-id]]]+ (api/check-superuser)+ (or (t2/select-one :model/ReplacementRun :id id)+ (throw (ex-info "Run not found" {:status-code 404}))))++(api.macros/defendpoint :post "/runs/:id/cancel" :- [:map [:success boolean?]]+ "Cancel a running source replacement."+ [{:keys [id]} :- [:map [:id ::replacement.schema/run-id]]]+ (api/check-superuser)+ (let [run (t2/select-one :model/ReplacementRun :id id)]+ (when-not run+ (throw (ex-info "Run not found" {:status-code 404})))+ (when-not (:is_active run)+ (throw (ex-info "Run is not active" {:status-code 409})))+ (replacement-run/cancel-run! id)+ {:success true}))++(def ^{:arglists '([request respond raise])} routes+ "`/api/ee/replacement` routes."+ (api.macros/ns-handler *ns* +auth))diff --git a/enterprise/backend/src/metabase_enterprise/replacement/execute.clj b/enterprise/backend/src/metabase_enterprise/replacement/execute.cljnew file mode 100644index 00000000000..3f36b1bec13--- /dev/null+++ b/enterprise/backend/src/metabase_enterprise/replacement/execute.clj@@ -0,0 +1,29 @@+(ns metabase-enterprise.replacement.execute+ "Async execution harness for source replacement. Launches work in a virtual thread,+ tracks progress in the `source_replacement_run` table, and provides a protocol for+ the work-fn to report progress and check for cancellation."+ (:require+ [metabase-enterprise.replacement.models.replacement-run :as replacement-run]+ [metabase-enterprise.replacement.protocols :as replacement.protocols]+ [metabase.util.jvm :as u.jvm]))++(set! *warn-on-reflection* true)++(defn execute-async!+ "Start an async source replacement run. Inserts a run record, launches `work-fn` in a+ virtual thread, and returns the run record immediately.++ `work-fn` receives a single argument implementing `IRunnerProgress`.++ Throws 409 if another run is already active."+ [work-fn progress]+ (when (replacement-run/active-run)+ (throw (ex-info "A source replacement is already running" {:status-code 409})))+ (replacement.protocols/start-run! progress)+ (u.jvm/in-virtual-thread*+ (try+ (work-fn progress)+ (replacement.protocols/succeed-run! progress)+ (catch Throwable t+ (when-not (replacement.protocols/canceled? progress)+ (replacement.protocols/fail-run! progress t))))))diff --git a/enterprise/backend/src/metabase_enterprise/replacement/field_refs.clj b/enterprise/backend/src/metabase_enterprise/replacement/field_refs.cljnew file mode 100644index 00000000000..e12454bd590--- /dev/null+++ b/enterprise/backend/src/metabase_enterprise/replacement/field_refs.clj@@ -0,0 +1,176 @@+(ns metabase-enterprise.replacement.field-refs+ (:require+ [metabase-enterprise.replacement.util :as replacement.util]+ [metabase-enterprise.replacement.walk :as replacement.walk]+ [metabase.lib.core :as lib]+ [metabase.lib.util :as lib.util]+ [metabase.models.visualization-settings :as vs]+ [metabase.source-swap.core :as source-swap]+ [toucan2.core :as t2]))++(set! *warn-on-reflection* true)++(defn- ref->column-name+ "Resolve a `ref` to a deduplicated column name used in `:visualization_settings`."+ [ref columns]+ (when-some [column (lib/find-matching-column ref columns)]+ ((some-fn :lib/deduplicated-name :name) column)))++(defn- upgrade-source-card-ref+ "Upgrade a ref used for a parameter source based on the card."+ [ref card-id card-id->card]+ (or (when (lib.util/field-clause? ref)+ (when-some [card (get card-id->card card-id)]+ (let [query (:dataset_query card)]+ (when (replacement.util/valid-query? query)+ (-> query+ lib/append-stage+ (source-swap/upgrade-field-ref -1 ref))))))+ ref))++(defn- card-upgrade-parameters+ "Upgrade refs in `:value_field` and `:label_field` in the `:values_source_config` for `parameters`."+ [parameters]+ (let [card-ids (replacement.walk/parameter-source-card-ids parameters)+ card-id->card (when (seq card-ids)+ (t2/select-pk->fn identity :model/Card :id [:in card-ids]))]+ (if (seq card-ids)+ (replacement.walk/walk-parameter-source-card-refs parameters #(upgrade-source-card-ref %1 %2 card-id->card))+ parameters)))++(defn- card-upgrade-field-refs!+ "Upgrade field refs in `:dataset_query` and `:visualization_settings` for a card."+ [card]+ (let [query (:dataset_query card)+ valid-query? (replacement.util/valid-query? query)+ query' (if valid-query?+ (source-swap/upgrade-field-refs-in-query query)+ query)+ viz-settings (:visualization_settings card)+ viz-settings' (if valid-query?+ (let [columns (lib/returned-columns query')]+ (lib/with-aggregation-list (lib/aggregations query')+ (some-> viz-settings+ vs/db->norm+ (replacement.walk/walk-viz-settings-refs #(ref->column-name % columns))+ vs/norm->db)))+ viz-settings)+ parameters (:parameters card)+ parameters' (if (seq parameters)+ (card-upgrade-parameters parameters)+ parameters)+ changes (cond-> {}+ (not= query query') (assoc :dataset_query query')+ (not= viz-settings viz-settings') (assoc :visualization_settings viz-settings')+ (not= parameters parameters') (assoc :parameters parameters'))+ ;; `:result_metadata` is set to nil for native queries if not present in changes.+ ;; `:verified-result-metadata?` prevents the Card model hooks from clearing it.+ changes (cond-> changes+ (and (seq changes)+ (lib/native-only-query? query'))+ (assoc :result_metadata (:result_metadata card)+ :verified-result-metadata? true))]+ (when (seq changes)+ (t2/update! :model/Card (:id card) changes))))++(defn- transform-upgrade-field-refs!+ "Upgrade field refs in `:source` for a transform."+ [transform]+ (let [source (:source transform)]+ (when (and (= :query (:type source))+ (replacement.util/valid-query? (:query source)))+ (let [query (:query source)+ query' (source-swap/upgrade-field-refs-in-query query)]+ (when (not= query query')+ (t2/update! :model/Transform (:id transform) {:source (assoc source :query query')}))))))++(defn- segment-upgrade-field-refs!+ "Upgrade field refs in `:definition` for a segment."+ [segment]+ (when (replacement.util/valid-query? (:definition segment))+ (let [query (:definition segment)+ query' (source-swap/upgrade-field-refs-in-query query)]+ (when (not= query query')+ (t2/update! :model/Segment (:id segment) {:definition query'})))))++(defn- measure-upgrade-field-refs!+ "Upgrade field refs in `:definition` for a measure."+ [measure]+ (when (replacement.util/valid-query? (:definition measure))+ (let [query (:definition measure)+ query' (source-swap/upgrade-field-refs-in-query query)]+ (when (not= query query')+ (t2/update! :model/Measure (:id measure) {:definition query'})))))++(defn- upgrade-parameter-target+ "Upgrade field refs in a parameter target."+ [target card-id card-id->card]+ (or (when-some [card (get card-id->card card-id)]+ (let [query (:dataset_query card)]+ (when (replacement.util/valid-query? query)+ (source-swap/upgrade-field-ref-in-parameter-target query target))))+ target))++(defn- dashcard-upgrade-field-refs!+ "Upgrade field refs in `:parameter_mappings` and `:visualization_settings` for a dashcard."+ [dashcard card-id->card]+ (let [parameter-mappings (:parameter_mappings dashcard)+ parameter-mappings' (replacement.walk/walk-parameter-mapping-targets parameter-mappings+ #(upgrade-parameter-target %1 %2 card-id->card))+ viz-settings (:visualization_settings dashcard)+ viz-settings' (some-> viz-settings+ vs/db->norm+ (replacement.walk/walk-viz-settings-click-behaviors #(upgrade-parameter-target %1 %2 card-id->card))+ vs/norm->db)+ changes (cond-> {}+ (not= parameter-mappings parameter-mappings')+ (assoc :parameter_mappings parameter-mappings')+ (not= viz-settings viz-settings')+ (assoc :visualization_settings viz-settings'))]+ (when (seq changes)+ (t2/update! :model/DashboardCard (:id dashcard) changes))))++(defn dashboard-upgrade-field-refs!+ "Upgrade field refs in `:parameters` for the `dashboard`, `:parameter_mappings` and `:visualization_settings` for all dashcards in the `dashboard`."+ [dashboard]+ (let [dashcards (t2/select :model/DashboardCard :dashboard_id (:id dashboard))+ parameters (or (:parameters dashboard) [])+ all-card-ids (into (replacement.walk/parameter-source-card-ids parameters)+ (mapcat (fn [dashcard]+ (concat+ (replacement.walk/parameter-mapping-card-ids (:parameter_mappings dashcard))+ (replacement.walk/viz-settings-click-behavior-card-ids (-> dashcard :visualization_settings vs/db->norm)))))+ dashcards)+ card-id->card (if (seq all-card-ids)+ (t2/select-pk->fn identity :model/Card :id [:in all-card-ids])+ {})+ parameters' (replacement.walk/walk-parameter-source-card-refs parameters #(upgrade-source-card-ref %1 %2 card-id->card))+ changes (cond-> {}+ (not= parameters parameters')+ (assoc :parameters parameters'))]+ (when (seq changes)+ (t2/update! :model/Dashboard (:id dashboard) changes))+ (doseq [dashcard dashcards]+ (dashcard-upgrade-field-refs! dashcard card-id->card))))++(defn upgrade-field-refs!+ "Upgrade field refs in an entity.++ `entity-ref` is a [type id] tuple like [:dashboard 123] or [:card 456].+ `entity` is an optional pre-fetched entity from bulk-load-metadata-for-entities!."+ ([[entity-type entity-id :as entity-ref]]+ (upgrade-field-refs! entity-ref (case entity-type+ :card (t2/select-one :model/Card :id entity-id)+ :transform (t2/select-one :model/Transform :id entity-id)+ :segment (t2/select-one :model/Segment :id entity-id)+ :measure (t2/select-one :model/Measure :id entity-id)+ :dashboard (t2/select-one :model/Dashboard :id entity-id)+ nil)))+ ([[entity-type _entity-id] entity]+ (case entity-type+ :card (card-upgrade-field-refs! entity)+ :transform (transform-upgrade-field-refs! entity)+ :segment (segment-upgrade-field-refs! entity)+ :measure (measure-upgrade-field-refs! entity)+ :dashboard (dashboard-upgrade-field-refs! entity)+ nil)))diff --git a/enterprise/backend/src/metabase_enterprise/replacement/models/replacement_run.clj b/enterprise/backend/src/metabase_enterprise/replacement/models/replacement_run.cljnew file mode 100644index 00000000000..51ae5401892--- /dev/null+++ b/enterprise/backend/src/metabase_enterprise/replacement/models/replacement_run.clj@@ -0,0 +1,150 @@+(ns metabase-enterprise.replacement.models.replacement-run+ (:require+ [metabase-enterprise.replacement.protocols :as replacement.protocols]+ [metabase.app-db.core :as mdb]+ [metabase.models.interface :as mi]+ [metabase.util.honey-sql-2 :as h2x]+ [metabase.util.log :as log]+ [methodical.core :as methodical]+ [toucan2.core :as t2]))++(set! *warn-on-reflection* true)++(methodical/defmethod t2/table-name :model/ReplacementRun [_model] :source_replacement_run)++(derive :model/ReplacementRun :metabase/model)++(t2/deftransforms :model/ReplacementRun+ {:status mi/transform-keyword+ :source_entity_type mi/transform-keyword+ :target_entity_type mi/transform-keyword})++(defn create-run!+ "Insert a new pending run. It becomes active when [[start-run!]] is called."+ [source-type source-id target-type target-id user-id]+ (t2/insert-returning-instance! :model/ReplacementRun+ {:source_entity_type source-type+ :source_entity_id source-id+ :target_entity_type target-type+ :target_entity_id target-id+ :user_id user-id+ :status :pending+ :is_active false+ :progress 0.0}))++(defn start-run!+ "Mark the active run as started."+ [run-id]+ (t2/update! :model/ReplacementRun+ :id run-id+ {:status :started+ :is_active true}))++(defn update-progress!+ "Update progress on the active run."+ [run-id progress]+ (t2/update! :model/ReplacementRun+ :id run-id+ :is_active true+ {:progress progress}))++(defn succeed-run!+ "Mark the active run as succeeded."+ [run-id]+ (t2/update! :model/ReplacementRun+ :id run-id+ :is_active true+ {:status :succeeded+ :progress 1.0+ :is_active nil+ :end_time :%now}))++(defn fail-run!+ "Mark the active run as failed."+ [run-id message]+ (t2/update! :model/ReplacementRun+ :id run-id+ :is_active true+ {:status :failed+ :is_active nil+ :end_time :%now+ :message message}))++(defn cancel-run!+ "Mark the active run as canceled."+ [run-id]+ (t2/update! :model/ReplacementRun+ :id run-id+ :is_active true+ {:status :canceled+ :is_active nil+ :end_time :%now+ :message "Canceled by user"}))++(defn timeout-old-runs!+ "Time out all active runs older than the specified age."+ [age unit]+ (t2/update! :model/ReplacementRun+ :is_active true+ :start_time [:< (h2x/add-interval-honeysql-form (mdb/db-type) :%now (- age) unit)]+ {:status :timeout+ :is_active nil+ :end_time :%now+ :message "Timed out by metabase"}))++(defn active-run+ "Return the single active run, or nil."+ []+ (t2/select-one :model/ReplacementRun :is_active true))++(def ^:private ^:const progress-batch-size+ "Write progress to DB every N items (and always on the final item)."+ 50)++(defn run-row->progress+ "Return an implementation of [[replacement.protocols/IRunnerProgress]] backed by the :model/ReplacementRun+ row. Advancing is periodically flushed to the db ever 50 writes. Can provide an optional promise that will delivered+ with the following values:++ - :run/success delivered when `succeed-run` is called+ - :run/fail delivered when `fail-run!` is called+ - :run/cancelled delivered when advance checks for cancellation. Note this might not be delivered on a cancelled run+ since we do not poll the database constantly. Just at the boundary or batch edges."+ ([row]+ (run-row->progress row nil))+ ([row on-complete]+ (let [run-id (:id row)+ total* (atom 0)+ completed* (atom 0)]+ (reify replacement.protocols/IRunnerProgress+ (set-total! [_ total] (reset! total* total))+ (advance! [this] (replacement.protocols/advance! this 1))+ (advance! [this n]+ (let [c (swap! completed* + n)+ t @total*+ prev (- c n)+ crossed-boundary? (or (= c t)+ (not= (quot prev progress-batch-size)+ (quot c progress-batch-size)))]+ (when crossed-boundary?+ (let [progress (if (pos? t) (double (/ c t)) 0.0)]+ (update-progress! run-id progress)+ (when (replacement.protocols/canceled? this)+ (log/infof "Replacement run %d was canceled" run-id)+ (when on-complete+ (deliver on-complete :run/cancelled))+ (throw (ex-info "Run canceled" {:run-id run-id})))))))+ (canceled? [_]+ (not (:is_active (t2/select-one [:model/ReplacementRun :is_active] :id run-id))))+ (start-run! [_]+ (start-run! run-id))+ (succeed-run! [_]+ (log/infof "Replacement run %d succeeded." run-id)+ (succeed-run! run-id)+ (when on-complete+ (deliver on-complete :run/success)))+ (fail-run! [_ throwable]+ (log/errorf throwable "Replacement run %d failed" run-id)+ (fail-run! run-id (ex-message throwable))+ (when on-complete+ (deliver on-complete :run/fail)))))))diff --git a/enterprise/backend/src/metabase_enterprise/replacement/protocols.clj b/enterprise/backend/src/metabase_enterprise/replacement/protocols.cljnew file mode 100644index 00000000000..56380a6e551--- /dev/null+++ b/enterprise/backend/src/metabase_enterprise/replacement/protocols.clj@@ -0,0 +1,16 @@+(ns metabase-enterprise.replacement.protocols+ (:require+ [potemkin.types :as p.types]))++(set! *warn-on-reflection* true)++(p.types/defprotocol+ IRunnerProgress+ (set-total! [this total]+ "Set the total number of items to process (across all phases). Called once.")+ (advance! [this] [this n]+ "Mark n items complete (default 1). Writes progress to DB periodically.")+ (canceled? [this]+ "Returns true if cancellation has been requested.")+ (start-run! [this])+ (succeed-run! [this])+ (fail-run! [this throwable]))diff --git a/enterprise/backend/src/metabase_enterprise/replacement/runner.clj b/enterprise/backend/src/metabase_enterprise/replacement/runner.cljnew file mode 100644index 00000000000..1bfd9c1d13c--- /dev/null+++ b/enterprise/backend/src/metabase_enterprise/replacement/runner.clj@@ -0,0 +1,166 @@+(ns metabase-enterprise.replacement.runner+ (:require+ [clojure.string :as str]+ [metabase-enterprise.replacement.field-refs :as replacement.field-refs]+ [metabase-enterprise.replacement.protocols :as replacement.protocols]+ [metabase-enterprise.replacement.source-swap :as replacement.source-swap]+ [metabase-enterprise.replacement.usages :as replacement.usages]+ [metabase-enterprise.replacement.util :as replacement.util]+ [metabase.lib-be.core :as lib-be]+ [metabase.lib.core :as lib]+ [metabase.lib.metadata.protocols :as lib.metadata.protocols]+ [metabase.util.log :as log]+ [toucan2.core :as t2]))++(set! *warn-on-reflection* true)++(def noop-progress+ "No-op progress tracker for REPL / non-async usage."+ (reify replacement.protocols/IRunnerProgress+ (set-total! [_ _total])+ (advance! [_])+ (advance! [_ _n])+ (canceled? [_] false)+ (start-run! [_])+ (succeed-run! [_])+ (fail-run! [_ _throwable])))++(defn bulk-load-metadata-for-entities!+ "Bulk load metadata for a batch of entities into the metadata provider cache.++ Fetches all card dataset queries in one query, converts them to lib queries, extracts all referenced entity IDs, and+ bulk loads all metadata. Entities are tuples from the dependency graph."+ [metadata-provider entities]+ (when-not (lib.metadata.protocols/cached-metadata-provider-with-cache? metadata-provider)+ (throw (ex-info "Must provided a cached metadata provider" {})))+ (letfn [(id->instances [m]+ (let [ids (into []+ (comp (filter #(= m (first %)))+ (map second))+ entities)]+ (when (seq ids)+ (into {}+ (map (juxt (fn [o] [m (:id o)]) identity))+ (t2/select (case m+ :card :model/Card+ :table :model/Table+ :dashboard :model/Dashboard+ :transform :model/Transform+ :segment :model/Segment+ :measure :model/Measure)+ {:where [:in :id ids]})))))]+ (let [cards (id->instances :card)+ tables (id->instances :table)+ dashboards (id->instances :dashboard)+ transforms (id->instances :transform)+ segments (id->instances :segment)+ measures (id->instances :measure)+ queries (into []+ (concat+ ;; Card dataset queries+ (eduction (comp (keep :dataset_query)+ (filter replacement.util/valid-query?)+ (map #(lib/query metadata-provider %)))+ (vals cards))+ ;; Transform query sources+ (eduction (comp (keep :source)+ (filter #(= :query (:type %)))+ (keep :query)+ (map #(lib/query metadata-provider %)))+ (vals transforms))+ ;; Segment definitions+ (eduction (comp (keep :definition)+ (map #(lib/query metadata-provider %)))+ (vals segments))+ ;; Measure definitions+ (eduction (comp (keep :definition)+ (map #(lib/query metadata-provider %)))+ (vals measures))))]++ (when (seq queries)+ ;; Extract all referenced entity IDs across all queries+ (let [referenced-ids (lib/all-referenced-entity-ids queries)]+ ;; Bulk load all metadata at once+ (lib-be/bulk-load-query-metadata! metadata-provider referenced-ids)))+ (merge {} cards tables dashboards transforms segments measures))))++(defn- failure-message+ "Build a human-readable message summarizing swap failures, showing at most 10 individual errors."+ [failures total]+ (let [n (count failures)+ max-shown 10+ lines (into []+ (comp (take max-shown)+ (map (fn [{:keys [entity error]}]+ (str " " entity ": " error))))+ failures)]+ (str n " of " total " entities failed\n"+ (str/join "\n" lines)+ (when (> n max-shown)+ (str "\n ... and " (- n max-shown) " more")))))++(defn- run-swap* [{:keys [all-transitive-dependents]}+ old-source new-source progress]+ (replacement.protocols/set-total! progress+ (+ (count all-transitive-dependents) ;; phase 1: upgrade+ (count all-transitive-dependents))) ;; phase 2: swap++ (let [db-id (case (first old-source)+ :card (t2/select-one-fn :database_id :model/Card :id (second old-source))+ :table (t2/select-one-fn :db_id :model/Table :id (second old-source)))+ batch-size 500]++ ;; phase 1: Upgrade field refs for ALL transitive dependents+ (doseq [batch (partition-all batch-size all-transitive-dependents)]+ (lib-be/with-metadata-provider-cache+ (let [metadata-provider (lib-be/application-database-metadata-provider db-id)+ loaded (bulk-load-metadata-for-entities! metadata-provider batch)]+ (doseq [entity batch+ :let [object (get loaded entity)]]+ ;; upgrade! knows how to handle all entity types including dashboards+ (replacement.field-refs/upgrade-field-refs! entity object)+ (replacement.protocols/advance! progress)))))++ ;; phase 2: Swap sources for ALL transitive dependents (with batched metadata warming)+ (let [failures (atom [])]+ (doseq [batch (partition-all batch-size all-transitive-dependents)]+ (lib-be/with-metadata-provider-cache+ (let [metadata-provider (lib-be/application-database-metadata-provider db-id)+ loaded (bulk-load-metadata-for-entities! metadata-provider batch)]++ (doseq [entity batch+ :let [object (get loaded entity)]]+ (try+ (replacement.source-swap/swap-source! entity object old-source new-source)+ (catch Exception e+ (log/warnf e "Failed to swap %s, continuing with next entity" entity)+ (swap! failures conj {:entity entity :error (ex-message e)})))+ (replacement.protocols/advance! progress)))))++ (when-let [fs (seq @failures)]+ (throw (ex-info (failure-message fs (count all-transitive-dependents))+ {:failures fs}))))))++(defn run-swap+ "Replace all usages of `old-source` with `new-source` across all dependent entities.++ Both arguments are [type id] pairs like [:card 123] or [:table 45].+ `progress` implements `IRunnerProgress` for tracking and cancellation.++ Example:+ (swap-source [:card 123] [:card 789])++ This finds all entities that depend on the old source and updates their queries+ to reference the new source instead. This includes ALL transitive dependents,+ which is necessary for implicit joins to work correctly (e.g., when card D filters+ on Products.Category but is based on card C → card B → card A → Orders).++ Returns {:swapped [...]} with the list of entities that were updated."+ ([old-source new-source]+ (run-swap old-source new-source noop-progress))+ ([old-source+ new-source+ progress]+ (let [all-transitive (replacement.usages/transitive-usages old-source)]+ (run-swap* {:all-transitive-dependents all-transitive}+ old-source new-source progress))))diff --git a/enterprise/backend/src/metabase_enterprise/replacement/schema.clj b/enterprise/backend/src/metabase_enterprise/replacement/schema.cljnew file mode 100644index 00000000000..a2561afbdcf--- /dev/null+++ b/enterprise/backend/src/metabase_enterprise/replacement/schema.clj@@ -0,0 +1,59 @@+(ns metabase-enterprise.replacement.schema+ (:require+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.source-swap.schema :as source-swap.schema]+ [metabase.util.malli.registry :as mr]+ [metabase.util.malli.schema :as ms]))++(set! *warn-on-reflection* true)++(mr/def ::source-entity-id+ [:or ::lib.schema.id/card ::lib.schema.id/table])++(mr/def ::source-entity-type+ [:enum :card :table])++(mr/def ::run-id+ pos-int?)++(mr/def ::run-status+ [:enum :pending :started :succeeded :failed :canceled :timeout])++(mr/def ::run+ [:map+ [:id ::run-id]+ [:status ::run-status]+ [:is_active [:maybe :boolean]]+ [:source_entity_type ::source-entity-type]+ [:source_entity_id ::source-entity-id]+ [:target_entity_type ::source-entity-type]+ [:target_entity_id ::source-entity-id]+ [:progress [:maybe number?]]+ [:message [:maybe :string]]+ [:user_id [:maybe ::lib.schema.id/user]]+ [:start_time ms/TemporalInstant]+ [:end_time [:maybe ms/TemporalInstant]]])++(mr/def ::column+ [:map+ [:id [:maybe ::lib.schema.id/field]]+ [:name :string]+ [:display_name :string]+ [:base_type [:maybe :string]]+ [:effective_type [:maybe :string]]+ [:semantic_type [:maybe :string]]])++(mr/def ::error-type+ [:enum :cycle-detected :database-mismatch :incompatible-implicit-joins])++(mr/def ::column-mapping+ [:map+ [:source {:optional true} [:maybe ::column]]+ [:target {:optional true} [:maybe ::column]]+ [:errors {:optional true} [:sequential ::source-swap.schema/column-error]]])++(mr/def ::check-replace-source-response+ [:map+ [:success :boolean]+ [:errors {:optional true} [:sequential ::error-type]]+ [:column_mappings {:optional true} [:sequential ::column-mapping]]])diff --git a/enterprise/backend/src/metabase_enterprise/replacement/source_check.clj b/enterprise/backend/src/metabase_enterprise/replacement/source_check.cljnew file mode 100644index 00000000000..607354678b2--- /dev/null+++ b/enterprise/backend/src/metabase_enterprise/replacement/source_check.clj@@ -0,0 +1,74 @@+(ns metabase-enterprise.replacement.source-check+ (:require+ [metabase-enterprise.replacement.schema :as replacement.schema]+ [metabase-enterprise.replacement.usages :as replacement.usages]+ [metabase.lib-be.core :as lib-be]+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.lib.schema.metadata :as lib.schema.metadata]+ [metabase.source-swap.core :as source-swap]+ [metabase.source-swap.schema :as source-swap.schema]+ [metabase.util :as u]+ [metabase.util.malli :as mu]+ [toucan2.core :as t2]))++(set! *warn-on-reflection* true)++(mu/defn- has-incoming-fks? :- :boolean+ "Returns true if any active field has a FK pointing to a field in `table-id`."+ [table-id :- ::lib.schema.id/table]+ (if-let [field-ids (not-empty (t2/select-pks-set :model/Field :table_id table-id :active true))]+ (t2/exists? :model/Field :fk_target_field_id [:in field-ids] :active true)+ false))++(mu/defn- format-column :- ::replacement.schema/column+ [col :- ::lib.schema.metadata/column]+ {:id (:id col)+ :name ((some-fn :lib/desired-column-alias :name) col)+ :display_name (or (:display-name col) "")+ :base_type (some-> (:base-type col) u/qualified-name)+ :effective_type (some-> (:effective-type col) u/qualified-name)+ :semantic_type (some-> (:semantic-type col) u/qualified-name)})++(mu/defn- fetch-source-database-id :- ::lib.schema.id/database+ "Fetch a database ID for a source."+ [entity-type :- ::replacement.schema/source-entity-type+ entity-id :- ::replacement.schema/source-entity-id]+ (case entity-type+ :card (t2/select-one-fn :database_id :model/Card :id entity-id)+ :table (t2/select-one-fn :db_id :model/Table :id entity-id)))++(mu/defn- format-column-mappings :- [:sequential ::replacement.schema/column-mapping]+ "Format column mappings by applying format-column to source and target columns."+ [mappings :- [:sequential ::source-swap.schema/column-mapping]]+ (mapv #(-> %+ (u/update-some :source format-column)+ (u/update-some :target format-column))+ mappings))++(mu/defn check-replace-source :- ::replacement.schema/check-replace-source-response+ "Check whether `old-source` can be replaced by `new-source`. Returns a map with+ `:success`, `:errors` (unique top-level error types), and `:column_mappings`.+ Arguments match `swap-source`: each is a `[entity-type entity-id]` pair."+ [[old-type old-id :as old-ref]+ [new-type new-id :as new-ref]]+ (if (= old-ref new-ref)+ {:success false}+ (let [source-db-id (fetch-source-database-id old-type old-id)+ target-db-id (fetch-source-database-id new-type new-id)+ db-mismatch? (not= source-db-id target-db-id)+ cycle? (some #(= new-ref %) (replacement.usages/transitive-usages old-ref))+ mappings (when-not db-mismatch?+ (-> (lib-be/application-database-metadata-provider source-db-id)+ (source-swap/check-column-mappings old-ref new-ref)+ format-column-mappings))+ has-missing? (some (fn [m] (and (:source m) (nil? (:target m)))) mappings)+ has-col-errors? (seq (mapcat :errors mappings))+ implicit-joins? (and (= old-type :table) (has-incoming-fks? old-id))+ success? (not (or db-mismatch? cycle? has-missing? has-col-errors? implicit-joins?))+ reported-errors (cond-> []+ db-mismatch? (conj :database-mismatch)+ cycle? (conj :cycle-detected)+ implicit-joins? (conj :incompatible-implicit-joins))]+ (cond-> {:success success?}+ (seq reported-errors) (assoc :errors reported-errors)+ (seq mappings) (assoc :column_mappings mappings)))))diff --git a/enterprise/backend/src/metabase_enterprise/replacement/source_swap.clj b/enterprise/backend/src/metabase_enterprise/replacement/source_swap.cljnew file mode 100644index 00000000000..de3b9a284a5--- /dev/null+++ b/enterprise/backend/src/metabase_enterprise/replacement/source_swap.clj@@ -0,0 +1,180 @@+(ns metabase-enterprise.replacement.source-swap+ "Swap sources namespace. Entrypoint is [[swap-source!]]. Internally, we rely on the field references having been+ already upgraded. Then we only have to swap concrete usages.++ We also need to update dependencies, which we do in two ways. We explicitly+ call [[models.dependency/swap-dependency!]] regardless of whether the query actually changed (dependency edges may+ shift even without query text changes). Then we fire the entity's update event only when actual changes occurred;+ the event handler will redo dependency calculation, but also triggers other necessary side-effects like revisions.+ This means dependency analysis runs twice when there are changes, which is intentional: suppressing the event+ handler's dependency work would be complicated and could risk the dep graph getting out of sync. If we measure a+ performance problem here, that would be a good optimization step. In practice most swaps don't change queries+ (the table was already upgraded and the query references are the same), so this is rarely an issue."+ (:require+ [metabase-enterprise.dependencies.models.dependency :as models.dependency]+ [metabase-enterprise.replacement.util :as replacement.util]+ [metabase-enterprise.replacement.walk :as replacement.walk]+ [metabase.api.common :as api]+ [metabase.events.core :as events]+ [metabase.lib.core :as lib]+ [metabase.models.visualization-settings :as vs]+ [metabase.queries.models.query :as queries.query]+ [metabase.source-swap.core :as source-swap]+ [toucan2.core :as t2]))++(set! *warn-on-reflection* true)++(defn- transform-swap-source!+ [transform old-source new-source]+ (when-let [query (get-in transform [:source :query])]+ (when (replacement.util/valid-query? query)+ (let [query' (source-swap/swap-source-in-query query old-source new-source)+ changes (cond-> {}+ (not= query query')+ (assoc :source (assoc (:source transform) :query query')))]+ (models.dependency/swap-dependency! :transform (:id transform) old-source new-source)+ (when (seq changes)+ (t2/update! :model/Transform (:id transform) changes)+ (events/publish-event! :event/transform-update+ {:object (merge transform changes) :user-id api/*current-user-id*}))))))++(defn- card-swap-source!+ [card old-source new-source]+ (when (replacement.util/valid-query? (:dataset_query card))+ (let [query (:dataset_query card)+ query' (source-swap/swap-source-in-query query old-source new-source)+ table-id (:table_id card)+ table-id' (:table-id (queries.query/query->database-and-table-ids query'))+ changes (cond-> {}+ (not= query query')+ (assoc :dataset_query query')++ (not= table-id table-id')+ (assoc :table_id table-id'))+ ;; `:result_metadata` is set to nil for native queries if not present in changes.+ ;; `verified-result-metadata?` prevents the Card model hooks from clearing it.+ changes (cond-> changes+ (and (seq changes)+ (lib/native-only-query? query'))+ (assoc :result_metadata (:result_metadata card)+ :verified-result-metadata? true))]+ (models.dependency/swap-dependency! :card (:id card) old-source new-source)+ (when (seq changes)+ (t2/update! :model/Card (:id card) changes)+ (events/publish-event! :event/card-update+ {:object (merge card changes)+ :user-id api/*current-user-id*+ :previous-object card})))))++(defn- segment-swap-source!+ [segment old-source new-source]+ (when (replacement.util/valid-query? (:definition segment))+ (let [query (:definition segment)+ query' (source-swap/swap-source-in-query query old-source new-source)+ table-id (:table_id segment)+ table-id' (:table-id (queries.query/query->database-and-table-ids query'))+ changes (cond-> {}+ (not= query query')+ (assoc :definition query')+ (not= table-id table-id')+ (assoc :table_id table-id'))]+ (models.dependency/swap-dependency! :segment (:id segment) old-source new-source)+ (when (seq changes)+ (t2/update! :model/Segment (:id segment) changes)+ (events/publish-event! :event/segment-update+ {:object (merge segment changes) :user-id api/*current-user-id*})))))++(defn- measure-swap-source!+ [measure old-source new-source]+ (when (replacement.util/valid-query? (:definition measure))+ (let [query (:definition measure)+ query' (source-swap/swap-source-in-query query old-source new-source)+ table-id (:table_id measure)+ table-id' (:table-id (queries.query/query->database-and-table-ids query'))+ changes (cond-> {}+ (not= query query')+ (assoc :definition query')+ (not= table-id table-id')+ (assoc :table_id table-id'))]+ (models.dependency/swap-dependency! :measure (:id measure) old-source new-source)+ (when (seq changes)+ (t2/update! :model/Measure (:id measure) changes)+ (events/publish-event! :event/measure-update+ {:object (merge measure changes) :user-id api/*current-user-id*})))))++(defn- swap-parameter-target+ "Swap field refs in a parameter target to reference the new source."+ [target card-id card-id->card old-source new-source]+ (or (when-some [card (get card-id->card card-id)]+ (let [query (:dataset_query card)]+ (when (replacement.util/valid-query? query)+ (source-swap/swap-source-in-parameter-target+ query target old-source new-source))))+ target))++(defn- dashcard-swap-source!+ [dashcard card-id->card old-source new-source]+ (let [update-fn #(swap-parameter-target %1 %2 card-id->card old-source new-source)+ parameter-mappings (:parameter_mappings dashcard)+ parameter-mappings' (replacement.walk/walk-parameter-mapping-targets parameter-mappings update-fn)+ viz-settings (:visualization_settings dashcard)+ viz-settings' (some-> viz-settings+ vs/db->norm+ (replacement.walk/walk-viz-settings-click-behaviors update-fn)+ vs/norm->db)+ changes (cond-> {}+ (not= parameter-mappings parameter-mappings')+ (assoc :parameter_mappings parameter-mappings')+ (not= viz-settings viz-settings')+ (assoc :visualization_settings viz-settings'))]+ (when (seq changes)+ (t2/update! :model/DashboardCard (:id dashcard) changes))))++(defn- dashboard-swap-source!+ [dashboard old-source new-source]+ (let [dashcards (t2/select :model/DashboardCard :dashboard_id (:id dashboard))+ all-card-ids (into #{}+ (mapcat (fn [dashcard]+ (concat+ (replacement.walk/parameter-mapping-card-ids+ (:parameter_mappings dashcard))+ (replacement.walk/viz-settings-click-behavior-card-ids+ (-> dashcard :visualization_settings vs/db->norm)))))+ dashcards)+ card-id->card (if (seq all-card-ids)+ (t2/select-pk->fn identity :model/Card :id [:in all-card-ids])+ {})+ any-changed? (reduce (fn [changed? dashcard]+ (or+ (dashcard-swap-source! dashcard card-id->card old-source new-source)+ changed?))+ false+ dashcards)]+ (when any-changed?+ (events/publish-event!+ :event/dashboard-update {:object (t2/select-one :model/Dashboard :id (:id dashboard))+ :user-id api/*current-user-id*}))))++(defn swap-source!+ "Swap old-source to new-source in an entity.++ `entity-ref` is a [type id] tuple like [:card 123] or [:dashboard 456].+ `entity` is an optional pre-fetched entity.."+ ([[entity-type entity-id :as entity-ref] old-source new-source]+ (swap-source! entity-ref+ (case entity-type+ :card (t2/select-one :model/Card :id entity-id)+ :transform (t2/select-one :model/Transform :id entity-id)+ :segment (t2/select-one :model/Segment :id entity-id)+ :measure (t2/select-one :model/Measure :id entity-id)+ :dashboard (t2/select-one :model/Dashboard :id entity-id)+ nil)+ old-source new-source))+ ([[entity-type _entity-id] entity old-source new-source]+ (case entity-type+ :card (card-swap-source! entity old-source new-source)+ :transform (transform-swap-source! entity old-source new-source)+ :segment (segment-swap-source! entity old-source new-source)+ :measure (measure-swap-source! entity old-source new-source)+ :dashboard (dashboard-swap-source! entity old-source new-source)+ nil)))diff --git a/enterprise/backend/src/metabase_enterprise/replacement/timeout.clj b/enterprise/backend/src/metabase_enterprise/replacement/timeout.cljnew file mode 100644index 00000000000..cb12924b5ee--- /dev/null+++ b/enterprise/backend/src/metabase_enterprise/replacement/timeout.clj@@ -0,0 +1,42 @@+(ns metabase-enterprise.replacement.timeout+ (:require+ [clojurewerkz.quartzite.jobs :as jobs]+ [clojurewerkz.quartzite.schedule.calendar-interval :as calendar-interval]+ [clojurewerkz.quartzite.triggers :as triggers]+ [metabase-enterprise.replacement.models.replacement-run :as replacement-run]+ [metabase.task.core :as task]+ [metabase.util.log :as log]))++(set! *warn-on-reflection* true)++(def ^:private job-key "metabase-enterprise.replacement.timeout")++(defn- timeout-replacement-runs! [_ctx]+ (log/trace "Timing out old source replacement runs.")+ (try+ (replacement-run/timeout-old-runs! 30 :minute)+ (catch Throwable t+ (log/error t "Error timing out old source replacement runs."))))++(task/defjob ^{:doc "Timeout long-running source replacement runs."+ org.quartz.DisallowConcurrentExecution true}+ TimeoutReplacementRuns [ctx]+ (timeout-replacement-runs! ctx))++(defn- start-job! []+ (when (not (task/job-exists? job-key))+ (let [job (jobs/build+ (jobs/of-type TimeoutReplacementRuns)+ (jobs/with-identity (jobs/key job-key)))+ trigger (triggers/build+ (triggers/with-identity (triggers/key job-key))+ (triggers/start-now)+ (triggers/with-schedule+ (calendar-interval/schedule+ (calendar-interval/with-interval-in-minutes 10)+ (calendar-interval/with-misfire-handling-instruction-do-nothing))))]+ (task/schedule-task! job trigger))))++(defmethod task/init! ::TimeoutReplacementRuns [_]+ (log/info "Scheduling timeout source replacement runs task.")+ (start-job!))diff --git a/enterprise/backend/src/metabase_enterprise/replacement/usages.clj b/enterprise/backend/src/metabase_enterprise/replacement/usages.cljnew file mode 100644index 00000000000..762ad64a6aa--- /dev/null+++ b/enterprise/backend/src/metabase_enterprise/replacement/usages.clj@@ -0,0 +1,26 @@+(ns metabase-enterprise.replacement.usages+ "Functions for finding all usages of a given source (card or table)."+ (:require+ [metabase-enterprise.dependencies.models.dependency :as deps]))++(set! *warn-on-reflection* true)++(defn transitive-usages+ "Returns a seq of [entity-type entity-id] for all entities that use the given source.++ `source` is a [type id] pair like [:table 1] or [:card 42].++ Optionally takes a `graph` argument for testing with in-memory graphs.++ Example:+ (transitive-usages [:card 123])+ ;; => ([:card 456] [:card 789] [:transform 12])++ These are the entities whose queries reference the given source and would need+ to be updated if the source were replaced."+ ([[source-type source-id]]+ (transitive-usages nil [source-type source-id]))+ ([graph [source-type source-id]]+ (for [[entity-type entity-ids] (deps/transitive-dependents graph {source-type [{:id source-id}]})+ entity-id entity-ids]+ [entity-type entity-id])))diff --git a/enterprise/backend/src/metabase_enterprise/replacement/util.clj b/enterprise/backend/src/metabase_enterprise/replacement/util.cljnew file mode 100644index 00000000000..ead0ab04466--- /dev/null+++ b/enterprise/backend/src/metabase_enterprise/replacement/util.clj@@ -0,0 +1,12 @@+(ns metabase-enterprise.replacement.util+ (:require+ [metabase.util.malli :as mu]))++(set! *warn-on-reflection* true)++(mu/defn valid-query? :- :boolean+ "Returns true if `maybe-query` has at least one stage.++ This is used to skip fully broken queries in the app db."+ [maybe-query :- [:maybe :map]]+ (some? (seq (:stages maybe-query))))diff --git a/enterprise/backend/src/metabase_enterprise/replacement/walk.clj b/enterprise/backend/src/metabase_enterprise/replacement/walk.cljnew file mode 100644index 00000000000..ed05f89cca0--- /dev/null+++ b/enterprise/backend/src/metabase_enterprise/replacement/walk.clj@@ -0,0 +1,141 @@+(ns metabase-enterprise.replacement.walk+ (:require+ [medley.core :as m]+ [metabase.lib.core :as lib]+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.lib.schema.parameter :as lib.schema.parameter]+ [metabase.lib.util :as lib.util]+ [metabase.models.visualization-settings :as vs]+ [metabase.util.malli :as mu]))++(set! *warn-on-reflection* true)++(mu/defn parameter-source-card-ids :- [:set ::lib.schema.id/card]+ "Get all card IDs referenced by `:values_source_config` in the given parameters."+ [parameters :- [:sequential :map]]+ (into #{} (keep #(-> % :values_source_config :card_id)) parameters))++(mu/defn walk-parameter-source-card-refs :- [:sequential :map]+ "Walk the parameters and update the refs in `:value_field` and `:label_field`+ in the `:values_source_config` using the provided function.++ `ref-fn` will be called with a ref and a card ID and should return a new ref."+ [parameters :- [:sequential :map]+ ref-fn :- fn?]+ (letfn [(update-legacy-ref [legacy-ref card-id]+ (try+ ;; `value_field` and `label_field` are legacy refs+ #_{:clj-kondo/ignore [:discouraged-var]}+ (-> legacy-ref lib/->pMBQL (ref-fn card-id) lib/->legacy-MBQL)+ (catch Exception _+ legacy-ref)))]+ (mapv (fn [parameter]+ (if-some [card-id (-> parameter :values_source_config :card_id)]+ (-> parameter+ (m/update-existing-in [:values_source_config :value_field] #(update-legacy-ref % card-id))+ (m/update-existing-in [:values_source_config :label_field] #(update-legacy-ref % card-id)))+ parameter))+ parameters)))++(mu/defn parameter-mapping-card-ids :- [:set ::lib.schema.id/card]+ "Get all card IDs referenced by the parameter mappings."+ [parameter-mappings :- [:sequential :map]]+ (into #{} (keep :card_id) parameter-mappings))++(mu/defn walk-parameter-mapping-targets :- [:sequential :map]+ "Walk the parameter mappings and update the targets using the provided function.++ `target-fn` will be called with a parameter target and a card ID and should return a new parameter target."+ [parameter-mappings :- [:sequential :map]+ target-fn :- fn?]+ (mapv (fn [mapping]+ (or (when-some [card-id (:card_id mapping)]+ (when-some [target' (target-fn (:target mapping) card-id)]+ (assoc mapping :target target')))+ mapping))+ parameter-mappings))++(mu/defn walk-viz-settings-refs :- :map+ "Walk the viz settings and update the refs using the provided function.++ `ref-fn` will be called with a ref and should return either a string to be used as a column name."+ [viz-settings :- :map+ ref-fn :- fn?]+ (letfn [(update-legacy-ref-or-name [ref-or-name]+ (or (when (vector? ref-or-name)+ (when-some [ref (try (lib/->pMBQL ref-or-name) (catch Exception _ nil))]+ (when-some [ref-or-name' (ref-fn ref)]+ (when (string? ref-or-name')+ ref-or-name'))))+ ref-or-name))+ (update-legacy-refs-or-names [refs-or-names]+ (into [] (keep update-legacy-ref-or-name) refs-or-names))+ (column-setting-ref [#::vs{:keys [field-id field-metadata]}]+ (-> [:field (or field-metadata {}) field-id]+ lib/ensure-uuid))+ (update-column-setting [k v]+ (or (when (::vs/field-id k)+ (let [ref (column-setting-ref k)]+ (when-some [new-ref-or-name (ref-fn ref)]+ (cond+ (string? new-ref-or-name)+ [{::vs/column-name new-ref-or-name} v]+ (lib.util/field-clause? new-ref-or-name)+ [{::vs/field-id (lib/field-ref-id new-ref-or-name)+ ::vs/field-metadata (lib/options new-ref-or-name)} v]))))+ [k v]))+ (update-column-settings [column-settings]+ (m/map-kv update-column-setting column-settings))+ (update-column-formatting [column-formatting]+ (into [] (map #(m/update-existing % :columns update-legacy-refs-or-names)) column-formatting))]+ (-> viz-settings+ (m/update-existing ::vs/column-settings update-column-settings)+ (m/update-existing :table.column_formatting update-column-formatting)+ (m/update-existing-in [:pivot_table.column_split :rows] update-legacy-refs-or-names)+ (m/update-existing-in [:pivot_table.column_split :columns] update-legacy-refs-or-names)+ (m/update-existing-in [:pivot_table.column_split :values] update-legacy-refs-or-names)+ (m/update-existing-in [:pivot_table.collapsed_rows :rows] update-legacy-refs-or-names))))++(mu/defn click-behavior-card-id :- [:maybe ::lib.schema.id/card]+ "Returns the card ID from a click behavior if it links to a card."+ [click-behavior :- :map]+ (when (= ::vs/card (::vs/link-type click-behavior))+ (::vs/link-target-id click-behavior)))++(mu/defn viz-settings-click-behavior-card-ids :- [:set ::lib.schema.id/card]+ "Get all card IDs referenced by the click behaviors in the viz settings."+ [viz-settings :- :map]+ (into #{}+ (concat+ ;; global click behavior (non-table cards)+ (when-some [id (some-> viz-settings ::vs/click-behavior click-behavior-card-id)]+ [id])+ ;; per-column click behaviors+ (keep (fn [[_col-key col-viz]]+ (some-> col-viz ::vs/click-behavior click-behavior-card-id))+ (::vs/column-settings viz-settings)))))++(mu/defn walk-viz-settings-click-behaviors :- :map+ "Walk the click behaviors in the viz settings and update the targets using the provided function.++ `target-fn` will be called with a parameter target and a card ID and should return a new parameter target."+ [viz-settings :- :map+ target-fn :- fn?]+ (letfn [(update-mapping [card-id mapping]+ (or (when-some [target (some->> mapping ::vs/param-mapping-target ::vs/param-dimension+ (lib/normalize ::lib.schema.parameter/target))]+ (when-some [target' (target-fn target card-id)]+ (assoc-in mapping [::vs/param-mapping-target ::vs/param-dimension] target')))+ mapping))+ (update-click-behavior [click-behavior]+ (or (when-some [card-id (some-> click-behavior click-behavior-card-id)]+ (m/update-existing click-behavior ::vs/parameter-mapping+ (fn [mappings]+ (m/map-vals #(update-mapping card-id %) mappings))))+ click-behavior))+ (update-column-settings [col-settings]+ (m/map-vals #(m/update-existing % ::vs/click-behavior update-click-behavior)+ col-settings))]+ (-> viz-settings+ (m/update-existing ::vs/click-behavior update-click-behavior)+ (m/update-existing ::vs/column-settings update-column-settings))))diff --git a/enterprise/backend/src/metabase_enterprise/serialization/v2/models.clj b/enterprise/backend/src/metabase_enterprise/serialization/v2/models.cljindex 1071222c441..f3fa4be9d08 100644--- a/enterprise/backend/src/metabase_enterprise/serialization/v2/models.clj+++ b/enterprise/backend/src/metabase_enterprise/serialization/v2/models.clj@@ -109,6 +109,7 @@"QueryField""QueryTable""RecentViews"+ "ReplacementRun""RemoteSyncObject""RemoteSyncTask""Revision"diff --git a/enterprise/backend/test/metabase_enterprise/dependencies/models/dependency_test.clj b/enterprise/backend/test/metabase_enterprise/dependencies/models/dependency_test.cljindex 3060c2cf690..653edd57f1c 100644--- a/enterprise/backend/test/metabase_enterprise/dependencies/models/dependency_test.clj+++ b/enterprise/backend/test/metabase_enterprise/dependencies/models/dependency_test.clj@@ -292,3 +292,60 @@(is (= #{}(set deps))"Should be empty, chain is broken at card2")))))))))++(deftest swap-dependency!-test+ (testing "swap-dependency! handles the various cases correctly"+ (mt/with-temp [:model/Card card1 {}+ :model/Card card2 {}+ :model/Table table1 {:db_id (mt/id)}+ :model/Table table2 {:db_id (mt/id)}]+ (mt/with-model-cleanup [:model/Dependency]+ (testing "basic swap - old dep exists, new dep doesn't"+ (t2/insert! :model/Dependency {:from_entity_type :card+ :from_entity_id (:id card1)+ :to_entity_type :table+ :to_entity_id (:id table1)})+ (deps.graph/swap-dependency! :card (:id card1) [:table (:id table1)] [:table (:id table2)])+ (is (not (t2/exists? :model/Dependency+ :from_entity_type :card+ :from_entity_id (:id card1)+ :to_entity_type :table+ :to_entity_id (:id table1)))+ "Old dependency should be gone")+ (is (t2/exists? :model/Dependency+ :from_entity_type :card+ :from_entity_id (:id card1)+ :to_entity_type :table+ :to_entity_id (:id table2))+ "New dependency should exist"))++ (testing "swap when new dep already exists - should just delete old"+ ;; Set up: card2 depends on both table1 and table2+ (t2/insert! :model/Dependency [{:from_entity_type :card+ :from_entity_id (:id card2)+ :to_entity_type :table+ :to_entity_id (:id table1)}+ {:from_entity_type :card+ :from_entity_id (:id card2)+ :to_entity_type :table+ :to_entity_id (:id table2)}])+ ;; This would fail with duplicate key error before the fix+ (deps.graph/swap-dependency! :card (:id card2) [:table (:id table1)] [:table (:id table2)])+ (is (not (t2/exists? :model/Dependency+ :from_entity_type :card+ :from_entity_id (:id card2)+ :to_entity_type :table+ :to_entity_id (:id table1)))+ "Old dependency should be deleted")+ (is (t2/exists? :model/Dependency+ :from_entity_type :card+ :from_entity_id (:id card2)+ :to_entity_type :table+ :to_entity_id (:id table2))+ "New dependency should still exist (only one copy)")+ (is (= 1 (t2/count :model/Dependency+ :from_entity_type :card+ :from_entity_id (:id card2)+ :to_entity_type :table+ :to_entity_id (:id table2)))+ "Should have exactly one dependency to table2"))))))diff --git a/enterprise/backend/test/metabase_enterprise/replacement/api_test.clj b/enterprise/backend/test/metabase_enterprise/replacement/api_test.cljnew file mode 100644index 00000000000..932c1b23df0--- /dev/null+++ b/enterprise/backend/test/metabase_enterprise/replacement/api_test.clj@@ -0,0 +1,373 @@+(ns metabase-enterprise.replacement.api-test+ (:require+ [clojure.test :refer :all]+ [metabase-enterprise.dependencies.events]+ [metabase-enterprise.replacement.execute :as replacement.execute]+ [metabase-enterprise.replacement.models.replacement-run :as replacement-run]+ [metabase-enterprise.replacement.protocols :as replacement.protocols]+ [metabase.events.core :as events]+ [metabase.lib.core :as lib]+ [metabase.lib.metadata :as lib.metadata]+ [metabase.test :as mt]+ [metabase.util :as u]+ [toucan2.core :as t2]))++(set! *warn-on-reflection* true)++(defn- poll-run+ "Poll a run until it's no longer active or we hit the timeout.+ Returns the final run state."+ [run-id & {:keys [timeout-ms interval-ms]+ :or {timeout-ms 10000 interval-ms 100}}]+ (let [deadline (+ (System/currentTimeMillis) timeout-ms)]+ (loop []+ (let [run (mt/user-http-request :crowberto :get 200 (str "ee/replacement/runs/" run-id))]+ (if (or (not (:is_active run))+ (> (System/currentTimeMillis) deadline))+ run+ (do (Thread/sleep (long interval-ms))+ (recur)))))))++;;; ------------------------------------------------ POST /check-replace-source ------------------------------------------------++(deftest check-replace-source-success-test+ (testing "POST /check-replace-source — success with matching tables"+ (mt/with-premium-features #{:dependencies}+ (mt/with-temp [:model/Database {db-id :id} {}+ :model/Table {t1-id :id} {:db_id db-id}+ :model/Table {t2-id :id} {:db_id db-id}+ :model/Field _ {:table_id t1-id :name "id" :base_type :type/Integer+ :effective_type :type/Integer}+ :model/Field _ {:table_id t2-id :name "id" :base_type :type/Integer+ :effective_type :type/Integer}]+ (let [result (mt/user-http-request :crowberto :post 200 "ee/replacement/check-replace-source"+ {:source_entity_id t1-id+ :source_entity_type :table+ :target_entity_id t2-id+ :target_entity_type :table})]+ (is (true? (:success result)))+ (is (seq (:column_mappings result))))))))++(deftest check-replace-source-failure-test+ (testing "POST /check-replace-source — failure when databases differ"+ (mt/with-premium-features #{:dependencies}+ (mt/with-temp [:model/Database {db1-id :id} {}+ :model/Database {db2-id :id} {}+ :model/Table {t1-id :id} {:db_id db1-id}+ :model/Table {t2-id :id} {:db_id db2-id}+ :model/Field _ {:table_id t1-id :name "id" :base_type :type/Integer}+ :model/Field _ {:table_id t2-id :name "id" :base_type :type/Integer}]+ (let [result (mt/user-http-request :crowberto :post 200 "ee/replacement/check-replace-source"+ {:source_entity_id t1-id+ :source_entity_type :table+ :target_entity_id t2-id+ :target_entity_type :table})]+ (is (false? (:success result)))+ (is (= ["database-mismatch"] (:errors result))))))))++;;; ------------------------------------------------ POST /replace-source ------------------------------------------------++(deftest replace-model-acceptance-test+ (testing "Full replacement: MBQL children, native children, grandchildren, transforms, dashboards"+ (mt/with-premium-features #{:dependencies}+ (let [mp (mt/metadata-provider)]+ (mt/with-temp [:model/Card {old-id :id :as old-model}+ {:database_id (mt/id)+ :dataset_query (lib/query mp (lib.metadata/table mp (mt/id :orders)))+ :type :model+ :name "Old Model"}++ :model/Card {new-id :id}+ {:database_id (mt/id)+ :dataset_query (lib/query mp (lib.metadata/table mp (mt/id :orders)))+ :type :model+ :name "New Model"}++ :model/Card {mbql-child-1-id :id :as mbql-child-1}+ {:database_id (mt/id)+ :dataset_query (lib/query mp (lib.metadata/card mp old-id))+ :type :question+ :name "MBQL Child 1"}++ :model/Card {mbql-child-2-id :id :as mbql-child-2}+ {:database_id (mt/id)+ :dataset_query (lib/query mp (lib.metadata/card mp old-id))+ :type :question+ :name "MBQL Child 2"}++ :model/Card {native-child-id :id :as native-child}+ {:database_id (mt/id)+ :dataset_query (lib/native-query mp (str "SELECT * FROM {{#" old-id "}}"))+ :type :question+ :name "Native Child"}++ :model/Card {grandchild-id :id :as grandchild}+ {:database_id (mt/id)+ :dataset_query (lib/query mp (lib.metadata/card mp mbql-child-1-id))+ :type :question+ :name "Grandchild"}++ :model/Card {grandchild-native-id :id :as grandchild-native}+ {:database_id (mt/id)+ :dataset_query (lib/query mp (lib.metadata/card mp native-child-id))+ :type :question+ :name "Grandchild via Native"}++ :model/Transform {transform-id :id}+ {:source {:type "query"+ :query (lib/query mp (lib.metadata/card mp old-id))}+ :name "acceptance_transform"+ :target {:database (mt/id) :table "acceptance_transform"}}++ :model/Dashboard {dashboard-id :id}+ {:name "Acceptance Dashboard"}++ :model/DashboardCard _+ {:dashboard_id dashboard-id :card_id old-id}++ :model/DashboardCard _+ {:dashboard_id dashboard-id :card_id mbql-child-1-id}]++ (mt/with-model-cleanup [:model/ReplacementRun :model/Dependency]+ ;; Populate dependencies via events+ (doseq [card [old-model mbql-child-1 mbql-child-2 native-child grandchild grandchild-native]]+ (events/publish-event! :event/card-create {:object card :user-id (mt/user->id :crowberto)}))++ (let [response (mt/user-http-request :crowberto :post 202 "ee/replacement/replace-source"+ {:source_entity_id old-id+ :source_entity_type :card+ :target_entity_id new-id+ :target_entity_type :card})+ run-id (:run_id response)+ final (poll-run run-id)]+ (is (= "succeeded" (:status final)))++ (testing "MBQL children have updated source-card"+ (doseq [[label card-id] [["MBQL Child 1" mbql-child-1-id]+ ["MBQL Child 2" mbql-child-2-id]]]+ (testing label+ (let [q (t2/select-one-fn :dataset_query :model/Card :id card-id)]+ (is (= new-id (lib/primary-source-card-id q)))))))++ (testing "Native child references new model in SQL"+ (let [q (t2/select-one-fn :dataset_query :model/Card :id native-child-id)+ sql (get-in q [:stages 0 :native])]+ (is (re-find (re-pattern (str "\\{\\{#" new-id "[^0-9]")) sql))+ (is (not (re-find (re-pattern (str "\\{\\{#" old-id "[^0-9]")) sql)))))++ (testing "Grandchild still references its direct parent"+ (let [q (t2/select-one-fn :dataset_query :model/Card :id grandchild-id)]+ (is (= mbql-child-1-id (lib/primary-source-card-id q)))))++ (testing "Grandchild via native still references native child"+ (let [q (t2/select-one-fn :dataset_query :model/Card :id grandchild-native-id)]+ (is (= native-child-id (lib/primary-source-card-id q)))))++ (testing "Transform's source query references new model"+ (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)))))))))))++(deftest concurrent-run-returns-409-test+ (testing "POST /replace-source — returns 409 when another run is already active"+ (mt/with-premium-features #{:dependencies}+ (mt/with-temp [:model/Card {old-id :id} {:database_id (mt/id)+ :dataset_query (let [mp (mt/metadata-provider)]+ (lib/query mp (lib.metadata/table mp (mt/id :products))))+ :type :model+ :name "Old source"}+ :model/Card {new-id :id} {:database_id (mt/id)+ :dataset_query (let [mp (mt/metadata-provider)]+ (lib/query mp (lib.metadata/table mp (mt/id :products))))+ :type :model+ :name "New source"}+ :model/Card child-card {:database_id (mt/id)+ :dataset_query (let [mp (mt/metadata-provider)]+ (lib/query mp (lib.metadata/card mp old-id)))+ :type :question+ :name "Child card"}]+ (mt/with-model-cleanup [:model/ReplacementRun :model/Dependency]+ (events/publish-event! :event/card-create {:object child-card :user-id (mt/user->id :crowberto)})+ ;; Insert a fake active run to simulate one already running+ (let [run (replacement-run/create-run! :card old-id :card new-id (mt/user->id :crowberto))]+ (replacement-run/start-run! (:id run)))+ (mt/user-http-request :crowberto :post 409 "ee/replacement/replace-source"+ {:source_entity_id old-id+ :source_entity_type :card+ :target_entity_id new-id+ :target_entity_type :card}))))))++;;; ------------------------------------------------ GET /runs/:id ------------------------------------------------++(deftest replace-source-swaps-child-card-test+ (testing "POST /replace-source — swaps source reference in child card"+ (mt/with-premium-features #{:dependencies}+ (mt/with-temp [:model/Card {old-id :id} {:database_id (mt/id)+ :dataset_query (let [mp (mt/metadata-provider)]+ (lib/query mp (lib.metadata/table mp (mt/id :orders))))+ :type :model+ :name "Old Orders"}+ :model/Card {new-id :id} {:database_id (mt/id)+ :dataset_query (let [mp (mt/metadata-provider)]+ (lib/query mp (lib.metadata/table mp (mt/id :orders))))+ :type :model+ :name "New Orders"}+ :model/Card child-card {:database_id (mt/id)+ :dataset_query (let [mp (mt/metadata-provider)]+ (lib/query mp (lib.metadata/card mp old-id)))+ :type :question+ :name "Child Card"}]+ (mt/with-model-cleanup [:model/ReplacementRun :model/Dependency]+ (events/publish-event! :event/card-create {:object child-card :user-id (mt/user->id :crowberto)})+ (let [response (mt/user-http-request :crowberto :post 202 "ee/replacement/replace-source"+ {:source_entity_id old-id+ :source_entity_type :card+ :target_entity_id new-id+ :target_entity_type :card})+ run-id (:run_id response)+ final (poll-run run-id)]+ (is (= "succeeded" (:status final)))+ (let [child-query (t2/select-one-fn :dataset_query :model/Card :id (:id child-card))]+ (is (= new-id (get-in child-query [:stages 0 :source-card]))+ "Child card should now reference the new source card"))))))))++(deftest replace-source-poll-until-complete-test+ (testing "POST replace-source then GET /runs/:id — run reaches 'succeeded' status"+ (mt/with-premium-features #{:dependencies}+ (mt/with-temp [:model/Card {old-id :id} {:database_id (mt/id)+ :dataset_query (let [mp (mt/metadata-provider)]+ (lib/query mp (lib.metadata/table mp (mt/id :products))))+ :type :model+ :name "Old source"}+ :model/Card {new-id :id} {:database_id (mt/id)+ :dataset_query (let [mp (mt/metadata-provider)]+ (lib/query mp (lib.metadata/table mp (mt/id :products))))+ :type :model+ :name "New source"}+ :model/Card child-card {:database_id (mt/id)+ :dataset_query (let [mp (mt/metadata-provider)]+ (lib/query mp (lib.metadata/card mp old-id)))+ :type :question+ :name "Child card"}]+ (mt/with-model-cleanup [:model/ReplacementRun :model/Dependency]+ (events/publish-event! :event/card-create {:object child-card :user-id (mt/user->id :crowberto)})+ (let [response (mt/user-http-request :crowberto :post 202 "ee/replacement/replace-source"+ {:source_entity_id old-id+ :source_entity_type :card+ :target_entity_id new-id+ :target_entity_type :card})+ run-id (:run_id response)+ final-run (poll-run run-id)]+ (is (= "succeeded" (:status final-run))+ "Run should reach 'succeeded' status")+ (is (= 1.0 (:progress final-run))+ "Progress should be 1.0 when done")+ (is (nil? (:is_active final-run))+ "is_active should be nil when run is complete")+ (is (some? (:end_time final-run))+ "end_time should be set")))))))++(deftest get-run-not-found-test+ (testing "GET /runs/:id — returns 404 for non-existent run"+ (mt/with-premium-features #{:dependencies}+ (mt/user-http-request :crowberto :get 404 "ee/replacement/runs/999999"))))++(deftest execute-async-progress-tracking-test+ (testing "execute-async! invokes the protocol methods correctly"+ (mt/with-premium-features #{:dependencies}+ (let [state (atom {:total 0 :so-far 0 :status :pending})+ done? (promise)+ progress (reify replacement.protocols/IRunnerProgress+ (set-total! [_ total] (swap! state assoc :total total))+ (advance! [_] (swap! state update :so-far inc))+ (advance! [_ n] (swap! state update :so-far + n))+ (canceled? [_] false)+ (start-run! [_]+ (swap! state assoc :status :running))+ (succeed-run! [_]+ (swap! state assoc :status :succeeded)+ (deliver done? true))+ (fail-run! [_ throwable]+ (swap! state assoc :status :failed :message (ex-message throwable))+ (deliver done? true)))+ work-fn (fn [progress]+ (replacement.protocols/set-total! progress 3)+ (is (false? (replacement.protocols/canceled? progress)))+ (dotimes [_ 3]+ (replacement.protocols/advance! progress)))]+ ;; Poll until done+ (replacement.execute/execute-async! work-fn progress)+ (u/deref-with-timeout done? 2000)+ (is (=? {:total 3 :so-far 3 :status :succeeded}+ @state))))))++;;; ------------------------------------------------ POST /runs/:id/cancel ------------------------------------------------++(deftest cancel-run-test+ (testing "POST /runs/:id/cancel — cancels an active run"+ (mt/dataset test-data+ (mt/with-premium-features #{:dependencies}+ (mt/with-model-cleanup [:model/ReplacementRun]+ ;; Insert a run directly so we can cancel it without racing the async completion+ (let [run (replacement-run/create-run! :card 1 :card 2 (mt/user->id :crowberto))+ _ (replacement-run/start-run! (:id run))+ response (mt/user-http-request :crowberto :post 200+ (str "ee/replacement/runs/" (:id run) "/cancel"))]+ (is (true? (:success response)))+ (let [updated (t2/select-one :model/ReplacementRun :id (:id run))]+ (is (= :canceled (:status updated)))+ (is (nil? (:is_active updated))))))))))++(deftest cancel-inactive-run-returns-409-test+ (testing "POST /runs/:id/cancel — returns 409 for already-completed run"+ (mt/dataset test-data+ (mt/with-premium-features #{:dependencies}+ (mt/with-model-cleanup [:model/ReplacementRun]+ (let [run (replacement-run/create-run! :card 1 :card 2 (mt/user->id :crowberto))]+ (replacement-run/start-run! (:id run))+ (replacement-run/succeed-run! (:id run))+ (mt/user-http-request :crowberto :post 409+ (str "ee/replacement/runs/" (:id run) "/cancel"))))))))++(deftest cancel-not-found-run-returns-404-test+ (testing "POST /runs/:id/cancel — returns 404 for non-existent run"+ (mt/with-premium-features #{:dependencies}+ (mt/user-http-request :crowberto :post 404 "ee/replacement/runs/999999/cancel"))))++(deftest all-endpoints-require-superuser-test+ (testing "All /ee/replacement/ endpoints return 403 for non-admin users"+ (mt/with-premium-features #{:dependencies}+ (let [body {:source_entity_id 1+ :source_entity_type :table+ :target_entity_id 2+ :target_entity_type :table}]+ (doseq [[method url params] [[:post "ee/replacement/check-replace-source" body]+ [:post "ee/replacement/replace-source" body]+ [:get "ee/replacement/runs/1" nil]+ [:post "ee/replacement/runs/1/cancel" nil]]]+ (testing (str (name method) " " url)+ (is (= "You don't have permissions to do that."+ (if params+ (mt/user-http-request :rasta method 403 url params)+ (mt/user-http-request :rasta method 403 url))))))))))++(deftest all-endpoints-require-dependencies-feature-test+ (testing "All /ee/replacement/ endpoints return 402 without the :dependencies feature flag"+ (mt/with-premium-features #{}+ (doseq [[method url] [[:post "ee/replacement/check-replace-source"]+ [:post "ee/replacement/replace-source"]+ [:get "ee/replacement/runs/1"]+ [:post "ee/replacement/runs/1/cancel"]]]+ (testing (str (name method) " " url)+ (mt/assert-has-premium-feature-error+ "Dependency Tracking"+ (mt/user-http-request :crowberto method 402 url)))))))diff --git a/enterprise/backend/test/metabase_enterprise/replacement/execute_test.clj b/enterprise/backend/test/metabase_enterprise/replacement/execute_test.cljnew file mode 100644index 00000000000..8f0dc622c01--- /dev/null+++ b/enterprise/backend/test/metabase_enterprise/replacement/execute_test.clj@@ -0,0 +1,118 @@+(ns metabase-enterprise.replacement.execute-test+ (:require+ [clojure.string :as str]+ [clojure.test :refer :all]+ [metabase-enterprise.replacement.execute :as replacement.execute]+ [metabase-enterprise.replacement.models.replacement-run :as replacement-run]+ [metabase-enterprise.replacement.protocols :as replacement.protocols]+ [metabase.test :as mt]+ [metabase.util :as u]+ [toucan2.core :as t2]))++(set! *warn-on-reflection* true)++(deftest advance!-progress-write-boundaries-test+ (testing "advance! writes progress at batch boundaries and on final item"+ (mt/with-premium-features #{:dependencies}+ (mt/with-model-cleanup [:model/ReplacementRun]+ (let [writes (atom [])+ done? (promise)]+ (with-redefs [replacement-run/update-progress!+ (fn [_run-id progress]+ (swap! writes conj progress))]+ (let [record (replacement-run/create-run! :card 1 :card 2 (mt/user->id :rasta))+ progress (replacement-run/run-row->progress record done?)]+ (replacement.execute/execute-async!+ (fn [progress]+ (replacement.protocols/set-total! progress 120)+ ;; advance one-at-a-time for 120 items+ (dotimes [_ 120]+ (replacement.protocols/advance! progress)))+ progress)+ ;; wait for virtual thread+ (is (= :run/success (u/deref-with-timeout done? 500)))+ ;; Writes at items 50, 100, 120 (final) → progress 50/120, 100/120, 120/120+ (is (= [(double (/ 50 120)) (double (/ 100 120)) 1.0]+ @writes))+ (is (=? {:is_active nil :progress 1.0 :status :succeeded}+ (t2/select-one :model/ReplacementRun :id (:id record)))))))))))++(deftest advance!-count-arity-test+ (testing "advance! with count arity crosses boundaries correctly"+ (mt/with-premium-features #{:dependencies}+ (mt/with-model-cleanup [:model/ReplacementRun]+ (let [writes (atom [])+ done? (promise)]+ (with-redefs [replacement-run/update-progress!+ (fn [_run-id progress]+ (swap! writes conj progress))]+ (let [record (replacement-run/create-run! :card 1 :card 2 (mt/user->id :rasta))+ progress (replacement-run/run-row->progress record done?)]+ (replacement.execute/execute-async!+ (fn [progress]+ (replacement.protocols/set-total! progress 100)+ ;; advance by 30 four times: 30, 60, 90, then by 10 to finish+ (replacement.protocols/advance! progress 30)+ (replacement.protocols/advance! progress 30)+ (replacement.protocols/advance! progress 30)+ (replacement.protocols/advance! progress 10))+ progress)+ (is (= :run/success (u/deref-with-timeout done? 500)))+ ;; advance(30): 0→30, crosses 0/50→0/50 boundary? quot(0,50)=0, quot(30,50)=0 → no+ ;; advance(30): 30→60, crosses? quot(30,50)=0, quot(60,50)=1 → yes, writes 0.6+ ;; advance(30): 60→90, crosses? quot(60,50)=1, quot(90,50)=1 → no+ ;; advance(10): 90→100, = total → yes, writes 1.0+ (is (= [0.6 1.0] @writes)))))))))++(deftest advance!-cancellation-on-boundary-test+ (testing "canceled? is checked on every progress write and throws"+ (mt/with-premium-features #{:dependencies}+ (mt/with-model-cleanup [:model/ReplacementRun]+ (let [writes (atom [])+ done? (promise)+ pause (promise)+ ready (promise)]+ (with-redefs [replacement-run/update-progress!+ (fn [_run-id progress]+ (swap! writes conj progress))]+ (let [record (replacement-run/create-run! :card 1 :card 2 (mt/user->id :rasta))+ progress (replacement-run/run-row->progress record done?)]+ (replacement.execute/execute-async!+ (fn [progress]+ (replacement.protocols/set-total! progress 130)+ ;; enough to trigger one boundary+ (dotimes [_ 60] (replacement.protocols/advance! progress))+ (deliver ready :ready) ;; let outside thread cancel+ @pause ;; wait for cancel to happen+ ;; next to do the rest, including a boundary+ (dotimes [_ 70] (replacement.protocols/advance! progress)))+ progress)+ @ready+ (replacement-run/cancel-run! (:id record))+ (deliver pause :continue)+ ;; Poll until run is no longer active (cancel-run! flips is_active)+ (is (= :run/cancelled (u/deref-with-timeout done? 500)))+ ;; First boundary write at 50/100 succeeds, canceled? is false.+ ;; cancel-run! sets is_active=nil. Next boundary at 100 writes progress,+ ;; then checks canceled? → true → throws. Both writes happen.+ (is (= [(double (/ 50 130)) (double (/ 100 130))] @writes)+ "Both boundary writes happen; cancellation fires after the second write")+ (is (= :canceled (:status (t2/select-one :model/ReplacementRun :id (:id record))))+ "Run status should be :canceled"))))))))++(deftest work-fn-exception-marks-run-as-failed-test+ (testing "when work-fn throws, the run is marked as failed with the exception message"+ (mt/with-premium-features #{:dependencies}+ (mt/with-model-cleanup [:model/ReplacementRun]+ (let [done? (promise)+ record (replacement-run/create-run! :card 1 :card 2 (mt/user->id :rasta))+ progress (replacement-run/run-row->progress record done?)]+ (replacement.execute/execute-async!+ (fn [_progress]+ (throw (ex-info "1 of 5 entities failed\n [:card 42]: boom"+ {:failures [{:entity [:card 42] :error "boom"}]})))+ progress)+ (is (= :run/fail (u/deref-with-timeout done? 500)))+ (let [run (t2/select-one :model/ReplacementRun :id (:id record))]+ (is (= :failed (:status run)))+ (is (str/starts-with? (:message run) "1 of 5 entities failed"))))))))diff --git a/enterprise/backend/test/metabase_enterprise/replacement/field_refs_test.clj b/enterprise/backend/test/metabase_enterprise/replacement/field_refs_test.cljnew file mode 100644index 00000000000..9ee588da443--- /dev/null+++ b/enterprise/backend/test/metabase_enterprise/replacement/field_refs_test.clj@@ -0,0 +1,511 @@+(ns metabase-enterprise.replacement.field-refs-test+ (:require+ [clojure.test :refer [deftest is testing]]+ [metabase-enterprise.replacement.field-refs :as replacement.field-refs]+ [metabase.lib.core :as lib]+ [metabase.lib.metadata :as lib.metadata]+ [metabase.test :as mt]+ [metabase.util.json :as json]+ [toucan2.core :as t2]))++(set! *warn-on-reflection* true)++(defn- field-id-ref+ [mp field-id]+ (-> (lib.metadata/field mp field-id) lib/ref lib/->legacy-MBQL))++(defn- dimension-field-id-ref+ ([mp field-id]+ [:dimension (field-id-ref mp field-id)])+ ([mp field-id stage-number]+ [:dimension (field-id-ref mp field-id) {:stage-number stage-number}]))++;;; ------------------------------------------------ Card --------------------------------------------------------++(deftest card-upgrade-field-refs!-query-test+ (testing "should upgrade refs in a query"+ (let [mp (mt/metadata-provider)+ query (-> (lib/query mp (lib.metadata/table mp (mt/id :orders)))+ (lib/aggregate (lib/count))+ (lib/breakout (lib.metadata/field mp (mt/id :orders :id)))+ lib/append-stage+ (lib/filter (lib/= (lib.metadata/field mp (mt/id :orders :id)) 1)))]+ (mt/with-temp [:model/Card {card-id :id} {:dataset_query query}]+ (replacement.field-refs/upgrade-field-refs! [:card card-id])+ (is (=? {:dataset_query+ {:stages [{:source-table (mt/id :orders)+ :aggregation [[:count {}]]+ :breakout [[:field {} (mt/id :orders :id)]]}+ {:filters [[:= {} [:field {} "ID"] 1]]}]}}+ (t2/select-one :model/Card card-id)))))))++(deftest card-upgrade-field-refs!-viz-settings-test+ (testing "should upgrade refs in viz settings"+ (let [mp (mt/metadata-provider)+ query (-> (lib/query mp (lib.metadata/table mp (mt/id :orders)))+ (lib/aggregate (lib/count))+ (lib/aggregate (lib/count))+ (lib/breakout (lib.metadata/field mp (mt/id :orders :id)))+ (lib/breakout (lib.metadata/field mp (mt/id :orders :created_at))))+ id-ref (field-id-ref mp (mt/id :orders :id))+ created-ref (field-id-ref mp (mt/id :orders :created_at))+ viz-settings {:column_settings+ {(json/encode ["ref" id-ref])+ {:column_title "Order ID"}++ (json/encode ["ref" created-ref])+ {:column_title "Created"}}++ :table.column_formatting+ [{:columns [id-ref]+ :value 10}+ {:columns [created-ref]+ :value "1970-01-01"}]++ :pivot_table.column_split+ {:rows [id-ref]+ :columns [created-ref]+ :values [[:aggregation 0] [:aggregation 1]]}++ :pivot_table.collapsed_rows+ {:rows [id-ref]+ :value [10]}}]+ (mt/with-temp [:model/Card {card-id :id} {:dataset_query query+ :visualization_settings viz-settings}]+ (replacement.field-refs/upgrade-field-refs! [:card card-id])+ (is (=? {:visualization_settings+ {:column_settings+ {(json/encode ["name" "ID"]) {:column_title "Order ID"}+ (json/encode ["name" "CREATED_AT"]) {:column_title "Created"}}++ :table.column_formatting+ [{:columns ["ID"]+ :value 10}+ {:columns ["CREATED_AT"]+ :value "1970-01-01"}]++ :pivot_table.column_split+ {:rows ["ID"]+ :columns ["CREATED_AT"]+ :values ["count" "count_2"]}++ :pivot_table.collapsed_rows+ {:rows ["ID"]+ :value [10]}}}+ (t2/select-one :model/Card card-id)))))))++(deftest card-upgrade-field-refs!-viz-settings-broken-test+ (testing "should ignore refs in viz settings that cannot be resolved"+ (let [mp (mt/metadata-provider)+ query (-> (lib/query mp (lib.metadata/table mp (mt/id :orders)))+ (lib/aggregate (lib/count))+ (lib/breakout (lib.metadata/field mp (mt/id :orders :id)))+ (lib/breakout (lib.metadata/field mp (mt/id :orders :created_at))))+ viz-settings {:pivot_table.column_split+ {:rows [(field-id-ref mp (mt/id :orders :id))]+ :columns [[:field 9999 nil]]+ :values [[:aggregation 0] [:aggregation 1]]}}]+ (mt/with-temp [:model/Card {card-id :id} {:dataset_query query+ :visualization_settings viz-settings}]+ (replacement.field-refs/upgrade-field-refs! [:card card-id])+ (is (=? {:visualization_settings+ {:pivot_table.column_split+ {:rows ["ID"]+ :columns [[:field 9999 nil]]+ :values ["count" [:aggregation 1]]}}}+ (t2/select-one :model/Card card-id)))))))++(deftest card-upgrade-field-refs!-parameters-test+ (testing "should upgrade refs in `:values_source_config` of parameters"+ (let [mp (mt/metadata-provider)]+ (mt/with-temp [:model/Card {card1-id :id} {:dataset_query (lib/query mp (lib.metadata/table mp (mt/id :products)))}+ :model/Card {card2-id :id} {:dataset_query (-> (lib/native-query mp "SELECT 1")+ (lib/with-native-query "SELECT * FROM products WHERE category = {{category}}"))+ :parameters [{:id "test-param"+ :name "category"+ :type :string/=+ :target [:dimension [:template-tag "category"]]+ :values_source_type "card"+ :values_source_config+ {:card_id card1-id+ :value_field (field-id-ref mp (mt/id :products :category))}}]}]+ (replacement.field-refs/upgrade-field-refs! [:card card2-id])+ (is (=? {:parameters [{:name "category"+ :values_source_config {:card_id card1-id+ :value_field [:field "CATEGORY" {}]}}]}+ (t2/select-one :model/Card card2-id)))))))++(deftest card-upgrade-field-refs!-non-card-sourced-parameters-test+ (testing "should not change parameters that don't reference cards"+ (let [mp (mt/metadata-provider)+ query (lib/query mp (lib.metadata/table mp (mt/id :orders)))]+ (mt/with-temp [:model/Card {card-id :id, updated-at :updated_at}+ {:dataset_query query+ :parameters [{:id "test-param"+ :name "category"+ :type :string/=}]}]+ (replacement.field-refs/upgrade-field-refs! [:card card-id])+ (is (= updated-at (:updated_at (t2/select-one :model/Card card-id)))+ "Card should not be updated when parameters don't reference cards")+ (is (= [{:id "test-param" :name "category" :type :string/=}]+ (:parameters (t2/select-one :model/Card card-id)))+ "Parameters should be unchanged")))))++(deftest card-upgrade-field-refs!-native-query-test+ (testing "should not change a card with a native query"+ (let [mp (mt/metadata-provider)+ query (lib/native-query mp "SELECT * FROM orders")]+ (mt/with-temp [:model/Card {card-id :id} {:dataset_query query}]+ (replacement.field-refs/upgrade-field-refs! [:card card-id])+ (is (=? {:dataset_query {:stages [{:native "SELECT * FROM orders"}]}}+ (t2/select-one :model/Card card-id)))))))++(deftest card-upgrade-field-refs!-native-query-preserves-result-metadata-test+ (testing "should preserve `:result_metadata` for native queries when parameters change"+ (let [mp (mt/metadata-provider)+ result-metadata [{:name "ID" :base_type :type/Integer}]]+ (mt/with-temp [:model/Card {source-card-id :id}+ {:dataset_query (lib/query mp (lib.metadata/table mp (mt/id :products)))}++ :model/Card {card-id :id}+ {:dataset_query (-> (lib/native-query mp "SELECT 1")+ (lib/with-native-query "SELECT * FROM products WHERE category = {{category}}"))+ :result_metadata result-metadata+ :parameters [{:id "test-param"+ :name "category"+ :type :string/=+ :target [:dimension [:template-tag "category"]]+ :values_source_type "card"+ :values_source_config+ {:card_id source-card-id+ :value_field (field-id-ref mp (mt/id :products :category))}}]}]+ (replacement.field-refs/upgrade-field-refs! [:card card-id])+ (let [card (t2/select-one :model/Card card-id)]+ (testing "result_metadata should be preserved"+ (is (=? result-metadata+ (:result_metadata card))))+ (testing "parameters should be upgraded"+ (is (=? [{:values_source_config {:value_field [:field "CATEGORY" {}]}}]+ (:parameters card))))))))++ (testing "should not update when there are no changes"+ (let [mp (mt/metadata-provider)+ query (lib/native-query mp "SELECT * FROM orders")+ result-metadata [{:name "ID" :base_type :type/Integer}]]+ (mt/with-temp [:model/Card {card-id :id, updated-at :updated_at} {:dataset_query query+ :result_metadata result-metadata}]+ (replacement.field-refs/upgrade-field-refs! [:card card-id])+ (is (= updated-at (:updated_at (t2/select-one :model/Card card-id)))+ "Card should not be updated when native query has no changes")))))++(deftest card-upgrade-field-refs!-broken-query-test+ (testing "should not crash on a card with a broken query"+ (let [mp (mt/metadata-provider)]+ (mt/with-temp [:model/Card {card-id :id} {:dataset_query (lib/native-query mp "SELECT 1")}]+ ;; simulate a broken query by updating the dataset_query directly+ (t2/update! :model/Card card-id {:dataset_query {}})+ (replacement.field-refs/upgrade-field-refs! [:card card-id])+ (is (=? {:dataset_query {}}+ (t2/select-one :model/Card card-id)))))))++(deftest card-upgrade-field-refs!-broken-source-card-test+ (testing "should not crash when a parameter references a card with a broken query"+ (let [mp (mt/metadata-provider)]+ (mt/with-temp [:model/Card {broken-card-id :id} {:dataset_query (lib/native-query mp "SELECT 1")}+ :model/Card {card-id :id} {:dataset_query (lib/native-query mp "SELECT 1")+ :parameters [{:id "test-param"+ :name "category"+ :type :string/=+ :values_source_type "card"+ :values_source_config+ {:card_id broken-card-id+ :value_field [:field 1 nil]}}]}]+ ;; simulate a broken source card+ (t2/update! :model/Card broken-card-id {:dataset_query {}})+ (replacement.field-refs/upgrade-field-refs! [:card card-id])+ (is (=? {:parameters [{:name "category"+ :values_source_config {:card_id broken-card-id+ :value_field [:field 1 nil]}}]}+ (t2/select-one :model/Card card-id)))))))++(deftest card-upgrade-field-refs!-nonexistent-source-card-test+ (testing "should not crash when a parameter references a non-existent card"+ (let [mp (mt/metadata-provider)]+ (mt/with-temp [:model/Card {source-card-id :id} {:dataset_query (lib/native-query mp "SELECT 1")}+ :model/Card {card-id :id} {:dataset_query (lib/native-query mp "SELECT 1")+ :parameters [{:id "test-param"+ :name "category"+ :type :string/=+ :values_source_type "card"+ :values_source_config+ {:card_id source-card-id+ :value_field [:field 1 nil]}}]}]+ ;; delete the source card to simulate a non-existent reference+ (t2/delete! :model/Card source-card-id)+ (replacement.field-refs/upgrade-field-refs! [:card card-id])+ (is (=? {:parameters [{:name "category"+ :values_source_config {:card_id source-card-id+ :value_field [:field 1 nil]}}]}+ (t2/select-one :model/Card card-id)))))))++(deftest card-upgrade-field-refs!-no-changes-test+ (testing "should not update a card when there are no changes"+ (let [mp (mt/metadata-provider)+ query (lib/query mp (lib.metadata/table mp (mt/id :orders)))]+ (mt/with-temp [:model/Card {card-id :id, updated-at :updated_at} {:dataset_query query}]+ (replacement.field-refs/upgrade-field-refs! [:card card-id])+ (is (= updated-at (:updated_at (t2/select-one :model/Card card-id))))))))++;;; ------------------------------------------------ Transform --------------------------------------------------------++(deftest transform-upgrade-field-refs!-mbql-query-test+ (testing "should upgrade refs in a transform with an mbql query"+ (let [mp (mt/metadata-provider)+ query (-> (lib/query mp (lib.metadata/table mp (mt/id :orders)))+ (lib/breakout (lib.metadata/field mp (mt/id :orders :id)))+ lib/append-stage+ (lib/filter (lib/= (lib.metadata/field mp (mt/id :orders :id)) 1)))]+ (mt/with-temp [:model/Transform {transform-id :id} {:name "test transform"+ :source {:type "query" :query query}+ :target {:database (mt/id) :table "out"}}]+ (replacement.field-refs/upgrade-field-refs! [:transform transform-id])+ (is (=? {:source {:type :query+ :query {:stages [{:source-table (mt/id :orders)+ :breakout [[:field {} (mt/id :orders :id)]]}+ {:filters [[:= {} [:field {} "ID"] 1]]}]}}}+ (t2/select-one :model/Transform transform-id)))))))++(deftest transform-upgrade-field-refs!-native-query-test+ (testing "should not change a transform with a native query"+ (let [mp (mt/metadata-provider)+ query (lib/native-query mp "SELECT * FROM orders")]+ (mt/with-temp [:model/Transform {transform-id :id} {:name "test transform"+ :source {:type "query" :query query}+ :target {:database (mt/id) :table "out"}}]+ (is (nil? (replacement.field-refs/upgrade-field-refs! [:transform transform-id])))))))++(deftest transform-upgrade-field-refs!-broken-query-test+ (testing "should not crash on a transform with a broken query"+ (let [mp (mt/metadata-provider)]+ (mt/with-temp [:model/Transform {transform-id :id} {:name "test transform"+ :source {:type "query" :query (lib/native-query mp "SELECT 1")}+ :target {:database (mt/id) :table "out"}}]+ ;; simulate a broken query by updating source directly in the DB+ (t2/query-one {:update :transform+ :set {:source "{\"type\":\"query\",\"query\":{}}"}+ :where [:= :id transform-id]})+ (is (nil? (replacement.field-refs/upgrade-field-refs! [:transform transform-id])))))))++(deftest transform-upgrade-field-refs!-no-changes-test+ (testing "should not update a transform when there are no changes"+ (let [mp (mt/metadata-provider)+ query (lib/query mp (lib.metadata/table mp (mt/id :orders)))]+ (mt/with-temp [:model/Transform {transform-id :id, updated-at :updated_at} {:name "test transform"+ :source {:type "query" :query query}+ :target {:database (mt/id) :table "out"}}]+ (replacement.field-refs/upgrade-field-refs! [:transform transform-id])+ (is (= updated-at (:updated_at (t2/select-one :model/Transform transform-id))))))))++;;; ------------------------------------------------- Segment ----------------------------------------------------------++(deftest segment-upgrade-field-refs!-mbql-query-test+ (testing "should upgrade id-based field refs in a segment with a card source"+ (let [mp (mt/metadata-provider)+ card-query (lib/query mp (lib.metadata/table mp (mt/id :orders)))]+ (mt/with-temp [:model/Card {card-id :id} {:dataset_query card-query}]+ (let [seg-definition (-> (lib/query mp (lib.metadata/card mp card-id))+ (lib/filter (lib/= (lib.metadata/field mp (mt/id :orders :id)) 10)))]+ (mt/with-temp [:model/Segment {segment-id :id} {:table_id (mt/id :orders)+ :definition seg-definition}]+ (replacement.field-refs/upgrade-field-refs! [:segment segment-id])+ (is (=? {:definition {:stages [{:source-card card-id+ :filters [[:= {} [:field {} "ID"] 10]]}]}}+ (t2/select-one :model/Segment segment-id)))))))))++(deftest segment-upgrade-field-refs!-broken-query-test+ (testing "should not crash or update a segment with a broken definition"+ (let [mp (mt/metadata-provider)+ query (-> (lib/query mp (lib.metadata/table mp (mt/id :orders)))+ (lib/filter (lib/> (lib.metadata/field mp (mt/id :orders :id)) 10)))]+ (mt/with-temp [:model/Segment {segment-id :id} {:table_id (mt/id :orders)+ :definition query}]+ (t2/query-one {:update :segment+ :set {:definition "{\"x\": \"y\"}"}+ :where [:= :id segment-id]})+ (replacement.field-refs/upgrade-field-refs! [:segment segment-id])+ ;; segments return nil when the query is not valid+ (is (nil? (:definition (t2/select-one :model/Segment segment-id))))))))++(deftest segment-upgrade-field-refs!-no-changes-test+ (testing "should not update a segment when there are no changes"+ (let [mp (mt/metadata-provider)+ query (-> (lib/query mp (lib.metadata/table mp (mt/id :orders)))+ (lib/filter (lib/> (lib.metadata/field mp (mt/id :orders :id)) 10)))]+ (mt/with-temp [:model/Segment {segment-id :id, updated-at :updated_at} {:table_id (mt/id :orders)+ :definition query}]+ (replacement.field-refs/upgrade-field-refs! [:segment segment-id])+ (is (= updated-at (:updated_at (t2/select-one :model/Segment segment-id))))))))++;;; ------------------------------------------------- Measure ----------------------------------------------------------++(deftest measure-upgrade-field-refs!-mbql-query-test+ (testing "should upgrade field refs in a measure definition and update the DB"+ (let [mp (mt/metadata-provider)+ query (-> (lib/query mp (lib.metadata/table mp (mt/id :orders)))+ (lib/aggregate (lib/sum (lib.metadata/field mp (mt/id :orders :total)))))]+ (mt/with-temp [:model/Measure {measure-id :id} {:table_id (mt/id :orders)+ :name "test measure"+ :definition query}]+ ;; Measure schema requires :source-table (no :source-card), so no real field ref upgrade can happen.+ (replacement.field-refs/upgrade-field-refs! [:measure measure-id])+ (is (=? {:definition {:stages [{:source-table (mt/id :orders)+ :aggregation [[:sum {} [:field {} (mt/id :orders :total)]]]}]}}+ (t2/select-one :model/Measure measure-id)))))))++(deftest measure-upgrade-field-refs!-broken-query-test+ (testing "should not crash or update a measure with a broken definition"+ (let [mp (mt/metadata-provider)+ query (-> (lib/query mp (lib.metadata/table mp (mt/id :orders)))+ (lib/aggregate (lib/count)))]+ (mt/with-temp [:model/Measure {measure-id :id} {:table_id (mt/id :orders)+ :name "test measure"+ :definition query}]+ (t2/query-one {:update :measure+ :set {:definition "{\"x\": \"y\"}"}+ :where [:= :id measure-id]})+ (replacement.field-refs/upgrade-field-refs! [:measure measure-id])+ ;; Measure return {} when query is invalid+ (is (= {} (:definition (t2/select-one :model/Measure measure-id))))))))++(deftest measure-upgrade-field-refs!-no-changes-test+ (testing "should not update a measure when there are no changes"+ (let [mp (mt/metadata-provider)+ query (-> (lib/query mp (lib.metadata/table mp (mt/id :orders)))+ (lib/aggregate (lib/sum (lib.metadata/field mp (mt/id :orders :id)))))]+ (mt/with-temp [:model/Measure {measure-id :id, updated-at :updated_at} {:table_id (mt/id :orders)+ :name "test measure"+ :definition query}]+ (replacement.field-refs/upgrade-field-refs! [:measure measure-id])+ (is (= updated-at (:updated_at (t2/select-one :model/Measure measure-id))))))))++;;; ------------------------------------------------ Dashboard --------------------------------------------------------++(deftest dashboard-upgrade-field-refs!-parameters-test+ (testing "should upgrade refs in dashboard parameters with values_source_config"+ (let [mp (mt/metadata-provider)]+ (mt/with-temp [:model/Card {card-id :id} {:dataset_query (lib/query mp (lib.metadata/table mp (mt/id :products)))}+ :model/Dashboard {dashboard-id :id} {:parameters [{:id "test-param"+ :name "category"+ :type :string/=+ :values_source_type "card"+ :values_source_config+ {:card_id card-id+ :value_field (field-id-ref mp (mt/id :products :category))}}]}]+ (replacement.field-refs/upgrade-field-refs! [:dashboard dashboard-id])+ (is (=? {:parameters [{:name "category"+ :values_source_config {:card_id card-id+ :value_field [:field "CATEGORY" {}]}}]}+ (t2/select-one :model/Dashboard dashboard-id)))))))++(deftest dashboard-upgrade-field-refs!-parameter-mappings-test+ (testing "should upgrade refs in dashcard parameter mappings"+ (let [mp (mt/metadata-provider)+ query (-> (lib/query mp (lib.metadata/table mp (mt/id :orders)))+ (lib/breakout (lib.metadata/field mp (mt/id :orders :id)))+ lib/append-stage)]+ (mt/with-temp [:model/Card {card-id :id} {:dataset_query query}+ :model/Dashboard {dashboard-id :id} {}+ :model/DashboardCard {dashcard-id :id} {:dashboard_id dashboard-id+ :card_id card-id+ :parameter_mappings [{:parameter_id "my-param"+ :card_id card-id+ :target (dimension-field-id-ref mp (mt/id :orders :id) 1)}]}]+ (replacement.field-refs/upgrade-field-refs! [:dashboard dashboard-id])+ (is (=? {:parameter_mappings [{:parameter_id "my-param"+ :card_id card-id+ :target [:dimension [:field "ID" {}] {:stage-number 1}]}]}+ (t2/select-one :model/DashboardCard dashcard-id)))))))++(deftest dashboard-upgrade-field-refs!-broken-source-card-test+ (testing "should not crash when a dashcard references a card with a broken query"+ (let [mp (mt/metadata-provider)]+ (mt/with-temp [:model/Card {card-id :id} {:dataset_query (lib/native-query mp "SELECT 1")}+ :model/Dashboard {dashboard-id :id} {}+ :model/DashboardCard {dashcard-id :id} {:dashboard_id dashboard-id+ :card_id card-id+ :parameter_mappings [{:parameter_id "my-param"+ :card_id card-id+ :target (dimension-field-id-ref mp (mt/id :orders :id))}]}]+ ;; simulate a broken card+ (t2/update! :model/Card card-id {:dataset_query {}})+ (replacement.field-refs/upgrade-field-refs! [:dashboard dashboard-id])+ (is (=? {:parameter_mappings [{:target (dimension-field-id-ref mp (mt/id :orders :id))}]}+ (t2/select-one :model/DashboardCard dashcard-id)))))))++(defn- make-click-behavior+ [card-id target]+ {:type "link"+ :linkType "question"+ :targetId card-id+ :parameterMapping+ {(json/encode target)+ {:id (json/encode target)+ :source {:type :column+ :id "ID"+ :name "ID"}+ :target {:type :dimension+ :id (json/encode target)+ :dimension target}}}})++(deftest dashboard-upgrade-field-refs!-click-behaviors-test+ (testing "should upgrade refs in click behavior parameter mappings (both global and per-column)"+ (let [mp (mt/metadata-provider)+ source-query (lib/query mp (lib.metadata/table mp (mt/id :products)))+ target-query (-> (lib/query mp (lib.metadata/table mp (mt/id :orders)))+ (lib/breakout (lib.metadata/field mp (mt/id :orders :id)))+ lib/append-stage)+ target (dimension-field-id-ref mp (mt/id :orders :id) 1)]+ (mt/with-temp [:model/Card {source-card-id :id} {:dataset_query source-query}+ :model/Card {target-card-id :id} {:dataset_query target-query}+ :model/Dashboard {dashboard-id :id} {}+ :model/DashboardCard {dashcard-id :id}+ {:dashboard_id dashboard-id+ :card_id source-card-id+ :visualization_settings+ {:click_behavior (make-click-behavior target-card-id target)+ :column_settings+ {(json/encode [:name "ID"])+ {:click_behavior (make-click-behavior target-card-id target)}}}}]+ (replacement.field-refs/upgrade-field-refs! [:dashboard dashboard-id])+ (let [viz (:visualization_settings (t2/select-one :model/DashboardCard dashcard-id))+ viz-key (keyword (json/encode target))]+ (testing "global click behavior dimension should be upgraded"+ (is (=? {:click_behavior+ {:targetId target-card-id+ :parameterMapping+ {viz-key {:target {:dimension ["dimension" [:field "ID" {}] {:stage-number 1}]}}}}}+ viz)))+ (testing "per-column click behavior dimension should be upgraded"+ (is (=? {:column_settings+ {(json/encode [:name "ID"])+ {:click_behavior+ {:targetId target-card-id+ :parameterMapping+ {viz-key {:target {:dimension ["dimension" [:field "ID" {}] {:stage-number 1}]}}}}}}}+ viz))))))))++(deftest dashboard-upgrade-field-refs!-no-changes-test+ (testing "should not update a dashboard or dashcards when there are no changes"+ (let [mp (mt/metadata-provider)+ query (lib/query mp (lib.metadata/table mp (mt/id :orders)))]+ (mt/with-temp [:model/Card {card-id :id} {:dataset_query query}+ :model/Dashboard {dashboard-id :id, dash-updated :updated_at} {}+ :model/DashboardCard {dashcard-id :id, dc-updated :updated_at}+ {:dashboard_id dashboard-id+ :card_id card-id+ :parameter_mappings [{:parameter_id "my-param"+ :card_id card-id+ :target (dimension-field-id-ref mp (mt/id :orders :id))}]}]+ (replacement.field-refs/upgrade-field-refs! [:dashboard dashboard-id])+ (is (= dash-updated (:updated_at (t2/select-one :model/Dashboard dashboard-id))))+ (is (= dc-updated (:updated_at (t2/select-one :model/DashboardCard dashcard-id))))))))diff --git a/enterprise/backend/test/metabase_enterprise/replacement/runner_test.clj b/enterprise/backend/test/metabase_enterprise/replacement/runner_test.cljnew file mode 100644index 00000000000..ef1d9f5e8c8--- /dev/null+++ b/enterprise/backend/test/metabase_enterprise/replacement/runner_test.clj@@ -0,0 +1,224 @@+(ns metabase-enterprise.replacement.runner-test+ "Tests for bulk metadata loading in the replacement runner."+ (:require+ [clojure.test :refer [deftest is testing]]+ [metabase-enterprise.dependencies.events]+ [metabase-enterprise.replacement.protocols :as replacement.protocols]+ [metabase-enterprise.replacement.runner :as replacement.runner]+ [metabase.events.core :as events]+ [metabase.lib-be.core :as lib-be]+ [metabase.lib.core :as lib]+ [metabase.lib.metadata :as lib.metadata]+ [metabase.lib.metadata.protocols :as lib.metadata.protocols]+ [metabase.test :as mt]+ [toucan2.core :as t2]))++(set! *warn-on-reflection* true)++(comment+ metabase-enterprise.dependencies.events/keep-me)++(deftest bulk-load-metadata-for-entities-test+ (testing "bulk-load-metadata-for-entities! fetches all entity types in bulk"+ (mt/with-temp [:model/Card {card-id-1 :id} {:name "Card 1"+ :database_id (mt/id)+ :dataset_query (mt/mbql-query venues)}+ :model/Card {card-id-2 :id} {:name "Card 2"+ :database_id (mt/id)+ :dataset_query (mt/mbql-query checkins)}+ :model/Table {table-id :id} {:name "Custom Table"+ :db_id (mt/id)+ :active true}+ :model/Segment {segment-id :id} {:table_id (mt/id :venues)+ :name "Test Segment"+ :definition (mt/mbql-query venues+ {:filter [:> $price 3]})}]+ (let [entities [[:card card-id-1]+ [:card card-id-2]+ [:table table-id]+ [:segment segment-id]]+ metadata-provider (lib-be/application-database-metadata-provider (mt/id))+ loaded (#'replacement.runner/bulk-load-metadata-for-entities!+ metadata-provider+ entities)]++ (testing "returns map with all fetched entities"+ (is (map? loaded))+ (is (= 4 (count loaded))))++ (testing "card entities are keyed by [:card id]"+ (is (contains? loaded [:card card-id-1]))+ (is (contains? loaded [:card card-id-2]))+ (let [card1 (get loaded [:card card-id-1])+ card2 (get loaded [:card card-id-2])]+ (is (= "Card 1" (:name card1)))+ (is (= "Card 2" (:name card2)))+ (is (some? (:dataset_query card1)))+ (is (some? (:dataset_query card2)))))++ (testing "table entities are keyed by [:table id]"+ (is (contains? loaded [:table table-id]))+ (let [table (get loaded [:table table-id])]+ (is (= "Custom Table" (:name table)))))++ (testing "segment entities are keyed by [:segment id]"+ (is (contains? loaded [:segment segment-id]))+ (let [segment (get loaded [:segment segment-id])]+ (is (= "Test Segment" (:name segment)))+ (is (some? (:definition segment)))))))))++(deftest bulk-load-metadata-extracts-referenced-entities-test+ (testing "bulk-load-metadata-for-entities! extracts and loads referenced entities"+ (mt/with-temp [:model/Card {source-card-id :id} {:name "Source Card"+ :database_id (mt/id)+ :dataset_query (mt/mbql-query venues)}+ :model/Card {dep-1-card-id :id} {:name "Dependent Card 1"+ :database_id (mt/id)+ :dataset_query (mt/mbql-query venues+ {:source-table (str "card__" source-card-id)})}+ :model/Card {dep-2-card-id :id} {:name "Dependent Card 2"+ :database_id (mt/id)+ :dataset_query (mt/mbql-query venues+ {:source-table (str "card__" dep-1-card-id)})}]+ (lib-be/with-metadata-provider-cache+ (let [entities [[:card source-card-id]+ [:card dep-1-card-id]+ [:card dep-2-card-id]]+ metadata-provider (lib-be/application-database-metadata-provider (mt/id))+ value (#'replacement.runner/bulk-load-metadata-for-entities!+ metadata-provider+ entities)+ ;; After bulk loading, the source card should be in the metadata provider's cache+ source-card-meta (lib.metadata/card metadata-provider source-card-id)]++ (testing "referenced cards are loaded into metadata provider cache"+ (is (some? source-card-meta))+ (is (= "Source Card" (:name source-card-meta)))+ (is (some? (lib.metadata.protocols/cached-value+ metadata-provider :metadata/card source-card-id)))+ (is (some? (lib.metadata.protocols/cached-value+ metadata-provider :metadata/card dep-1-card-id)))+ (is (some? (lib.metadata.protocols/cached-value+ metadata-provider :metadata/card dep-2-card-id)))+ (is (some? (lib.metadata/card metadata-provider dep-2-card-id)))+ (is (=? #{[:card source-card-id] [:card dep-1-card-id] [:card dep-2-card-id]}+ (-> value keys set)))))))))++(deftest bulk-load-handles-multiple-entity-types-test+ (testing "bulk-load-metadata-for-entities! handles mixed entity types"+ (mt/with-temp [:model/Card {card-id :id} {:name "Test Card"+ :database_id (mt/id)+ :dataset_query (mt/mbql-query venues)}+ :model/Segment {segment-id :id} {:table_id (mt/id :venues)+ :name "Test Segment"+ :definition (mt/mbql-query venues+ {:filter [:> $price 3]})}+ :model/Measure {measure-id :id} {:table_id (mt/id :venues)+ :name "Test Measure"+ :definition (let [mp (mt/metadata-provider)+ table-metadata (lib.metadata/table mp (mt/id :venues))]+ (-> (lib/query mp table-metadata)+ (lib/aggregate (lib/count))))}+ :model/Dashboard {dashboard-id :id} {:name "Test Dashboard"+ :parameters [{:id "param1" :type :string/= :name "Test Param"}]}]+ (let [entities [[:card card-id]+ [:segment segment-id]+ [:measure measure-id]+ [:dashboard dashboard-id]]+ metadata-provider (lib-be/application-database-metadata-provider (mt/id))+ loaded (#'replacement.runner/bulk-load-metadata-for-entities!+ metadata-provider+ entities)]++ (testing "handles cards, segments, measures, and dashboards in one batch"+ (is (= 4 (count loaded)))+ (is (contains? loaded [:card card-id]))+ (is (contains? loaded [:segment segment-id]))+ (is (contains? loaded [:measure measure-id]))+ (is (contains? loaded [:dashboard dashboard-id])))++ (testing "all entities have required fields"+ (is (some? (:dataset_query (get loaded [:card card-id]))))+ (is (some? (:definition (get loaded [:segment segment-id]))))+ (is (some? (:definition (get loaded [:measure measure-id]))))+ (is (= [{:id "param1" :type :string/= :name "Test Param"}] (:parameters (get loaded [:dashboard dashboard-id])))))))))++(deftest bulk-load-handles-empty-batch-test+ (testing "bulk-load-metadata-for-entities! handles empty batch gracefully"+ (let [entities []+ metadata-provider (lib-be/application-database-metadata-provider (mt/id))+ loaded (#'replacement.runner/bulk-load-metadata-for-entities!+ metadata-provider+ entities)]++ (testing "returns empty map for empty batch"+ (is (= {} loaded))))))++(deftest bulk-load-handles-dashboards-and-documents-test+ (testing "bulk-load-metadata-for-entities! pre-loads dashboards but ignores documents"+ (mt/with-temp [:model/Dashboard {dashboard-id :id} {:name "Test Dashboard"+ :parameters [{:id "param1" :type :string/= :name "Test Param"}]}]+ (let [entities [[:dashboard dashboard-id]+ [:document 123]]+ metadata-provider (lib-be/application-database-metadata-provider (mt/id))+ loaded (#'replacement.runner/bulk-load-metadata-for-entities!+ metadata-provider+ entities)]++ (testing "dashboards are pre-loaded"+ (is (= [{:id "param1" :type :string/= :name "Test Param"}] (:parameters (get loaded [:dashboard dashboard-id])))))++ (testing "documents are not fetched (no-op entities)"+ (is (not (contains? loaded [:document 123]))))))))++(deftest run-swap-updates-dependent-cards-test+ (testing "run-swap upgrades field refs and swaps source for all transitive dependents"+ (mt/with-premium-features #{:dependencies}+ (mt/with-test-user :rasta+ (let [mp (mt/metadata-provider)]+ (mt/with-temp [:model/Card {old-id :id :as old-card}+ {:database_id (mt/id)+ :dataset_query (lib/query mp (lib.metadata/table mp (mt/id :orders)))+ :type :model+ :name "Old Model"}++ :model/Card {new-id :id}+ {:database_id (mt/id)+ :dataset_query (lib/query mp (lib.metadata/table mp (mt/id :orders)))+ :type :model+ :name "New Model"}++ :model/Card {child-id :id :as child-card}+ {:database_id (mt/id)+ :dataset_query (lib/query mp (lib.metadata/card mp old-id))+ :type :question+ :name "Child Card"}]+ (mt/with-model-cleanup [:model/Dependency]+ ;; populate dependencies+ (events/publish-event! :event/card-create {:object old-card :user-id (mt/user->id :rasta)})+ (events/publish-event! :event/card-create {:object child-card :user-id (mt/user->id :rasta)})++ (testing "child card initially points to old model"+ (is (= old-id (get-in (t2/select-one-fn :dataset_query :model/Card :id child-id)+ [:stages 0 :source-card]))))++ (let [progress-log (atom [])+ progress (reify replacement.protocols/IRunnerProgress+ (set-total! [_ total] (swap! progress-log conj [:set-total total]))+ (advance! [_] (swap! progress-log conj [:advance 1]))+ (advance! [_ n] (swap! progress-log conj [:advance n]))+ (canceled? [_] false)+ (start-run! [_])+ (succeed-run! [_])+ (fail-run! [_ _]))]+ (replacement.runner/run-swap [:card old-id] [:card new-id] progress)++ (testing "child card's source-card is updated to new model"+ (is (= new-id (get-in (t2/select-one-fn :dataset_query :model/Card :id child-id)+ [:stages 0 :source-card]))))++ (testing "progress was tracked"+ (is (some #(= :set-total (first %)) @progress-log)+ "set-total! should have been called")+ (is (seq (filter #(= :advance (first %)) @progress-log))+ "advance! should have been called"))))))))))diff --git a/enterprise/backend/test/metabase_enterprise/replacement/source_check_test.clj b/enterprise/backend/test/metabase_enterprise/replacement/source_check_test.cljnew file mode 100644index 00000000000..9db9b1ae943--- /dev/null+++ b/enterprise/backend/test/metabase_enterprise/replacement/source_check_test.clj@@ -0,0 +1,293 @@+(ns metabase-enterprise.replacement.source-check-test+ (:require+ [clojure.test :refer :all]+ [metabase-enterprise.replacement.source-check :as replacement.source-check]+ [metabase-enterprise.replacement.usages :as replacement.usages]+ [metabase.test :as mt]))++(set! *warn-on-reflection* true)++(deftest same-source-returns-failure-test+ (testing "replacing a source with itself returns {:success false} with no errors or mappings"+ (mt/with-temp [:model/Database {db-id :id} {}+ :model/Table {table-id :id} {:db_id db-id}]+ (is (= {:success false}+ (replacement.source-check/check-replace-source+ [:table table-id] [:table table-id])))))+ (testing "same card source also returns {:success false}"+ (mt/with-temp [:model/Database {db-id :id} {}+ :model/Card {card-id :id} {:database_id db-id}]+ (is (= {:success false}+ (replacement.source-check/check-replace-source+ [:card card-id] [:card card-id]))))))++(deftest database-mismatch-table-test+ (testing "table sources in different databases produce :database-mismatch and no column_mappings"+ (mt/with-temp [:model/Database {db1-id :id} {}+ :model/Database {db2-id :id} {}+ :model/Table {t1-id :id} {:db_id db1-id}+ :model/Table {t2-id :id} {:db_id db2-id}+ :model/Field _ {:table_id t1-id :name "col" :base_type :type/Integer}+ :model/Field _ {:table_id t2-id :name "col" :base_type :type/Integer}]+ (let [result (replacement.source-check/check-replace-source+ [:table t1-id] [:table t2-id])]+ (is (false? (:success result)))+ (is (= [:database-mismatch] (:errors result)))+ (is (nil? (:column_mappings result))+ "column_mappings should not be computed when databases differ")))))++(deftest database-mismatch-card-test+ (testing "card sources in different databases produce :database-mismatch"+ (mt/with-temp [:model/Database {db1-id :id} {}+ :model/Database {db2-id :id} {}+ :model/Card {c1-id :id} {:database_id db1-id}+ :model/Card {c2-id :id} {:database_id db2-id}]+ (let [result (replacement.source-check/check-replace-source+ [:card c1-id] [:card c2-id])]+ (is (false? (:success result)))+ (is (= [:database-mismatch] (:errors result)))+ (is (nil? (:column_mappings result)))))))++(deftest cycle-detected-test+ (testing "cycle detection when new source transitively uses old source"+ (mt/with-temp [:model/Database {db-id :id} {}+ :model/Table {t1-id :id} {:db_id db-id}+ :model/Table {t2-id :id} {:db_id db-id}+ :model/Field _ {:table_id t1-id :name "id" :base_type :type/Integer}+ :model/Field _ {:table_id t2-id :name "id" :base_type :type/Integer}]+ (with-redefs [replacement.usages/transitive-usages+ (constantly [[:table t2-id]])]+ (let [result (replacement.source-check/check-replace-source+ [:table t1-id] [:table t2-id])]+ (is (false? (:success result)))+ (is (some #{:cycle-detected} (:errors result))))))))++(deftest implicit-joins-with-incoming-fks-test+ (testing "table source with incoming FKs produces :incompatible-implicit-joins"+ (mt/with-temp [:model/Database {db-id :id} {}+ :model/Table {t1-id :id} {:db_id db-id}+ :model/Table {t2-id :id} {:db_id db-id}+ :model/Table {t3-id :id} {:db_id db-id}+ :model/Field {f1-id :id} {:table_id t1-id :name "id" :base_type :type/Integer}+ :model/Field _ {:table_id t2-id :name "id" :base_type :type/Integer}+ ;; FK from t3 pointing to a field in t1+ :model/Field _ {:table_id t3-id :name "t1_fk" :base_type :type/Integer+ :semantic_type :type/FK :fk_target_field_id f1-id}]+ (let [result (replacement.source-check/check-replace-source+ [:table t1-id] [:table t2-id])]+ (is (false? (:success result)))+ (is (some #{:incompatible-implicit-joins} (:errors result)))))))++(deftest no-implicit-joins-without-incoming-fks-test+ (testing "table source without incoming FKs does not produce :incompatible-implicit-joins"+ (mt/with-temp [:model/Database {db-id :id} {}+ :model/Table {t1-id :id} {:db_id db-id}+ :model/Table {t2-id :id} {:db_id db-id}+ :model/Field _ {:table_id t1-id :name "id" :base_type :type/Integer+ :effective_type :type/Integer}+ :model/Field _ {:table_id t2-id :name "id" :base_type :type/Integer+ :effective_type :type/Integer}]+ (let [result (replacement.source-check/check-replace-source+ [:table t1-id] [:table t2-id])]+ (is (nil? (some #{:incompatible-implicit-joins} (:errors result))))))))++(deftest no-implicit-joins-for-card-source-test+ (testing "implicit joins check only applies to :table sources, not :card sources"+ (mt/with-temp [:model/Database {db-id :id} {}+ :model/Table {t1-id :id} {:db_id db-id}+ :model/Table {t2-id :id} {:db_id db-id}+ :model/Field {f1-id :id} {:table_id t1-id :name "id" :base_type :type/Integer}+ ;; FK from t2 pointing to t1+ :model/Field _ {:table_id t2-id :name "t1_fk" :base_type :type/Integer+ :semantic_type :type/FK :fk_target_field_id f1-id}+ :model/Card {c1-id :id} {:database_id db-id+ :result_metadata [{:name "id"+ :base_type :type/Integer+ :effective_type :type/Integer+ :display_name "ID"+ :field_ref [:field "id" {:base-type :type/Integer}]}]}+ :model/Card {c2-id :id} {:database_id db-id+ :result_metadata [{:name "id"+ :base_type :type/Integer+ :effective_type :type/Integer+ :display_name "ID"+ :field_ref [:field "id" {:base-type :type/Integer}]}]}]+ (let [result (replacement.source-check/check-replace-source+ [:card c1-id] [:card c2-id])]+ (is (nil? (some #{:incompatible-implicit-joins} (:errors result))))))))++(deftest no-implicit-joins-for-empty-table-test+ (testing "table with no fields: has-incoming-fks? returns false via not-empty nil branch"+ (mt/with-temp [:model/Database {db-id :id} {}+ :model/Table {t1-id :id} {:db_id db-id}+ :model/Table {t2-id :id} {:db_id db-id}]+ (let [result (replacement.source-check/check-replace-source+ [:table t1-id] [:table t2-id])]+ (is (true? (:success result)))+ (is (nil? (:errors result)))+ (is (nil? (:column_mappings result))+ "no column_mappings when both tables have no fields")))))++(deftest successful-replacement-test+ (testing "tables with matching columns produce a successful result with column_mappings"+ (mt/with-temp [:model/Database {db-id :id} {}+ :model/Table {t1-id :id} {:db_id db-id}+ :model/Table {t2-id :id} {:db_id db-id}+ :model/Field _ {:table_id t1-id :name "id" :base_type :type/Integer+ :effective_type :type/Integer}+ :model/Field _ {:table_id t1-id :name "name" :base_type :type/Text+ :effective_type :type/Text}+ :model/Field _ {:table_id t2-id :name "id" :base_type :type/Integer+ :effective_type :type/Integer}+ :model/Field _ {:table_id t2-id :name "name" :base_type :type/Text+ :effective_type :type/Text}]+ (let [result (replacement.source-check/check-replace-source+ [:table t1-id] [:table t2-id])]+ (is (true? (:success result)))+ (is (nil? (:errors result)))+ (is (seq (:column_mappings result)))+ (doseq [m (:column_mappings result)]+ (is (:source m))+ (is (:target m)))))))++(deftest missing-column-in-target-test+ (testing "source column with no matching target causes failure via has-missing?"+ (mt/with-temp [:model/Database {db-id :id} {}+ :model/Table {t1-id :id} {:db_id db-id}+ :model/Table {t2-id :id} {:db_id db-id}+ :model/Field _ {:table_id t1-id :name "id" :base_type :type/Integer}+ :model/Field _ {:table_id t1-id :name "extra" :base_type :type/Text}+ :model/Field _ {:table_id t2-id :name "id" :base_type :type/Integer}]+ (let [result (replacement.source-check/check-replace-source+ [:table t1-id] [:table t2-id])]+ (is (false? (:success result)))+ (is (seq (:column_mappings result)))+ (is (some (fn [m] (and (:source m) (nil? (:target m))))+ (:column_mappings result))+ "should have a mapping with source but no target for the missing column")))))++(deftest column-type-mismatch-test+ (testing "columns with same name but different effective types produce column-level errors"+ (mt/with-temp [:model/Database {db-id :id} {}+ :model/Table {t1-id :id} {:db_id db-id}+ :model/Table {t2-id :id} {:db_id db-id}+ :model/Field _ {:table_id t1-id :name "col" :base_type :type/Integer+ :effective_type :type/Integer}+ :model/Field _ {:table_id t2-id :name "col" :base_type :type/Text+ :effective_type :type/Text}]+ (let [result (replacement.source-check/check-replace-source+ [:table t1-id] [:table t2-id])]+ (is (false? (:success result)))+ (is (seq (:column_mappings result)))+ (is (some (fn [m] (seq (:errors m)))+ (:column_mappings result))+ "should have column-level errors for type mismatches")))))++(deftest extra-column-in-target-test+ (testing "extra columns in target (not in source) do not cause failure"+ (mt/with-temp [:model/Database {db-id :id} {}+ :model/Table {t1-id :id} {:db_id db-id}+ :model/Table {t2-id :id} {:db_id db-id}+ :model/Field _ {:table_id t1-id :name "id" :base_type :type/Integer+ :effective_type :type/Integer}+ :model/Field _ {:table_id t2-id :name "id" :base_type :type/Integer+ :effective_type :type/Integer}+ :model/Field _ {:table_id t2-id :name "extra" :base_type :type/Text+ :effective_type :type/Text}]+ (let [result (replacement.source-check/check-replace-source+ [:table t1-id] [:table t2-id])]+ (is (true? (:success result)))+ (is (seq (:column_mappings result)))+ (is (some (fn [m] (and (nil? (:source m)) (:target m)))+ (:column_mappings result))+ "extra target column appears as a mapping with target only")))))++(deftest multiple-errors-test+ (testing "cycle + implicit-joins errors are both reported"+ (mt/with-temp [:model/Database {db-id :id} {}+ :model/Table {t1-id :id} {:db_id db-id}+ :model/Table {t2-id :id} {:db_id db-id}+ :model/Table {t3-id :id} {:db_id db-id}+ :model/Field {f1-id :id} {:table_id t1-id :name "id" :base_type :type/Integer}+ :model/Field _ {:table_id t2-id :name "id" :base_type :type/Integer}+ ;; FK from t3 to t1 triggers implicit-joins+ :model/Field _ {:table_id t3-id :name "t1_fk" :base_type :type/Integer+ :semantic_type :type/FK :fk_target_field_id f1-id}]+ (with-redefs [replacement.usages/transitive-usages+ (constantly [[:table t2-id]])]+ (let [result (replacement.source-check/check-replace-source+ [:table t1-id] [:table t2-id])]+ (is (false? (:success result)))+ (is (some #{:cycle-detected} (:errors result)))+ (is (some #{:incompatible-implicit-joins} (:errors result))))))))++(deftest database-mismatch-with-implicit-joins-test+ (testing "db-mismatch and implicit-joins errors are both reported, still no column_mappings"+ (mt/with-temp [:model/Database {db1-id :id} {}+ :model/Database {db2-id :id} {}+ :model/Table {t1-id :id} {:db_id db1-id}+ :model/Table {t2-id :id} {:db_id db2-id}+ :model/Table {t3-id :id} {:db_id db1-id}+ :model/Field {f1-id :id} {:table_id t1-id :name "id" :base_type :type/Integer}+ :model/Field _ {:table_id t2-id :name "id" :base_type :type/Integer}+ ;; FK from t3 to t1+ :model/Field _ {:table_id t3-id :name "t1_fk" :base_type :type/Integer+ :semantic_type :type/FK :fk_target_field_id f1-id}]+ (let [result (replacement.source-check/check-replace-source+ [:table t1-id] [:table t2-id])]+ (is (false? (:success result)))+ (is (some #{:database-mismatch} (:errors result)))+ (is (some #{:incompatible-implicit-joins} (:errors result)))+ (is (nil? (:column_mappings result))+ "column_mappings still not computed when databases differ")))))++(deftest format-column-structure-test+ (testing "column_mappings entries have the expected structure from format-column"+ (mt/with-temp [:model/Database {db-id :id} {}+ :model/Table {t1-id :id} {:db_id db-id}+ :model/Table {t2-id :id} {:db_id db-id}+ :model/Field _ {:table_id t1-id :name "test_col" :base_type :type/Integer+ :effective_type :type/Integer :semantic_type :type/PK+ :display_name "Test Column"}+ :model/Field _ {:table_id t2-id :name "test_col" :base_type :type/Integer+ :effective_type :type/Integer :semantic_type :type/PK+ :display_name "Test Column"}]+ (let [result (replacement.source-check/check-replace-source+ [:table t1-id] [:table t2-id])+ mapping (first (:column_mappings result))]+ (is (true? (:success result)))+ (is (some? mapping))+ (testing "source column structure"+ (let [src (:source mapping)]+ (is (integer? (:id src)))+ (is (string? (:name src)))+ (is (string? (:display_name src)))+ (is (string? (:base_type src)))+ (is (string? (:effective_type src)))))+ (testing "target column structure"+ (let [tgt (:target mapping)]+ (is (integer? (:id tgt)))+ (is (string? (:name tgt)))+ (is (string? (:display_name tgt)))+ (is (string? (:base_type tgt)))+ (is (string? (:effective_type tgt)))))))))++(deftest card-same-database-test+ (testing "card sources in the same database do not produce :database-mismatch"+ (mt/with-temp [:model/Database {db-id :id} {}+ :model/Card {c1-id :id} {:database_id db-id+ :result_metadata [{:name "id"+ :base_type :type/Integer+ :effective_type :type/Integer+ :display_name "ID"+ :field_ref [:field "id" {:base-type :type/Integer}]}]}+ :model/Card {c2-id :id} {:database_id db-id+ :result_metadata [{:name "id"+ :base_type :type/Integer+ :effective_type :type/Integer+ :display_name "ID"+ :field_ref [:field "id" {:base-type :type/Integer}]}]}]+ (let [result (replacement.source-check/check-replace-source+ [:card c1-id] [:card c2-id])]+ (is (true? (:success result)))+ (is (nil? (some #{:database-mismatch} (:errors result))))))))diff --git a/enterprise/backend/test/metabase_enterprise/replacement/source_swap_test.clj b/enterprise/backend/test/metabase_enterprise/replacement/source_swap_test.cljnew file mode 100644index 00000000000..c66e04d9c1f--- /dev/null+++ b/enterprise/backend/test/metabase_enterprise/replacement/source_swap_test.clj@@ -0,0 +1,512 @@+(ns metabase-enterprise.replacement.source-swap-test+ (:require+ [clojure.test :refer [deftest is testing]]+ [medley.core :as m]+ [metabase-enterprise.dependencies.events]+ [metabase-enterprise.replacement.field-refs :as replacement.field-refs]+ [metabase-enterprise.replacement.source-swap :as replacement.source-swap]+ [metabase.events.core :as events]+ [metabase.lib.core :as lib]+ [metabase.lib.metadata :as lib.metadata]+ [metabase.test :as mt]+ [metabase.util.json :as json]+ [toucan2.core :as t2]))++(set! *warn-on-reflection* true)++(comment+ metabase-enterprise.dependencies.events/keep-me)++;;; ------------------------------------------------ Card --------------------------------------------------------++(deftest card-swap-source!-source-table-test+ (testing "swap-source! should update source-table and table_id when swapping tables"+ (mt/with-premium-features #{:dependencies}+ (mt/with-test-user :rasta+ (let [mp (mt/metadata-provider)+ query (lib/query mp (lib.metadata/table mp (mt/id :orders)))]+ (mt/with-temp [:model/Card {card-id :id} {:dataset_query query+ :table_id (mt/id :orders)}]+ (replacement.field-refs/upgrade-field-refs! [:card card-id])+ (replacement.source-swap/swap-source! [:card card-id]+ [:table (mt/id :orders)]+ [:table (mt/id :reviews)])+ (let [card (t2/select-one :model/Card card-id)]+ (is (= (mt/id :reviews) (:table_id card)))+ (is (= (mt/id :reviews)+ (get-in card [:dataset_query :stages 0 :source-table]))))))))))++(deftest card-swap-source!-source-card-test+ (testing "swap-source! should update source-card when swapping card sources"+ (mt/with-premium-features #{:dependencies}+ (mt/with-test-user :rasta+ (let [mp (mt/metadata-provider)]+ (mt/with-temp [:model/Card {old-source-id :id} {:dataset_query (lib/query mp (lib.metadata/table mp (mt/id :orders)))}+ :model/Card {new-source-id :id} {:dataset_query (lib/query mp (lib.metadata/table mp (mt/id :reviews)))}+ :model/Card {card-id :id} {:dataset_query {:lib/type :mbql/query+ :database (mt/id)+ :stages [{:lib/type :mbql.stage/mbql+ :source-card old-source-id}]}}]+ (replacement.field-refs/upgrade-field-refs! [:card card-id])+ (replacement.source-swap/swap-source! [:card card-id]+ [:card old-source-id]+ [:card new-source-id])+ (is (= new-source-id+ (get-in (t2/select-one :model/Card card-id)+ [:dataset_query :stages 0 :source-card])))))))))++(deftest card-swap-source!-native-query-test+ (testing "swap-source! should replace table name in native SQL"+ (mt/with-premium-features #{:dependencies}+ (mt/with-test-user :rasta+ (let [mp (mt/metadata-provider)+ query (lib/native-query mp "SELECT ID FROM ORDERS")]+ (mt/with-temp [:model/Card {card-id :id} {:dataset_query query}]+ (replacement.field-refs/upgrade-field-refs! [:card card-id])+ (replacement.source-swap/swap-source! [:card card-id]+ [:table (mt/id :orders)]+ [:table (mt/id :reviews)])+ (is (= "SELECT ID FROM PUBLIC.REVIEWS"+ (lib/raw-native-query (:dataset_query (t2/select-one :model/Card card-id)))))))))))++(deftest card-swap-source!-native-query-preserves-result-metadata-test+ (testing "swap-source! should preserve `:result_metadata` for native queries"+ (mt/with-premium-features #{:dependencies}+ (mt/with-test-user :rasta+ (let [mp (mt/metadata-provider)+ query (lib/native-query mp "SELECT ID FROM ORDERS")+ result-metadata [{:name "ID" :base_type :type/Integer}]]+ (mt/with-temp [:model/Card {card-id :id} {:dataset_query query+ :result_metadata result-metadata}]+ (replacement.field-refs/upgrade-field-refs! [:card card-id])+ (replacement.source-swap/swap-source! [:card card-id]+ [:table (mt/id :orders)]+ [:table (mt/id :reviews)])+ (let [card (t2/select-one :model/Card card-id)]+ (is (= "SELECT ID FROM PUBLIC.REVIEWS"+ (lib/raw-native-query (:dataset_query card))))+ (is (=? [{:name "ID" :base_type :type/Integer}]+ (:result_metadata card))))))))))++(deftest card-swap-source!-broken-query-test+ (testing "swap-source! should not crash on a card with a broken query"+ (mt/with-premium-features #{:dependencies}+ (mt/with-test-user :rasta+ (let [mp (mt/metadata-provider)]+ (mt/with-temp [:model/Card {card-id :id} {:dataset_query (lib/native-query mp "SELECT 1")}]+ (t2/update! :model/Card card-id {:dataset_query {}})+ (replacement.source-swap/swap-source! [:card card-id]+ [:table (mt/id :orders)]+ [:table (mt/id :reviews)])+ (is (= {} (:dataset_query (t2/select-one :model/Card card-id))))))))))++;;; ------------------------------------------------ Transform --------------------------------------------------------++(deftest transform-swap-source!-source-table-test+ (testing "swap-source! should update source-table in transform query"+ (mt/with-premium-features #{:dependencies}+ (mt/with-test-user :rasta+ (let [mp (mt/metadata-provider)+ query (lib/query mp (lib.metadata/table mp (mt/id :orders)))]+ (mt/with-temp [:model/Transform {transform-id :id} {:name "test transform"+ :source {:type "query" :query query}+ :target {:database (mt/id) :table "out"}}]+ (replacement.field-refs/upgrade-field-refs! [:transform transform-id])+ (replacement.source-swap/swap-source! [:transform transform-id]+ [:table (mt/id :orders)]+ [:table (mt/id :reviews)])+ (is (= (mt/id :reviews)+ (get-in (t2/select-one :model/Transform transform-id)+ [:source :query :stages 0 :source-table])))))))))++(deftest transform-swap-source!-broken-query-test+ (testing "swap-source! should not crash on a transform with a broken query"+ (mt/with-premium-features #{:dependencies}+ (mt/with-test-user :rasta+ (let [mp (mt/metadata-provider)]+ (mt/with-temp [:model/Transform {transform-id :id} {:name "test transform"+ :source {:type "query"+ :query (lib/native-query mp "SELECT 1")}+ :target {:database (mt/id) :table "out"}}]+ (t2/query-one {:update :transform+ :set {:source "{\"type\":\"query\",\"query\":{}}"}+ :where [:= :id transform-id]})+ (is (nil? (replacement.source-swap/swap-source! [:transform transform-id]+ [:table (mt/id :orders)]+ [:table (mt/id :reviews)])))))))))++;;; ------------------------------------------------- Segment ----------------------------------------------------------++(deftest segment-swap-source!-source-table-test+ (testing "swap-source! should update source-table and table_id in segment definition"+ (mt/with-premium-features #{:dependencies}+ (mt/with-test-user :rasta+ (let [mp (mt/metadata-provider)+ query (-> (lib/query mp (lib.metadata/table mp (mt/id :orders)))+ (lib/filter (lib/> (lib.metadata/field mp (mt/id :orders :id)) 10)))]+ (mt/with-temp [:model/Segment {segment-id :id} {:table_id (mt/id :orders)+ :definition query}]+ (replacement.field-refs/upgrade-field-refs! [:segment segment-id])+ (replacement.source-swap/swap-source! [:segment segment-id]+ [:table (mt/id :orders)]+ [:table (mt/id :reviews)])+ (let [segment (t2/select-one :model/Segment segment-id)]+ (is (= (mt/id :reviews) (:table_id segment)))+ (is (= (mt/id :reviews)+ (get-in segment [:definition :stages 0 :source-table]))))))))))++(deftest segment-swap-source!-broken-query-test+ (testing "swap-source! should not crash on a segment with a broken definition"+ (mt/with-premium-features #{:dependencies}+ (mt/with-test-user :rasta+ (let [mp (mt/metadata-provider)+ query (-> (lib/query mp (lib.metadata/table mp (mt/id :orders)))+ (lib/filter (lib/> (lib.metadata/field mp (mt/id :orders :id)) 10)))]+ (mt/with-temp [:model/Segment {segment-id :id} {:table_id (mt/id :orders)+ :definition query}]+ (t2/query-one {:update :segment+ :set {:definition "{}"}+ :where [:= :id segment-id]})+ (is (nil? (replacement.source-swap/swap-source! [:segment segment-id]+ [:table (mt/id :orders)]+ [:table (mt/id :reviews)])))))))))++;;; ------------------------------------------------- Measure ----------------------------------------------------------++(deftest measure-swap-source!-source-table-test+ (testing "swap-source! should update source-table and table_id in measure definition"+ (mt/with-premium-features #{:dependencies}+ (mt/with-test-user :rasta+ (let [mp (mt/metadata-provider)+ query (-> (lib/query mp (lib.metadata/table mp (mt/id :orders)))+ (lib/aggregate (lib/sum (lib.metadata/field mp (mt/id :orders :id)))))]+ (mt/with-temp [:model/Measure {measure-id :id} {:table_id (mt/id :orders)+ :name "test measure"+ :definition query}]+ (replacement.field-refs/upgrade-field-refs! [:measure measure-id])+ (replacement.source-swap/swap-source! [:measure measure-id]+ [:table (mt/id :orders)]+ [:table (mt/id :reviews)])+ (let [measure (t2/select-one :model/Measure measure-id)]+ (is (= (mt/id :reviews) (:table_id measure)))+ (is (= (mt/id :reviews)+ (get-in measure [:definition :stages 0 :source-table]))))))))))++(deftest measure-swap-source!-broken-query-test+ (testing "swap-source! should not crash on a measure with a broken definition"+ (mt/with-premium-features #{:dependencies}+ (mt/with-test-user :rasta+ (let [mp (mt/metadata-provider)+ query (-> (lib/query mp (lib.metadata/table mp (mt/id :orders)))+ (lib/aggregate (lib/count)))]+ (mt/with-temp [:model/Measure {measure-id :id} {:table_id (mt/id :orders)+ :name "test measure"+ :definition query}]+ (t2/query-one {:update :measure+ :set {:definition "{}"}+ :where [:= :id measure-id]})+ (is (nil? (replacement.source-swap/swap-source! [:measure measure-id]+ [:table (mt/id :orders)]+ [:table (mt/id :reviews)])))))))))++;;; ------------------------------------------------ Dashboard --------------------------------------------------------++(deftest dashboard-swap-source!-parameter-mappings-card-to-card-test+ (testing "swap-source! should walk parameter mapping targets without corruption during card-to-card swap"+ (mt/with-premium-features #{:dependencies}+ (mt/with-test-user :rasta+ (let [mp (mt/metadata-provider)+ query (-> (lib/query mp (lib.metadata/table mp (mt/id :orders)))+ (lib/breakout (lib.metadata/field mp (mt/id :orders :id))))]+ (mt/with-temp [:model/Card {old-card-id :id} {:dataset_query query}+ :model/Card {new-card-id :id} {:dataset_query query}+ :model/Dashboard {dashboard-id :id} {}+ :model/DashboardCard {dashcard-id :id}+ {:dashboard_id dashboard-id+ :card_id old-card-id+ :parameter_mappings [{:parameter_id "my-param"+ :card_id old-card-id+ :target [:dimension+ [:field "ID" {:base-type :type/Integer}]]}]}]+ (replacement.source-swap/swap-source! [:dashboard dashboard-id]+ [:card old-card-id]+ [:card new-card-id])+ (let [dashcard (t2/select-one :model/DashboardCard dashcard-id)]+ (is (=? {:parameter_mappings [{:parameter_id "my-param"+ :card_id old-card-id+ :target [:dimension [:field (mt/id :orders :id) {}]]}]}+ dashcard)))))))))++(deftest dashboard-swap-source!-parameter-mappings-implicit-join-test+ (testing "swap-source! should update :source-field in implicitly joined parameter targets"+ (mt/with-premium-features #{:dependencies}+ (mt/with-test-user :rasta+ (let [mp (mt/metadata-provider)+ query (lib/query mp (lib.metadata/table mp (mt/id :orders)))+ category-col (m/find-first #(= "CATEGORY" (:name %)) (lib/filterable-columns query))+ target [:dimension (lib/->legacy-MBQL (lib/ref category-col))]]+ (mt/with-temp [:model/Card {card-id :id} {:dataset_query query}+ :model/Dashboard {dashboard-id :id} {}+ :model/DashboardCard {dashcard-id :id}+ {:dashboard_id dashboard-id+ :card_id card-id+ :parameter_mappings [{:parameter_id "cat-param"+ :card_id card-id+ :target target}]}]+ (replacement.field-refs/upgrade-field-refs! [:dashboard dashboard-id])+ (replacement.source-swap/swap-source! [:dashboard dashboard-id]+ [:table (mt/id :orders)]+ [:table (mt/id :reviews)])+ (let [updated-target (get-in (t2/select-one :model/DashboardCard dashcard-id)+ [:parameter_mappings 0 :target])]+ (is (=? [:dimension [:field (mt/id :products :category)+ {:source-field (mt/id :reviews :product_id)}]]+ updated-target)))))))))++(defn- make-click-behavior+ [card-id target]+ {:type "link"+ :linkType "question"+ :targetId card-id+ :parameterMapping+ {(json/encode target)+ {:id (json/encode target)+ :source {:type :column+ :id "ID"+ :name "ID"}+ :target {:type :dimension+ :id (json/encode target)+ :dimension target}}}})++(deftest dashboard-swap-source!-click-behavior-implicit-join-test+ (testing "swap-source! should update :source-field in click behavior dimension targets"+ (mt/with-premium-features #{:dependencies}+ (mt/with-test-user :rasta+ (let [mp (mt/metadata-provider)+ target-query (-> (lib/query mp (lib.metadata/table mp (mt/id :orders)))+ (lib/breakout (lib.metadata/field mp (mt/id :orders :id))))+ source-query (lib/query mp (lib.metadata/table mp (mt/id :products)))+ category-col (m/find-first #(= "CATEGORY" (:name %)) (lib/filterable-columns target-query))+ target [:dimension (lib/->legacy-MBQL (lib/ref category-col))]]+ (mt/with-temp [:model/Card {target-card-id :id} {:dataset_query target-query}+ :model/Card {source-card-id :id} {:dataset_query source-query}+ :model/Dashboard {dashboard-id :id} {}+ :model/DashboardCard {dashcard-id :id}+ {:dashboard_id dashboard-id+ :card_id source-card-id+ :visualization_settings {:click_behavior (make-click-behavior target-card-id target)}}]+ (replacement.field-refs/upgrade-field-refs! [:dashboard dashboard-id])+ (replacement.source-swap/swap-source! [:dashboard dashboard-id]+ [:table (mt/id :orders)]+ [:table (mt/id :reviews)])+ (let [viz (:visualization_settings (t2/select-one :model/DashboardCard dashcard-id))+ viz-key (keyword (json/encode target))]+ (is (=? {:click_behavior+ {:targetId target-card-id+ :parameterMapping+ {viz-key+ {:target {:dimension ["dimension"+ [:field (mt/id :products :category)+ {:source-field (mt/id :reviews :product_id)}]]}}}}}+ viz)))))))))++(deftest card-swap-source!-dependencies-updated-test+ (testing "swap-source! for card updates dependency records from old-source to new-source"+ (mt/with-premium-features #{:dependencies}+ (mt/with-test-user :rasta+ (let [mp (mt/metadata-provider)+ query (lib/query mp (lib.metadata/table mp (mt/id :orders)))]+ (mt/with-temp [:model/Card {card-id :id} {:dataset_query query+ :table_id (mt/id :orders)}]+ (replacement.field-refs/upgrade-field-refs! [:card card-id])+ (mt/with-model-cleanup [:model/Dependency]+ ;; Create a dependency: card -> orders table+ (events/publish-event! :event/card-create {:object (t2/select-one :model/Card card-id)+ :user-id (mt/user->id :rasta)})+ (is (t2/exists? :model/Dependency+ :from_entity_type :card :from_entity_id card-id+ :to_entity_type :table :to_entity_id (mt/id :orders))+ "Dependency on orders should exist before swap")+ (replacement.source-swap/swap-source! [:card card-id]+ [:table (mt/id :orders)]+ [:table (mt/id :reviews)])+ (is (not (t2/exists? :model/Dependency+ :from_entity_type :card :from_entity_id card-id+ :to_entity_type :table :to_entity_id (mt/id :orders)))+ "Dependency on orders should be gone after swap")+ (is (t2/exists? :model/Dependency+ :from_entity_type :card :from_entity_id card-id+ :to_entity_type :table :to_entity_id (mt/id :reviews))+ "Dependency on reviews should exist after swap"))))))))++(deftest segment-swap-source!-dependencies-updated-test+ (testing "swap-source! for segment updates dependency records"+ (mt/with-premium-features #{:dependencies}+ (mt/with-test-user :rasta+ (let [mp (mt/metadata-provider)+ query (-> (lib/query mp (lib.metadata/table mp (mt/id :orders)))+ (lib/filter (lib/> (lib.metadata/field mp (mt/id :orders :id)) 10)))]+ (mt/with-temp [:model/Segment {segment-id :id} {:table_id (mt/id :orders)+ :definition query}]+ (replacement.field-refs/upgrade-field-refs! [:segment segment-id])+ (mt/with-model-cleanup [:model/Dependency]+ (events/publish-event! :event/segment-create {:object (t2/select-one :model/Segment segment-id)+ :user-id (mt/user->id :rasta)})+ (is (t2/exists? :model/Dependency+ :from_entity_type :segment :from_entity_id segment-id+ :to_entity_type :table :to_entity_id (mt/id :orders)))+ (replacement.source-swap/swap-source! [:segment segment-id]+ [:table (mt/id :orders)]+ [:table (mt/id :reviews)])+ (is (not (t2/exists? :model/Dependency+ :from_entity_type :segment :from_entity_id segment-id+ :to_entity_type :table :to_entity_id (mt/id :orders)))+ "Dependency on orders should be gone")+ (is (t2/exists? :model/Dependency+ :from_entity_type :segment :from_entity_id segment-id+ :to_entity_type :table :to_entity_id (mt/id :reviews))+ "Dependency on reviews should exist"))))))))++(deftest measure-swap-source!-dependencies-updated-test+ (testing "swap-source! for measure updates dependency records"+ (mt/with-premium-features #{:dependencies}+ (mt/with-test-user :rasta+ (let [mp (mt/metadata-provider)+ query (-> (lib/query mp (lib.metadata/table mp (mt/id :orders)))+ (lib/aggregate (lib/sum (lib.metadata/field mp (mt/id :orders :id)))))]+ (mt/with-temp [:model/Measure {measure-id :id} {:table_id (mt/id :orders)+ :name "test measure"+ :definition query}]+ (replacement.field-refs/upgrade-field-refs! [:measure measure-id])+ (mt/with-model-cleanup [:model/Dependency]+ (events/publish-event! :event/measure-create {:object (t2/select-one :model/Measure measure-id)+ :user-id (mt/user->id :rasta)})+ (is (t2/exists? :model/Dependency+ :from_entity_type :measure :from_entity_id measure-id+ :to_entity_type :table :to_entity_id (mt/id :orders)))+ (replacement.source-swap/swap-source! [:measure measure-id]+ [:table (mt/id :orders)]+ [:table (mt/id :reviews)])+ (is (not (t2/exists? :model/Dependency+ :from_entity_type :measure :from_entity_id measure-id+ :to_entity_type :table :to_entity_id (mt/id :orders)))+ "Dependency on orders should be gone")+ (is (t2/exists? :model/Dependency+ :from_entity_type :measure :from_entity_id measure-id+ :to_entity_type :table :to_entity_id (mt/id :reviews))+ "Dependency on reviews should exist"))))))))++(deftest transform-swap-source!-dependencies-updated-test+ (testing "swap-source! for transform updates dependency records"+ (mt/with-premium-features #{:dependencies}+ (mt/with-test-user :rasta+ (let [mp (mt/metadata-provider)+ query (lib/query mp (lib.metadata/table mp (mt/id :orders)))]+ (mt/with-temp [:model/Transform {transform-id :id} {:name "test transform"+ :source {:type "query" :query query}+ :target {:database (mt/id) :table "out"}}]+ (replacement.field-refs/upgrade-field-refs! [:transform transform-id])+ (mt/with-model-cleanup [:model/Dependency]+ (events/publish-event! :event/transform-create {:object (t2/select-one :model/Transform transform-id)+ :user-id (mt/user->id :rasta)})+ (is (t2/exists? :model/Dependency+ :from_entity_type :transform :from_entity_id transform-id+ :to_entity_type :table :to_entity_id (mt/id :orders)))+ (replacement.source-swap/swap-source! [:transform transform-id]+ [:table (mt/id :orders)]+ [:table (mt/id :reviews)])+ (is (not (t2/exists? :model/Dependency+ :from_entity_type :transform :from_entity_id transform-id+ :to_entity_type :table :to_entity_id (mt/id :orders)))+ "Dependency on orders should be gone")+ (is (t2/exists? :model/Dependency+ :from_entity_type :transform :from_entity_id transform-id+ :to_entity_type :table :to_entity_id (mt/id :reviews))+ "Dependency on reviews should exist"))))))))++(deftest card-swap-source!-no-change-preserves-state-test+ (testing "swap-source! should not modify the card when the source is not in the query"+ (mt/with-premium-features #{:dependencies}+ (mt/with-test-user :rasta+ (let [mp (mt/metadata-provider)+ query (lib/query mp (lib.metadata/table mp (mt/id :orders)))]+ (mt/with-temp [:model/Card {card-id :id} {:dataset_query query+ :table_id (mt/id :orders)}]+ (replacement.field-refs/upgrade-field-refs! [:card card-id])+ (let [card-before (t2/select-one :model/Card card-id)]+ ;; Swap a source that doesn't appear in this query+ (replacement.source-swap/swap-source! [:card card-id]+ [:table (mt/id :people)]+ [:table (mt/id :reviews)])+ (let [card-after (t2/select-one :model/Card card-id)]+ (is (= (:dataset_query card-before) (:dataset_query card-after))+ "Query should not change")+ (is (= (:table_id card-before) (:table_id card-after))+ "table_id should not change")))))))))++(deftest card-swap-source!-mbql-query-does-not-preserve-result-metadata-test+ (testing "swap-source! should not force-preserve old result_metadata for MBQL queries (only for native)"+ (mt/with-premium-features #{:dependencies}+ (mt/with-test-user :rasta+ (let [mp (mt/metadata-provider)+ mbql-query (lib/query mp (lib.metadata/table mp (mt/id :orders)))+ result-metadata [{:name "FAKE_COLUMN" :base_type :type/Integer}]]+ (mt/with-temp [:model/Card {card-id :id} {:dataset_query mbql-query+ :table_id (mt/id :orders)+ :result_metadata result-metadata}]+ (replacement.field-refs/upgrade-field-refs! [:card card-id])+ (replacement.source-swap/swap-source! [:card card-id]+ [:table (mt/id :orders)]+ [:table (mt/id :reviews)])+ ;; For MBQL queries, result_metadata should NOT be the old stale value+ ;; because :verified-result-metadata? is NOT set, so model hooks recalculate it+ (let [card (t2/select-one :model/Card card-id)]+ (is (not= result-metadata (:result_metadata card))+ "Old result_metadata should not be force-preserved for MBQL queries"))))))))++(deftest dashcard-swap-source!-no-change-preserves-state-test+ (testing "dashcard-swap-source! should not update when parameter-mappings and viz-settings don't change"+ (mt/with-premium-features #{:dependencies}+ (mt/with-test-user :rasta+ (let [mp (mt/metadata-provider)+ query (lib/query mp (lib.metadata/table mp (mt/id :orders)))]+ (mt/with-temp [:model/Card {card-id :id} {:dataset_query query}+ :model/Dashboard {dashboard-id :id} {}+ :model/DashboardCard {dashcard-id :id}+ {:dashboard_id dashboard-id+ :card_id card-id+ :parameter_mappings []}]+ (let [dashcard-before (t2/select-one :model/DashboardCard dashcard-id)]+ ;; Swap from a source that doesn't affect dashcard params+ (replacement.source-swap/swap-source! [:dashboard dashboard-id]+ [:table (mt/id :people)]+ [:table (mt/id :reviews)])+ (let [dashcard-after (t2/select-one :model/DashboardCard dashcard-id)]+ (is (= (:parameter_mappings dashcard-before) (:parameter_mappings dashcard-after)))+ (is (= (:visualization_settings dashcard-before) (:visualization_settings dashcard-after)))))))))))++(deftest dashboard-swap-source!-parameter-mappings-change-correctly-test+ (testing "dashcard-swap-source! correctly compares and updates parameter_mappings"+ (mt/with-premium-features #{:dependencies}+ (mt/with-test-user :rasta+ (let [mp (mt/metadata-provider)+ query (-> (lib/query mp (lib.metadata/table mp (mt/id :orders)))+ (lib/breakout (lib.metadata/field mp (mt/id :orders :id))))]+ (mt/with-temp [:model/Card {old-card-id :id} {:dataset_query query}+ :model/Card {new-card-id :id} {:dataset_query query}+ :model/Dashboard {dashboard-id :id} {}+ :model/DashboardCard {dashcard-id :id}+ {:dashboard_id dashboard-id+ :card_id old-card-id+ :parameter_mappings [{:parameter_id "my-param"+ :card_id old-card-id+ :target [:dimension+ [:field "ID" {:base-type :type/Integer}]]}]}]+ (let [original-pm (:parameter_mappings (t2/select-one :model/DashboardCard dashcard-id))]+ (replacement.source-swap/swap-source! [:dashboard dashboard-id]+ [:card old-card-id]+ [:card new-card-id])+ ;; The parameter_mappings should be updated (walked through)+ (let [updated-pm (:parameter_mappings (t2/select-one :model/DashboardCard dashcard-id))]+ (is (not= original-pm updated-pm)+ "parameter_mappings should have been updated")))))))))diff --git a/enterprise/backend/test/metabase_enterprise/replacement/usages_test.clj b/enterprise/backend/test/metabase_enterprise/replacement/usages_test.cljnew file mode 100644index 00000000000..e056c8c5695--- /dev/null+++ b/enterprise/backend/test/metabase_enterprise/replacement/usages_test.clj@@ -0,0 +1,163 @@+(ns metabase-enterprise.replacement.usages-test+ (:require+ [clojure.test :refer [deftest is testing]]+ [metabase-enterprise.dependencies.events]+ [metabase-enterprise.replacement.usages :as replacement.usages]+ [metabase.graph.core :as graph]+ [metabase.lib.core :as lib]+ [metabase.lib.metadata :as lib.metadata]+ [metabase.queries.models.card :as card]+ [metabase.test :as mt]))++(set! *warn-on-reflection* true)++(comment+ metabase-enterprise.dependencies.events/keep-me)++;;; ------------------------------------------------ In-memory graph helpers ------------------------------------------------++(defn- deps->adjacency-map+ "Convert a seq of dependency edges into an adjacency map for the dependents graph.++ Each edge is `[from-entity to-entity]` meaning `from-entity` depends on `to-entity`.+ For the dependents graph, we want `to-entity -> #{from-entity}` (who depends on me?).++ Example:+ (deps->adjacency-map [[[:card 2] [:card 1]] ; card 2 depends on card 1+ [[:card 3] [:card 2]] ; card 3 depends on card 2+ [[:dashboard 1] [:card 3]]]) ; dashboard 1 depends on card 3+ => {[:card 1] #{[:card 2]}+ [:card 2] #{[:card 3]}+ [:card 3] #{[:dashboard 1]}}"+ [edges]+ (reduce (fn [acc [from to]]+ (update acc to (fnil conj #{}) from))+ {}+ edges))++(defn- test-graph+ "Create an in-memory dependency graph from a seq of edges.++ Each edge is `[dependent dependency]` - meaning the first entity depends on the second.+ Returns a graph suitable for passing to `replacement.usages/transitive-usages`."+ [edges]+ (graph/in-memory (deps->adjacency-map edges)))++;;; ------------------------------------------------ Database-backed test helpers ------------------------------------------------++(defn- card-with-query+ "Create a card map for the given table keyword."+ [card-name table-kw]+ (let [mp (mt/metadata-provider)]+ {:name card-name+ :database_id (mt/id)+ :display :table+ :query_type :query+ :type :question+ :dataset_query (lib/query mp (lib.metadata/table mp (mt/id table-kw)))+ :visualization_settings {}}))++(defn- card-sourced-from+ "Create a card map whose query sources `inner-card`."+ [card-name inner-card]+ (let [mp (mt/metadata-provider)+ card-meta (lib.metadata/card mp (:id inner-card))]+ {:name card-name+ :database_id (mt/id)+ :display :table+ :query_type :query+ :type :question+ :dataset_query (lib/query mp card-meta)+ :visualization_settings {}}))++(deftest usages-returns-dependent-cards-test+ (testing "usages returns cards that depend on a source card"+ (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]+ (let [source-card (card/create-card! (card-with-query "Source card" :products) user)+ child-card (card/create-card! (card-sourced-from "Child card" source-card) user)+ found-usages (replacement.usages/transitive-usages [:card (:id source-card)])]+ (is (some #(= [:card (:id child-card)] %) found-usages)+ "Child card should appear in usages"))))))))++(deftest usages-returns-transitive-dependents-test+ (testing "usages returns transitive dependents (grandchildren)"+ (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]+ (let [source-card (card/create-card! (card-with-query "Source card" :products) user)+ child-card (card/create-card! (card-sourced-from "Child card" source-card) user)+ grandchild-card (card/create-card! (card-sourced-from "Grandchild card" child-card) user)+ found-usages (replacement.usages/transitive-usages [:card (:id source-card)])]+ (is (some #(= [:card (:id child-card)] %) found-usages)+ "Child card should appear in usages")+ (is (some #(= [:card (:id grandchild-card)] %) found-usages)+ "Grandchild card should appear in usages"))))))))++(deftest usages-returns-empty-for-no-dependents-test+ (testing "usages returns empty seq for cards with no dependents"+ (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]+ (let [lonely-card (card/create-card! (card-with-query "Lonely card" :products) user)+ found-usages (replacement.usages/transitive-usages [:card (:id lonely-card)])]+ (is (empty? found-usages)+ "Card with no dependents should have empty usages"))))))))++(deftest usages-returns-table-dependents-test+ (testing "usages returns cards that depend on a table"+ (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]+ (let [card-on-table (card/create-card! (card-with-query "Card on products" :products) user)+ found-usages (replacement.usages/transitive-usages [:table (mt/id :products)])]+ (is (some #(= [:card (:id card-on-table)] %) found-usages)+ "Card using table should appear in usages"))))))))++;;; ------------------------------------------------ In-memory graph tests ------------------------------------------------+;; These tests use an in-memory dependency graph, avoiding database transactions and event publishing.++(deftest ^:parallel transitive-usages-includes-dashboards-test+ (testing "transitive-usages returns dashboards that contain transitive dependent cards"+ ;; Graph: Table 1 <- Card A <- Card B <- Card C <- Dashboard D+ (let [graph (test-graph [[[:card 1] [:table 1]] ; card 1 depends on table 1+ [[:card 2] [:card 1]] ; card 2 depends on card 1+ [[:card 3] [:card 2]] ; card 3 depends on card 2+ [[:dashboard 1] [:card 3]]]) ; dashboard 1 depends on card 3+ found-usages (set (replacement.usages/transitive-usages graph [:table 1]))]+ (testing "Card A (direct dependent of table) is included"+ (is (contains? found-usages [:card 1])))+ (testing "Card B (depends on Card A) is included"+ (is (contains? found-usages [:card 2])))+ (testing "Card C (depends on Card B) is included"+ (is (contains? found-usages [:card 3])))+ (testing "Dashboard D (contains Card C) is included"+ (is (contains? found-usages [:dashboard 1]))))))++(deftest ^:parallel transitive-usages-includes-dashboards-from-card-source-test+ (testing "transitive-usages from a card source includes dashboards containing transitive dependents"+ ;; Graph: Card A <- Card B <- Card C <- Dashboard D+ (let [graph (test-graph [[[:card 2] [:card 1]] ; card 2 depends on card 1+ [[:card 3] [:card 2]] ; card 3 depends on card 2+ [[:dashboard 1] [:card 3]]]) ; dashboard 1 depends on card 3+ found-usages (set (replacement.usages/transitive-usages graph [:card 1]))]+ (testing "Card B (depends on Card A) is included"+ (is (contains? found-usages [:card 2])))+ (testing "Card C (depends on Card B) is included"+ (is (contains? found-usages [:card 3])))+ (testing "Dashboard D (contains Card C) is included"+ (is (contains? found-usages [:dashboard 1]))))))++(deftest ^:parallel transitive-usages-dashboard-not-included-if-only-contains-unrelated-cards-test+ (testing "transitive-usages does NOT include dashboards that only contain unrelated cards"+ ;; Graph: Card A depends on nothing relevant, Dashboard D contains unrelated Card X+ (let [graph (test-graph [[[:card 2] [:table 2]] ; unrelated card depends on table 2+ [[:dashboard 1] [:card 2]]]) ; dashboard depends on unrelated card+ found-usages (set (replacement.usages/transitive-usages graph [:card 1]))]+ (testing "Dashboard is NOT in the transitive usages since it doesn't contain dependent cards"+ (is (not (contains? found-usages [:dashboard 1])))))))diff --git a/enterprise/backend/test/metabase_enterprise/serialization/models/entity_id_test.clj b/enterprise/backend/test/metabase_enterprise/serialization/models/entity_id_test.cljindex 93317944d95..3cf00879db1 100644--- a/enterprise/backend/test/metabase_enterprise/serialization/models/entity_id_test.clj+++ b/enterprise/backend/test/metabase_enterprise/serialization/models/entity_id_test.clj@@ -83,6 +83,7 @@:model/QueryField:model/QueryTable:model/RecentViews+ :model/ReplacementRun:model/RemoteSyncObject:model/RemoteSyncTask:model/Revisiondiff --git a/enterprise/frontend/src/metabase-enterprise/api/index.ts b/enterprise/frontend/src/metabase-enterprise/api/index.tsindex f8bb044e0b2..d34be1599f7 100644--- a/enterprise/frontend/src/metabase-enterprise/api/index.ts+++ b/enterprise/frontend/src/metabase-enterprise/api/index.ts@@ -11,6 +11,7 @@ export * from "./library";export * from "./metabot";export * from "./remote-sync";export * from "./oidc";+export * from "./replacement";export * from "./saml";export * from "./scim";export * from "./support-access-grants";diff --git a/enterprise/frontend/src/metabase-enterprise/api/replacement.ts b/enterprise/frontend/src/metabase-enterprise/api/replacement.tsnew file mode 100644index 00000000000..d9c2d85c89c--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/api/replacement.ts@@ -0,0 +1,59 @@+import type {+ CheckReplaceSourceInfo,+ ReplaceSourceRequest,+ ReplaceSourceResponse,+ ReplaceSourceRun,+ ReplaceSourceRunId,+} from "metabase-types/api";++import { EnterpriseApi } from "./api";+import {+ idTag,+ invalidateTags,+ provideReplaceSourceRunTags,+ tag,+} from "./tags";++export const replacementApi = EnterpriseApi.injectEndpoints({+ endpoints: (builder) => ({+ checkReplaceSource: builder.query<+ CheckReplaceSourceInfo,+ ReplaceSourceRequest+ >({+ query: (body) => ({+ method: "POST",+ url: "/api/ee/replacement/check-replace-source",+ body,+ }),+ providesTags: (_response, _error, request) => [+ idTag(request.source_entity_type, request.source_entity_id),+ idTag(request.target_entity_type, request.target_entity_id),+ ],+ }),+ replaceSource: builder.mutation<+ ReplaceSourceResponse,+ ReplaceSourceRequest+ >({+ query: (body) => ({+ method: "POST",+ url: "/api/ee/replacement/replace-source",+ body,+ }),+ invalidatesTags: (_response, error) =>+ invalidateTags(error, [tag("table"), tag("card")]),+ }),+ getReplaceSourceRun: builder.query<ReplaceSourceRun, ReplaceSourceRunId>({+ query: (id) => ({+ method: "GET",+ url: `/api/ee/replacement/runs/${id}`,+ }),+ providesTags: (run) => (run ? provideReplaceSourceRunTags(run) : []),+ }),+ }),+});++export const {+ useCheckReplaceSourceQuery,+ useReplaceSourceMutation,+ useGetReplaceSourceRunQuery,+} = replacementApi;diff --git a/enterprise/frontend/src/metabase-enterprise/api/tags.ts b/enterprise/frontend/src/metabase-enterprise/api/tags.tsindex 1f32715f5e2..dc6a8f706fa 100644--- a/enterprise/frontend/src/metabase-enterprise/api/tags.ts+++ b/enterprise/frontend/src/metabase-enterprise/api/tags.ts@@ -19,6 +19,7 @@ import {type ExternalTransform,type MeasureDependencyNode,type PythonLibrary,+ type ReplaceSourceRun,type SandboxDependencyNode,type SegmentDependencyNode,type SnippetDependencyNode,@@ -48,6 +49,7 @@ export const ENTERPRISE_TAG_TYPES = ["remote-sync-branches","remote-sync-current-task","remote-sync-has-remote-changes",+ "replace-source-run","python-transform-library","workspace","support-access-grant",@@ -278,3 +280,9 @@ export function provideWorkspaceAllowedDatabaseTags(...databases.map((db) => idTag("database", db.id)),];}++export function provideReplaceSourceRunTags(+ run: ReplaceSourceRun,+): TagDescription<EnterpriseTagType>[] {+ return [idTag("replace-source-run", run.id)];+}diff --git a/enterprise/frontend/src/metabase-enterprise/dependencies/components/DependencyDiagnostics/DiagnosticsTable/utils.tsx b/enterprise/frontend/src/metabase-enterprise/dependencies/components/DependencyDiagnostics/DiagnosticsTable/utils.tsxindex 6b861f0202f..a31eeb590d5 100644--- a/enterprise/frontend/src/metabase-enterprise/dependencies/components/DependencyDiagnostics/DiagnosticsTable/utils.tsx+++ b/enterprise/frontend/src/metabase-enterprise/dependencies/components/DependencyDiagnostics/DiagnosticsTable/utils.tsx@@ -1,135 +1,17 @@import type { SortingState } from "@tanstack/react-table";import { t } from "ttag";-import { DateTime } from "metabase/common/components/DateTime";-import { Ellipsified } from "metabase/common/components/Ellipsified";-import { getUserName } from "metabase/lib/user";-import type { TreeTableColumnDef } from "metabase/ui";-import {- DEPENDENCY_SORT_COLUMNS,- type DependencyNode,- type DependencySortColumn,-} from "metabase-types/api";+import { DEPENDENCY_SORT_COLUMNS } from "metabase-types/api";import type { DependencySortOptions } from "../../../types";import {- getDependentErrorNodesCount,- getNodeLabel,- getNodeLastEditedAt,- getNodeLastEditedBy,- getNodeLocationInfo,-} from "../../../utils";+ getDependentsErrorsColumn,+ getDependentsWithErrorsColumn,+ getLocationColumn,+ getNameColumn,+} from "../../DependencyTable";import type { DependencyDiagnosticsMode } from "../types";-import { ErrorsCell } from "./ErrorsCell";-import { LocationCell } from "./LocationCell";-import { NameCell } from "./NameCell";--function getNameColumn(- header: string = t`Name`,-): TreeTableColumnDef<DependencyNode> {- return {- id: "name" satisfies DependencySortColumn,- header,- width: "auto",- maxAutoWidth: 520,- enableSorting: true,- accessorFn: (node) => getNodeLabel(node),- cell: ({ row }) => {- const node = row.original;- return <NameCell node={node} />;- },- };-}--function getLocationColumn(): TreeTableColumnDef<DependencyNode> {- return {- id: "location" satisfies DependencySortColumn,- header: t`Location`,- width: "auto",- maxAutoWidth: 520,- enableSorting: true,- accessorFn: (node) => {- const location = getNodeLocationInfo(node);- const links = location?.links ?? [];- return links.map((link) => link.label).join(", ");- },- cell: ({ row }) => {- const node = row.original;- return <LocationCell node={node} />;- },- };-}--function getDependentsErrorsColumn(): TreeTableColumnDef<DependencyNode> {- return {- id: "dependents-errors" satisfies DependencySortColumn,- header: t`Problems`,- width: "auto",- maxAutoWidth: 520,- enableSorting: true,- accessorFn: (node) => node.dependents_errors?.length ?? 0,- cell: ({ row }) => {- const node = row.original;- const errors = node.dependents_errors ?? [];- if (errors.length === 0) {- return null;- }- return <ErrorsCell node={node} />;- },- };-}--function getDependentsWithErrorsColumn(): TreeTableColumnDef<DependencyNode> {- return {- id: "dependents-with-errors" satisfies DependencySortColumn,- header: t`Broken dependents`,- width: "auto",- minWidth: 100,- enableSorting: true,- accessorFn: (node) =>- getDependentErrorNodesCount(node.dependents_errors ?? []),- cell: ({ row }) => {- const node = row.original;- return getDependentErrorNodesCount(node.dependents_errors ?? []);- },- };-}--export function getLastEditedByColumn(): TreeTableColumnDef<DependencyNode> {- return {- id: "last-edited-by",- header: t`Last edited by`,- width: "auto",- maxAutoWidth: 520,- accessorFn: (node) => getUserName(getNodeLastEditedBy(node) ?? undefined),- cell: ({ row }) => {- const user = getNodeLastEditedBy(row.original);- if (user == null) {- return null;- }- return <Ellipsified>{getUserName(user)}</Ellipsified>;- },- };-}--export function getLastEditedAtColumn(): TreeTableColumnDef<DependencyNode> {- return {- id: "last-edited-at",- header: t`Last edited at`,- width: "auto",- maxAutoWidth: 520,- accessorFn: (node) => getNodeLastEditedAt(node),- cell: ({ row }) => {- const timestamp = getNodeLastEditedAt(row.original);- if (timestamp == null) {- return null;- }- return <DateTime value={timestamp} unit="minute" />;- },- };-}-export function getColumns(mode: DependencyDiagnosticsMode) {return [getNameColumn(mode === "broken" ? t`Dependency` : t`Name`),diff --git a/enterprise/frontend/src/metabase-enterprise/dependencies/components/DependencyGraph/DependencyGraph.tsx b/enterprise/frontend/src/metabase-enterprise/dependencies/components/DependencyGraph/DependencyGraph.tsxindex 7e17318fb9d..0e42e2c533b 100644--- a/enterprise/frontend/src/metabase-enterprise/dependencies/components/DependencyGraph/DependencyGraph.tsx+++ b/enterprise/frontend/src/metabase-enterprise/dependencies/components/DependencyGraph/DependencyGraph.tsx@@ -1,4 +1,3 @@-import { skipToken } from "@reduxjs/toolkit/query";import {Background,Controls,@@ -8,12 +7,17 @@ import {useEdgesState,useNodesState,} from "@xyflow/react";-import { useEffect, useMemo, useState } from "react";+import {+ type ReactNode,+ useEffect,+ useLayoutEffect,+ useMemo,+ useState,+} from "react";import { t } from "ttag";import { useMetadataToasts } from "metabase/metadata/hooks";import { Group, useColorScheme } from "metabase/ui";-import { useGetDependencyGraphQuery } from "metabase-enterprise/api";import type {DependencyEntry,DependencyGraph,@@ -46,23 +50,23 @@ const PRO_OPTIONS = {};type DependencyGraphProps = {- graph?: DependencyGraph | WorkspaceDependencyGraph | null;+ graph?: DependencyGraph | WorkspaceDependencyGraph;isFetching?: boolean;error?: unknown;- getGraphUrl: (entry?: DependencyEntry) => string;- withEntryPicker?: boolean;- headerRightSide?: React.ReactNode;entry?: DependencyEntry;nodeTypes?: typeof NODE_TYPES;edgeTypes?: typeof EDGE_TYPES;+ headerRightSide?: ReactNode;+ withEntryPicker?: boolean;openLinksInNewTab?: boolean;+ getGraphUrl: (entry?: DependencyEntry) => string;};export function DependencyGraph({entry,- graph: externalGraph,- isFetching: isFetchingExternally = false,- error: externalError,+ graph,+ isFetching = false,+ error,getGraphUrl,withEntryPicker,headerRightSide = null,@@ -70,14 +74,6 @@ export function DependencyGraph({edgeTypes = EDGE_TYPES,openLinksInNewTab = true,}: DependencyGraphProps) {- const shouldFetch = entry != null && !externalGraph;- const dependencyGraph = useGetDependencyGraphQuery(- shouldFetch ? entry : skipToken,- );- const isFetching = isFetchingExternally || dependencyGraph.isFetching;- const graph = externalGraph ?? dependencyGraph.data;- const error = externalError ?? dependencyGraph.error;-const [nodes, setNodes, onNodesChange] = useNodesState<NodeType>([]);const [edges, setEdges, onEdgesChange] = useEdgesState<Edge>([]);const [selection, setSelection] = useState<GraphSelection | null>(null);@@ -92,19 +88,23 @@ export function DependencyGraph({return selection != null ? findNode(nodes, selection) : null;}, [nodes, selection]);- useEffect(() => {- if (entry == null || error != null) {+ useLayoutEffect(() => {+ if (graph == null) {setNodes([]);setEdges([]);- setSelection(null);} else if (graph != null) {const { nodes: initialNodes, edges: initialEdges } =getInitialGraph(graph);setNodes(initialNodes);setEdges(initialEdges);+ }+ }, [graph, setNodes, setEdges]);++ useLayoutEffect(() => {+ if (selection != null && selectedNode == null) {setSelection(null);}- }, [entry, graph, error, setNodes, setEdges]);+ }, [selection, selectedNode, setSelection]);useEffect(() => {if (error != null) {diff --git a/enterprise/frontend/src/metabase-enterprise/dependencies/components/DependencyGraph/GraphInfoPanel/PanelHeader/PanelHeader.tsx b/enterprise/frontend/src/metabase-enterprise/dependencies/components/DependencyGraph/GraphInfoPanel/PanelHeader/PanelHeader.tsxindex 84afe784407..8d84c7764a0 100644--- a/enterprise/frontend/src/metabase-enterprise/dependencies/components/DependencyGraph/GraphInfoPanel/PanelHeader/PanelHeader.tsx+++ b/enterprise/frontend/src/metabase-enterprise/dependencies/components/DependencyGraph/GraphInfoPanel/PanelHeader/PanelHeader.tsx@@ -1,6 +1,9 @@+import { useDisclosure } from "@mantine/hooks";import { t } from "ttag";import CS from "metabase/css/core/index.css";+import { useSelector } from "metabase/lib/redux";+import { PLUGIN_REPLACEMENT } from "metabase/plugins";import {ActionIcon,Center,@@ -8,10 +11,12 @@ import {Group,Stack,Title,+ Tooltip,} from "metabase/ui";import type { DependencyNode } from "metabase-types/api";import {+ getNodeDataSourceEntry,getNodeIcon,getNodeLabel,getNodeLink,@@ -30,26 +35,53 @@ type PanelHeaderProps = {export function PanelHeader({ node, onClose }: PanelHeaderProps) {const link = getNodeLink(node);const location = getNodeLocationInfo(node);+ const sourceEntry = getNodeDataSourceEntry(node);+ const canUserReplaceSources = useSelector(+ PLUGIN_REPLACEMENT.canUserReplaceSources,+ );+ const [+ isReplaceModalOpened,+ { open: openReplaceModal, close: closeReplaceModal },+ ] = useDisclosure();return (- <Group className={S.root} p="lg" gap="0.75rem" wrap="nowrap">- <Center w="2.75rem" h="2.75rem" bdrs="50%" bg="background-secondary">- <FixedSizeIcon name={getNodeIcon(node)} c="brand" size={20} />- </Center>- <Stack gap="xs" flex={1}>- <Title className={CS.textWrap} order={3} lh="1.5rem">- {getNodeLabel(node)}- </Title>- {location != null && <GraphBreadcrumbs links={location.links} />}- </Stack>- <Group m="-sm" gap="xs" wrap="nowrap">- {link != null && (- <GraphExternalLink label={link.label} url={link.url} />- )}- <ActionIcon aria-label={t`Close`} onClick={onClose}>- <FixedSizeIcon name="close" />- </ActionIcon>+ <>+ <Group className={S.root} p="lg" gap="0.75rem" wrap="nowrap">+ <Center w="2.75rem" h="2.75rem" bdrs="50%" bg="background-secondary">+ <FixedSizeIcon name={getNodeIcon(node)} c="brand" size={20} />+ </Center>+ <Stack gap="xs" flex={1}>+ <Title className={CS.textWrap} order={3} lh="1.5rem">+ {getNodeLabel(node)}+ </Title>+ {location != null && <GraphBreadcrumbs links={location.links} />}+ </Stack>+ <Group m="-sm" gap="xs" wrap="nowrap">+ {link != null && (+ <GraphExternalLink label={link.label} url={link.url} />+ )}+ {sourceEntry != null && canUserReplaceSources && (+ <Tooltip label={t`Find and replace`}>+ <ActionIcon+ aria-label={t`Replace data source`}+ onClick={openReplaceModal}+ >+ <FixedSizeIcon name="find_replace" />+ </ActionIcon>+ </Tooltip>+ )}+ <ActionIcon aria-label={t`Close`} onClick={onClose}>+ <FixedSizeIcon name="close" />+ </ActionIcon>+ </Group></Group>- </Group>+ {sourceEntry != null && (+ <PLUGIN_REPLACEMENT.ReplaceDataSourceModal+ isOpened={isReplaceModalOpened}+ initialSource={sourceEntry}+ onClose={closeReplaceModal}+ />+ )}+ </>);}diff --git a/enterprise/frontend/src/metabase-enterprise/dependencies/components/DependencyDiagnostics/DiagnosticsTable/ErrorsCell/ErrorsCell.tsx b/enterprise/frontend/src/metabase-enterprise/dependencies/components/DependencyTable/ErrorsCell/ErrorsCell.tsxsimilarity index 97%rename from enterprise/frontend/src/metabase-enterprise/dependencies/components/DependencyDiagnostics/DiagnosticsTable/ErrorsCell/ErrorsCell.tsxrename to enterprise/frontend/src/metabase-enterprise/dependencies/components/DependencyTable/ErrorsCell/ErrorsCell.tsxindex 2e3f8460f4d..85fd6ba6022 100644--- a/enterprise/frontend/src/metabase-enterprise/dependencies/components/DependencyDiagnostics/DiagnosticsTable/ErrorsCell/ErrorsCell.tsx+++ b/enterprise/frontend/src/metabase-enterprise/dependencies/components/DependencyTable/ErrorsCell/ErrorsCell.tsx@@ -1,7 +1,7 @@import { Ellipsified } from "metabase/common/components/Ellipsified";import type { DependencyNode } from "metabase-types/api";-import { getDependencyErrorInfo, getDependencyErrors } from "../../../../utils";+import { getDependencyErrorInfo, getDependencyErrors } from "../../../utils";type ErrorsCellProps = {node: DependencyNode;diff --git a/enterprise/frontend/src/metabase-enterprise/dependencies/components/DependencyDiagnostics/DiagnosticsTable/ErrorsCell/index.ts b/enterprise/frontend/src/metabase-enterprise/dependencies/components/DependencyTable/ErrorsCell/index.tssimilarity index 100%rename from enterprise/frontend/src/metabase-enterprise/dependencies/components/DependencyDiagnostics/DiagnosticsTable/ErrorsCell/index.tsrename to enterprise/frontend/src/metabase-enterprise/dependencies/components/DependencyTable/ErrorsCell/index.tsdiff --git a/enterprise/frontend/src/metabase-enterprise/dependencies/components/DependencyDiagnostics/DiagnosticsTable/LocationCell/LocationCell.tsx b/enterprise/frontend/src/metabase-enterprise/dependencies/components/DependencyTable/LocationCell/LocationCell.tsxsimilarity index 92%rename from enterprise/frontend/src/metabase-enterprise/dependencies/components/DependencyDiagnostics/DiagnosticsTable/LocationCell/LocationCell.tsxrename to enterprise/frontend/src/metabase-enterprise/dependencies/components/DependencyTable/LocationCell/LocationCell.tsxindex c631bad6372..4972349c0ba 100644--- a/enterprise/frontend/src/metabase-enterprise/dependencies/components/DependencyDiagnostics/DiagnosticsTable/LocationCell/LocationCell.tsx+++ b/enterprise/frontend/src/metabase-enterprise/dependencies/components/DependencyTable/LocationCell/LocationCell.tsx@@ -2,7 +2,7 @@ import { Ellipsified } from "metabase/common/components/Ellipsified";import { FixedSizeIcon, Group } from "metabase/ui";import type { DependencyNode } from "metabase-types/api";-import { getNodeLocationInfo } from "../../../../utils";+import { getNodeLocationInfo } from "../../../utils";type LocationCellProps = {node: DependencyNode;diff --git a/enterprise/frontend/src/metabase-enterprise/dependencies/components/DependencyDiagnostics/DiagnosticsTable/LocationCell/index.ts b/enterprise/frontend/src/metabase-enterprise/dependencies/components/DependencyTable/LocationCell/index.tssimilarity index 100%rename from enterprise/frontend/src/metabase-enterprise/dependencies/components/DependencyDiagnostics/DiagnosticsTable/LocationCell/index.tsrename to enterprise/frontend/src/metabase-enterprise/dependencies/components/DependencyTable/LocationCell/index.tsdiff --git a/enterprise/frontend/src/metabase-enterprise/dependencies/components/DependencyDiagnostics/DiagnosticsTable/NameCell/NameCell.tsx b/enterprise/frontend/src/metabase-enterprise/dependencies/components/DependencyTable/NameCell/NameCell.tsxsimilarity index 89%rename from enterprise/frontend/src/metabase-enterprise/dependencies/components/DependencyDiagnostics/DiagnosticsTable/NameCell/NameCell.tsxrename to enterprise/frontend/src/metabase-enterprise/dependencies/components/DependencyTable/NameCell/NameCell.tsxindex 4e07f0ba112..c0c7d7516fb 100644--- a/enterprise/frontend/src/metabase-enterprise/dependencies/components/DependencyDiagnostics/DiagnosticsTable/NameCell/NameCell.tsx+++ b/enterprise/frontend/src/metabase-enterprise/dependencies/components/DependencyTable/NameCell/NameCell.tsx@@ -2,7 +2,7 @@ import { Ellipsified } from "metabase/common/components/Ellipsified";import { FixedSizeIcon, Group } from "metabase/ui";import type { DependencyNode } from "metabase-types/api";-import { getNodeIcon, getNodeLabel } from "../../../../utils";+import { getNodeIcon, getNodeLabel } from "../../../utils";type NameCellProps = {node: DependencyNode;diff --git a/enterprise/frontend/src/metabase-enterprise/dependencies/components/DependencyDiagnostics/DiagnosticsTable/NameCell/index.ts b/enterprise/frontend/src/metabase-enterprise/dependencies/components/DependencyTable/NameCell/index.tssimilarity index 100%rename from enterprise/frontend/src/metabase-enterprise/dependencies/components/DependencyDiagnostics/DiagnosticsTable/NameCell/index.tsrename to enterprise/frontend/src/metabase-enterprise/dependencies/components/DependencyTable/NameCell/index.tsdiff --git a/enterprise/frontend/src/metabase-enterprise/dependencies/components/DependencyTable/index.ts b/enterprise/frontend/src/metabase-enterprise/dependencies/components/DependencyTable/index.tsnew file mode 100644index 00000000000..178cd64f81d--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/dependencies/components/DependencyTable/index.ts@@ -0,0 +1 @@+export * from "./utils";diff --git a/enterprise/frontend/src/metabase-enterprise/dependencies/components/DependencyTable/utils.tsx b/enterprise/frontend/src/metabase-enterprise/dependencies/components/DependencyTable/utils.tsxnew file mode 100644index 00000000000..106f92b9094--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/dependencies/components/DependencyTable/utils.tsx@@ -0,0 +1,88 @@+import { t } from "ttag";++import type { TreeTableColumnDef } from "metabase/ui";+import type { DependencyNode, DependencySortColumn } from "metabase-types/api";++import {+ getDependentErrorNodesCount,+ getNodeLabel,+ getNodeLocationInfo,+} from "../../utils";++import { ErrorsCell } from "./ErrorsCell";+import { LocationCell } from "./LocationCell";+import { NameCell } from "./NameCell";++export function getNameColumn(+ header: string = t`Name`,+): TreeTableColumnDef<DependencyNode> {+ return {+ id: "name" satisfies DependencySortColumn,+ header,+ width: "auto",+ minWidth: 120,+ maxAutoWidth: 520,+ enableSorting: true,+ accessorFn: (node) => getNodeLabel(node),+ cell: ({ row }) => {+ const node = row.original;+ return <NameCell node={node} />;+ },+ };+}++export function getLocationColumn(): TreeTableColumnDef<DependencyNode> {+ return {+ id: "location" satisfies DependencySortColumn,+ header: t`Location`,+ width: "auto",+ minWidth: 100,+ maxAutoWidth: 520,+ enableSorting: true,+ accessorFn: (node) => {+ const location = getNodeLocationInfo(node);+ const links = location?.links ?? [];+ return links.map((link) => link.label).join(", ");+ },+ cell: ({ row }) => {+ const node = row.original;+ return <LocationCell node={node} />;+ },+ };+}++export function getDependentsErrorsColumn(): TreeTableColumnDef<DependencyNode> {+ return {+ id: "dependents-errors" satisfies DependencySortColumn,+ header: t`Problems`,+ width: "auto",+ minWidth: 100,+ maxAutoWidth: 520,+ enableSorting: true,+ accessorFn: (node) => node.dependents_errors?.length ?? 0,+ cell: ({ row }) => {+ const node = row.original;+ const errors = node.dependents_errors ?? [];+ if (errors.length === 0) {+ return null;+ }+ return <ErrorsCell node={node} />;+ },+ };+}++export function getDependentsWithErrorsColumn(): TreeTableColumnDef<DependencyNode> {+ return {+ id: "dependents-with-errors" satisfies DependencySortColumn,+ header: t`Broken dependents`,+ width: "auto",+ minWidth: 100,+ enableSorting: true,+ accessorFn: (node) =>+ getDependentErrorNodesCount(node.dependents_errors ?? []),+ cell: ({ row }) => {+ const node = row.original;+ return getDependentErrorNodesCount(node.dependents_errors ?? []);+ },+ };+}diff --git a/enterprise/frontend/src/metabase-enterprise/dependencies/utils.ts b/enterprise/frontend/src/metabase-enterprise/dependencies/utils.tsindex 716ee4177f1..f267f9e4346 100644--- a/enterprise/frontend/src/metabase-enterprise/dependencies/utils.ts+++ b/enterprise/frontend/src/metabase-enterprise/dependencies/utils.ts@@ -14,6 +14,7 @@ import type {DependencyNode,DependencyType,Field,+ ReplaceSourceEntry,Transform,VisualizationDisplay,} from "metabase-types/api";@@ -488,6 +489,22 @@ export function getNodeFieldsLabelWithCount(fieldCount: number) {);}+export function getNodeDataSourceEntry(+ node: DependencyNode,+): ReplaceSourceEntry | null {+ switch (node.type) {+ case "table":+ return { id: node.id, type: node.type };+ case "card":+ if (node.data.type === "question" || node.data.type === "model") {+ return { id: node.id, type: node.type };+ }+ return null;+ default:+ return null;+ }+}+export function getCardType(groupType: DependencyGroupType): CardType | null {switch (groupType) {case "question":diff --git a/enterprise/frontend/src/metabase-enterprise/dependencies/utils.unit.spec.ts b/enterprise/frontend/src/metabase-enterprise/dependencies/utils.unit.spec.tsindex 88293a6ce3a..4965308ca4d 100644--- a/enterprise/frontend/src/metabase-enterprise/dependencies/utils.unit.spec.ts+++ b/enterprise/frontend/src/metabase-enterprise/dependencies/utils.unit.spec.ts@@ -57,6 +57,7 @@ import {getErrorTypeLabelWithCount,getNodeCreatedAt,getNodeCreatedBy,+ getNodeDataSourceEntry,getNodeDescription,getNodeIcon,getNodeId,@@ -898,3 +899,63 @@ describe("getDependentErrorNodesLabel", () => {expect(getDependentErrorNodesLabel()).toBe("Broken dependents");});});++describe("getNodeDataSourceEntry", () => {+ it.each<{+ description: string;+ node: DependencyNode;+ expected: { id: number; type: string } | null;+ }>([+ {+ description: "table node",+ node: createMockTableDependencyNode({ id: 1 }),+ expected: { id: 1, type: "table" },+ },+ {+ description: "question card node",+ node: createMockCardDependencyNode({+ id: 2,+ data: createMockCardDependencyNodeData({ type: "question" }),+ }),+ expected: { id: 2, type: "card" },+ },+ {+ description: "model card node",+ node: createMockCardDependencyNode({+ id: 3,+ data: createMockCardDependencyNodeData({ type: "model" }),+ }),+ expected: { id: 3, type: "card" },+ },+ {+ description: "metric card node",+ node: createMockCardDependencyNode({+ id: 4,+ data: createMockCardDependencyNodeData({ type: "metric" }),+ }),+ expected: null,+ },+ {+ description: "dashboard node",+ node: createMockDashboardDependencyNode({ id: 5 }),+ expected: null,+ },+ {+ description: "transform node",+ node: createMockTransformDependencyNode({ id: 6 }),+ expected: null,+ },+ {+ description: "snippet node",+ node: createMockSnippetDependencyNode({ id: 7 }),+ expected: null,+ },+ {+ description: "sandbox node",+ node: createMockSandboxDependencyNode({ id: 8 }),+ expected: null,+ },+ ])("should return $expected for $description", ({ node, expected }) => {+ expect(getNodeDataSourceEntry(node)).toEqual(expected);+ });+});diff --git a/enterprise/frontend/src/metabase-enterprise/plugins.ts b/enterprise/frontend/src/metabase-enterprise/plugins.tsindex 7bef4d9779c..2ce19a65241 100644--- a/enterprise/frontend/src/metabase-enterprise/plugins.ts+++ b/enterprise/frontend/src/metabase-enterprise/plugins.ts@@ -35,6 +35,7 @@ import { initializePlugin as initializeMetabot } from "./metabot";import { initializePlugin as initializeModelPersistence } from "./model_persistence";import { initializePlugin as initializeModeration } from "./moderation";import { initializePlugin as initializeRemoteSync } from "./remote_sync";+import { initializePlugin as initializeReplacement } from "./replacement";import { initializePlugin as initializeResourceDownloads } from "./resource_downloads";import { initializePlugin as initializeSandboxes } from "./sandboxes";import { initializePlugin as initializeSemanticSearch } from "./semantic_search";@@ -66,6 +67,7 @@ export function initializePlugins() {initializeContentVerification();initializeDatabaseRouting();initializeRemoteSync();+ initializeReplacement();initializeWhitelabel();initializeEmbedding();initializeEmbeddingSdk();diff --git a/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/ConfirmAndProgressModal/ConfirmAndProgressModal.tsx b/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/ConfirmAndProgressModal/ConfirmAndProgressModal.tsxnew file mode 100644index 00000000000..d0a26766b5c--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/ConfirmAndProgressModal/ConfirmAndProgressModal.tsx@@ -0,0 +1,80 @@+import { useRef, useState } from "react";+import { msgid, ngettext, t } from "ttag";++import { Modal } from "metabase/ui";+import type {+ ReplaceSourceEntry,+ ReplaceSourceRunId,+} from "metabase-types/api";++import { ConfirmModalContent } from "./ConfirmModalContent";+import { ProgressModalContent } from "./ProgressModalContent";++type ConfirmAndProgressModalProps = {+ sourceEntry: ReplaceSourceEntry;+ targetEntry: ReplaceSourceEntry;+ dependentsCount: number;+ opened: boolean;+ onReplaceSuccess: (originalDependentsCount: number) => void;+ onReplaceFailure: () => void;+ onClose: () => void;+};++export function ConfirmAndProgressModal({+ sourceEntry,+ targetEntry,+ dependentsCount: initialDependentsCount,+ opened,+ onReplaceSuccess,+ onReplaceFailure,+ onClose,+}: ConfirmAndProgressModalProps) {+ const [runId, setRunId] = useState<ReplaceSourceRunId>();+ const { current: dependentsCount } = useRef(initialDependentsCount);+ const isStarted = runId != null;++ const handleReplaceSuccess = () => {+ setRunId(undefined);+ onReplaceSuccess(dependentsCount);+ };++ const handleReplaceFailure = () => {+ setRunId(undefined);+ onReplaceFailure();+ };++ return (+ <Modal+ title={getTitle(dependentsCount, isStarted)}+ opened={opened}+ onClose={onClose}+ >+ {runId == null ? (+ <ConfirmModalContent+ sourceEntry={sourceEntry}+ targetEntry={targetEntry}+ dependentsCount={dependentsCount}+ onSubmit={setRunId}+ onCancel={onClose}+ />+ ) : (+ <ProgressModalContent+ runId={runId}+ onReplaceSuccess={handleReplaceSuccess}+ onReplaceFailure={handleReplaceFailure}+ />+ )}+ </Modal>+ );+}++function getTitle(dependentsCount: number, isStarted: boolean) {+ if (isStarted) {+ return t`Replacing data sources…`;+ }+ return ngettext(+ msgid`Really replace the data source in this ${dependentsCount} item?`,+ `Really replace the data sources in these ${dependentsCount} items?`,+ dependentsCount,+ );+}diff --git a/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/ConfirmAndProgressModal/ConfirmModalContent/ConfirmModalContent.tsx b/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/ConfirmAndProgressModal/ConfirmModalContent/ConfirmModalContent.tsxnew file mode 100644index 00000000000..624e1a8cc3b--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/ConfirmAndProgressModal/ConfirmModalContent/ConfirmModalContent.tsx@@ -0,0 +1,64 @@+import { msgid, ngettext, t } from "ttag";++import { Form, FormErrorMessage, FormProvider } from "metabase/forms";+import { Box, Button, Group, Stack, Text } from "metabase/ui";+import { useReplaceSourceMutation } from "metabase-enterprise/api/replacement";+import type {+ ReplaceSourceEntry,+ ReplaceSourceRunId,+} from "metabase-types/api";++type ConfirmModalContentProps = {+ sourceEntry: ReplaceSourceEntry;+ targetEntry: ReplaceSourceEntry;+ dependentsCount: number;+ onSubmit: (runId: ReplaceSourceRunId) => void;+ onCancel: () => void;+};++export function ConfirmModalContent({+ sourceEntry,+ targetEntry,+ dependentsCount,+ onSubmit,+ onCancel,+}: ConfirmModalContentProps) {+ const [replaceSource] = useReplaceSourceMutation();++ const handleSubmit = async () => {+ const response = await replaceSource({+ source_entity_id: sourceEntry.id,+ source_entity_type: sourceEntry.type,+ target_entity_id: targetEntry.id,+ target_entity_type: targetEntry.type,+ }).unwrap();+ onSubmit(response.run_id);+ };++ return (+ <FormProvider initialValues={{}} onSubmit={handleSubmit}>+ <Form>+ <Stack>+ <Text>{t`This can't be undone.`}</Text>+ <Group>+ <Box flex={1}>+ <FormErrorMessage />+ </Box>+ <Button onClick={onCancel}>{t`Go back`}</Button>+ <Button type="submit" variant="filled" color="error">+ {getSubmitLabel(dependentsCount)}+ </Button>+ </Group>+ </Stack>+ </Form>+ </FormProvider>+ );+}++function getSubmitLabel(dependentsCount: number): string {+ return ngettext(+ msgid`Replace data source in ${dependentsCount} item`,+ `Replace data source in ${dependentsCount} items`,+ dependentsCount,+ );+}diff --git a/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/ConfirmAndProgressModal/ConfirmModalContent/index.ts b/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/ConfirmAndProgressModal/ConfirmModalContent/index.tsnew file mode 100644index 00000000000..d1436a46c71--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/ConfirmAndProgressModal/ConfirmModalContent/index.ts@@ -0,0 +1 @@+export * from "./ConfirmModalContent";diff --git a/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/ConfirmAndProgressModal/ProgressModalContent/ProgressModalContent.tsx b/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/ConfirmAndProgressModal/ProgressModalContent/ProgressModalContent.tsxnew file mode 100644index 00000000000..8e403f6600d--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/ConfirmAndProgressModal/ProgressModalContent/ProgressModalContent.tsx@@ -0,0 +1,92 @@+import { useForceUpdate } from "@mantine/hooks";+import { useEffect, useRef } from "react";+import { msgid, ngettext } from "ttag";++import { Api } from "metabase/api";+import { useDispatch } from "metabase/lib/redux";+import { Progress, Stack, Text } from "metabase/ui";+import { useGetReplaceSourceRunQuery } from "metabase-enterprise/api/replacement";+import type { ReplaceSourceRun, ReplaceSourceRunId } from "metabase-types/api";++import { INVALIDATE_TAGS } from "../../constants";++const POLLING_INTERVAL = 1000;++type ProgressModalContentProps = {+ runId: ReplaceSourceRunId;+ onReplaceSuccess: () => void;+ onReplaceFailure: () => void;+};++export function ProgressModalContent({+ runId,+ onReplaceSuccess,+ onReplaceFailure,+}: ProgressModalContentProps) {+ const { data: run } = useGetReplaceSourceRunQuery(runId, {+ pollingInterval: POLLING_INTERVAL,+ });+ const elapsedSeconds = useElapsedSeconds();+ const dispatch = useDispatch();++ useEffect(() => {+ if (run != null && run.status !== "started") {+ dispatch(Api.util.invalidateTags(INVALIDATE_TAGS));+ if (run.status === "succeeded") {+ onReplaceSuccess();+ } else {+ onReplaceFailure();+ }+ }+ }, [run, dispatch, onReplaceSuccess, onReplaceFailure]);++ return (+ <Stack gap="sm">+ <Text c="text-secondary">{getProgressLabel(elapsedSeconds)}</Text>+ <Progress value={getProgressValue(run)} />+ </Stack>+ );+}++function getProgressValue(run: ReplaceSourceRun | undefined): number {+ if (run == null || run.progress == null) {+ return 0;+ }+ return run.progress * 100;+}++function getProgressLabel(elapsedSeconds: number): string {+ return ngettext(+ msgid`It's been ${elapsedSeconds} second so far`,+ `It's been ${elapsedSeconds} seconds so far`,+ elapsedSeconds,+ );+}++function useElapsedSeconds(): number {+ const startTimeRef = useRef(new Date());+ const elapsedSecondsRef = useRef(0);+ const update = useForceUpdate();++ useEffect(() => {+ let handle: number;+ function tick() {+ const startTime = startTimeRef.current;+ const currentTime = new Date();+ const elapsedSeconds = getElapsedSeconds(startTime, currentTime);+ if (elapsedSecondsRef.current !== elapsedSeconds) {+ elapsedSecondsRef.current = elapsedSeconds;+ update();+ }+ handle = requestAnimationFrame(tick);+ }+ handle = requestAnimationFrame(tick);+ return () => cancelAnimationFrame(handle);+ }, [update]);++ return elapsedSecondsRef.current;+}++function getElapsedSeconds(startTime: Date, currentTime: Date): number {+ return Math.floor((currentTime.getTime() - startTime.getTime()) / 1000);+}diff --git a/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/ConfirmAndProgressModal/ProgressModalContent/index.ts b/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/ConfirmAndProgressModal/ProgressModalContent/index.tsnew file mode 100644index 00000000000..19707038b9d--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/ConfirmAndProgressModal/ProgressModalContent/index.ts@@ -0,0 +1 @@+export * from "./ProgressModalContent";diff --git a/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/ConfirmAndProgressModal/index.ts b/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/ConfirmAndProgressModal/index.tsnew file mode 100644index 00000000000..18302131c5a--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/ConfirmAndProgressModal/index.ts@@ -0,0 +1 @@+export * from "./ConfirmAndProgressModal";diff --git a/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/ModalBody/ColumnMappingTable/ColumnMappingTable.tsx b/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/ModalBody/ColumnMappingTable/ColumnMappingTable.tsxnew file mode 100644index 00000000000..be7b50ff32e--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/ModalBody/ColumnMappingTable/ColumnMappingTable.tsx@@ -0,0 +1,41 @@+import { useMemo } from "react";++import { Card, TreeTable, useTreeTableInstance } from "metabase/ui";+import type { ReplaceSourceColumnMapping } from "metabase-types/api";++import type { EntityItem } from "../../types";++import type { ColumnMappingItem } from "./types";+import { getColumns, getRows } from "./utils";++type ColumnComparisonTableProps = {+ sourceItem: EntityItem | undefined;+ targetItem: EntityItem | undefined;+ columnMappings: ReplaceSourceColumnMapping[];+};++export function ColumnComparisonTable({+ sourceItem,+ targetItem,+ columnMappings,+}: ColumnComparisonTableProps) {+ const rows = useMemo(() => {+ return getRows(columnMappings);+ }, [columnMappings]);++ const columns = useMemo(() => {+ return getColumns(sourceItem, targetItem, columnMappings);+ }, [sourceItem, targetItem, columnMappings]);++ const treeTableInstance = useTreeTableInstance<ColumnMappingItem>({+ data: rows,+ columns,+ getNodeId: (item) => item.id.toString(),+ });++ return (+ <Card flex="0 1 auto" miw={0} mih={0} p={0} shadow="none" withBorder>+ <TreeTable instance={treeTableInstance} />+ </Card>+ );+}diff --git a/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/ModalBody/ColumnMappingTable/NameCell/NameCell.tsx b/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/ModalBody/ColumnMappingTable/NameCell/NameCell.tsxnew file mode 100644index 00000000000..64587301e4a--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/ModalBody/ColumnMappingTable/NameCell/NameCell.tsx@@ -0,0 +1,24 @@+import { useMemo } from "react";++import { Ellipsified } from "metabase/common/components/Ellipsified";+import { getColumnIcon } from "metabase/common/utils/columns";+import { FixedSizeIcon, Group } from "metabase/ui";+import * as Lib from "metabase-lib";+import type { ReplaceSourceColumnInfo } from "metabase-types/api";++type NameCellProps = {+ column: ReplaceSourceColumnInfo;+};++export function NameCell({ column }: NameCellProps) {+ const icon = useMemo(() => {+ return getColumnIcon(Lib.legacyColumnTypeInfo(column));+ }, [column]);++ return (+ <Group gap="sm" wrap="nowrap">+ <FixedSizeIcon name={icon} />+ <Ellipsified>{column.name}</Ellipsified>+ </Group>+ );+}diff --git a/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/ModalBody/ColumnMappingTable/NameCell/index.ts b/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/ModalBody/ColumnMappingTable/NameCell/index.tsnew file mode 100644index 00000000000..a3df8b11b65--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/ModalBody/ColumnMappingTable/NameCell/index.ts@@ -0,0 +1 @@+export * from "./NameCell";diff --git a/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/ModalBody/ColumnMappingTable/index.ts b/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/ModalBody/ColumnMappingTable/index.tsnew file mode 100644index 00000000000..34a8f8e81dd--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/ModalBody/ColumnMappingTable/index.ts@@ -0,0 +1 @@+export * from "./ColumnMappingTable";diff --git a/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/ModalBody/ColumnMappingTable/types.ts b/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/ModalBody/ColumnMappingTable/types.tsnew file mode 100644index 00000000000..864964f43bf--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/ModalBody/ColumnMappingTable/types.ts@@ -0,0 +1,11 @@+import type {+ ReplaceSourceColumnErrorType,+ ReplaceSourceColumnInfo,+} from "metabase-types/api";++export type ColumnMappingItem = {+ id: number;+ source?: ReplaceSourceColumnInfo;+ target?: ReplaceSourceColumnInfo;+ errors?: ReplaceSourceColumnErrorType[];+};diff --git a/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/ModalBody/ColumnMappingTable/utils.tsx b/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/ModalBody/ColumnMappingTable/utils.tsxnew file mode 100644index 00000000000..00dc77a7a59--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/ModalBody/ColumnMappingTable/utils.tsx@@ -0,0 +1,140 @@+import { t } from "ttag";++import { Ellipsified } from "metabase/common/components/Ellipsified";+import type { TreeTableColumnDef } from "metabase/ui";+import type {+ ReplaceSourceColumnErrorType,+ ReplaceSourceColumnMapping,+} from "metabase-types/api";++import { getColumnErrorMessage } from "../../../../utils";+import type { EntityItem } from "../../types";++import { NameCell } from "./NameCell";+import type { ColumnMappingItem } from "./types";++function getEntityName(entityInfo: EntityItem | undefined): string {+ if (entityInfo?.type === "table") {+ return entityInfo.data?.display_name ?? "";+ } else if (entityInfo?.type === "card") {+ return entityInfo.data?.name ?? "";+ }+ return "";+}++function getEntityHeader(entityName: string, columnCount: number): string {+ return `${entityName} (${columnCount})`;+}++function getSourceColumnCount(+ columnMappings: ReplaceSourceColumnMapping[],+): number {+ return columnMappings.filter(({ source }) => source != null).length;+}++function getTargetColumnCount(+ columnMappings: ReplaceSourceColumnMapping[],+): number {+ return columnMappings.filter(({ target }) => target != null).length;+}++function getSourceColumn(+ header: string,+): TreeTableColumnDef<ColumnMappingItem> {+ return {+ id: "source",+ header,+ width: "auto",+ minWidth: 100,+ maxAutoWidth: 520,+ enableSorting: true,+ accessorFn: (item) => item.source?.display_name,+ cell: ({ row }) => {+ const item = row.original;+ if (item.source == null) {+ return null;+ }+ return <NameCell column={item.source} />;+ },+ };+}++function getTargetColumn(+ header: string,+): TreeTableColumnDef<ColumnMappingItem> {+ return {+ id: "target",+ header,+ width: "auto",+ minWidth: 100,+ maxAutoWidth: 520,+ enableSorting: true,+ accessorFn: (item) => item.target?.display_name,+ cell: ({ row }) => {+ const item = row.original;+ if (item.target == null) {+ return null;+ }+ return <NameCell column={item.target} />;+ },+ };+}++function getErrorMessage(errors: ReplaceSourceColumnErrorType[]) {+ const messages = errors+ .map(getColumnErrorMessage)+ .filter((message) => message != null);+ return messages.length > 0 ? messages.join(" ") : null;+}++function getDetailsColumn(): TreeTableColumnDef<ColumnMappingItem> {+ return {+ id: "errors",+ header: t`Details`,+ width: "auto",+ minWidth: 100,+ maxAutoWidth: 520,+ enableSorting: true,+ accessorFn: (item) => getErrorMessage(item.errors ?? []),+ cell: ({ row }) => {+ const { errors } = row.original;+ const message = getErrorMessage(errors ?? []);+ if (message == null) {+ return null;+ }+ return <Ellipsified>{message}</Ellipsified>;+ },+ };+}++export function getColumns(+ sourceItem: EntityItem | undefined,+ targetItem: EntityItem | undefined,+ columnMappings: ReplaceSourceColumnMapping[],+): TreeTableColumnDef<ColumnMappingItem>[] {+ const sourceHeader = getEntityHeader(+ getEntityName(sourceItem),+ getSourceColumnCount(columnMappings),+ );+ const targetHeader = getEntityHeader(+ getEntityName(targetItem),+ getTargetColumnCount(columnMappings),+ );++ return [+ getSourceColumn(sourceHeader),+ getTargetColumn(targetHeader),+ getDetailsColumn(),+ ];+}++export function getRows(+ columnMappings: ReplaceSourceColumnMapping[],+): ColumnMappingItem[] {+ return columnMappings.map((columnMapping, index) => ({+ id: index,+ source: columnMapping.source,+ target: columnMapping.target,+ errors: columnMapping.errors,+ }));+}diff --git a/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/ModalBody/DependentsTable/DependentsTable.tsx b/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/ModalBody/DependentsTable/DependentsTable.tsxnew file mode 100644index 00000000000..4fa94decd17--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/ModalBody/DependentsTable/DependentsTable.tsx@@ -0,0 +1,27 @@+import { useMemo } from "react";++import { Card, TreeTable, useTreeTableInstance } from "metabase/ui";+import { getNodeId } from "metabase-enterprise/dependencies/utils";+import type { DependencyNode } from "metabase-types/api";++import { getColumns } from "./utils";++type DependentsTableProps = {+ dependents: DependencyNode[];+};++export function DependentsTable({ dependents }: DependentsTableProps) {+ const columns = useMemo(() => getColumns(), []);++ const treeTableInstance = useTreeTableInstance<DependencyNode>({+ data: dependents,+ columns,+ getNodeId: (node) => getNodeId(node.id, node.type),+ });++ return (+ <Card flex="0 1 auto" miw={0} mih={0} p={0} shadow="none" withBorder>+ <TreeTable instance={treeTableInstance} />+ </Card>+ );+}diff --git a/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/ModalBody/DependentsTable/index.ts b/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/ModalBody/DependentsTable/index.tsnew file mode 100644index 00000000000..5f1ddaca26b--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/ModalBody/DependentsTable/index.ts@@ -0,0 +1 @@+export * from "./DependentsTable";diff --git a/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/ModalBody/DependentsTable/utils.tsx b/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/ModalBody/DependentsTable/utils.tsxnew file mode 100644index 00000000000..f18dd9760c6--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/ModalBody/DependentsTable/utils.tsx@@ -0,0 +1,10 @@+import type { TreeTableColumnDef } from "metabase/ui";+import {+ getLocationColumn,+ getNameColumn,+} from "metabase-enterprise/dependencies/components/DependencyTable";+import type { DependencyNode } from "metabase-types/api";++export function getColumns(): TreeTableColumnDef<DependencyNode>[] {+ return [getNameColumn(), getLocationColumn()];+}diff --git a/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/ModalBody/EmptyState/EmptyState.tsx b/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/ModalBody/EmptyState/EmptyState.tsxnew file mode 100644index 00000000000..be5ed047563--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/ModalBody/EmptyState/EmptyState.tsx@@ -0,0 +1,16 @@+import { t } from "ttag";++import { Center, FixedSizeIcon, Stack, Text } from "metabase/ui";++export function EmptyState() {+ return (+ <Center flex={1}>+ <Stack align="center" gap="xl">+ <Center w="5rem" h="5rem" bg="background-disabled" bdrs="50%">+ <FixedSizeIcon name="list" c="icon-disabled" />+ </Center>+ <Text ta="center">{t`The items that will be affected will show up here.`}</Text>+ </Stack>+ </Center>+ );+}diff --git a/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/ModalBody/EmptyState/index.ts b/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/ModalBody/EmptyState/index.tsnew file mode 100644index 00000000000..986154cb77c--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/ModalBody/EmptyState/index.ts@@ -0,0 +1 @@+export * from "./EmptyState";diff --git a/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/ModalBody/ModalBody.tsx b/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/ModalBody/ModalBody.tsxnew file mode 100644index 00000000000..fda5d706d35--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/ModalBody/ModalBody.tsx@@ -0,0 +1,63 @@+import { Flex } from "metabase/ui";+import type {+ DependencyNode,+ ReplaceSourceColumnMapping,+} from "metabase-types/api";++import type { EntityItem, TabType } from "../types";++import { ColumnComparisonTable } from "./ColumnMappingTable";+import { DependentsTable } from "./DependentsTable";+import { EmptyState } from "./EmptyState";+import { TabPanel } from "./TabPanel";++type ModalBodyProps = {+ sourceItem: EntityItem | undefined;+ targetItem: EntityItem | undefined;+ selectedTab: TabType;+ canReplace: boolean;+ dependents: DependencyNode[] | undefined;+ columnMappings: ReplaceSourceColumnMapping[] | undefined;+ onTabChange: (tab: TabType) => void;+};++export function ModalBody({+ sourceItem,+ targetItem,+ selectedTab,+ canReplace,+ dependents = [],+ columnMappings = [],+ onTabChange,+}: ModalBodyProps) {+ if (columnMappings.length === 0) {+ return (+ <Flex flex={1} direction="column" p="lg" bg="background-secondary">+ <EmptyState />+ </Flex>+ );+ }++ return (+ <Flex flex={1} direction="column" miw={0} mih={0} bg="background-secondary">+ <TabPanel+ selectedTab={selectedTab}+ canReplace={canReplace}+ dependentsCount={dependents.length}+ onTabChange={onTabChange}+ />+ <Flex flex={1} direction="column" p="lg" miw={0} mih={0}>+ {selectedTab === "column-mappings" && (+ <ColumnComparisonTable+ sourceItem={sourceItem}+ targetItem={targetItem}+ columnMappings={columnMappings}+ />+ )}+ {selectedTab === "dependents" && (+ <DependentsTable dependents={dependents} />+ )}+ </Flex>+ </Flex>+ );+}diff --git a/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/ModalBody/TabPanel/TabPanel.module.css b/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/ModalBody/TabPanel/TabPanel.module.cssnew file mode 100644index 00000000000..12c901c5530--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/ModalBody/TabPanel/TabPanel.module.css@@ -0,0 +1,9 @@+.panel {+ border-bottom: 1px solid var(--mb-color-border);+}++.tabs {+ &:before {+ display: none;+ }+}diff --git a/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/ModalBody/TabPanel/TabPanel.tsx b/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/ModalBody/TabPanel/TabPanel.tsxnew file mode 100644index 00000000000..55f8ef60557--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/ModalBody/TabPanel/TabPanel.tsx@@ -0,0 +1,50 @@+import { msgid, ngettext, t } from "ttag";++import { Box, Tabs } from "metabase/ui";++import type { TabType } from "../../types";++import S from "./TabPanel.module.css";++type TabPanelProps = {+ selectedTab: TabType;+ canReplace: boolean;+ dependentsCount: number;+ onTabChange: (tab: TabType) => void;+};++export function TabPanel({+ selectedTab,+ canReplace,+ dependentsCount,+ onTabChange,+}: TabPanelProps) {+ const handleChange = (value: string | null) => {+ if (value === "column-mappings" || value === "dependents") {+ onTabChange(value);+ }+ };++ return (+ <Box className={S.panel} px="lg" bg="background-primary">+ <Tabs value={selectedTab} onChange={handleChange}>+ <Tabs.List className={S.tabs}>+ <Tabs.Tab value="column-mappings">{t`Column comparison`}</Tabs.Tab>+ {canReplace && (+ <Tabs.Tab value="dependents">+ {getDependentsTabLabel(dependentsCount)}+ </Tabs.Tab>+ )}+ </Tabs.List>+ </Tabs>+ </Box>+ );+}++function getDependentsTabLabel(dependentsCount: number) {+ return ngettext(+ msgid`${dependentsCount} item will be changed`,+ `${dependentsCount} items will be changed`,+ dependentsCount,+ );+}diff --git a/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/ModalBody/TabPanel/index.ts b/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/ModalBody/TabPanel/index.tsnew file mode 100644index 00000000000..bed5cc54b96--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/ModalBody/TabPanel/index.ts@@ -0,0 +1 @@+export * from "./TabPanel";diff --git a/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/ModalBody/index.ts b/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/ModalBody/index.tsnew file mode 100644index 00000000000..4442bbaf53c--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/ModalBody/index.ts@@ -0,0 +1 @@+export * from "./ModalBody";diff --git a/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/ModalSidebar/EntitySection/EntitySection.tsx b/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/ModalSidebar/EntitySection/EntitySection.tsxnew file mode 100644index 00000000000..d6612743e33--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/ModalSidebar/EntitySection/EntitySection.tsx@@ -0,0 +1,37 @@+import type { ReactNode } from "react";++import {+ Box,+ Card,+ FixedSizeIcon,+ Group,+ type IconName,+ Stack,+ Text,+} from "metabase/ui";++type EntitySectionProps = {+ icon: IconName;+ error?: string;+ children?: ReactNode;+};++export function EntitySection({ icon, error, children }: EntitySectionProps) {+ return (+ <Card px="md" py="lg" withBorder shadow="none">+ <Group align="start" wrap="nowrap">+ <Box p="sm" bg="background-brand" bdrs="md">+ <FixedSizeIcon c="brand" name={icon} />+ </Box>+ <Stack gap="sm">+ {children}+ {error && (+ <Text c="error" size="sm">+ {error}+ </Text>+ )}+ </Stack>+ </Group>+ </Card>+ );+}diff --git a/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/ModalSidebar/EntitySection/index.ts b/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/ModalSidebar/EntitySection/index.tsnew file mode 100644index 00000000000..2e7859551c8--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/ModalSidebar/EntitySection/index.ts@@ -0,0 +1 @@+export * from "./EntitySection";diff --git a/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/ModalSidebar/EntitySelect/EntitySelect.module.css b/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/ModalSidebar/EntitySelect/EntitySelect.module.cssnew file mode 100644index 00000000000..a045e3f5efe--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/ModalSidebar/EntitySelect/EntitySelect.module.css@@ -0,0 +1,3 @@+.button {+ display: block;+}diff --git a/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/ModalSidebar/EntitySelect/EntitySelect.tsx b/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/ModalSidebar/EntitySelect/EntitySelect.tsxnew file mode 100644index 00000000000..1dbba0ab71a--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/ModalSidebar/EntitySelect/EntitySelect.tsx@@ -0,0 +1,110 @@+import { useDisclosure } from "@mantine/hooks";+import { Fragment } from "react";+import { t } from "ttag";++import {+ EntityPickerModal,+ type OmniPickerItem,+} from "metabase/common/components/Pickers";+import { Box, Button, Icon, Input } from "metabase/ui";+import type { DatabaseId, ReplaceSourceEntry } from "metabase-types/api";++import type { EntityItem } from "../../types";++import S from "./EntitySelect.module.css";+import {+ RECENTS_CONTEXT,+ SOURCE_PICKER_MODELS,+ SOURCE_PICKER_OPTIONS,+} from "./constants";+import type { EntityItemInfo } from "./types";+import {+ getEntityItem,+ getEntityItemInfo,+ getIsPickerItemDisabled,+ getPickerValue,+} from "./utils";++type EntitySelectProps = {+ selectedItem: EntityItem | undefined;+ label: string;+ description: string;+ placeholder?: string;+ databaseId?: DatabaseId;+ disabledItem?: EntityItem;+ onChange: (entry: ReplaceSourceEntry) => void;+};++export function EntitySelect({+ selectedItem,+ label,+ description,+ placeholder = t`Pick a table, model, or saved question`,+ databaseId,+ disabledItem,+ onChange,+}: EntitySelectProps) {+ const [isPickerOpen, { open: openPicker, close: closePicker }] =+ useDisclosure(false);+ const sourceInfo =+ selectedItem != null ? getEntityItemInfo(selectedItem) : undefined;+ const isDisabledItem =+ disabledItem != null+ ? getIsPickerItemDisabled(databaseId, disabledItem)+ : undefined;++ const handleItemSelect = (item: OmniPickerItem) => {+ const entry = getEntityItem(item);+ if (entry != null) {+ onChange(entry);+ closePicker();+ }+ };++ return (+ <Input.Wrapper label={label} description={description}>+ <Button+ className={S.button}+ rightSection={<Icon name="chevrondown" />}+ fw="normal"+ onClick={openPicker}+ >+ {sourceInfo ? <ButtonContent displayInfo={sourceInfo} /> : placeholder}+ </Button>+ {isPickerOpen && (+ <EntityPickerModal+ title={t`Select a data source`}+ models={SOURCE_PICKER_MODELS}+ value={+ selectedItem != null ? getPickerValue(selectedItem) : undefined+ }+ options={SOURCE_PICKER_OPTIONS}+ recentsContext={RECENTS_CONTEXT}+ isDisabledItem={isDisabledItem}+ onChange={handleItemSelect}+ onClose={closePicker}+ />+ )}+ </Input.Wrapper>+ );+}++type ButtonContentProps = {+ displayInfo: EntityItemInfo;+};++function ButtonContent({ displayInfo }: ButtonContentProps) {+ return (+ <span>+ {displayInfo.breadcrumbs.map((breadcrumb, index) => (+ <Fragment key={index}>+ <span>{breadcrumb}</span>+ <span> / </span>+ </Fragment>+ ))}+ <Box component="span" fw="bold">+ {displayInfo.name}+ </Box>+ </span>+ );+}diff --git a/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/ModalSidebar/EntitySelect/constants.ts b/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/ModalSidebar/EntitySelect/constants.tsnew file mode 100644index 00000000000..d903bfb2865--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/ModalSidebar/EntitySelect/constants.ts@@ -0,0 +1,16 @@+import type { EntityPickerProps } from "metabase/common/components/Pickers";+import type { RecentContexts } from "metabase-types/api";++export const SOURCE_PICKER_MODELS: EntityPickerProps["models"] = [+ "table",+ "card",+ "dataset",+];++export const SOURCE_PICKER_OPTIONS = {+ hasDatabases: true,+ hasConfirmButtons: false,+ hasRecents: true,+};++export const RECENTS_CONTEXT: RecentContexts[] = ["selections"];diff --git a/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/ModalSidebar/EntitySelect/index.ts b/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/ModalSidebar/EntitySelect/index.tsnew file mode 100644index 00000000000..f946477868a--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/ModalSidebar/EntitySelect/index.ts@@ -0,0 +1 @@+export * from "./EntitySelect";diff --git a/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/ModalSidebar/EntitySelect/types.ts b/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/ModalSidebar/EntitySelect/types.tsnew file mode 100644index 00000000000..0c8f140ce88--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/ModalSidebar/EntitySelect/types.ts@@ -0,0 +1,4 @@+export type EntityItemInfo = {+ name: string;+ breadcrumbs: string[];+};diff --git a/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/ModalSidebar/EntitySelect/utils.ts b/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/ModalSidebar/EntitySelect/utils.tsnew file mode 100644index 00000000000..d16cbe67ed7--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/ModalSidebar/EntitySelect/utils.ts@@ -0,0 +1,114 @@+import type {+ OmniPickerItem,+ OmniPickerValue,+} from "metabase/common/components/Pickers";+import type { DatabaseId, ReplaceSourceEntry } from "metabase-types/api";++import { isSameEntity } from "../../../../utils";+import type { CardEntityData, EntityItem, TableEntityData } from "../../types";++import type { EntityItemInfo } from "./types";++export function getPickerValue(item: EntityItem): OmniPickerValue | undefined {+ if (item.type === "table") {+ return { id: item.id, model: "table" };+ }+ if (item.type === "card") {+ return {+ id: item.id,+ model: item.data?.type === "model" ? "dataset" : "card",+ };+ }+ return undefined;+}++export function getPickerItemDatabaseId(+ item: OmniPickerItem,+): DatabaseId | undefined {+ if (item.model === "database") {+ return item.id;+ }+ if ("database_id" in item) {+ return item.database_id;+ }+ return undefined;+}++export function getIsPickerItemDisabled(+ databaseId: DatabaseId | undefined,+ disabledItem: EntityItem | undefined,+): ((item: OmniPickerItem) => boolean) | undefined {+ if (databaseId == null && disabledItem == null) {+ return undefined;+ }+ return (item: OmniPickerItem) => {+ const entityItem = getEntityItem(item);+ if (+ entityItem != null &&+ disabledItem != null &&+ isSameEntity(entityItem, disabledItem)+ ) {+ return true;+ }+ if (databaseId != null) {+ const itemDatabaseId = getPickerItemDatabaseId(item);+ return itemDatabaseId != null && itemDatabaseId !== databaseId;+ }+ return false;+ };+}++export function getEntityItem(+ item: OmniPickerItem,+): ReplaceSourceEntry | undefined {+ if (item.model === "table") {+ return { id: Number(item.id), type: "table" };+ }+ if (item.model === "card" || item.model === "dataset") {+ return { id: Number(item.id), type: "card" };+ }+ return undefined;+}++export function getEntityItemInfo(+ item: EntityItem,+): EntityItemInfo | undefined {+ if (item.type === "table" && item.data != null) {+ return getTableEntityItemInfo(item.data);+ }+ if (item.type === "card" && item.data != null) {+ return getCardEntityItemInfo(item.data);+ }+ return undefined;+}++function getTableEntityItemInfo(table: TableEntityData): EntityItemInfo {+ const breadcrumbs: string[] = [];+ if (table.db != null) {+ breadcrumbs.push(table.db.name);+ }+ if (table.schema != null) {+ breadcrumbs.push(table.schema);+ }++ return {+ name: table.display_name,+ breadcrumbs,+ };+}++function getCardEntityItemInfo(card: CardEntityData): EntityItemInfo {+ const breadcrumbs: string[] = [];+ if (card.document != null) {+ breadcrumbs.push(card.document.name);+ } else if (card.dashboard != null) {+ breadcrumbs.push(card.dashboard.name);+ } else if (card.collection != null) {+ breadcrumbs.push(card.collection.name);+ }++ return {+ name: card.name,+ breadcrumbs,+ };+}diff --git a/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/ModalSidebar/ModalSidebar.module.css b/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/ModalSidebar/ModalSidebar.module.cssnew file mode 100644index 00000000000..ab0589f4502--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/ModalSidebar/ModalSidebar.module.css@@ -0,0 +1,3 @@+.sidebar {+ border-right: 1px solid var(--mb-color-border);+}diff --git a/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/ModalSidebar/ModalSidebar.tsx b/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/ModalSidebar/ModalSidebar.tsxnew file mode 100644index 00000000000..24c389a8f28--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/ModalSidebar/ModalSidebar.tsx@@ -0,0 +1,87 @@+import { t } from "ttag";++import { Button, Group, Stack, Text, Title } from "metabase/ui";+import type {+ CheckReplaceSourceInfo,+ ReplaceSourceEntry,+} from "metabase-types/api";++import type { EntityItem } from "../types";+import { getEntityDatabaseId } from "../utils";++import { EntitySection } from "./EntitySection";+import { EntitySelect } from "./EntitySelect";+import S from "./ModalSidebar.module.css";+import { getSourceError, getSubmitLabel, getTargetError } from "./utils";++type ModalSidebarProps = {+ sourceItem: EntityItem | undefined;+ targetItem: EntityItem | undefined;+ checkInfo: CheckReplaceSourceInfo | undefined;+ dependentsCount: number | undefined;+ canReplace: boolean;+ onSourceChange: (sourceEntry: ReplaceSourceEntry) => void;+ onTargetChange: (targetEntry: ReplaceSourceEntry) => void;+ onSubmit: () => void;+ onCancel: () => void;+};++export function ModalSidebar({+ sourceItem,+ targetItem,+ checkInfo,+ dependentsCount,+ canReplace,+ onSourceChange,+ onTargetChange,+ onSubmit,+ onCancel,+}: ModalSidebarProps) {+ const sourceDatabaseId =+ sourceItem != null ? getEntityDatabaseId(sourceItem) : undefined;+ const sourceError = getSourceError(checkInfo, dependentsCount);+ const targetError = getTargetError(checkInfo);++ return (+ <Stack+ className={S.sidebar}+ flex="0 0 auto"+ w="35rem"+ px="xl"+ pt="xl"+ pb="lg"+ gap="lg"+ >+ <Stack gap="sm">+ <Title order={2}>{t`Find and replace a data source`}</Title>+ <Text>{t`This lets you change the data source used in queries in bulk. `}</Text>+ </Stack>+ <Stack gap="lg">+ <EntitySection icon="search" error={sourceError}>+ <EntitySelect+ selectedItem={sourceItem}+ label={t`Find all occurrences of this data source`}+ description={t`We'll look for every query in your instance that uses this data source.`}+ onChange={onSourceChange}+ />+ </EntitySection>+ <EntitySection icon="find_replace" error={targetError}>+ <EntitySelect+ selectedItem={targetItem}+ label={t`Replace it with this data source`}+ description={t`It must be based on the same database and include all columns from the original data source.`}+ databaseId={sourceDatabaseId}+ disabledItem={sourceItem}+ onChange={onTargetChange}+ />+ </EntitySection>+ </Stack>+ <Group mt="auto" justify="flex-end" wrap="nowrap">+ <Button onClick={onCancel}>{t`Cancel`}</Button>+ <Button variant="filled" disabled={!canReplace} onClick={onSubmit}>+ {getSubmitLabel(dependentsCount, canReplace)}+ </Button>+ </Group>+ </Stack>+ );+}diff --git a/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/ModalSidebar/index.ts b/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/ModalSidebar/index.tsnew file mode 100644index 00000000000..792319c8ef2--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/ModalSidebar/index.ts@@ -0,0 +1 @@+export * from "./ModalSidebar";diff --git a/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/ModalSidebar/utils.ts b/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/ModalSidebar/utils.tsnew file mode 100644index 00000000000..738eceb3556--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/ModalSidebar/utils.ts@@ -0,0 +1,59 @@+import { msgid, ngettext, t } from "ttag";++import type { CheckReplaceSourceInfo } from "metabase-types/api";++import {+ getGenericErrorMessage,+ getSourceErrorMessage,+ getTargetErrorMessage,+} from "../../../utils";++export function getSourceError(+ checkInfo: CheckReplaceSourceInfo | undefined,+ dependentsCount: number | undefined,+) {+ if (dependentsCount != null && dependentsCount === 0) {+ return t`Nothing uses this data source, so there's nothing to replace.`;+ }+ if (checkInfo == null || checkInfo.success) {+ return undefined;+ }++ const errors = checkInfo.errors ?? [];+ return errors.map(getSourceErrorMessage).find((error) => error != null);+}++export function getTargetError(checkInfo: CheckReplaceSourceInfo | undefined) {+ if (checkInfo == null || checkInfo.success) {+ return undefined;+ }++ const errors = checkInfo.errors ?? [];+ const targetError = errors+ .map(getTargetErrorMessage)+ .find((error) => error != null);+ if (targetError != null) {+ return targetError;+ }++ const sourceError = errors+ .map(getSourceErrorMessage)+ .find((error) => error != null);+ if (sourceError == null) {+ return getGenericErrorMessage();+ }+}++export function getSubmitLabel(+ dependentsCount: number | undefined,+ canReplace: boolean,+) {+ if (dependentsCount == null || !canReplace) {+ return t`Replace data source`;+ }+ return ngettext(+ msgid`Replace data source in ${dependentsCount} items`,+ `Replace data source in ${dependentsCount} items`,+ dependentsCount,+ );+}diff --git a/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/ModalSidebar/utils.unit.spec.ts b/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/ModalSidebar/utils.unit.spec.tsnew file mode 100644index 00000000000..2c62d475a4c--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/ModalSidebar/utils.unit.spec.ts@@ -0,0 +1,129 @@+import { createMockCheckReplaceSourceInfo } from "metabase-types/api/mocks";++import { getSourceError, getSubmitLabel, getTargetError } from "./utils";++describe("getSourceError", () => {+ it.each([+ {+ name: "dependentsCount is 0",+ checkInfo: undefined,+ dependentsCount: 0,+ expected: "Nothing uses this data source, so there's nothing to replace.",+ },+ {+ name: "checkInfo is undefined",+ checkInfo: undefined,+ dependentsCount: undefined,+ expected: undefined,+ },+ {+ name: "checkInfo is successful",+ checkInfo: createMockCheckReplaceSourceInfo({ success: true }),+ dependentsCount: 5,+ expected: undefined,+ },+ {+ name: "checkInfo has incompatible-implicit-joins error",+ checkInfo: createMockCheckReplaceSourceInfo({+ success: false,+ errors: ["incompatible-implicit-joins"],+ }),+ dependentsCount: 5,+ expected:+ "The original table can't be referenced by a foreign key by another table.",+ },+ {+ name: "checkInfo has no source-level errors",+ checkInfo: createMockCheckReplaceSourceInfo({+ success: false,+ errors: ["database-mismatch"],+ }),+ dependentsCount: 5,+ expected: undefined,+ },+ ])("$name", ({ checkInfo, dependentsCount, expected }) => {+ expect(getSourceError(checkInfo, dependentsCount)).toBe(expected);+ });+});++describe("getTargetError", () => {+ it.each([+ {+ name: "checkInfo is undefined",+ checkInfo: undefined,+ expected: undefined,+ },+ {+ name: "checkInfo is successful",+ checkInfo: createMockCheckReplaceSourceInfo({ success: true }),+ expected: undefined,+ },+ {+ name: "checkInfo has database-mismatch error",+ checkInfo: createMockCheckReplaceSourceInfo({+ success: false,+ errors: ["database-mismatch"],+ }),+ expected:+ "The replacement data source is in a different database than the original data source.",+ },+ {+ name: "checkInfo has cycle-detected error",+ checkInfo: createMockCheckReplaceSourceInfo({+ success: false,+ errors: ["cycle-detected"],+ }),+ expected:+ "The replacement data source can't be based on the original data source.",+ },+ {+ name: "checkInfo has only source-level error, no target error shown",+ checkInfo: createMockCheckReplaceSourceInfo({+ success: false,+ errors: ["incompatible-implicit-joins"],+ }),+ expected: undefined,+ },+ {+ name: "checkInfo has failure with no errors, returns generic message",+ checkInfo: createMockCheckReplaceSourceInfo({+ success: false,+ errors: [],+ }),+ expected: "This data source isn't compatible.",+ },+ ])("$name", ({ checkInfo, expected }) => {+ expect(getTargetError(checkInfo)).toBe(expected);+ });+});++describe("getSubmitLabel", () => {+ it.each([+ {+ name: "dependentsCount is undefined",+ dependentsCount: undefined,+ canReplace: true,+ expected: "Replace data source",+ },+ {+ name: "canReplace is false",+ dependentsCount: 5,+ canReplace: false,+ expected: "Replace data source",+ },+ {+ name: "1 item",+ dependentsCount: 1,+ canReplace: true,+ expected: "Replace data source in 1 items",+ },+ {+ name: "multiple items",+ dependentsCount: 5,+ canReplace: true,+ expected: "Replace data source in 5 items",+ },+ ])("$name", ({ dependentsCount, canReplace, expected }) => {+ expect(getSubmitLabel(dependentsCount, canReplace)).toBe(expected);+ });+});diff --git a/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/ReplaceDataSourceModal.tsx b/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/ReplaceDataSourceModal.tsxnew file mode 100644index 00000000000..4ede11ecb32--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/ReplaceDataSourceModal.tsx@@ -0,0 +1,148 @@+import { useDisclosure } from "@mantine/hooks";+import { useLayoutEffect, useState } from "react";++import { useGetCardQuery, useGetTableQuery } from "metabase/api";+import { useToast } from "metabase/common/hooks";+import type { ReplaceDataSourceModalProps } from "metabase/plugins";+import { Flex, Modal } from "metabase/ui";+import {+ useCheckReplaceSourceQuery,+ useListNodeDependentsQuery,+} from "metabase-enterprise/api";+import type { ReplaceSourceEntry } from "metabase-types/api";++import { ConfirmAndProgressModal } from "./ConfirmAndProgressModal";+import { ModalBody } from "./ModalBody";+import { ModalSidebar } from "./ModalSidebar";+import type { TabType } from "./types";+import {+ canReplaceSource,+ getCardRequest,+ getCheckReplaceSourceRequest,+ getDependentsRequest,+ getEntityItem,+ getFailureMessage,+ getSuccessMessage,+ getTableRequest,+} from "./utils";++export function ReplaceDataSourceModal({+ initialSource,+ initialTarget,+ isOpened,+ onClose,+}: ReplaceDataSourceModalProps) {+ return (+ <Modal.Root opened={isOpened} fullScreen onClose={onClose}>+ <Modal.Overlay />+ <Modal.Content>+ <ModalContent+ initialSource={initialSource}+ initialTarget={initialTarget}+ onClose={onClose}+ />+ </Modal.Content>+ </Modal.Root>+ );+}++type ModalContentProps = {+ initialSource: ReplaceSourceEntry | undefined;+ initialTarget: ReplaceSourceEntry | undefined;+ onClose: () => void;+};++function ModalContent({+ initialSource,+ initialTarget,+ onClose,+}: ModalContentProps) {+ const [sourceEntry, setSourceEntry] = useState(initialSource);+ const [targetEntry, setTargetEntry] = useState(initialTarget);+ const [selectedTab, setSelectedTab] = useState<TabType>("column-mappings");+ const [isConfirming, { open: openConfirmation, close: closeConfirmation }] =+ useDisclosure();+ const [sendToast] = useToast();++ const { currentData: sourceTable } = useGetTableQuery(+ getTableRequest(sourceEntry),+ );+ const { currentData: sourceCard } = useGetCardQuery(+ getCardRequest(sourceEntry),+ );+ const { currentData: targetTable } = useGetTableQuery(+ getTableRequest(targetEntry),+ );+ const { currentData: targetCard } = useGetCardQuery(+ getCardRequest(targetEntry),+ );+ const { currentData: dependents } = useListNodeDependentsQuery(+ getDependentsRequest(sourceEntry),+ );+ const { currentData: checkInfo } = useCheckReplaceSourceQuery(+ getCheckReplaceSourceRequest(sourceEntry, targetEntry),+ );++ const sourceItem = getEntityItem(sourceEntry, sourceTable, sourceCard);+ const targetItem = getEntityItem(targetEntry, targetTable, targetCard);+ const columnMappings = checkInfo?.column_mappings;+ const canReplace = canReplaceSource(checkInfo, dependents);++ useLayoutEffect(() => {+ if (!canReplace && selectedTab === "dependents") {+ setSelectedTab("column-mappings");+ }+ }, [selectedTab, canReplace]);++ const handleSourceChange = (sourceEntry: ReplaceSourceEntry) => {+ setSourceEntry(sourceEntry);+ setTargetEntry(undefined);+ };++ const handleReplaceSuccess = (dependentsCount: number) => {+ sendToast({ message: getSuccessMessage(dependentsCount), icon: "check" });+ closeConfirmation();+ onClose();+ };++ const handleReplaceFailure = () => {+ sendToast({ message: getFailureMessage(), icon: "warning" });+ closeConfirmation();+ };++ return (+ <Flex h="100%">+ <ModalSidebar+ sourceItem={sourceItem}+ targetItem={targetItem}+ checkInfo={checkInfo}+ dependentsCount={dependents?.length}+ canReplace={canReplace}+ onSourceChange={handleSourceChange}+ onTargetChange={setTargetEntry}+ onSubmit={openConfirmation}+ onCancel={onClose}+ />+ <ModalBody+ sourceItem={sourceItem}+ targetItem={targetItem}+ selectedTab={selectedTab}+ dependents={dependents}+ canReplace={canReplace}+ columnMappings={columnMappings}+ onTabChange={setSelectedTab}+ />+ {sourceEntry != null && targetEntry != null && dependents != null && (+ <ConfirmAndProgressModal+ sourceEntry={sourceEntry}+ targetEntry={targetEntry}+ dependentsCount={dependents?.length}+ opened={isConfirming}+ onReplaceSuccess={handleReplaceSuccess}+ onReplaceFailure={handleReplaceFailure}+ onClose={closeConfirmation}+ />+ )}+ </Flex>+ );+}diff --git a/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/constants.ts b/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/constants.tsnew file mode 100644index 00000000000..4116392f30c--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/constants.ts@@ -0,0 +1,20 @@+import type { TagType } from "metabase/api/tags";+import type { DependencyType } from "metabase-types/api";++export const DEPENDENT_TYPES: DependencyType[] = [+ "card",+ "segment",+ "measure",+ "transform",+];++export const INVALIDATE_TAGS: TagType[] = [+ "table",+ "card",+ "transform",+ "segment",+ "measure",+ "dashboard",+];++export const MAX_WIDTH = "75rem";diff --git a/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/index.ts b/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/index.tsnew file mode 100644index 00000000000..6c6b623605b--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/index.ts@@ -0,0 +1 @@+export * from "./ReplaceDataSourceModal";diff --git a/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/types.ts b/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/types.tsnew file mode 100644index 00000000000..0fc0bbd3996--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/types.ts@@ -0,0 +1,28 @@+import type { Card, CardId, ConcreteTableId, Table } from "metabase-types/api";++export type TableEntityItem = {+ id: ConcreteTableId;+ type: "table";+ data?: TableEntityData;+};++export type TableEntityData = Pick<+ Table,+ "name" | "display_name" | "db_id" | "db" | "schema"+>;++export type CardEntityItem = {+ id: CardId;+ type: "card";+ data?: CardEntityData;+};++export type CardEntityData = Pick<+ Card,+ "name" | "type" | "database_id" | "collection" | "dashboard" | "document"+>;++export type EntityItem = TableEntityItem | CardEntityItem;+export type EntityItemType = EntityItem["type"];++export type TabType = "column-mappings" | "dependents";diff --git a/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/utils.ts b/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/utils.tsnew file mode 100644index 00000000000..4ce735031f0--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/replacement/components/ReplaceDataSourceModal/utils.ts@@ -0,0 +1,97 @@+import { msgid, ngettext, t } from "ttag";++import { skipToken } from "metabase/api";+import type {+ Card,+ CheckReplaceSourceInfo,+ DatabaseId,+ DependencyNode,+ ReplaceSourceEntry,+ Table,+} from "metabase-types/api";++import type { EntityItem } from "./types";++export function getEntityItem(+ entry: ReplaceSourceEntry | undefined,+ table: Table | undefined,+ card: Card | undefined,+): EntityItem | undefined {+ if (entry?.type === "table") {+ return {+ id: entry.id,+ type: "table",+ data: table?.id === entry.id ? table : undefined,+ };+ }+ if (entry?.type === "card") {+ return {+ id: entry.id,+ type: "card",+ data: card?.id === entry.id ? card : undefined,+ };+ }+}++export function getEntityDatabaseId(+ entity: EntityItem,+): DatabaseId | undefined {+ switch (entity.type) {+ case "table":+ return entity.data?.db_id;+ case "card":+ return entity.data?.database_id;+ }+}++export function getTableRequest(entry: ReplaceSourceEntry | undefined) {+ return entry != null && entry.type === "table" ? { id: entry.id } : skipToken;+}++export function getCardRequest(entry: ReplaceSourceEntry | undefined) {+ return entry != null && entry.type === "card" ? { id: entry.id } : skipToken;+}++export function getDependentsRequest(entry: ReplaceSourceEntry | undefined) {+ return entry != null ? { id: entry.id, type: entry.type } : skipToken;+}++export function getCheckReplaceSourceRequest(+ sourceEntry: ReplaceSourceEntry | undefined,+ targetEntry: ReplaceSourceEntry | undefined,+) {+ if (sourceEntry == null || targetEntry == null) {+ return skipToken;+ }++ return {+ source_entity_id: sourceEntry.id,+ source_entity_type: sourceEntry.type,+ target_entity_id: targetEntry.id,+ target_entity_type: targetEntry.type,+ };+}++export function canReplaceSource(+ checkInfo: CheckReplaceSourceInfo | undefined,+ dependents: DependencyNode[] | undefined,+) {+ return (+ checkInfo != null &&+ checkInfo.success &&+ dependents != null &&+ dependents.length > 0+ );+}++export function getSuccessMessage(dependentsCount: number) {+ return ngettext(+ msgid`Updated ${dependentsCount} item`,+ `Updated ${dependentsCount} items`,+ dependentsCount,+ );+}++export function getFailureMessage() {+ return t`Failed to replace data source`;+}diff --git a/enterprise/frontend/src/metabase-enterprise/replacement/index.ts b/enterprise/frontend/src/metabase-enterprise/replacement/index.tsnew file mode 100644index 00000000000..27df1f299e8--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/replacement/index.ts@@ -0,0 +1,13 @@+import { PLUGIN_REPLACEMENT } from "metabase/plugins";+import { getUserIsAdmin } from "metabase/selectors/user";+import { hasPremiumFeature } from "metabase-enterprise/settings";++import { ReplaceDataSourceModal } from "./components/ReplaceDataSourceModal";++export function initializePlugin() {+ if (hasPremiumFeature("dependencies")) {+ PLUGIN_REPLACEMENT.isEnabled = true;+ PLUGIN_REPLACEMENT.canUserReplaceSources = getUserIsAdmin;+ PLUGIN_REPLACEMENT.ReplaceDataSourceModal = ReplaceDataSourceModal;+ }+}diff --git a/enterprise/frontend/src/metabase-enterprise/replacement/utils.ts b/enterprise/frontend/src/metabase-enterprise/replacement/utils.tsnew file mode 100644index 00000000000..d57e3b028ad--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/replacement/utils.ts@@ -0,0 +1,57 @@+import { t } from "ttag";++import type {+ ReplaceSourceColumnErrorType,+ ReplaceSourceEntry,+ ReplaceSourceErrorType,+} from "metabase-types/api";++export function isSameEntity(+ entry1: ReplaceSourceEntry,+ entry2: ReplaceSourceEntry,+): boolean {+ return entry1.id === entry2.id && entry1.type === entry2.type;+}++export function getGenericErrorMessage(): string {+ return t`This data source isn't compatible.`;+}++export function getSourceErrorMessage(+ error: ReplaceSourceErrorType,+): string | undefined {+ switch (error) {+ case "incompatible-implicit-joins":+ return t`The original table can't be referenced by a foreign key by another table.`;+ default:+ return undefined;+ }+}++export function getTargetErrorMessage(+ error: ReplaceSourceErrorType,+): string | undefined {+ switch (error) {+ case "database-mismatch":+ return t`The replacement data source is in a different database than the original data source.`;+ case "cycle-detected":+ return t`The replacement data source can't be based on the original data source.`;+ default:+ return undefined;+ }+}++export function getColumnErrorMessage(+ error: ReplaceSourceColumnErrorType,+): string {+ switch (error) {+ case "column-type-mismatch":+ return t`This column has a different data type than the original column.`;+ case "missing-primary-key":+ return t`This column is not a primary key, while the original column is.`;+ case "missing-foreign-key":+ return t`This column is not a foreign key, while the original column is.`;+ case "foreign-key-mismatch":+ return t`This foreign key references a different primary key than the original foreign key.`;+ }+}diff --git a/enterprise/frontend/src/metabase-enterprise/replacement/utils.unit.spec.ts b/enterprise/frontend/src/metabase-enterprise/replacement/utils.unit.spec.tsnew file mode 100644index 00000000000..36885662f41--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/replacement/utils.unit.spec.ts@@ -0,0 +1,21 @@+import { isSameEntity } from "./utils";++describe("isSameEntity", () => {+ it("returns true for same id and type", () => {+ expect(+ isSameEntity({ id: 1, type: "table" }, { id: 1, type: "table" }),+ ).toBe(true);+ });++ it("returns false for different id", () => {+ expect(+ isSameEntity({ id: 1, type: "table" }, { id: 2, type: "table" }),+ ).toBe(false);+ });++ it("returns false for different type", () => {+ expect(+ isSameEntity({ id: 1, type: "table" }, { id: 1, type: "card" }),+ ).toBe(false);+ });+});diff --git a/frontend/src/metabase-lib/query/column_types.ts b/frontend/src/metabase-lib/query/column_types.tsindex a78f02fbaaa..b1b0b5afb4b 100644--- a/frontend/src/metabase-lib/query/column_types.ts+++ b/frontend/src/metabase-lib/query/column_types.ts@@ -1,9 +1,7 @@import * as ML from "cljs/metabase.lib.js";import * as TYPES from "cljs/metabase.lib.types.isa";-import type Field from "metabase-lib/v1/metadata/Field";-import type { Field as ApiField, DatasetColumn } from "metabase-types/api";-import type { ColumnMetadata, ColumnTypeInfo } from "./types";+import type { ColumnMetadata, ColumnTypeInfo, JsColumnTypeInfo } from "./types";type TypeFn = (column: ColumnMetadata | ColumnTypeInfo) => boolean;@@ -48,9 +46,8 @@ export const isTitle: TypeFn = TYPES.title_QMARK_;export const isURL: TypeFn = TYPES.URL_QMARK_;export const isZipCode: TypeFn = TYPES.zip_code_QMARK_;-export function legacyColumnTypeInfo(- column: DatasetColumn | Field | ApiField,-): ColumnTypeInfo {+// TODO (Alex P 2/13/26): rename to jsColumnTypeInfo+export function legacyColumnTypeInfo(column: JsColumnTypeInfo): ColumnTypeInfo {return ML.legacy_column__GT_type_info(column);}diff --git a/frontend/src/metabase-lib/query/types.ts b/frontend/src/metabase-lib/query/types.tsindex 3347139291f..0f9f6bbc794 100644--- a/frontend/src/metabase-lib/query/types.ts+++ b/frontend/src/metabase-lib/query/types.ts@@ -638,3 +638,9 @@ export type FieldItem = {export type DependentItem = DatabaseItem | SchemaItem | TableItem | FieldItem;export type ValidationError = { message: string };++export type JsColumnTypeInfo = {+ base_type?: string;+ effective_type?: string;+ semantic_type?: string | null;+};diff --git a/frontend/src/metabase-types/api/index.ts b/frontend/src/metabase-types/api/index.tsindex f87694f7863..cccb515753f 100644--- a/frontend/src/metabase-types/api/index.ts+++ b/frontend/src/metabase-types/api/index.ts@@ -41,6 +41,7 @@ export * from "./performance";export * from "./permissions";export * from "./persist";export * from "./query";+export * from "./replacement";export * from "./remote-sync";export * from "./revision";export * from "./schema";diff --git a/frontend/src/metabase-types/api/mocks/index.ts b/frontend/src/metabase-types/api/mocks/index.tsindex 24eecc0b710..435f8b21666 100644--- a/frontend/src/metabase-types/api/mocks/index.ts+++ b/frontend/src/metabase-types/api/mocks/index.ts@@ -28,6 +28,7 @@ export * from "./permissions";export * from "./pulse";export * from "./query";export * from "./remote-sync";+export * from "./replacement";export * from "./revision";export * from "./schema";export * from "./search";diff --git a/frontend/src/metabase-types/api/mocks/replacement.ts b/frontend/src/metabase-types/api/mocks/replacement.tsnew file mode 100644index 00000000000..b9e1429c140--- /dev/null+++ b/frontend/src/metabase-types/api/mocks/replacement.ts@@ -0,0 +1,55 @@+import type {+ CheckReplaceSourceInfo,+ ReplaceSourceColumnInfo,+ ReplaceSourceColumnMapping,+ ReplaceSourceRun,+} from "metabase-types/api";++export const createMockReplaceSourceColumnInfo = (+ opts?: Partial<ReplaceSourceColumnInfo>,+): ReplaceSourceColumnInfo => ({+ id: 1,+ name: "mock_column",+ display_name: "Mock Column",+ base_type: "type/Text",+ effective_type: "type/Text",+ semantic_type: null,+ ...opts,+});++export const createMockReplaceSourceColumnMapping = (+ opts?: Partial<ReplaceSourceColumnMapping>,+): ReplaceSourceColumnMapping => ({+ source: createMockReplaceSourceColumnInfo(),+ target: createMockReplaceSourceColumnInfo({+ id: 2,+ name: "target_column",+ display_name: "Target Column",+ }),+ ...opts,+});++export const createMockCheckReplaceSourceInfo = (+ opts?: Partial<CheckReplaceSourceInfo>,+): CheckReplaceSourceInfo => ({+ success: true,+ ...opts,+});++export const createMockReplaceSourceRun = (+ opts?: Partial<ReplaceSourceRun>,+): ReplaceSourceRun => ({+ id: 1,+ status: "started",+ is_active: true,+ source_entity_type: "card",+ source_entity_id: 1,+ target_entity_type: "card",+ target_entity_id: 2,+ progress: 0,+ message: null,+ user_id: null,+ start_time: new Date().toISOString(),+ end_time: null,+ ...opts,+});diff --git a/frontend/src/metabase-types/api/replacement.ts b/frontend/src/metabase-types/api/replacement.tsnew file mode 100644index 00000000000..7bf768b0810--- /dev/null+++ b/frontend/src/metabase-types/api/replacement.ts@@ -0,0 +1,88 @@+import type { CardId } from "./card";+import type { Field } from "./field";+import type { ConcreteTableId } from "./table";++export type ReplaceSourceEntityId = ConcreteTableId | CardId;++export const REPLACE_SOURCE_ENTITY_TYPES = ["card", "table"] as const;+export type ReplaceSourceEntityType =+ (typeof REPLACE_SOURCE_ENTITY_TYPES)[number];++export type ReplaceSourceEntry = {+ id: ReplaceSourceEntityId;+ type: ReplaceSourceEntityType;+};++export type ReplaceSourceColumnInfo = Pick<+ Field,+ | "id"+ | "name"+ | "display_name"+ | "base_type"+ | "effective_type"+ | "semantic_type"+>;++export const REPLACE_SOURCE_ERROR_TYPES = [+ "database-mismatch",+ "cycle-detected",+ "incompatible-implicit-joins",+] as const;+export type ReplaceSourceErrorType =+ (typeof REPLACE_SOURCE_ERROR_TYPES)[number];++export const REPLACE_SOURCE_COLUMN_ERROR_TYPES = [+ "column-type-mismatch",+ "missing-primary-key",+ "missing-foreign-key",+ "foreign-key-mismatch",+] as const;+export type ReplaceSourceColumnErrorType =+ (typeof REPLACE_SOURCE_COLUMN_ERROR_TYPES)[number];++export type ReplaceSourceColumnMapping = {+ source?: ReplaceSourceColumnInfo;+ target?: ReplaceSourceColumnInfo;+ errors?: ReplaceSourceColumnErrorType[];+};++export type CheckReplaceSourceInfo = {+ success: boolean;+ errors?: ReplaceSourceErrorType[];+ column_mappings?: ReplaceSourceColumnMapping[];+};++export type ReplaceSourceRunId = number;++export type ReplaceSourceRunStatus =+ | "started"+ | "succeeded"+ | "failed"+ | "canceled"+ | "timeout";++export type ReplaceSourceRun = {+ id: ReplaceSourceRunId;+ status: ReplaceSourceRunStatus;+ is_active: boolean | null;+ source_entity_type: ReplaceSourceEntityType;+ source_entity_id: ReplaceSourceEntityId;+ target_entity_type: ReplaceSourceEntityType;+ target_entity_id: ReplaceSourceEntityId;+ progress: number | null;+ message: string | null;+ user_id: number | null;+ start_time: string;+ end_time: string | null;+};++export type ReplaceSourceRequest = {+ source_entity_id: ReplaceSourceEntityId;+ source_entity_type: ReplaceSourceEntityType;+ target_entity_id: ReplaceSourceEntityId;+ target_entity_type: ReplaceSourceEntityType;+};++export type ReplaceSourceResponse = {+ run_id: ReplaceSourceRunId;+};diff --git a/frontend/src/metabase/data-studio/data-model/components/TableSection/TableSection.tsx b/frontend/src/metabase/data-studio/data-model/components/TableSection/TableSection.tsxindex 3354261a077..c8c3dc66b43 100644--- a/frontend/src/metabase/data-studio/data-model/components/TableSection/TableSection.tsx+++ b/frontend/src/metabase/data-studio/data-model/components/TableSection/TableSection.tsx@@ -26,6 +26,7 @@ import {PLUGIN_DEPENDENCIES,PLUGIN_LIBRARY,PLUGIN_REMOTE_SYNC,+ PLUGIN_REPLACEMENT,} from "metabase/plugins";import {Box,@@ -61,7 +62,7 @@ interface Props {onSyncOptionsClick: () => void;}-type TableModalType = "library" | "publish" | "unpublish";+type TableModalType = "library" | "publish" | "unpublish" | "replace";const TableSectionBase = ({table,@@ -81,6 +82,9 @@ const TableSectionBase = ({const [isSorting, setIsSorting] = useState(false);const hasFields = Boolean(table.fields && table.fields.length > 0);const isLibraryEnabled = PLUGIN_LIBRARY.isEnabled;+ const isReplacementEnabled = useSelector(+ PLUGIN_REPLACEMENT.canUserReplaceSources,+ );const isDependencyGraphEnabled = PLUGIN_DEPENDENCIES.isEnabled;const remoteSyncReadOnly = useSelector(PLUGIN_REMOTE_SYNC.getIsRemoteSyncReadOnly,@@ -257,6 +261,18 @@ const TableSectionBase = ({>{t`Sync settings`}</Button>+ {isReplacementEnabled && (+ <Tooltip label={t`Find and replace`}>+ <Button+ p="sm"+ w="2.5rem"+ flex="0 1 auto"+ leftSection={<Icon name="find_replace" />}+ aria-label={t`Find and replace`}+ onClick={() => setModalType("replace")}+ />+ </Tooltip>+ )}{isDependencyGraphEnabled && (<Tooltip label={t`Dependency graph`}><Button@@ -405,6 +421,11 @@ const TableSectionBase = ({onUnpublish={handleCloseModal}onClose={handleCloseModal}/>+ <PLUGIN_REPLACEMENT.ReplaceDataSourceModal+ isOpened={modalType === "replace"}+ initialSource={{ id: Number(table.id), type: "table" }}+ onClose={handleCloseModal}+ /></Stack>);};diff --git a/frontend/src/metabase/plugins/index.ts b/frontend/src/metabase/plugins/index.tsindex 9900b2ba7e2..de17676d5a2 100644--- a/frontend/src/metabase/plugins/index.ts+++ b/frontend/src/metabase/plugins/index.ts@@ -110,6 +110,10 @@ export {PLUGIN_GROUP_MANAGERS,} from "./oss/permissions";export { PLUGIN_REMOTE_SYNC } from "./oss/remote-sync";+export {+ PLUGIN_REPLACEMENT,+ type ReplaceDataSourceModalProps,+} from "./oss/replacement";export { PLUGIN_RESOURCE_DOWNLOADS } from "./oss/resource-downloads";export {PLUGIN_SEMANTIC_SEARCH,@@ -181,6 +185,7 @@ import { reinitialize as reinitializeModelPersistence } from "./oss/model-persisimport { reinitialize as reinitializeModeration } from "./oss/moderation";import { reinitialize as reinitializePermissions } from "./oss/permissions";import { reinitialize as reinitializeRemoteSync } from "./oss/remote-sync";+import { reinitialize as reinitializeReplacement } from "./oss/replacement";import { reinitialize as reinitializeResourceDownloads } from "./oss/resource-downloads";import { reinitialize as reinitializeSemanticSearch } from "./oss/semantic-search";import { reinitialize as reinitializeSettings } from "./oss/settings";@@ -192,7 +197,6 @@ import { reinitialize as reinitializeTransforms } from "./oss/transforms";import { reinitialize as reinitializeUploadManagement } from "./oss/upload-management";import { reinitialize as reinitializeWhitelabel } from "./oss/whitelabel";import { reinitialize as reinitializeWritableConnection } from "./oss/writable-connection";-/*** Mostly for test purposes, reinitialize all plugins.* You don't reinitialize plugins individually because some plugins depend on others,@@ -222,6 +226,7 @@ export function reinitialize() {reinitializeModeration();reinitializePermissions();reinitializeRemoteSync();+ reinitializeReplacement();reinitializeResourceDownloads();reinitializeSemanticSearch();reinitializeSettings();diff --git a/frontend/src/metabase/plugins/oss/replacement.ts b/frontend/src/metabase/plugins/oss/replacement.tsnew file mode 100644index 00000000000..aeafd01be3c--- /dev/null+++ b/frontend/src/metabase/plugins/oss/replacement.ts@@ -0,0 +1,31 @@+import type { ComponentType } from "react";++import type { ReplaceSourceEntry } from "metabase-types/api";+import type { State } from "metabase-types/store";++import { PluginPlaceholder } from "../components/PluginPlaceholder";++export type ReplaceDataSourceModalProps = {+ initialSource?: ReplaceSourceEntry;+ initialTarget?: ReplaceSourceEntry;+ isOpened: boolean;+ onClose: () => void;+};++type ReplacementPlugin = {+ isEnabled: boolean;+ canUserReplaceSources: (state: State) => boolean;+ ReplaceDataSourceModal: ComponentType<ReplaceDataSourceModalProps>;+};++const getDefaultReplacementPlugin = (): ReplacementPlugin => ({+ isEnabled: false,+ canUserReplaceSources: () => false,+ ReplaceDataSourceModal: PluginPlaceholder,+});++export const PLUGIN_REPLACEMENT = getDefaultReplacementPlugin();++export function reinitialize() {+ Object.assign(PLUGIN_REPLACEMENT, getDefaultReplacementPlugin());+}diff --git a/frontend/src/metabase/ui/components/icons/Icon/icons/find_replace.svg b/frontend/src/metabase/ui/components/icons/Icon/icons/find_replace.svgnew file mode 100644index 00000000000..5f15c03a517--- /dev/null+++ b/frontend/src/metabase/ui/components/icons/Icon/icons/find_replace.svg@@ -0,0 +1 @@+<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="none" viewBox="0 0 16 16"><path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="1.333" d="M9.333 2.667A.667.667 0 0 1 10 2M10 6.667A.667.667 0 0 1 9.333 6M14 2.667A.667.667 0 0 0 13.333 2M14 6a.667.667 0 0 1-.667.667M2 4.667l2 2 2-2"/><path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="1.333" d="M4 6.667V3.333A1.333 1.333 0 0 1 5.333 2h1.334M6 9.333H2.667A.667.667 0 0 0 2 10v3.333c0 .368.298.667.667.667H6a.667.667 0 0 0 .667-.667V10A.667.667 0 0 0 6 9.333"/></svg>diff --git a/frontend/src/metabase/ui/components/icons/Icon/icons/index.ts b/frontend/src/metabase/ui/components/icons/Icon/icons/index.tsindex c0fc135ffaa..9400569fbf4 100644--- a/frontend/src/metabase/ui/components/icons/Icon/icons/index.ts+++ b/frontend/src/metabase/ui/components/icons/Icon/icons/index.ts@@ -196,6 +196,8 @@ import filter_component from "./filter.svg?component";import filter_source from "./filter.svg?source";import filter_plus_component from "./filter_plus.svg?component";import filter_plus_source from "./filter_plus.svg?source";+import find_replace_component from "./find_replace.svg?component";+import find_replace_source from "./find_replace.svg?source";import folder_component from "./folder.svg?component";import folder_source from "./folder.svg?source";import folder_database_component from "./folder_database.svg?component";@@ -931,6 +933,10 @@ export const Icons: Record<IconName, { component: React.VFC; source: string }> =component: filter_plus_component,source: filter_plus_source,},+ find_replace: {+ component: find_replace_component,+ source: find_replace_source,+ },bug: {component: bug_component,source: bug_source,@@ -1797,6 +1803,7 @@ export type IconName =| "fields"| "filter"| "filter_plus"+ | "find_replace"| "bug"| "format_code"| "formula"diff --git a/resources/migrations/060_update_migrations.yaml b/resources/migrations/060_update_migrations.yamlindex 8983f08c4c6..0825a5c2782 100644--- a/resources/migrations/060_update_migrations.yaml+++ b/resources/migrations/060_update_migrations.yaml@@ -77,6 +77,125 @@ databaseChangeLog:tableName: report_cardcolumnName: dimension_mappings+ - changeSet:+ id: v60.2026-03-06T12:00:00+ author: bcm+ comment: Create source_replacement_run table to track async source replacement executions+ changes:+ - createTable:+ tableName: source_replacement_run+ remarks: Table to track source replacement executions and their status+ columns:+ - column:+ name: id+ type: int+ remarks: Unique ID+ autoIncrement: true+ constraints:+ primaryKey: true+ nullable: false+ - column:+ name: source_entity_type+ type: varchar(50)+ remarks: "Type of the source entity (card or table)"+ constraints:+ nullable: false+ - column:+ name: source_entity_id+ type: int+ remarks: "ID of the source entity"+ constraints:+ nullable: false+ - column:+ name: target_entity_type+ type: varchar(50)+ remarks: "Type of the target entity (card or table)"+ constraints:+ nullable: false+ - column:+ name: target_entity_id+ type: int+ remarks: "ID of the target entity"+ constraints:+ nullable: false+ - column:+ name: status+ type: varchar(50)+ remarks: "Current status (started, succeeded, failed, canceled, timeout)"+ constraints:+ nullable: false+ - column:+ name: is_active+ type: ${boolean.type}+ remarks: "True only for currently running replacements, null for completed"+ constraints:+ nullable: true+ - column:+ name: progress+ type: float+ remarks: "Progress from 0.0 to 1.0"+ constraints:+ nullable: true+ - column:+ name: message+ type: ${text.type}+ remarks: "Human-readable message; may contain error info"+ constraints:+ nullable: true+ - column:+ name: user_id+ type: int+ remarks: "User who initiated the replacement"+ constraints:+ nullable: true+ - column:+ name: start_time+ type: ${timestamp_type}+ remarks: "When the replacement started"+ defaultValueComputed: current_timestamp+ constraints:+ nullable: false+ - column:+ name: end_time+ type: ${timestamp_type}+ remarks: "When the replacement completed (null if still running)"+ constraints:+ nullable: true++ - changeSet:+ id: v60.2026-03-06T12:00:01+ author: bcm+ comment: Enforce at most one active source replacement run cluster-wide+ changes:+ - addUniqueConstraint:+ tableName: source_replacement_run+ constraintName: idx_unique_active_source_replacement_run+ columnNames: is_active++ - changeSet:+ id: v60.2026-03-06T12:00:02+ author: bcm+ comment: Add index on source_replacement_run.user_id+ changes:+ - createIndex:+ tableName: source_replacement_run+ indexName: idx_source_replacement_run_user_id+ columns:+ - column:+ name: user_id++ - changeSet:+ id: v60.2026-03-06T12:00:03+ author: bcm+ comment: Add foreign key on source_replacement_run(user_id)+ changes:+ - addForeignKeyConstraint:+ baseTableName: source_replacement_run+ baseColumnNames: user_id+ referencedTableName: core_user+ referencedColumnNames: id+ constraintName: fk_source_replacement_run_user_id+# >>>>>>>>>> DO NOT ADD NEW MIGRATIONS BELOW THIS LINE! ADD THEM ABOVE <<<<<<<<<<########################################################################################################################diff --git a/resources/python-sources/sql_tools.py b/resources/python-sources/sql_tools.pyindex c0799f5be7c..a3aedbc7bda 100644--- a/resources/python-sources/sql_tools.py+++ b/resources/python-sources/sql_tools.py@@ -551,6 +551,11 @@ def replace_names(sql: str, replacements_json: str, dialect: str = None) -> str:raw_schema, was_quoted = unquote_identifier(new_table["schema"], dialect)schema_quoted = original_schema_quoted or was_quoted or needs_quoting(raw_schema, dialect)node.set("db", exp.Identifier(this=raw_schema, quoted=schema_quoted))+ elif "schema" in new_table and new_table["schema"] is None:+ # Explicitly clear the schema from the AST node. This matches Macaw's+ # behavior: {:schema nil :table "x"} means "remove the schema qualifier",+ # turning e.g. `FROM public.orders` into `FROM x`.+ node.set("db", None)if new_table.get("table"):raw_table, was_quoted = unquote_identifier(new_table["table"], dialect)table_quoted = original_table_quoted or was_quoted or needs_quoting(raw_table, dialect)@@ -566,7 +571,7 @@ def replace_names(sql: str, replacements_json: str, dialect: str = None) -> str:col_name = node.namecol_table = node.table # May be None if column is unqualified (e.g., "SELECT id" not "SELECT t.id")# Preserve original quoting status- original_col_quoted = node.this.quoted if node.this else False+ original_col_quoted = node.this.quoted if isinstance(node.this, exp.Identifier) else False# Try to find a matching column rename.# The challenge: replacement key might be {:table "orders" :column "id"}diff --git a/src/metabase/lib/core.cljc b/src/metabase/lib/core.cljcindex ff27a7e9749..e5c1cc5ccdb 100644--- a/src/metabase/lib/core.cljc+++ b/src/metabase/lib/core.cljc@@ -178,6 +178,7 @@->legacy-MBQL->pMBQLlegacy-default-join-alias+ with-aggregation-listwithout-cleaning][metabase.lib.convert.metadata-to-legacylib-metadata-column->legacy-metadata-column@@ -430,8 +431,10 @@parameter-target-field-optionsparameter-target-field-refparameter-target-is-dimension?+ parameter-target-stage-numberparameter-target-template-tag-name- update-parameter-target-dimension-options]+ update-parameter-target-dimension-options+ update-parameter-target-field-ref][lib.parameters.parsematch-and-normalize-tag-name][lib.parse@@ -525,6 +528,7 @@validation-exception-error][metabase.lib.walk.utilall-field-ids+ all-referenced-entity-idsall-implicitly-joined-field-idsall-implicitly-joined-table-idsall-measure-idsdiff --git a/src/metabase/lib/field/resolution.cljc b/src/metabase/lib/field/resolution.cljcindex 114968d78f1..f99e923bfe3 100644--- a/src/metabase/lib/field/resolution.cljc+++ b/src/metabase/lib/field/resolution.cljc@@ -45,10 +45,8 @@"If this is a nested column, add metadata about the parent column."[metadata-providerable :- ::lib.schema.metadata/metadata-providerable{:keys [parent-id], :as metadata} :- ::lib.schema.metadata/column]- (if-not parent-id- metadata- (let [parent-metadata (lib.metadata/field metadata-providerable parent-id)- {parent-name :name+ (if-some [parent-metadata (when parent-id (lib.metadata/field metadata-providerable parent-id))]+ (let [{parent-name :nameparent-nfc-path :nfc-pathparent-display-name :display-name} (add-parent-column-metadata metadata-providerable parent-metadata)new-name (str parent-name@@ -63,7 +61,8 @@:nfc-path (conj (vec parent-nfc-path) (:name parent-metadata)):display-name new-display-name;; this is used by the `display-name-method` for `:metadata/column` in [[metabase.lib.field]]- :lib/simple-display-name new-display-name)))))+ :lib/simple-display-name new-display-name)))+ metadata))(mu/defn- field-metadata :- [:maybe ::lib.metadata.calculation/visible-column]"Metadata about the field from the metadata provider."@@ -334,7 +333,8 @@(if-some [source-cols (or (when-some [previous-stage-number (lib.util/previous-stage-number query stage-number)](lib.metadata.calculation/returned-columns query previous-stage-number))(when-some [source-card-id (:source-card (lib.util/query-stage query stage-number))]- (lib.metadata.calculation/returned-columns query (lib.metadata/card query source-card-id))))]+ (when-some [card (lib.metadata/card query source-card-id)]+ (lib.metadata.calculation/returned-columns query card))))](let [previous-stage-cols (filter #(= (:lib/original-join-alias %) join-alias)source-cols)];; try to resolve by what is PROBABLY the correct name in a previous stage e.g. `Join` + `COLUMN` becomes@@ -570,8 +570,9 @@;; `returned-columns`... we'd get fallback metadata, right?(and source-table-id(string? id-or-name))- (m/find-first #(= (:name %) id-or-name)- (lib.metadata.calculation/returned-columns query (lib.metadata/table query source-table-id)))+ (when-some [table (lib.metadata/table query source-table-id)]+ (m/find-first #(= (:name %) id-or-name)+ (lib.metadata.calculation/returned-columns query table)))(= (:lib/type stage) :mbql.stage/native)(when-some [col (resolve-in-current-stage-metadata query stage-number id-or-name)]diff --git a/src/metabase/lib/metadata/calculation.cljc b/src/metabase/lib/metadata/calculation.cljcindex 65cc26024a8..91192435761 100644--- a/src/metabase/lib/metadata/calculation.cljc+++ b/src/metabase/lib/metadata/calculation.cljc@@ -698,13 +698,15 @@id->target-fields (m/index-by :id (lib.metadata/bulk-metadataquery :metadata/column (into #{} (map :fk-target-field-id) fk-fields)))target-fields (into []- (comp (map (fn [{source-field-id :id- :keys [fk-target-field-id]- :as source}]- (-> (id->target-fields fk-target-field-id)- (assoc ::fk-field-id source-field-id- ::fk-field-name (lib.field.util/inherited-column-name source)- ::fk-join-alias (:lib/join-alias source)))))+ (comp (keep (fn [{source-field-id :id+ :keys [fk-target-field-id]+ :as source}]+ ;; the target field might not exist+ (when-let [target (id->target-fields fk-target-field-id)]+ (assoc target+ ::fk-field-id source-field-id+ ::fk-field-name (lib.field.util/inherited-column-name source)+ ::fk-join-alias (:lib/join-alias source)))))(remove #(contains? existing-table-ids (:table-id %))))fk-fields)id->table (m/index-by :id (lib.metadata/bulk-metadatadiff --git a/src/metabase/lib/order_by.cljc b/src/metabase/lib/order_by.cljcindex 159e8fbf8f2..33b709fbba2 100644--- a/src/metabase/lib/order_by.cljc+++ b/src/metabase/lib/order_by.cljc@@ -78,7 +78,7 @@(defn- order-by-distinct-key"Key for determining whether two order bys are distinct of not for purposes of ignoring duplicates in [[order-by]]."[[_dir _opts expr]]- (lib.schema.util/remove-lib-uuids expr))+ (lib.schema.util/mbql-clause-distinct-key expr))(mu/defn order-by"Add an MBQL order-by clause (i.e., `:asc` or `:desc`) from something that you can theoretically sort by -- maybe adiff --git a/src/metabase/lib/parameters.cljc b/src/metabase/lib/parameters.cljcindex 41f038cbcca..dc025d18548 100644--- a/src/metabase/lib/parameters.cljc+++ b/src/metabase/lib/parameters.cljc@@ -110,6 +110,31 @@opts)nil))+(mu/defn parameter-target-stage-number :- :int+ "Return the stage-number from a parameter target's dimension options,+ or 0 (first stage) if not specified."+ [target :- ::lib.schema.parameter/target]+ (or (some-> target parameter-target-dimension-options :stage-number)+ 0))++(mu/defn update-parameter-target-field-ref :- ::lib.schema.parameter/target+ "If parameter `:target` wraps a legacy `:field` ref, convert it to pMBQL, apply `(apply f field-ref args)`, convert+ the result back to legacy, and place it back in the target. If there is no `:field` ref, returns target unchanged."+ [target :- ::lib.schema.parameter/target+ f & args]+ (if-let [field-ref (parameter-target-field-ref target)]+ (let [new-legacy-ref (-> (apply f field-ref args)+ lib.convert/->legacy-MBQL)]+ (case (first target)+ :dimension (let [[tag _ref opts] target]+ (cond-> [tag new-legacy-ref]+ opts (conj opts)))+ :variable (let [[tag _ref] target]+ [tag new-legacy-ref])+ :field new-legacy-ref+ target))+ target))+(mu/defn update-parameter-target-dimension-options :- ::lib.schema.parameter/target"If parameter `:target` is a `:dimension` pseudo-clause, update the options map associated with it, if any. If it isnot a `:dimension` psuedo-clause, this function no-ops."diff --git a/src/metabase/lib/parse.cljc b/src/metabase/lib/parse.cljcindex ce767486074..156bd2f5d95 100644--- a/src/metabase/lib/parse.cljc+++ b/src/metabase/lib/parse.cljc@@ -141,7 +141,7 @@(recur (apply conj acc parsed) more in-string?)))(:line-comment-begin :block-comment-begin)- (if (or comment-mode (pos? optional-level) in-string?)+ (if (or comment-mode (pos? optional-level) (pos? param-level) in-string?)(recur (conj acc text) more in-string?)(let [[parsed more] (parse-tokens* opts more optional-level param-level in-string? token)](recur (into acc (cons text parsed)) more in-string?)))diff --git a/src/metabase/lib/ref.cljc b/src/metabase/lib/ref.cljcindex f725f22548c..518a43acf02 100644--- a/src/metabase/lib/ref.cljc+++ b/src/metabase/lib/ref.cljc@@ -32,12 +32,24 @@(when (pos-int? id-or-name)id-or-name))+(mu/defn with-field-ref-id :- :mbql.clause/field+ "Return a new field ref with `field-id` as the identifier."+ [[tag opts _id-or-name, :as _field-ref] :- :mbql.clause/field+ field-id :- ::lib.schema.id/field]+ [tag opts field-id])+(mu/defn field-ref-name :- [:maybe :string]"If a `:field` ref uses a Field name, return that name."[[_tag _opts id-or-name, :as _field-ref] :- :mbql.clause/field](when (string? id-or-name)id-or-name))+(mu/defn with-field-ref-name :- :mbql.clause/field+ "Return a new field ref with `field-name` as the identifier."+ [[tag opts _id-or-name, :as _field-ref] :- :mbql.clause/field+ field-name :- :string]+ [tag opts field-name])+(mu/defn expression-ref-name :- [:maybe :string]"Return the expression name from an `:expression` ref."[[_tag _opts expression-name, :as _expression-ref] :- :mbql.clause/expression]diff --git a/src/metabase/lib/schema/id.cljc b/src/metabase/lib/schema/id.cljcindex 1aa180b49e3..d74167a177b 100644--- a/src/metabase/lib/schema/id.cljc+++ b/src/metabase/lib/schema/id.cljc@@ -54,6 +54,10 @@"Valid Measure ID"pos-int?)+(mr/def ::metric+ "Valid Metric ID (metrics are Cards with type :metric)"+ pos-int?)+(mr/def ::snippet"Valid Snippet ID"pos-int?)diff --git a/src/metabase/lib/stage.cljc b/src/metabase/lib/stage.cljcindex 1f4aa94c25d..58dcba6be94 100644--- a/src/metabase/lib/stage.cljc+++ b/src/metabase/lib/stage.cljc@@ -209,7 +209,7 @@;; 1b: default visible Fields for the source Table(when source-table(assert (integer? source-table))- (let [table (lib.metadata/table query source-table)]+ (when-some [table (lib.metadata/table query source-table)](lib.metadata.calculation/returned-columns query stage-number table options)));; 1e. Metadata associated with a Metric(when metric-based?diff --git a/src/metabase/lib/walk/util.cljc b/src/metabase/lib/walk/util.cljcindex bca7ae92925..1b277d5dfd1 100644--- a/src/metabase/lib/walk/util.cljc+++ b/src/metabase/lib/walk/util.cljc@@ -11,6 +11,7 @@[metabase.lib.util.match :as lib.util.match][metabase.lib.walk :as lib.walk][metabase.util.malli :as mu]+ [metabase.util.malli.registry :as mr][metabase.util.performance :refer [not-empty]]))(defn- transduce-stages@@ -230,6 +231,14 @@(mapcat all-field-ids))(all-template-tags query))))+(mu/defn all-template-tag-table-ids :- [:maybe [:set {:min 1} ::lib.schema.id/table]]+ "Set of all Table IDs referenced in table template tags."+ [query :- ::lib.schema/query]+ (not-empty+ (into #{}+ (keep :table-id)+ (all-template-tags query))))+;;; TODO (Cam 10/1/25) -- overlapping responsibilities with [[metabase.lib.template-tags/template-tags->snippet-ids]](mu/defn all-template-tag-snippet-ids :- [:maybe [:set {:min 1} ::lib.schema.id/snippet]]"Set of all Native Query Snippet IDs used in template tags."@@ -239,3 +248,36 @@(comp (filter #(= (:type %) :snippet))(keep :snippet-id))(all-template-tags query))))++(mr/def ::referenced-entity-ids+ [:map+ [:table [:set ::lib.schema.id/table]]+ [:card [:set ::lib.schema.id/card]]+ [:metric [:set ::lib.schema.id/metric]]+ [:measure [:set ::lib.schema.id/measure]]+ [:segment [:set ::lib.schema.id/segment]]+ [:snippet [:set ::lib.schema.id/snippet]]])++(mu/defn all-referenced-entity-ids :- ::referenced-entity-ids+ "Return a map of all referenced entity IDs in `queries`."+ [queries :- [:sequential ::lib.schema/query]]+ (let [source-table-ids (into #{} (mapcat all-source-table-ids) queries)+ source-card-ids (into #{} (mapcat all-source-card-ids) queries)+ implicitly-joined-field-ids (into #{} (mapcat all-implicitly-joined-field-ids) queries)+ template-tag-field-ids (into #{} (mapcat all-template-tag-field-ids) queries)+ template-tag-table-ids (into #{} (mapcat all-template-tag-table-ids) queries)+ template-tag-card-ids (into #{} (mapcat all-template-tag-card-ids) queries)+ template-tag-snippet-ids (into #{} (mapcat all-template-tag-snippet-ids) queries)+ metric-ids (into #{} (mapcat all-metric-ids) queries)+ measure-ids (into #{} (mapcat all-measure-ids) queries)+ segment-ids (into #{} (mapcat all-segment-ids) queries)+ all-field-ids* (set/union implicitly-joined-field-ids template-tag-field-ids)+ all-field-table-ids (when (seq queries)+ (->> (lib.metadata/bulk-metadata (first queries) :metadata/column all-field-ids*)+ (into #{} (keep :table-id))))]+ {:table (set/union source-table-ids all-field-table-ids template-tag-table-ids)+ :card (set/union source-card-ids template-tag-card-ids)+ :metric metric-ids+ :measure measure-ids+ :segment segment-ids+ :snippet template-tag-snippet-ids}))diff --git a/src/metabase/lib_be/core.clj b/src/metabase/lib_be/core.cljindex 1d31b2dbde4..a9d2c0fad6d 100644--- a/src/metabase/lib_be/core.clj+++ b/src/metabase/lib_be/core.clj@@ -4,6 +4,7 @@[metabase.lib-be.metadata.bootstrap][metabase.lib-be.metadata.jvm][metabase.lib-be.models.transforms]+ [metabase.lib-be.query][metabase.lib-be.settings][potemkin :as p]))@@ -12,6 +13,7 @@metabase.lib-be.metadata.bootstrap/keep-memetabase.lib-be.metadata.jvm/keep-memetabase.lib-be.models.transforms/keep-me+ metabase.lib-be.query/keep-memetabase.lib-be.settings/keep-me)(p/import-vars@@ -32,4 +34,6 @@breakout-bin-widthbreakout-bins-numenable-nested-queries- start-of-week])+ start-of-week]+ [metabase.lib-be.query+ bulk-load-query-metadata!])diff --git a/src/metabase/lib_be/query.clj b/src/metabase/lib_be/query.cljnew file mode 100644index 00000000000..ece3fc94893--- /dev/null+++ b/src/metabase/lib_be/query.clj@@ -0,0 +1,18 @@+(ns metabase.lib-be.query+ (:require+ [metabase.lib.metadata :as lib.metadata]+ [metabase.lib.schema.metadata :as lib.schema.metadata]+ [metabase.lib.walk.util :as lib.walk.util]+ [metabase.util.malli :as mu]))++(mu/defn bulk-load-query-metadata!+ "Bulk-load all metadata referenced by `referenced-entity-ids` into `metadata-providerable`'s cache."+ [metadata-providerable :- ::lib.schema.metadata/metadata-providerable+ {:keys [table card metric measure segment snippet]} :- ::lib.walk.util/referenced-entity-ids]+ (lib.metadata/bulk-metadata metadata-providerable :metadata/table table)+ (lib.metadata/bulk-metadata metadata-providerable :metadata/card card)+ (lib.metadata/bulk-metadata metadata-providerable :metadata/metric metric)+ (lib.metadata/bulk-metadata metadata-providerable :metadata/measure measure)+ (lib.metadata/bulk-metadata metadata-providerable :metadata/segment segment)+ (lib.metadata/bulk-metadata metadata-providerable :metadata/native-query-snippet snippet)+ nil)diff --git a/src/metabase/models/resolution.clj b/src/metabase/models/resolution.cljindex d3ffd8cda60..3e70c79672d 100644--- a/src/metabase/models/resolution.clj+++ b/src/metabase/models/resolution.clj@@ -88,6 +88,7 @@:model/RecentViews metabase.activity-feed.models.recent-views:model/RemoteSyncObject metabase-enterprise.remote-sync.models.remote-sync-object:model/RemoteSyncTask metabase-enterprise.remote-sync.models.remote-sync-task+ :model/ReplacementRun metabase-enterprise.replacement.models.replacement-run:model/Revision metabase.revisions.models.revision:model/SearchIndexMetadata metabase.search.models.search-index-metadata:model/Secret metabase.secrets.models.secretdiff --git a/src/metabase/queries/schema.clj b/src/metabase/queries/schema.cljindex fe16f659f75..59b184724d9 100644--- a/src/metabase/queries/schema.clj+++ b/src/metabase/queries/schema.clj@@ -45,11 +45,13 @@;; have to do this -- we should try to fix flaws in metadata thru normalization if at all possible.[:schema{:decode/normalize (fn [xs]- (if-not (mr/validate [:sequential ::lib.schema.metadata/lib-or-legacy-column] xs)+ (if (or+ (nil? xs)+ (mr/validate [:sequential ::lib.schema.metadata/lib-or-legacy-column] xs))+ xs(do(log/warn "Ignoring invalid Card result_metadata")- nil)- xs))}+ nil)))}:any]]);;; TODO (Cam 9/29/25) -- fill this out more, `:metabase.lib.schema.metadata/card` has a lot of stuff and there's alsodiff --git a/src/metabase/query_processor/middleware/parameters.clj b/src/metabase/query_processor/middleware/parameters.cljindex 2aca0504074..674b359b813 100644--- a/src/metabase/query_processor/middleware/parameters.clj+++ b/src/metabase/query_processor/middleware/parameters.clj@@ -4,6 +4,7 @@[clojure.data :as data][clojure.set :as set][metabase.lib.core :as lib]+ [metabase.lib.parameters :as lib.parameters][metabase.lib.schema :as lib.schema][metabase.lib.schema.common :as lib.schema.common][metabase.lib.schema.id :as lib.schema.id]@@ -55,12 +56,8 @@the [[num-stages-prepended-by-preprocessing]])."[query :- ::lib.schema/queryparameter :- ::lib.schema.parameter/parameter]- (let [stage-number (or (-> parameter- :target- lib/->pMBQL- lib/options- :stage-number)- 0)]+ (let [stage-number (lib.parameters/parameter-target-stage-number+ (:target parameter))](if (not (neg? stage-number));; for a non-negative stage number add the offset to it as mentioned above(+ stage-number (num-stages-prepended-by-preprocessing query))diff --git a/src/metabase/source_swap/compatibility.clj b/src/metabase/source_swap/compatibility.cljnew file mode 100644index 00000000000..f37d57688c5--- /dev/null+++ b/src/metabase/source_swap/compatibility.clj@@ -0,0 +1,59 @@+(ns metabase.source-swap.compatibility+ (:require+ [medley.core :as m]+ [metabase.lib.metadata.protocols :as lib.metadata.protocols]+ [metabase.lib.schema.metadata :as lib.schema.metadata]+ [metabase.lib.types.isa :as lib.types.isa]+ [metabase.source-swap.schema :as source-swap.schema]+ [metabase.source-swap.util :as source-swap.util]+ [metabase.util.malli :as mu]+ [metabase.util.performance :as perf]))++(set! *warn-on-reflection* true)++(mu/defn column-errors :- [:sequential ::source-swap.schema/column-error]+ "Checks for column type mismatches, missing primary keys, extra primary keys, missing foreign keys, and foreign key mismatches."+ [old-column :- ::lib.schema.metadata/column+ new-column :- ::lib.schema.metadata/column+ old-source-type :- ::source-swap.schema/source-type+ new-source-type :- ::source-swap.schema/source-type]+ (cond-> []+ (not= (:effective-type old-column) (:effective-type new-column))+ (conj :column-type-mismatch)++ (and (= old-source-type :table)+ (= new-source-type :table)+ (lib.types.isa/primary-key? old-column)+ (not (lib.types.isa/primary-key? new-column)))+ (conj :missing-primary-key)++ (and (lib.types.isa/foreign-key? old-column)+ (not (lib.types.isa/foreign-key? new-column)))+ (conj :missing-foreign-key)++ (and (lib.types.isa/foreign-key? old-column)+ (lib.types.isa/foreign-key? new-column)+ (not= (:fk-target-field-id old-column) (:fk-target-field-id new-column)))+ (conj :foreign-key-mismatch)))++(mu/defn check-column-mappings :- [:sequential ::source-swap.schema/column-mapping]+ "Build column mappings between source and target columns."+ [metadata-providerable :- ::lib.metadata.protocols/metadata-providerable+ [old-source-type, :as old-source] :- ::source-swap.schema/source+ [new-source-type, :as new-source] :- ::source-swap.schema/source]+ (let [old-columns (source-swap.util/source-columns metadata-providerable old-source)+ new-columns (source-swap.util/source-columns metadata-providerable new-source)+ old-by-name (m/index-by source-swap.util/column-match-key old-columns)+ new-by-name (m/index-by source-swap.util/column-match-key new-columns)+ all-names (distinct (concat (map source-swap.util/column-match-key old-columns)+ (map source-swap.util/column-match-key new-columns)))]+ (perf/mapv (fn [column-name]+ (let [old-column (get old-by-name column-name)+ new-column (get new-by-name column-name)+ errors (when (and old-column new-column)+ (perf/not-empty (column-errors old-column new-column old-source-type new-source-type)))]+ (cond-> {}+ old-column (assoc :source old-column)+ new-column (assoc :target new-column)+ errors (assoc :errors errors))))+ all-names)))diff --git a/src/metabase/source_swap/core.clj b/src/metabase/source_swap/core.cljnew file mode 100644index 00000000000..7e1bae146ad--- /dev/null+++ b/src/metabase/source_swap/core.clj@@ -0,0 +1,67 @@+(ns metabase.source-swap.core+ (:require+ [metabase.lib.core :as lib]+ [metabase.lib.metadata.protocols :as lib.metadata.protocols]+ [metabase.lib.schema :as lib.schema]+ [metabase.lib.schema.parameter :as lib.schema.parameter]+ [metabase.lib.schema.ref :as lib.schema.ref]+ [metabase.source-swap.compatibility :as source-swap.compatibility]+ [metabase.source-swap.mbql :as source-swap.mbql]+ [metabase.source-swap.native :as source-swap.native]+ [metabase.source-swap.schema :as source-swap.schema]+ [metabase.util.malli :as mu]))++(set! *warn-on-reflection* true)++(mu/defn check-column-mappings :- [:sequential ::source-swap.schema/column-mapping]+ "Build column mappings between source and target columns."+ [metadata-providerable :- ::lib.metadata.protocols/metadata-providerable+ old-source :- ::source-swap.schema/source+ new-source :- ::source-swap.schema/source]+ (source-swap.compatibility/check-column-mappings metadata-providerable old-source new-source))++(mu/defn upgrade-field-ref :- ::lib.schema.ref/ref+ "Generate a new ref for a column."+ [query :- ::lib.schema/query+ stage-number :- :int+ field-ref :- :mbql.clause/field]+ (if (lib/native-only-query? query)+ field-ref+ (source-swap.mbql/upgrade-field-ref query stage-number field-ref)))++(mu/defn upgrade-field-refs-in-query :- ::lib.schema/query+ "Upgrade all field refs in `query` to use name-based field refs when possible."+ [query :- ::lib.schema/query]+ (cond-> query+ (not (lib/native-only-query? query))+ source-swap.mbql/upgrade-field-refs-in-mbql-stages))++(mu/defn upgrade-field-ref-in-parameter-target :- ::lib.schema.parameter/target+ "If the parameter target is a field ref, upgrade it to use a name-based field ref when possible."+ [query :- ::lib.schema/query+ target :- ::lib.schema.parameter/target]+ (if (lib/native-only-query? query)+ target+ (source-swap.mbql/upgrade-field-ref-in-parameter-mbql-target query target)))++(mu/defn swap-source-in-query :- ::lib.schema/query+ "Updates the query to use the new source table or card."+ [query :- ::lib.schema/query+ old-source :- ::source-swap.schema/source+ new-source :- ::source-swap.schema/source]+ (cond-> query+ (lib/any-native-stage? query)+ (source-swap.native/swap-source-in-native-stages old-source new-source)++ (not (lib/native-only-query? query))+ (source-swap.mbql/swap-source-in-mbql-stages old-source new-source)))++(mu/defn swap-source-in-parameter-target :- ::lib.schema.parameter/target+ "If the parameter target is a field ref, swap it to reference the new source."+ [query :- ::lib.schema/query+ target :- ::lib.schema.parameter/target+ old-source :- ::source-swap.schema/source+ new-source :- ::source-swap.schema/source]+ (if (lib/native-only-query? query)+ target+ (source-swap.mbql/swap-source-in-parameter-mbql-target query target old-source new-source)))diff --git a/src/metabase/source_swap/mbql.clj b/src/metabase/source_swap/mbql.cljnew file mode 100644index 00000000000..6fcee461e90--- /dev/null+++ b/src/metabase/source_swap/mbql.clj@@ -0,0 +1,309 @@+(ns metabase.source-swap.mbql+ (:require+ [medley.core :as m]+ [metabase.lib.core :as lib]+ [metabase.lib.field.resolution :as lib.field.resolution]+ [metabase.lib.options :as lib.options]+ [metabase.lib.ref :as lib.ref]+ [metabase.lib.schema :as lib.schema]+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.lib.schema.join :as lib.schema.join]+ [metabase.lib.schema.metadata :as lib.schema.metadata]+ [metabase.lib.schema.parameter :as lib.schema.parameter]+ [metabase.lib.schema.ref :as lib.schema.ref]+ [metabase.lib.schema.util :as lib.schema.util]+ [metabase.lib.util :as lib.util]+ [metabase.lib.walk :as lib.walk]+ [metabase.source-swap.schema :as source-swap.schema]+ [metabase.source-swap.util :as source-swap.util]+ [metabase.util.malli :as mu]+ [metabase.util.malli.registry :as mr]+ [metabase.util.performance :as perf]))++(set! *warn-on-reflection* true)++;;; ------------------------------------------------ upgrade-field-refs ------------------------------------------------++(mu/defn- same-ref? :- :boolean+ "Checks if two refs are the same. Ignores :lib/uuid, :base-type, and :effective-type."+ [ref-1 :- ::lib.schema.ref/ref+ ref-2 :- ::lib.schema.ref/ref]+ (= (lib.schema.util/mbql-clause-distinct-key ref-1)+ (lib.schema.util/mbql-clause-distinct-key ref-2)))++(mu/defn- walk-clause-field-refs :- :any+ "Walks a clause and applies a function to all `:field` clauses."+ [clause :- :any+ f :- fn?]+ (lib.walk/walk-clause clause+ (fn [clause]+ (cond-> clause+ (lib.util/field-clause? clause)+ f))))++(mu/defn- preserve-expression-name :- ::lib.schema.ref/ref+ "Copy the expression name from `old-field-ref` to `new-field-ref`."+ [old-ref :- ::lib.schema.ref/ref+ new-ref :- ::lib.schema.ref/ref]+ (let [expression-name (lib.util/expression-name old-ref)]+ (cond-> new-ref+ expression-name+ (lib.options/update-options assoc :lib/expression-name expression-name))))++(mu/defn upgrade-field-ref :- ::lib.schema.ref/ref+ "Generate a new ref for a column. Always takes a `:field` ref, may return a `:field` ref or an `:expression` ref."+ [query :- ::lib.schema/query+ stage-number :- :int+ field-ref :- :mbql.clause/field]+ (or (when-let [column (lib.field.resolution/resolve-field-ref query stage-number field-ref)]+ (when-not (::lib.field.resolution/fallback-metadata? column)+ (let [new-field-ref (preserve-expression-name field-ref (lib.ref/ref column))]+ (when-not (same-ref? field-ref new-field-ref)+ new-field-ref))))+ field-ref))++(mu/defn- upgrade-field-refs-in-clauses :- [:sequential :any]+ "Upgrade all field refs in a list of clauses to use name-based field refs when possible."+ [query :- ::lib.schema/query+ stage-number :- :int+ clauses :- [:sequential :any]+ {:keys [distinct?]} :- [:map [:distinct? :boolean]]]+ (into []+ (cond-> (map (fn [clause]+ (walk-clause-field-refs clause #(upgrade-field-ref query stage-number %))))+ distinct? (comp (m/distinct-by lib.schema.util/mbql-clause-distinct-key)))+ clauses))++(mu/defn- upgrade-field-refs-in-join :- ::lib.schema.join/join+ "Upgrade all field refs in a join. :fields in a join can be a keyword :all or :none, or a list of field refs."+ [query :- ::lib.schema/query+ stage-number :- :int+ join :- ::lib.schema.join/join]+ (-> join+ (m/update-existing :fields #(if (keyword? %) % (upgrade-field-refs-in-clauses query stage-number % {:distinct? true})))+ (m/update-existing :conditions #(upgrade-field-refs-in-clauses query stage-number % {:distinct? false}))))++(mu/defn- upgrade-field-refs-in-joins :- [:sequential ::lib.schema.join/join]+ "Upgrade all field refs in a list of joins."+ [query :- ::lib.schema/query+ stage-number :- :int+ joins :- [:sequential ::lib.schema.join/join]]+ (perf/mapv #(upgrade-field-refs-in-join query stage-number %) joins))++(mu/defn- upgrade-field-refs-in-stage :- ::lib.schema/stage+ "Upgrade all field refs in a stage."+ [query :- ::lib.schema/query+ stage-number :- :int]+ (let [stage (lib.util/query-stage query stage-number)]+ (-> stage+ (m/update-existing :fields #(upgrade-field-refs-in-clauses query stage-number % {:distinct? true}))+ (m/update-existing :joins #(upgrade-field-refs-in-joins query stage-number %))+ (m/update-existing :expressions #(upgrade-field-refs-in-clauses query stage-number % {:distinct? false}))+ (m/update-existing :filters #(upgrade-field-refs-in-clauses query stage-number % {:distinct? false}))+ (m/update-existing :aggregation #(upgrade-field-refs-in-clauses query stage-number % {:distinct? false}))+ (m/update-existing :breakout #(upgrade-field-refs-in-clauses query stage-number % {:distinct? true}))+ (m/update-existing :order-by #(upgrade-field-refs-in-clauses query stage-number % {:distinct? true})))))++(mu/defn upgrade-field-refs-in-mbql-stages :- ::lib.schema/query+ "Upgrade all field refs in `query` to use name-based field refs when possible."+ [query :- ::lib.schema/query]+ (update query :stages+ (fn [stages]+ (into []+ (map-indexed (fn [stage-number _]+ (upgrade-field-refs-in-stage query stage-number)))+ stages))))++(mu/defn- parameter-target-stage-number :- [:maybe :int]+ "Gets the stage number from the parameter target, if it exists and is valid."+ [query :- ::lib.schema/query+ target :- ::lib.schema.parameter/target]+ (let [stage-number (lib/parameter-target-stage-number target)+ stage-count (lib/stage-count query)]+ (when (and (>= stage-number -1) (< stage-number stage-count) (pos-int? stage-count))+ stage-number)))++(mu/defn upgrade-field-ref-in-parameter-mbql-target :- ::lib.schema.parameter/target+ "If the parameter target is a field ref, upgrade it to use a name-based field ref when possible."+ [query :- ::lib.schema/query+ target :- ::lib.schema.parameter/target]+ (or (when (lib/parameter-target-field-ref target)+ (when-let [stage-number (parameter-target-stage-number query target)]+ (lib/update-parameter-target-field-ref+ target+ #(upgrade-field-ref query stage-number %))))+ target))++;;; ------------------------------------------------ swap-source -------------------------------------------------------++(mu/defn- swap-source-table-or-card :- ::lib.schema/stage+ "Swaps the source table or card in a stage if the stage uses the old source."+ [{:keys [source-table source-card], :as stage} :- ::lib.schema/stage+ [old-type old-id] :- ::source-swap.schema/source+ [new-type new-id] :- ::source-swap.schema/source]+ (if (or (and (= old-type :table) (= old-id source-table))+ (and (= old-type :card) (= old-id source-card)))+ (-> stage+ (dissoc :source-table :source-card)+ (assoc (case new-type :table :source-table :card :source-card) new-id))+ stage))++(mu/defn- swap-source-table-or-card-in-stage :- ::lib.schema/stage+ "Swaps the source table or card in a stage."+ [stage :- ::lib.schema/stage+ old-source :- ::source-swap.schema/source+ new-source :- ::source-swap.schema/source]+ (-> (swap-source-table-or-card stage old-source new-source)+ (m/update-existing :joins+ (fn [joins]+ (perf/mapv (fn [join]+ (m/update-existing join :stages+ (fn [stages]+ (perf/mapv #(swap-source-table-or-card-in-stage % old-source new-source) stages))))+ joins)))))++(mu/defn- swap-source-table-or-card-in-query :- ::lib.schema/query+ "Swaps the source table or card in a query."+ [query :- ::lib.schema/query+ old-source :- ::source-swap.schema/source+ new-source :- ::source-swap.schema/source]+ (update query :stages (fn [stages] (perf/mapv #(swap-source-table-or-card-in-stage % old-source new-source) stages))))++(mr/def ::field-id-mapping+ [:map-of ::lib.schema.id/field ::lib.schema.metadata/column])++(mu/defn- build-field-id-mapping :- ::field-id-mapping+ "Builds a mapping of old field IDs to new columns."+ [query :- ::lib.schema/query+ old-source :- ::source-swap.schema/source+ new-source :- ::source-swap.schema/source]+ (let [old-columns (source-swap.util/source-columns query old-source)+ new-columns (source-swap.util/source-columns query new-source)+ new-column-by-key (m/index-by source-swap.util/column-match-key new-columns)]+ (into {}+ (keep (fn [old-column]+ (when-let [new-column (get new-column-by-key (source-swap.util/column-match-key old-column))]+ (when (:id old-column)+ [(:id old-column) new-column]))))+ old-columns)))++(mu/defn- swap-field-ref :- ::lib.schema.ref/ref+ "Swaps a field ref to reference the new source. Assumes that query has been upgraded to use alias-based field refs.++ For ID-based refs:+ - Uses the new field ID when swapping table->table.+ - Uses the new column alias when swapping table->card.++ For implicit joins:+ - Uses the ID of the new column in :source-field.++ Also:+ - Resolves the new field ref and generates a new one based on the resolved column.+ - Always takes a `:field` ref, may return a `:field` ref or an `:expression` ref.+ - Preserves the expression name from the original field ref."+ [query :- ::lib.schema/query+ stage-number :- :int+ field-id-mapping :- ::field-id-mapping+ field-ref :- :mbql.clause/field]+ (let [old-id (lib.ref/field-ref-id field-ref)+ new-id-column (get field-id-mapping old-id)+ old-fk-id (-> field-ref lib.options/options :source-field)+ new-fk-id-column (get field-id-mapping old-fk-id)+ swapped-field-ref (cond-> field-ref+ ;; base-type is required for name-based refs, make sure it's set+ ;; don't use the column name for implicit joins or [[resolve-field-ref]] won't resolve it+ (and new-id-column (not old-fk-id))+ (-> (lib.options/update-options assoc :base-type (:base-type new-id-column))+ (lib.ref/with-field-ref-name (source-swap.util/column-match-key new-id-column)))++ ;; implicit joins FK table field ID+ (and new-fk-id-column (:id new-fk-id-column))+ (lib.options/update-options assoc :source-field (:id new-fk-id-column)))]+ (or (when-let [new-column (lib.field.resolution/resolve-field-ref query stage-number swapped-field-ref)]+ (when-not (::lib.field.resolution/fallback-metadata? new-column)+ (let [new-field-ref (preserve-expression-name field-ref (lib.ref/ref new-column))]+ (when-not (same-ref? field-ref new-field-ref)+ new-field-ref))))+ field-ref)))++(mu/defn- swap-field-refs-in-clauses :- [:sequential :any]+ "Swaps field refs in a list of clauses."+ [query :- ::lib.schema/query+ stage-number :- :int+ field-id-mapping :- ::field-id-mapping+ clauses :- [:sequential :any]+ {:keys [distinct?]} :- [:map [:distinct? :boolean]]]+ (into []+ (cond-> (map (fn [clause]+ (walk-clause-field-refs clause #(swap-field-ref query stage-number field-id-mapping %))))+ distinct? (comp (m/distinct-by lib.schema.util/mbql-clause-distinct-key)))+ clauses))++(mu/defn- swap-field-refs-in-join :- ::lib.schema.join/join+ "Swaps field refs in a join."+ [query :- ::lib.schema/query+ stage-number :- :int+ field-id-mapping :- ::field-id-mapping+ join :- ::lib.schema.join/join]+ (-> join+ (m/update-existing :fields (fn [fields]+ (if (keyword? fields)+ fields+ (swap-field-refs-in-clauses query stage-number field-id-mapping fields {:distinct? true}))))+ (m/update-existing :conditions #(swap-field-refs-in-clauses query stage-number field-id-mapping % {:distinct? false}))))++(mu/defn- swap-field-refs-in-joins :- [:sequential ::lib.schema.join/join]+ "Swaps field refs in a list of joins."+ [query :- ::lib.schema/query+ stage-number :- :int+ field-id-mapping :- ::field-id-mapping+ joins :- [:sequential ::lib.schema.join/join]]+ (perf/mapv #(swap-field-refs-in-join query stage-number field-id-mapping %) joins))++(mu/defn- swap-field-refs-in-stage :- ::lib.schema/stage+ "Swaps field refs in a stage."+ [query :- ::lib.schema/query+ stage-number :- :int+ field-id-mapping :- ::field-id-mapping]+ (-> (lib.util/query-stage query stage-number)+ (m/update-existing :fields #(swap-field-refs-in-clauses query stage-number field-id-mapping % {:distinct? true}))+ (m/update-existing :joins #(swap-field-refs-in-joins query stage-number field-id-mapping %))+ (m/update-existing :expressions #(swap-field-refs-in-clauses query stage-number field-id-mapping % {:distinct? false}))+ (m/update-existing :filters #(swap-field-refs-in-clauses query stage-number field-id-mapping % {:distinct? false}))+ (m/update-existing :aggregation #(swap-field-refs-in-clauses query stage-number field-id-mapping % {:distinct? false}))+ (m/update-existing :breakout #(swap-field-refs-in-clauses query stage-number field-id-mapping % {:distinct? true}))+ (m/update-existing :order-by #(swap-field-refs-in-clauses query stage-number field-id-mapping % {:distinct? true}))))++(mu/defn- swap-field-refs-in-query :- ::lib.schema/query+ "Swaps field refs in a query."+ [query :- ::lib.schema/query+ field-id-mapping :- ::field-id-mapping]+ (m/update-existing query :stages+ (fn [stages]+ (into []+ (map-indexed (fn [stage-number _]+ (swap-field-refs-in-stage query stage-number field-id-mapping)))+ stages))))++(mu/defn swap-source-in-mbql-stages :- ::lib.schema/query+ "Updates the query to use the new source table or card."+ [query :- ::lib.schema/query+ old-source :- ::source-swap.schema/source+ new-source :- ::source-swap.schema/source]+ (swap-field-refs-in-query (swap-source-table-or-card-in-query query old-source new-source)+ (build-field-id-mapping query old-source new-source)))++(mu/defn swap-source-in-parameter-mbql-target :- ::lib.schema.parameter/target+ "If the parameter target is a field ref, swap it to reference the new source."+ [query :- ::lib.schema/query+ target :- ::lib.schema.parameter/target+ old-source :- ::source-swap.schema/source+ new-source :- ::source-swap.schema/source]+ (or (when (lib/parameter-target-field-ref target)+ (when-let [stage-number (parameter-target-stage-number query target)]+ (let [new-query (swap-source-table-or-card-in-query query old-source new-source)+ field-id-mapping (build-field-id-mapping query old-source new-source)]+ (lib/update-parameter-target-field-ref+ target+ #(swap-field-ref new-query stage-number field-id-mapping %)))))+ target))diff --git a/src/metabase/source_swap/native.clj b/src/metabase/source_swap/native.cljnew file mode 100644index 00000000000..b302e719fe6--- /dev/null+++ b/src/metabase/source_swap/native.clj@@ -0,0 +1,480 @@+(ns metabase.source-swap.native+ (:require+ [clojure.string :as str]+ [metabase.driver.sql.util :as sql.u]+ [metabase.lib.core :as lib]+ [metabase.lib.metadata :as lib.metadata]+ [metabase.lib.parameters.parse :as lib.params.parse]+ [metabase.lib.parameters.parse.types :as lib.params.parse.types]+ [metabase.sql-tools.core :as sql-tools]+ ;; sql-tools.init registers multimethod implementations for :macaw parser backend+ [metabase.sql-tools.init]+ [metabase.util :as u]+ [metabase.util.performance :as perf]))++(set! *warn-on-reflection* true)++;;; ====================== Placeholder Helpers for Native SQL ======================+;;;+;;; These functions convert Metabase template tag syntax to/from placeholder syntax+;;; that SQL parsers can handle:+;;; - {{tags}} → __MB_N__ (valid SQL identifiers)+;;; - [[optionals]] → /*__MB_OPT_START__*/.../*__MB_OPT_END__*/ (SQL comments)+;;;+;;; We use unique markers (__MB_OPT_START__, __MB_OPT_END__) to avoid collisions+;;; with user SQL that might contain [[, ]], or similar patterns in comments.+;;;+;;; This allows sql-tools/replace-names to do AST-level table renaming while+;;; preserving the template tags.++(def ^:private opt-start-marker "/*__MB_OPT_START__*/")+(def ^:private opt-end-marker "/*__MB_OPT_END__*/")++(defn- process-tokens+ "Process a sequence of parsed tokens, returning {:sql ... :placeholders ... :idx ...}.+ Recursively handles optional clause contents."+ [tokens initial-idx]+ (loop [tokens tokens+ sql ""+ placeholders {}+ idx initial-idx]+ (if (perf/empty? tokens)+ {:sql sql :placeholders placeholders :idx idx}+ (let [[token & rest-tokens] tokens]+ (if (string? token)+ (recur rest-tokens (str sql token) placeholders idx)+ ;; It's a param or optional+ (case (:lib/type token)+ ::lib.params.parse.types/param+ (let [placeholder (str "__MB_" idx "__")+ original (str "{{" (:k token) "}}")]+ (recur rest-tokens+ (str sql placeholder)+ (assoc placeholders placeholder original)+ (inc idx)))++ ::lib.params.parse.types/optional+ ;; Wrap optional with unique comment markers and recursively process contents+ (let [{inner-sql :sql+ inner-placeholders :placeholders+ new-idx :idx} (process-tokens (:args token) idx)]+ (recur rest-tokens+ (str sql opt-start-marker inner-sql opt-end-marker)+ (merge placeholders inner-placeholders)+ new-idx))++ ;; Unknown token type - skip+ (recur rest-tokens sql placeholders idx)))))))++(defn- sql->placeholders+ "Convert SQL with template tags to SQL with placeholders.+ - Template tags {{x}} become __MB_N__ placeholders+ - Optional clauses [[...]] become /*__MB_OPT_START__*/.../*__MB_OPT_END__*/++ Returns {:sql \"...\" :placeholders {\"__MB_0__\" \"{{x}}\" ...}}"+ [sql]+ (let [parsed (lib.params.parse/parse sql)+ {:keys [sql placeholders]} (process-tokens parsed 0)]+ {:sql sql :placeholders placeholders}))++(def ^:private mb-start-re #"/\*\s*__MB_OPT_START__\s*\*/")+(def ^:private mb-stop-re #"/\*\s*__MB_OPT_END__\s*\*/")++(defn- restore-placeholders+ "Restore original syntax from placeholder SQL.+ - Replaces __MB_N__ with original {{tag}} syntax+ - Replaces /*__MB_OPT_START__*/ and /*__MB_OPT_END__*/ markers back to [[ and ]]+ - Handles spacing variations SQL parsers may introduce in comments"+ [sql placeholders]+ (-> (reduce (fn [s [placeholder original]]+ (str/replace s placeholder original))+ sql+ placeholders)+ ;; Restore optional clause markers (handle spacing variations)+ (str/replace mb-start-re "[[")+ (str/replace mb-stop-re "]]")))++(defn- card-slug+ "Generate a slug for a card name, matching the frontend `slugg` library behavior.+ Collapses consecutive hyphens and trims leading/trailing hyphens."+ [card-name]+ (-> (u/slugify card-name)+ (str/replace "_" "-")+ (str/replace #"-{2,}" "-")+ (str/replace #"^-|-$" "")))++;;; ====================== Table Tag Helpers ======================+;;;+;;; Table tags are `:type :table` template tags like {{my_table}} where+;;; the SQL uses a stable variable name and the tag has a `:table-id`.+;;; Unlike card tags ({{#123}}), the SQL doesn't embed the ID directly.++(defn- find-table-tags+ "Find all template tags of :type :table that reference the given table-id.+ Returns a seq of [tag-key tag-map] pairs."+ [template-tags table-id]+ (filter (fn [[_k v]]+ (and (= (:type v) :table)+ (= (:table-id v) table-id)))+ template-tags))++(defn- replace-tag-in-sql+ "Replace {{old-tag-name}} with {{new-tag-name}} in parsed SQL tokens.+ Returns the reconstructed SQL string."+ [parsed old-tag-name new-tag-name]+ (apply str+ (for [token parsed]+ (cond+ (string? token)+ token++ (lib.params.parse.types/param? token)+ (str "{{" (if (= (:k token) old-tag-name) new-tag-name (:k token)) "}}")++ (lib.params.parse.types/optional? token)+ (str "[[" (replace-tag-in-sql (:args token) old-tag-name new-tag-name) "]]")++ :else+ (str token)))))++(defn- update-table-tags-for-table-swap+ "Update :type :table template tags when swapping table→table.+ Just updates the :table-id field."+ [template-tags old-table-id new-table-id]+ (reduce-kv+ (fn [acc k tag]+ (if (and (= (:type tag) :table)+ (= (:table-id tag) old-table-id))+ (assoc acc k (assoc tag :table-id new-table-id))+ (assoc acc k tag)))+ {}+ template-tags))++;;; ====================== Dimension Tag Helpers ======================+;;;+;;; Dimension tags are `:type :dimension` (field filters) containing+;;; `:dimension [:field <opts> <id>]`. When swapping tables, we need+;;; to remap field IDs to the equivalent fields on the new table.++(defn- update-dimension-tags+ "Update :type :dimension and :type :temporal-unit template tags, remapping field IDs from old table to new table.+ Finds matching fields by name."+ [query template-tags old-table-id new-table-id]+ (let [old-fields (lib.metadata/fields query old-table-id)+ new-fields (lib.metadata/fields query new-table-id)]+ (reduce-kv+ (fn [acc k tag]+ (assoc acc k+ (or (when (#{:dimension :temporal-unit} (:type tag))+ (when-some [field (lib/find-matching-column (:dimension tag) old-fields)]+ (when-some [new-field (perf/some #(when (= (:name %) (:name field)) %) new-fields)]+ (assoc tag :dimension (lib/ref new-field)))))+ tag)))+ {}+ template-tags)))++(defn- update-table-tags-for-card-swap+ "Update :type :table template tags when swapping table→card.+ Changes tag type from :table to :card, updates SQL to use {{#id-slug}} syntax.+ Preserves optional fields like :required and :default from the original tag.+ Returns {:sql new-sql :template-tags new-tags}."+ [sql template-tags old-table-id new-card-id new-card-name]+ (let [table-tags (find-table-tags template-tags old-table-id)+ slug (card-slug new-card-name)+ card-tag-key (str "#" new-card-id "-" slug)]+ (if (perf/empty? table-tags)+ {:sql sql :template-tags template-tags}+ ;; Replace each table tag with a card tag+ (let [;; Replace all matching tag names in SQL+ new-sql (reduce (fn [s [old-tag-name _]]+ (replace-tag-in-sql (lib.params.parse/parse s) old-tag-name card-tag-key))+ sql+ table-tags)+ ;; Get first old tag to preserve its optional fields (:required, :default)+ ;; Note: :id is NOT preserved - new tag gets a new UUID+ [_ first-old-tag] (first table-tags)+ preserved-fields (perf/select-keys first-old-tag [:required :default])+ ;; Update template-tags: remove old table tags, add new card tag+ new-tags (as-> template-tags tags+ ;; Remove old table tags+ (reduce (fn [t [k _]] (dissoc t k)) tags table-tags)+ ;; Add new card tag with preserved fields+ (assoc tags card-tag-key (merge preserved-fields+ {:type :card+ :card-id new-card-id+ :name card-tag-key+ :display-name card-tag-key})))]+ {:sql new-sql :template-tags new-tags}))))++(defn- unquote-template-refs+ "The SQL AST tool quotes identifiers containing special characters. Card template refs+ like {{#123-slug}} get turned into \"{{#123-slug}}\", but they must be unquoted for the+ native query processor to recognize them."+ [sql]+ (-> sql+ (str/replace "\"{{" "{{")+ (str/replace "}}\"" "}}")))++(defn- replace-table-in-native-sql+ "Replace table names in native SQL, preserving template tags.+ Uses sql-tools/replace-names for AST-level table renaming.++ Both old-table and new-table use the same convention:+ - A string like \"ORDERS\" (no schema)+ - A map like {:table \"ORDERS\" :schema \"PUBLIC\"} (with schema)+ - {:table \"ORDERS\"} or {:table \"ORDERS\" :schema nil} (no schema, same as string)++ When old-table has a schema and new-table doesn't, the schema is actively cleared+ from the SQL AST (e.g., `FROM public.orders` → `FROM new_orders`, not+ `FROM public.new_orders`)."+ [driver sql old-table new-table]+ (let [{placeholder-sql :sql+ placeholders :placeholders} (sql->placeholders sql)+ ;; Normalize both to maps+ old-spec (if (string? old-table) {:table old-table} old-table)+ new-spec (if (string? new-table) {:table new-table} new-table)+ old-schema (:schema old-spec)+ new-schema (:schema new-spec)+ ;; When old has a schema but new doesn't, actively clear the schema via {:schema nil}+ ;; so `FROM public.orders` becomes `FROM new_orders` (not `FROM public.new_orders`)+ new-spec (if (and old-schema (not new-schema))+ (assoc new-spec :schema nil)+ new-spec)+ ;; Build replacement entries with both schema-qualified and unqualified keys+ ;; so both `FROM orders` and `FROM public.orders` are matched.+ base-key {:table (:table old-spec)}+ schema-key (cond-> base-key old-schema (assoc :schema old-schema))+ table-entries (cond-> {schema-key new-spec}+ old-schema (assoc base-key new-spec))+ replaced (sql-tools/replace-names+ driver+ placeholder-sql+ {:tables table-entries}+ ;; allow-unused? needed when new-table contains special chars+ ;; like {{#123}} which aren't valid SQL identifiers, and also when+ ;; only one of the two keys (qualified/unqualified) matches.+ {:allow-unused? true})+ ;; The SQL AST tool quotes identifiers with special chars, but card template refs+ ;; like {{#123-slug}} must stay unquoted for the native query processor.+ unquoted (unquote-template-refs replaced)+ restored (restore-placeholders unquoted placeholders)]+ restored))++(defn- replace-table-in-native-query+ "Replace table references in a native query's SQL.+ Handles both:+ - Raw SQL table names (AST-level replacement)+ - Table template tags (:type :table with :table-id)++ Parameters:+ - query: pMBQL query with native stage+ - old-table-id: ID of table to replace+ - new-table-id: ID of replacement table"+ [query old-table-id new-table-id]+ (or (when-let [database (lib.metadata/database query)]+ (when-let [old-table (lib.metadata/table query old-table-id)]+ (when-let [new-table (lib.metadata/table query new-table-id)]+ (let [driver (:engine database)+ sql (lib/raw-native-query query)+ tags (lib/template-tags query)+ old-spec (cond-> {:table (:name old-table)}+ (:schema old-table) (assoc :schema (:schema old-table)))+ new-spec (cond-> {:table (:name new-table)}+ (:schema new-table) (assoc :schema (:schema new-table)))+ new-sql (replace-table-in-native-sql driver sql old-spec new-spec)+ new-tags (update-dimension-tags+ query+ (update-table-tags-for-table-swap tags old-table-id new-table-id)+ old-table-id+ new-table-id)]+ (-> query+ (lib/with-native-query new-sql)+ (lib/with-template-tags new-tags))))))+ query))++(defn- replace-table-with-card-in-native+ "Replace table references in native SQL with a card template tag.+ Handles both:+ - Raw SQL table names → {{#card-id-slug}}+ - Table template tags ({{my_table}}) → {{#card-id-slug}}++ E.g., `FROM orders` → `FROM {{#123-my-card}}`+ E.g., `FROM {{my_table}}` → `FROM {{#123-my-card}}`++ Parameters:+ - query: pMBQL query with native stage+ - old-table-id: ID of table to replace+ - new-card-id: ID of card to reference"+ [query old-table-id new-card-id]+ (or (when-let [database (lib.metadata/database query)]+ (when-let [old-table (lib.metadata/table query old-table-id)]+ (when-let [new-card (lib.metadata/card query new-card-id)]+ (let [driver (:engine database)+ sql (lib/raw-native-query query)+ template-tags (lib/template-tags query)+ slug (card-slug (:name new-card))+ card-tag (str "#" new-card-id "-" slug)+ card-ref (str "{{" card-tag "}}")+ old-spec (cond-> {:table (:name old-table)}+ (:schema old-table) (assoc :schema (:schema old-table)))+ sql-after-raw (replace-table-in-native-sql driver sql old-spec card-ref)+ {:keys [sql template-tags]} (update-table-tags-for-card-swap+ sql-after-raw+ template-tags+ old-table-id+ new-card-id+ (:name new-card))+ new-tag {:type :card+ :card-id new-card-id+ :name card-tag+ :display-name card-tag}+ template-tags (if (contains? template-tags card-tag)+ template-tags+ (assoc template-tags card-tag new-tag))]+ (-> query+ (lib/with-native-query sql)+ (lib/with-template-tags template-tags))))))+ query))++(defn- replace-card-refs-with-table+ "Walk parsed SQL tokens, replacing card references to old-card-id with table-name.+ Returns the reconstructed SQL string."+ [parsed old-card-id table-name]+ (let [old-tag (str "#" old-card-id)]+ (apply str+ (for [token parsed]+ (cond+ (string? token)+ token++ (lib.params.parse.types/param? token)+ (let [k (:k token)]+ (if (or (= k old-tag)+ (str/starts-with? k (str old-tag "-")))+ table-name+ (str "{{" k "}}")))++ (lib.params.parse.types/optional? token)+ (str "[[" (replace-card-refs-with-table (:args token) old-card-id table-name) "]]")++ :else+ (str token))))))++(defn- replace-card-with-table-in-native+ "Replace card template tag reference in native SQL with a direct table reference.+ E.g., `FROM {{#123-my-card}}` → `FROM orders`++ This is the inverse of `replace-table-with-card-in-native`.++ Parameters:+ - query: pMBQL query with native stage+ - old-card-id: ID of card template tag to replace+ - new-table-id: ID of table to reference directly"+ [query old-card-id new-table-id]+ (or (when-let [database (lib.metadata/database query)]+ (when-let [new-table (lib.metadata/table query new-table-id)]+ (let [driver (:engine database)+ sql (lib/raw-native-query query)+ table-ref (if (:schema new-table)+ (sql.u/quote-name driver :table (:schema new-table) (:name new-table))+ (sql.u/quote-name driver :table (:name new-table)))+ parsed (lib.params.parse/parse sql)+ new-sql (replace-card-refs-with-table parsed old-card-id table-ref)]+ ;; with-native-query re-extracts template tags from the SQL;+ ;; since the card ref is gone, its tag is automatically removed.+ (lib/with-native-query query new-sql))))+ query))++(defn- replace-template-tags+ "Replaces references to `old-card-id` with `new-card-id` in a template-tags map.+ Preserves slug format: if old key was `#42-my-query`, new key will be `#99-new-query-name`."+ [tags old-card-id new-card-id new-card-name]+ (let [old-tag (str "#" old-card-id)+ new-slug (when new-card-name+ (card-slug new-card-name))]+ (reduce-kv+ (fn [acc k v]+ (if (= (:card-id v) old-card-id)+ (let [;; Check if old key had a slug (e.g., "#42-my-query" vs "#42")+ had-slug? (str/starts-with? k (str old-tag "-"))+ new-key (if (and had-slug? new-slug)+ (str "#" new-card-id "-" new-slug)+ (str "#" new-card-id))]+ (assoc acc new-key+ (assoc v+ :card-id new-card-id+ :name new-key+ :display-name new-key)))+ (assoc acc k v)))+ {}+ tags)))++(defn- replace-card-refs-in-parsed+ "Walk parsed SQL tokens, replacing card references to old-card-id with new-card-id.+ Returns the reconstructed SQL string.+ Handles card refs with slugs like {{#42-my-query-name}}.+ When a slugged ref is found, generates a new slug from `new-card-name`."+ [parsed old-card-id new-card-id new-card-name]+ (let [old-tag (str "#" old-card-id)+ new-slug (when new-card-name+ (card-slug new-card-name))]+ (apply str+ (for [token parsed]+ (cond+ (string? token)+ token++ (lib.params.parse.types/param? token)+ (let [k (:k token)]+ (cond+ ;; Match exact tag (no slug) -> replace with plain format+ (= k old-tag)+ (str "{{#" new-card-id "}}")++ ;; Match tag with slug suffix (e.g., #42-my-query) -> replace with new slug+ (str/starts-with? k (str old-tag "-"))+ (if new-slug+ (str "{{#" new-card-id "-" new-slug "}}")+ (str "{{#" new-card-id "}}"))++ :else+ (str "{{" k "}}")))++ (lib.params.parse.types/optional? token)+ (str "[[" (replace-card-refs-in-parsed (:args token) old-card-id new-card-id new-card-name) "]]")++ :else+ (str token))))))++(defn- swap-card-in-native-query+ "Pure transformation: replaces references to `old-card-id` with `new-card-id`+ in a native query's SQL text and template-tag map.++ Uses the Metabase parameter parser to properly identify card references,+ handling edge cases like:+ - Card refs with slugs: {{#42-my-query}}+ - Card refs with whitespace: {{ #42 }}+ - Card refs in optional clauses: [[...{{#42}}...]]"+ [query old-card-id new-card-id]+ (or (when-let [new-card (lib.metadata/card query new-card-id)]+ (let [sql (lib/raw-native-query query)+ new-card-name (:name new-card)+ parsed (lib.params.parse/parse sql)+ new-sql (replace-card-refs-in-parsed parsed old-card-id new-card-id new-card-name)+ new-tags (replace-template-tags (lib/template-tags query) old-card-id new-card-id new-card-name)]+ (-> query+ (lib/with-native-query new-sql)+ (lib/with-template-tags new-tags))))+ query))++(defn swap-source-in-native-stages+ "Dispatch native query updates based on old/new source types.+ Handles all four combinations: card→card, table→table, table→card, card→table."+ [query [old-source-type old-source-id] [new-source-type new-source-id]]+ (case [old-source-type new-source-type]+ [:card :card] (swap-card-in-native-query query old-source-id new-source-id)+ [:table :table] (replace-table-in-native-query query old-source-id new-source-id)+ [:table :card] (replace-table-with-card-in-native query old-source-id new-source-id)+ [:card :table] (replace-card-with-table-in-native query old-source-id new-source-id)+ ;; No-op for unknown combinations+ query))diff --git a/src/metabase/source_swap/schema.clj b/src/metabase/source_swap/schema.cljnew file mode 100644index 00000000000..c33eb382198--- /dev/null+++ b/src/metabase/source_swap/schema.clj@@ -0,0 +1,29 @@+(ns metabase.source-swap.schema+ (:require+ [metabase.lib.schema.id :as lib.schema.id]+ [metabase.lib.schema.metadata :as lib.schema.metadata]+ [metabase.util.malli.registry :as mr]))++(set! *warn-on-reflection* true)++(mr/def ::source-type+ [:enum :table :card])++(mr/def ::source-id+ [:or ::lib.schema.id/table ::lib.schema.id/card])++(mr/def ::source+ [:tuple ::source-type ::source-id])++(mr/def ::column-error+ [:enum+ :column-type-mismatch+ :missing-primary-key+ :missing-foreign-key+ :foreign-key-mismatch])++(mr/def ::column-mapping+ [:map+ [:source {:optional true} ::lib.schema.metadata/column]+ [:target {:optional true} ::lib.schema.metadata/column]+ [:errors {:optional true} [:sequential ::column-error]]])diff --git a/src/metabase/source_swap/util.clj b/src/metabase/source_swap/util.cljnew file mode 100644index 00000000000..a2d4136328e--- /dev/null+++ b/src/metabase/source_swap/util.clj@@ -0,0 +1,23 @@+(ns metabase.source-swap.util+ (:require+ [metabase.lib.card :as lib.card]+ [metabase.lib.metadata :as lib.metadata]+ [metabase.lib.metadata.protocols :as lib.metadata.protocols]+ [metabase.lib.schema.metadata :as lib.schema.metadata]+ [metabase.source-swap.schema :as source-swap.schema]+ [metabase.util.malli :as mu]))++(set! *warn-on-reflection* true)++(mu/defn column-match-key :- :string+ "Gets the column match key from a column. This is used to match columns by name or alias."+ [column :- ::lib.schema.metadata/column]+ (or (:lib/desired-column-alias column) (:name column)))++(mu/defn source-columns :- [:sequential ::lib.schema.metadata/column]+ "Get columns for a source. For cards, filters out remapped columns."+ [metadata-providerable :- ::lib.metadata.protocols/metadata-providerable+ [source-type source-id] :- ::source-swap.schema/source]+ (case source-type+ :table (lib.metadata/fields metadata-providerable source-id)+ :card (into [] (remove :remapped-from) (lib.card/saved-question-metadata metadata-providerable source-id))))diff --git a/test/metabase/cmd/copy_test.clj b/test/metabase/cmd/copy_test.cljindex 30b9edc0489..8d00c0473c4 100644--- a/test/metabase/cmd/copy_test.clj+++ b/test/metabase/cmd/copy_test.clj@@ -44,6 +44,7 @@:model/QueryTable:model/RemoteSyncObject:model/RemoteSyncTask+ :model/ReplacementRun:model/SearchIndexMetadata:model/SemanticSearchTokenTracking:model/SupportAccessGrantLogdiff --git a/test/metabase/lib/field/resolution_test.cljc b/test/metabase/lib/field/resolution_test.cljcindex be10e3b3da8..450910af930 100644--- a/test/metabase/lib/field/resolution_test.cljc+++ b/test/metabase/lib/field/resolution_test.cljc@@ -1806,3 +1806,44 @@(testing "ref with :source-field-name should resolve to the column with matching :fk-field-name"(is (= (:lib/desired-column-alias stage-1-renamed-cat)(:lib/source-column-alias (lib.field.resolution/resolve-field-ref query 2 renamed-ref))))))))++(deftest ^:parallel resolve-field-ref-missing-table-previous-stage-metadata-test+ (testing "resolve-field-ref should not throw when the source table is missing from the metadata provider"+ (let [table-id 9999+ mp (lib.tu/mock-metadata-provider+ meta/metadata-provider+ {:tables [{:id table-id :name "ORDERS"}]+ :fields [{:id 1 :table-id table-id :name "ID" :base-type :type/BigInteger}]})+ query (as-> (lib/query mp (lib.metadata/table mp table-id)) q+ (lib/with-fields q (lib/fieldable-columns q))+ (lib/append-stage q)+ (lib/with-fields q (lib/fieldable-columns q)))+ field-ref (lib/ref (first (lib/fieldable-columns query)))+ query (assoc query :lib/metadata meta/metadata-provider)]+ (is (=? {::lib.field.resolution/fallback-metadata? true}+ (lib.field.resolution/resolve-field-ref query -1 field-ref))))))++(deftest ^:parallel resolve-field-ref-wrong-join-alias-missing-card-previous-stage-metadata-test+ (testing "resolve-field-ref should not throw when a join's source-card is missing from the metadata provider"+ (let [orders-with-join (-> (lib/query meta/metadata-provider (meta/table-metadata :orders))+ (lib/join (meta/table-metadata :products)))+ field-ref (lib/ref (m/find-first #(= (:name %) "CATEGORY") (lib/filterable-columns orders-with-join)))+ mp (lib.tu/metadata-provider-with-card-from-query 1 orders-with-join)+ query (-> (lib/query mp (lib.metadata/card mp 1))+ (assoc :lib/metadata meta/metadata-provider))]+ (is (=? {::lib.field.resolution/fallback-metadata? true}+ (lib.field.resolution/resolve-field-ref query -1 field-ref))))))++(deftest ^:parallel resolve-field-ref-missing-parent-field-test+ (testing "resolve-field-ref should not throw when a nested column's parent-id points to a non-existent field"+ (let [mp (lib.tu/mock-metadata-provider+ meta/metadata-provider+ {:fields [{:id 999998+ :table-id (meta/id :orders)+ :name "nested_col"+ :base-type :type/Text+ :parent-id 999999}]})+ query (lib/query mp (meta/table-metadata :orders))+ field-ref (lib/ref (m/find-first #(= (:name %) "nested_col") (lib/fieldable-columns query)))]+ (is (=? {:name "nested_col"}+ (lib.field.resolution/resolve-field-ref query -1 field-ref))))))diff --git a/test/metabase/lib/metadata/calculation_test.cljc b/test/metabase/lib/metadata/calculation_test.cljcindex b3081781f3d..37f464be124 100644--- a/test/metabase/lib/metadata/calculation_test.cljc+++ b/test/metabase/lib/metadata/calculation_test.cljc@@ -134,6 +134,19 @@visible-columns-with-desired-aliases(map :lib/desired-column-alias))))))+(deftest ^:parallel visible-columns-fk-to-deleted-field-test+ (testing "visible-columns doesn't crash when an FK target field no longer exists"+ (let [mp (lib.tu/merged-mock-metadata-provider+ meta/metadata-provider+ {:fields [{:id (meta/id :orders :user-id)+ :fk-target-field-id 999999999}]})+ query (lib/query mp (meta/table-metadata :orders))+ cols (lib.metadata.calculation/visible-columns query)]+ (is (seq (filter #(= (:table-id %) (meta/id :products)) cols))+ "Products implicitly-joinable columns should still be present")+ (is (empty? (filter #(= (:table-id %) (meta/id :people)) cols))+ "People implicitly-joinable columns should be absent since the FK target field doesn't exist"))))+(deftest ^:parallel visible-columns-test-4(testing "multiple aggregations"(let [query (-> (lib/query meta/metadata-provider (meta/table-metadata :orders))diff --git a/test/metabase/lib/order_by_test.cljc b/test/metabase/lib/order_by_test.cljcindex fb09ab5a086..f19866ad94d 100644--- a/test/metabase/lib/order_by_test.cljc+++ b/test/metabase/lib/order_by_test.cljc@@ -7,8 +7,10 @@[metabase.lib.core :as lib][metabase.lib.metadata :as lib.metadata][metabase.lib.metadata.calculation :as lib.metadata.calculation]+ [metabase.lib.options :as lib.options][metabase.lib.order-by :as lib.order-by][metabase.lib.query :as lib.query]+ [metabase.lib.ref :as lib.ref][metabase.lib.test-metadata :as meta][metabase.lib.test-util :as lib.tu][metabase.lib.util :as lib.util]@@ -884,3 +886,14 @@(let [query' (lib.order-by/remove-all-order-bys query)](is (=? {:stages [{:order-by (symbol "nil #_\"key is not present.\"")}]}query')))))++(deftest ^:parallel order-by-duplicate-without-base-type-test+ (testing "order-by ref without :base-type/:effective-type should be detected as duplicate"+ (let [query (lib/query meta/metadata-provider (meta/table-metadata :products))+ category (m/find-first #(= (:name %) "CATEGORY")+ (lib/orderable-columns query))+ a-ref (lib.ref/ref category)+ query' (lib/order-by query a-ref)+ ref-no-types (lib.options/update-options a-ref dissoc :base-type :effective-type)+ query'' (lib/order-by query' ref-no-types)]+ (is (= 1 (count (lib/order-bys query'')))))))diff --git a/test/metabase/lib/parameters/parse_test.cljc b/test/metabase/lib/parameters/parse_test.cljcindex 80112688861..353742e4039 100644--- a/test/metabase/lib/parameters/parse_test.cljc+++ b/test/metabase/lib/parameters/parse_test.cljc@@ -103,6 +103,14 @@"[[{{this}} -- and]] that" [(optional (param "this") " -- and") " that"]"SELECT /* \n --{{foo}} */ {{bar}}" ["SELECT /* \n --{{foo}} */ " (param "bar")]}+ "-- inside {{...}} card ref tags should not trigger comment mode"+ {"SELECT * FROM {{#35885-monthly-revenue--customer---replacement-}}"+ ["SELECT * FROM " (param "#35885-monthly-revenue--customer---replacement-")]}++ "/* */ inside {{...}} card ref tags should not trigger comment mode"+ {"SELECT * FROM {{#1-revenue/*monthly*/}}"+ ["SELECT * FROM " (param "#1-revenue/*monthly*/")]}+"JSON queries that contain non-param fragments like '}}'"{"{x: {y: \"{{param}}\"}}" ["{x: {y: \"" (param "param") "\"}}"]"{$match: {{{date}}, field: 1}}}" ["{$match: {" (param "date") ", field: 1}}}"]}diff --git a/test/metabase/lib/parameters_test.cljc b/test/metabase/lib/parameters_test.cljcindex 94a127a9a16..507142bbcba 100644--- a/test/metabase/lib/parameters_test.cljc+++ b/test/metabase/lib/parameters_test.cljc@@ -1,8 +1,54 @@(ns metabase.lib.parameters-test(:require- [clojure.test :refer [deftest is]]- [metabase.lib.core :as lib]))+ [clojure.test :refer [deftest is testing]]+ [metabase.lib.core :as lib]+ [metabase.lib.ref :as lib.ref]+ [metabase.lib.test-metadata :as meta]))(deftest ^:parallel parameter-target-field-id-test(is (= 256(lib/parameter-target-field-id [:dimension [:field 256 nil]]))))++(deftest ^:parallel parameter-target-stage-number-test+ (let [query (lib/query meta/metadata-provider (meta/table-metadata :orders))+ col (first (lib/filterable-columns query))+ field-ref (lib/->legacy-MBQL (lib.ref/ref col))+ target [:dimension field-ref]]+ (testing "defaults to 0 when no stage-number specified"+ (is (= 0+ (lib/parameter-target-stage-number target))))+ (testing "returns stage-number from dimension options"+ (is (= 0+ (lib/parameter-target-stage-number (conj target {:stage-number 0}))))+ (is (= 2+ (lib/parameter-target-stage-number (conj target {:stage-number 2})))))))++(deftest ^:parallel update-parameter-target-field-ref-test+ (let [query (lib/query meta/metadata-provider (meta/table-metadata :orders))+ col (first (lib/filterable-columns query))+ field-ref (lib/->legacy-MBQL (lib.ref/ref col))+ other-col (second (lib/filterable-columns query))+ other-ref (lib/->legacy-MBQL (lib.ref/ref other-col))]+ (testing "dimension target: replaces the field ref"+ (is (= [:dimension other-ref]+ (lib/update-parameter-target-field-ref+ [:dimension field-ref]+ (fn [_pmbql-ref] (lib.ref/ref other-col))))))+ (testing "dimension target with options: preserves options"+ (is (= [:dimension other-ref {:stage-number 0}]+ (lib/update-parameter-target-field-ref+ [:dimension field-ref {:stage-number 0}]+ (fn [_pmbql-ref] (lib.ref/ref other-col))))))+ (testing "no field ref: returns target unchanged"+ (doseq [target [[:variable [:template-tag "foo"]]+ [:dimension [:template-tag "tag"]]]]+ (is (= target+ (lib/update-parameter-target-field-ref+ target+ (fn [_] (throw (ex-info "should not be called" {}))))))))+ (testing "passes extra args to f"+ (is (= [:dimension other-ref]+ (lib/update-parameter-target-field-ref+ [:dimension field-ref]+ (fn [_pmbql-ref extra-col] (lib.ref/ref extra-col))+ other-col))))))diff --git a/test/metabase/lib/walk/util_test.cljc b/test/metabase/lib/walk/util_test.cljcindex afffca9bbd4..32a0746c863 100644--- a/test/metabase/lib/walk/util_test.cljc+++ b/test/metabase/lib/walk/util_test.cljc@@ -3,6 +3,7 @@[clojure.test :refer [deftest is testing]][metabase.lib.core :as lib][metabase.lib.metadata :as lib.metadata]+ [metabase.lib.query :as lib.query][metabase.lib.test-metadata :as meta][metabase.lib.test-util :as lib.tu][metabase.lib.test-util.macros :as lib.tu.macros]@@ -88,6 +89,18 @@(is (= #{1}(lib/all-source-card-ids query-with-source-card)))))+(deftest ^:parallel all-template-tag-table-ids-test+ (let [query (lib.query/query-with-stages+ meta/metadata-provider+ [{:lib/type :mbql.stage/native+ :native "SELECT * FROM {{orders}}"+ :template-tags {"orders" {:name "orders"+ :display-name "Orders"+ :type :table+ :table-id (meta/id :orders)}}}])]+ (is (= #{(meta/id :orders)}+ (lib.walk.util/all-template-tag-table-ids query)))))+(deftest ^:parallel all-implicitly-joined-field-ids-test(testing "Returns field IDs from implicitly joined tables (fields with :source-field but no :join-alias)"(is (= (lib.tu.macros/$ids nil #{%venues.name %users.name})@@ -227,3 +240,75 @@:joins [{:source-table $$users:alias "U":condition [:= [:field $categories.name {:source-field %venues.category-id}] &U.users.name]}]})))))))++(deftest ^:parallel all-referenced-entity-ids-mbql-test+ (let [card-id 1+ metric-id 100+ segment-id 200+ measure-id 300+ products-query (lib/query meta/metadata-provider (meta/table-metadata :products))+ metric-query (-> (lib/query meta/metadata-provider (meta/table-metadata :orders))+ (lib/aggregate (lib/sum (meta/field-metadata :orders :total))))+ measure-query (-> (lib/query meta/metadata-provider (meta/table-metadata :orders))+ (lib/aggregate (lib/sum (meta/field-metadata :orders :total))))+ segment-query (-> (lib/query meta/metadata-provider (meta/table-metadata :orders))+ (lib/filter (lib/> (meta/field-metadata :orders :total) 100)))+ mp (-> meta/metadata-provider+ (lib.tu/metadata-provider-with-card-from-query card-id products-query)+ (lib.tu/metadata-provider-with-card-from-query metric-id metric-query {:type :metric})+ (lib.tu/mock-metadata-provider+ {:segments [{:id segment-id+ :name "Big orders"+ :table-id (meta/id :orders)+ :definition segment-query}]+ :measures [{:id measure-id+ :name "Sum of totals"+ :table-id (meta/id :orders)+ :definition measure-query}]}))+ query (-> (lib/query mp (meta/table-metadata :orders))+ (lib/join (lib.metadata/card mp card-id))+ (lib/aggregate (lib.metadata/metric mp metric-id))+ (lib/aggregate (lib.metadata/measure mp measure-id))+ (lib/filter (lib.metadata/segment mp segment-id)))]+ (is (= {:table #{(meta/id :orders)}+ :card #{card-id metric-id}+ :metric #{metric-id}+ :measure #{measure-id}+ :segment #{segment-id}+ :snippet #{}}+ (lib/all-referenced-entity-ids [query])))))++(deftest ^:parallel all-referenced-entity-ids-native-test+ (let [card-id 1+ snippet-id 2+ table-id (meta/id :orders)+ card-tag (str "#" card-id "-my-card")+ query (lib.query/query-with-stages+ meta/metadata-provider+ [{:lib/type :mbql.stage/native+ :native (str "SELECT * FROM {{" card-tag "}} INNER JOIN {{table}} ON 1 = 1 WHERE {{category}} AND {{snippet}}")+ :template-tags {card-tag {:name card-tag+ :display-name "My Card"+ :type :card+ :card-id card-id}+ "table" {:name "table"+ :display-name "Orders"+ :type :table+ :table-id table-id}+ "category" {:name "category"+ :display-name "Category"+ :type :dimension+ :dimension [:field (meta/id :products :category) nil]+ :widget-type :string/=}+ "snippet" {:name "snippet"+ :display-name "Snippet"+ :type :snippet+ :snippet-name "my-snippet"+ :snippet-id snippet-id}}}])]+ (is (= {:table #{(meta/id :products) table-id}+ :card #{card-id}+ :metric #{}+ :measure #{}+ :segment #{}+ :snippet #{snippet-id}}+ (lib/all-referenced-entity-ids [query])))))diff --git a/test/metabase/queries/schema_test.clj b/test/metabase/queries/schema_test.cljindex 714858715dc..e273d6d727b 100644--- a/test/metabase/queries/schema_test.clj+++ b/test/metabase/queries/schema_test.clj@@ -3,6 +3,7 @@[clojure.test :refer :all][malli.error :as me][metabase.queries.schema :as queries.schema]+ [metabase.util.log.capture :as log.capture][metabase.util.malli.registry :as mr]))(deftest ^:parallel validate-query-test@@ -14,3 +15,10 @@(deftest ^:parallel set-invalid-metadata-to-nil-test(is (= {:result_metadata nil}(queries.schema/normalize-card {:result_metadata [{}]}))))++(deftest ^:parallel nil-result-metadata-should-not-warn-test+ (log.capture/with-log-messages-for-level [messages [metabase.queries.schema :warn]]+ (is (= {:result_metadata nil}+ (queries.schema/normalize-card {:result_metadata nil})))+ (is (empty? (messages))+ "nil result_metadata should not produce a warning")))diff --git a/test/metabase/search/api_test.clj b/test/metabase/search/api_test.cljindex f46b838dbb6..00b599e486f 100644--- a/test/metabase/search/api_test.clj+++ b/test/metabase/search/api_test.clj@@ -230,8 +230,8 @@:model/Dashboard dashboard (coll-data-map "dashboard %s dashboard" coll):model/Card metric (assoc (coll-data-map "metric %s metric" coll):type :metric)- :model/Segment segment (data-map "segment %s segment")- :model/Measure measure (data-map "measure %s measure")]+ :model/Segment segment (assoc (data-map "segment %s segment") :table_id (mt/id :checkins))+ :model/Measure measure (assoc (data-map "measure %s measure") :table_id (mt/id :checkins))](f {:action action:collection coll:card card@@ -854,7 +854,7 @@:model/Dashboard _ (archived {:name "dashboard test dashboard 2"}):model/Collection _ (archived-collection {:name "collection test collection 2"}):model/Card _ (archived {:name "metric test metric 2" :type :metric})- :model/Segment _ (archived {:name "segment test segment 2"})]+ :model/Segment _ (archived {:name "segment test segment 2" :table_id (mt/id :checkins)})](is (= (default-search-results)(search-request-data :crowberto :q "test")))))))@@ -875,8 +875,8 @@:model/Dashboard _ (archived {:name "dashboard test dashboard"}):model/Collection _ (archived-collection {:name "collection test collection"}):model/Card _ (archived {:name "metric test metric" :type :metric})- :model/Segment _ (archived {:name "segment test segment"})- :model/Measure _ (archived {:name "measure test measure"})]+ :model/Segment _ (archived {:name "segment test segment" :table_id (mt/id :checkins)})+ :model/Measure _ (archived {:name "measure test measure" :table_id (mt/id :checkins)})](is (= (default-archived-results)(search-request-data :crowberto :q "test", :archived "true")))))))@@ -893,8 +893,8 @@:model/Dashboard _ (archived {:name "dashboard test dashboard"}):model/Collection _ (archived-collection {:name "collection test collection"}):model/Card _ (archived {:name "metric test metric" :type :metric})- :model/Segment _ (archived {:name "segment test segment"})- :model/Measure _ (archived {:name "measure test measure"})]+ :model/Segment _ (archived {:name "segment test segment" :table_id (mt/id :checkins)})+ :model/Measure _ (archived {:name "measure test measure" :table_id (mt/id :checkins)})](is (mt/ordered-subset? (default-archived-results)(search-request-data :crowberto :archived "true")))))))diff --git a/test/metabase/source_swap/compatibility_test.clj b/test/metabase/source_swap/compatibility_test.cljnew file mode 100644index 00000000000..cb31c3f42e0--- /dev/null+++ b/test/metabase/source_swap/compatibility_test.clj@@ -0,0 +1,57 @@+(ns metabase.source-swap.compatibility-test+ (:require+ [clojure.test :refer :all]+ [metabase.lib.metadata :as lib.metadata]+ [metabase.lib.test-util :as lib.tu]+ [metabase.source-swap.compatibility :as source-swap.compatibility]))++(set! *warn-on-reflection* true)++(def ^:private mp+ (lib.tu/mock-metadata-provider+ {:database {:id 1 :name "db" :engine :h2}+ :tables [{:id 100 :name "t" :db-id 1}]+ :fields [{:id 1 :table-id 100 :name "PLAIN"+ :base-type :type/Integer :effective-type :type/Integer}+ {:id 2 :table-id 100 :name "DIFFERENT_TYPE"+ :base-type :type/Text :effective-type :type/Text}+ {:id 3 :table-id 100 :name "PK"+ :base-type :type/Integer :effective-type :type/Integer+ :semantic-type :type/PK}+ {:id 4 :table-id 100 :name "FK_A"+ :base-type :type/Integer :effective-type :type/Integer+ :semantic-type :type/FK :fk-target-field-id 999}+ {:id 5 :table-id 100 :name "FK_B"+ :base-type :type/Integer :effective-type :type/Integer+ :semantic-type :type/FK :fk-target-field-id 888}+ {:id 6 :table-id 100 :name "NOT_FK"+ :base-type :type/Integer :effective-type :type/Integer}]}))++(defn- field [id]+ (lib.metadata/field mp id))++(deftest ^:parallel column-errors-no-errors-test+ (testing "should return empty errors for matching columns"+ (is (= []+ (source-swap.compatibility/column-errors (field 1) (field 1) :table :table)))))++(deftest ^:parallel column-errors-type-mismatch-test+ (testing "should detect column type mismatch"+ (is (= [:column-type-mismatch]+ (source-swap.compatibility/column-errors (field 1) (field 2) :table :table)))))++(deftest ^:parallel column-errors-missing-primary-key-test+ (testing "should detect missing primary key for table->table"+ (is (= [:column-type-mismatch :missing-primary-key]+ (source-swap.compatibility/column-errors (field 3) (field 2) :table :table))))+ (testing "should not flag missing primary key for table->card"+ (is (= [:column-type-mismatch]+ (source-swap.compatibility/column-errors (field 3) (field 2) :table :card)))))++(deftest ^:parallel column-errors-foreign-key-test+ (testing "should detect missing foreign key"+ (is (= [:missing-foreign-key]+ (source-swap.compatibility/column-errors (field 4) (field 6) :table :table))))+ (testing "should detect foreign key target mismatch"+ (is (= [:foreign-key-mismatch]+ (source-swap.compatibility/column-errors (field 4) (field 5) :table :table)))))diff --git a/test/metabase/source_swap/core_test.clj b/test/metabase/source_swap/core_test.cljnew file mode 100644index 00000000000..35c4738a6d5--- /dev/null+++ b/test/metabase/source_swap/core_test.clj@@ -0,0 +1,87 @@+(ns metabase.source-swap.core-test+ (:require+ [clojure.test :refer :all]+ [metabase.lib.core :as lib]+ [metabase.lib.metadata :as lib.metadata]+ [metabase.lib.test-metadata :as meta]+ [metabase.lib.test-util :as lib.tu]+ [metabase.source-swap.core :as source-swap]))++(set! *warn-on-reflection* true)++(deftest ^:parallel check-column-mappings-test+ (testing "should match columns by :lib/desired-column-alias (column-match-key)"+ (let [mp (lib.tu/mock-metadata-provider+ {:database {:id 1 :name "db" :engine :h2}+ :cards [{:id 1 :name "old_card" :database-id 1+ :result-metadata+ [{:name "ID" :lib/desired-column-alias "ID"+ :base-type :type/Integer :effective-type :type/Integer}+ {:name "OLD_NAME" :lib/desired-column-alias "T2__ID"+ :base-type :type/Integer :effective-type :type/Integer}+ {:name "SOURCE_ONLY" :lib/desired-column-alias "SOURCE_ONLY"+ :base-type :type/Text :effective-type :type/Text}]}+ {:id 2 :name "new_card" :database-id 1+ :result-metadata+ [{:name "ID" :lib/desired-column-alias "ID"+ :base-type :type/Integer :effective-type :type/Integer}+ {:name "NEW_NAME" :lib/desired-column-alias "T2__ID"+ :base-type :type/Integer :effective-type :type/Integer}+ {:name "TARGET_ONLY" :lib/desired-column-alias "TARGET_ONLY"+ :base-type :type/Text :effective-type :type/Text}]}]})]+ (is (=? [{:source {:name "ID"}, :target {:name "ID"}}+ {:source {:name "OLD_NAME"}, :target {:name "NEW_NAME"}}+ {:source {:name "SOURCE_ONLY"}}+ {:target {:name "TARGET_ONLY"}}]+ (source-swap/check-column-mappings mp [:card 1] [:card 2]))))))++(deftest ^:parallel upgrade-field-ref-mbql-test+ (testing "should upgrade id-based field ref to name-based ref for a card source"+ (let [orders-query (lib/query meta/metadata-provider (meta/table-metadata :orders))+ mp (lib.tu/metadata-provider-with-card-from-query 1 orders-query)+ query (-> (lib/query mp (lib.metadata/card mp 1))+ (lib/with-fields [(meta/field-metadata :orders :id)]))]+ (is (=? [:field {} "ID"]+ (source-swap/upgrade-field-ref query 0+ (lib/ensure-uuid [:field {:base-type :type/BigInteger} (meta/id :orders :id)])))))))++(deftest ^:parallel upgrade-field-ref-native-test+ (testing "should return field ref unchanged for native-only queries"+ (let [query (lib/native-query meta/metadata-provider "SELECT 1")+ field-ref (lib/ensure-uuid [:field {:base-type :type/Integer} 1])]+ (is (= field-ref (source-swap/upgrade-field-ref query 0 field-ref))))))++(deftest ^:parallel upgrade-field-refs-in-query-mbql-test+ (testing "should upgrade all id-based field refs to name-based refs for a card source"+ (let [orders-query (lib/query meta/metadata-provider (meta/table-metadata :orders))+ mp (lib.tu/metadata-provider-with-card-from-query 1 orders-query)+ query (-> (lib/query mp (lib.metadata/card mp 1))+ (lib/with-fields [(meta/field-metadata :orders :id)]))]+ (is (=? {:stages [{:source-card 1+ :fields [[:field {} "ID"]]}]}+ (source-swap/upgrade-field-refs-in-query query))))))++(deftest ^:parallel upgrade-field-refs-in-query-native-test+ (testing "should return query unchanged for native-only queries"+ (let [query (lib/native-query meta/metadata-provider "SELECT 1")]+ (is (= query (source-swap/upgrade-field-refs-in-query query))))))++(deftest ^:parallel swap-source-in-query-mbql-test+ (testing "should swap source table and update field refs to new table"+ (let [query (-> (lib/query meta/metadata-provider (meta/table-metadata :orders))+ (lib/with-fields [(meta/field-metadata :orders :id)]))]+ (is (=? {:stages [{:source-table (meta/id :products)+ :fields [[:field {} (meta/id :products :id)]]}]}+ (source-swap/swap-source-in-query query+ [:table (meta/id :orders)]+ [:table (meta/id :products)]))))))++(deftest ^:parallel swap-source-in-query-native-test+ (testing "should replace card reference in native SQL query text and template tags"+ (let [mp (lib.tu/mock-metadata-provider meta/metadata-provider+ {:cards [{:id 1 :name "Card 1" :database-id (meta/id)}+ {:id 2 :name "Card 2" :database-id (meta/id)}]})+ query (-> (lib/native-query mp "SELECT * FROM {{#1}}")+ (lib/with-template-tags {"#1" {:type :card :card-id 1 :name "#1" :display-name "#1"}}))]+ (is (= "SELECT * FROM {{#2}}"+ (lib/raw-native-query (source-swap/swap-source-in-query query [:card 1] [:card 2])))))))diff --git a/test/metabase/source_swap/mbql_test.clj b/test/metabase/source_swap/mbql_test.cljnew file mode 100644index 00000000000..73fa881d956--- /dev/null+++ b/test/metabase/source_swap/mbql_test.clj@@ -0,0 +1,921 @@+(ns metabase.source-swap.mbql-test+ (:require+ [clojure.test :refer :all]+ [medley.core :as m]+ [metabase.lib.core :as lib]+ [metabase.lib.metadata :as lib.metadata]+ [metabase.lib.schema :as lib.schema]+ [metabase.lib.schema.metadata :as lib.schema.metadata]+ [metabase.lib.test-metadata :as meta]+ [metabase.lib.test-util :as lib.tu]+ [metabase.source-swap.mbql :as source-swap.mbql]+ [metabase.util.malli :as mu]))++(set! *warn-on-reflection* true)++(mu/defn- find-column :- [:maybe ::lib.schema.metadata/column]+ "Find the first column in `cols` whose `:name` matches `col-name`."+ [cols :- [:sequential ::lib.schema.metadata/column]+ col-name :- :string]+ (m/find-first #(= col-name (:name %)) cols))++(deftest ^:parallel swap-source-in-query-table->table-test+ (let [query (-> (lib/query meta/metadata-provider (meta/table-metadata :orders))+ (lib/with-fields [(meta/field-metadata :orders :id)+ (meta/field-metadata :orders :created-at)]))+ upgraded-query (source-swap.mbql/upgrade-field-refs-in-mbql-stages query)+ swapped-query (source-swap.mbql/swap-source-in-mbql-stages upgraded-query+ [:table (meta/id :orders)]+ [:table (meta/id :products)])]+ (testing "should preserve id-based field refs when upgrading"+ (is (=? {:stages [{:source-table (meta/id :orders)+ :fields [[:field {} (meta/id :orders :id)]+ [:field {} (meta/id :orders :created-at)]]}]}+ upgraded-query)))+ (testing "should return an identical query if upgrade is not needed"+ (is (= upgraded-query (source-swap.mbql/upgrade-field-refs-in-mbql-stages upgraded-query))))+ (testing "should preserve id-based field refs when swapping"+ (is (=? {:stages [{:source-table (meta/id :products)+ :fields [[:field {} (meta/id :products :id)]+ [:field {} (meta/id :products :created-at)]]}]}+ swapped-query)))))++(deftest ^:parallel swap-source-in-query-card->card-test+ (let [orders-query (lib/query meta/metadata-provider (meta/table-metadata :orders))+ reviews-query (lib/query meta/metadata-provider (meta/table-metadata :reviews))+ mp (-> meta/metadata-provider+ (lib.tu/metadata-provider-with-card-from-query 1 orders-query)+ (lib.tu/metadata-provider-with-card-from-query 2 reviews-query))+ query (-> (lib/query mp (lib.metadata/card mp 1))+ (lib/with-fields [(meta/field-metadata :orders :id)+ (meta/field-metadata :orders :created-at)]))+ upgraded-query (source-swap.mbql/upgrade-field-refs-in-mbql-stages query)+ swapped-query (source-swap.mbql/swap-source-in-mbql-stages upgraded-query+ [:card 1]+ [:card 2])]+ (testing "should convert id-based field refs to name-based field refs when upgrading"+ (is (=? {:stages [{:source-card 1+ :fields [[:field {} "ID"]+ [:field {} "CREATED_AT"]]}]}+ upgraded-query)))+ (testing "should return an identical query if upgrade is not needed"+ (is (= upgraded-query (source-swap.mbql/upgrade-field-refs-in-mbql-stages upgraded-query))))+ (testing "should change source-card and preserve name-based refs when swapping"+ (is (=? {:stages [{:source-card 2+ :fields [[:field {} "ID"]+ [:field {} "CREATED_AT"]]}]}+ swapped-query)))))++(deftest ^:parallel swap-source-in-query-card->table-test+ (let [orders-query (lib/query meta/metadata-provider (meta/table-metadata :orders))+ mp (lib.tu/metadata-provider-with-card-from-query 1 orders-query)+ query (-> (lib/query mp (lib.metadata/card mp 1))+ (lib/with-fields [(meta/field-metadata :orders :id)+ (meta/field-metadata :orders :created-at)]))+ upgraded-query (source-swap.mbql/upgrade-field-refs-in-mbql-stages query)+ swapped-query (source-swap.mbql/swap-source-in-mbql-stages upgraded-query+ [:card 1]+ [:table (meta/id :reviews)])]+ (testing "should convert id-based field refs to name-based field refs when upgrading"+ (is (=? {:stages [{:source-card 1+ :fields [[:field {} "ID"]+ [:field {} "CREATED_AT"]]}]}+ upgraded-query)))+ (testing "should return an identical query if upgrade is not needed"+ (is (= upgraded-query (source-swap.mbql/upgrade-field-refs-in-mbql-stages upgraded-query))))+ (testing "should swap source-card with source-table and convert name-based refs to id-based refs"+ (is (=? {:stages [{:source-table (meta/id :reviews)+ :fields [[:field {} (meta/id :reviews :id)]+ [:field {} (meta/id :reviews :created-at)]]}]}+ swapped-query)))))++(deftest ^:parallel swap-source-in-query-table->card-test+ (let [reviews-query (lib/query meta/metadata-provider (meta/table-metadata :reviews))+ mp (lib.tu/metadata-provider-with-card-from-query 1 reviews-query)+ query (-> (lib/query mp (meta/table-metadata :orders))+ (lib/with-fields [(meta/field-metadata :orders :id)+ (meta/field-metadata :orders :created-at)]))+ upgraded-query (source-swap.mbql/upgrade-field-refs-in-mbql-stages query)+ swapped-query (source-swap.mbql/swap-source-in-mbql-stages upgraded-query+ [:table (meta/id :orders)]+ [:card 1])]+ (testing "should preserve id-based field refs when upgrading"+ (is (=? {:stages [{:source-table (meta/id :orders)+ :fields [[:field {} (meta/id :orders :id)]+ [:field {} (meta/id :orders :created-at)]]}]}+ upgraded-query)))+ (testing "should return an identical query if upgrade is not needed"+ (is (= upgraded-query (source-swap.mbql/upgrade-field-refs-in-mbql-stages upgraded-query))))+ (testing "should swap source-table with source-card and convert to name-based refs"+ (is (=? {:stages [{:source-card 1+ :fields [[:field {} "ID"]+ [:field {} "CREATED_AT"]]}]}+ swapped-query)))))++(deftest ^:parallel swap-source-in-query-all-clauses-test+ (let [query (-> (lib/query meta/metadata-provider (meta/table-metadata :orders))+ (lib/with-fields [(meta/field-metadata :orders :id)+ (meta/field-metadata :orders :created-at)])+ (lib/join (meta/table-metadata :products))+ (lib/expression "double-id" (lib/+ (meta/field-metadata :orders :id)+ (meta/field-metadata :orders :id)))+ (lib/aggregate (lib/sum (meta/field-metadata :orders :id)))+ (lib/breakout (meta/field-metadata :orders :created-at))+ (lib/order-by (meta/field-metadata :orders :created-at)))+ upgraded-query (source-swap.mbql/upgrade-field-refs-in-mbql-stages query)+ swapped-query (source-swap.mbql/swap-source-in-mbql-stages upgraded-query+ [:table (meta/id :orders)]+ [:table (meta/id :reviews)])]+ (testing "should preserve id-based field refs when upgrading"+ (is (=? {:stages [{:source-table (meta/id :orders)+ :fields [[:field {} (meta/id :orders :id)]+ [:field {} (meta/id :orders :created-at)]+ [:expression {} "double-id"]]+ :joins [{:stages [{:source-table (meta/id :products)}]+ :conditions [[:= {}+ [:field {} (meta/id :orders :product-id)]+ [:field {:join-alias "Products"} (meta/id :products :id)]]]}]+ :expressions [[:+ {:lib/expression-name "double-id"}+ [:field {} (meta/id :orders :id)]+ [:field {} (meta/id :orders :id)]]]+ :aggregation [[:sum {} [:field {} (meta/id :orders :id)]]]+ :breakout [[:field {} (meta/id :orders :created-at)]]+ :order-by [[:asc {} [:field {} (meta/id :orders :created-at)]]]}]}+ upgraded-query)))+ (testing "should swap orders refs to reviews refs and preserve products refs"+ (is (=? {:stages [{:source-table (meta/id :reviews)+ :fields [[:field {} (meta/id :reviews :id)]+ [:field {} (meta/id :reviews :created-at)]+ [:expression {} "double-id"]]+ :joins [{:stages [{:source-table (meta/id :products)}]+ :conditions [[:= {}+ [:field {} (meta/id :reviews :product-id)]+ [:field {:join-alias "Products"} (meta/id :products :id)]]]}]+ :expressions [[:+ {:lib/expression-name "double-id"}+ [:field {} (meta/id :reviews :id)]+ [:field {} (meta/id :reviews :id)]]]+ :aggregation [[:sum {} [:field {} (meta/id :reviews :id)]]]+ :breakout [[:field {} (meta/id :reviews :created-at)]]+ :order-by [[:asc {} [:field {} (meta/id :reviews :created-at)]]]}]}+ swapped-query)))))++(deftest ^:parallel swap-source-in-query-join-filter-table->table-test+ (let [query (as-> (lib/query meta/metadata-provider (meta/table-metadata :products)) q+ (lib/join q (meta/table-metadata :orders))+ (lib/filter q (lib/not-null (find-column (lib/filterable-columns q) "PRODUCT_ID"))))+ upgraded-query (source-swap.mbql/upgrade-field-refs-in-mbql-stages query)+ swapped-query (source-swap.mbql/swap-source-in-mbql-stages upgraded-query+ [:table (meta/id :orders)]+ [:table (meta/id :reviews)])]+ (testing "should preserve id-based field refs when upgrading"+ (is (=? {:stages [{:source-table (meta/id :products)+ :joins [{:stages [{:source-table (meta/id :orders)}]+ :conditions [[:= {}+ [:field {} (meta/id :products :id)]+ [:field {:join-alias "Orders"} (meta/id :orders :product-id)]]]}]+ :filters [[:not-null {} [:field {:join-alias "Orders"} (meta/id :orders :product-id)]]]}]}+ upgraded-query)))+ (testing "should return an identical query if upgrade is not needed"+ (is (= upgraded-query (source-swap.mbql/upgrade-field-refs-in-mbql-stages upgraded-query))))+ (testing "should swap orders to reviews in join and update joined column refs"+ (is (=? {:stages [{:source-table (meta/id :products)+ :joins [{:stages [{:source-table (meta/id :reviews)}]+ :conditions [[:= {}+ [:field {} (meta/id :products :id)]+ [:field {:join-alias "Orders"} (meta/id :reviews :product-id)]]]}]+ :filters [[:not-null {} [:field {:join-alias "Orders"} (meta/id :reviews :product-id)]]]}]}+ swapped-query)))))++(deftest ^:parallel swap-source-in-query-join-filter-table->card-test+ (let [reviews-query (lib/query meta/metadata-provider (meta/table-metadata :reviews))+ mp (lib.tu/metadata-provider-with-card-from-query 1 reviews-query)+ query (as-> (lib/query mp (meta/table-metadata :products)) q+ (lib/join q (meta/table-metadata :orders))+ (lib/filter q (lib/not-null (find-column (lib/filterable-columns q) "PRODUCT_ID"))))+ upgraded-query (source-swap.mbql/upgrade-field-refs-in-mbql-stages query)+ swapped-query (source-swap.mbql/swap-source-in-mbql-stages upgraded-query+ [:table (meta/id :orders)]+ [:card 1])]+ (testing "should preserve id-based field refs when upgrading"+ (is (=? {:stages [{:source-table (meta/id :products)+ :joins [{:stages [{:source-table (meta/id :orders)}]+ :conditions [[:= {}+ [:field {} (meta/id :products :id)]+ [:field {:join-alias "Orders"} (meta/id :orders :product-id)]]]}]+ :filters [[:not-null {} [:field {:join-alias "Orders"} (meta/id :orders :product-id)]]]}]}+ upgraded-query)))+ (testing "should return an identical query if upgrade is not needed"+ (is (= upgraded-query (source-swap.mbql/upgrade-field-refs-in-mbql-stages upgraded-query))))+ (testing "should swap orders join to card with name-based refs and preserve products refs"+ (is (=? {:stages [{:source-table (meta/id :products)+ :joins [{:stages [{:source-card 1}]+ :conditions [[:= {}+ [:field {} (meta/id :products :id)]+ [:field {:join-alias "Orders"} "PRODUCT_ID"]]]}]+ :filters [[:not-null {} [:field {:join-alias "Orders"} "PRODUCT_ID"]]]}]}+ swapped-query)))))++(deftest ^:parallel swap-source-in-query-join-wrong-id-with-alias-test+ (let [query (-> (lib/query meta/metadata-provider (meta/table-metadata :products))+ (lib/join (meta/table-metadata :orders))+ (lib/filter (lib/not-null (lib/ensure-uuid [:field {:base-type :type/Integer+ :join-alias "Orders"}+ (meta/id :reviews :product-id)]))))+ upgraded-query (source-swap.mbql/upgrade-field-refs-in-mbql-stages query)+ swapped-query (source-swap.mbql/swap-source-in-mbql-stages upgraded-query+ [:table (meta/id :orders)]+ [:table (meta/id :reviews)])]+ (testing "upgrade should preserve the wrong field id as-is"+ (is (=? {:stages [{:filters [[:not-null {} [:field {:join-alias "Orders"}+ (meta/id :reviews :product-id)]]]}]}+ upgraded-query)))+ (testing "should return an identical query if upgrade is not needed"+ (is (= upgraded-query (source-swap.mbql/upgrade-field-refs-in-mbql-stages upgraded-query))))+ (testing "should preserve the wrong field id when swapping"+ (is (=? {:stages [{:filters [[:not-null {} [:field {:join-alias "Orders"}+ (meta/id :reviews :product-id)]]]}]}+ swapped-query)))))++(deftest ^:parallel swap-source-in-query-join-wrong-id-without-alias-test+ (let [query (-> (lib/query meta/metadata-provider (meta/table-metadata :products))+ (lib/join (meta/table-metadata :orders))+ (lib/filter (lib/not-null (lib/ensure-uuid [:field {:base-type :type/Integer}+ (meta/id :reviews :product-id)]))))+ upgraded-query (source-swap.mbql/upgrade-field-refs-in-mbql-stages query)+ swapped-query (source-swap.mbql/swap-source-in-mbql-stages upgraded-query+ [:table (meta/id :orders)]+ [:table (meta/id :reviews)])]+ (testing "upgrade should preserve the wrong field id and missing join-alias as-is"+ (is (=? {:stages [{:filters [[:not-null {} [:field (complement :join-alias)+ (meta/id :reviews :product-id)]]]}]}+ upgraded-query)))+ (testing "should return an identical query if upgrade is not needed"+ (is (= upgraded-query (source-swap.mbql/upgrade-field-refs-in-mbql-stages upgraded-query))))+ (testing "swap should resolve the ref and add the correct join-alias"+ (is (=? {:stages [{:filters [[:not-null {} [:field {:join-alias "Orders"}+ (meta/id :reviews :product-id)]]]}]}+ swapped-query)))))++(deftest ^:parallel swap-source-in-query-next-stage-wrong-join-alias-test+ (let [query (-> (lib/query meta/metadata-provider (meta/table-metadata :products))+ (lib/join (meta/table-metadata :orders))+ (lib/append-stage)+ (lib/filter (lib/not-null (lib/ensure-uuid [:field {:base-type :type/DateTimeWithLocalTZ+ :join-alias "Orders"}+ (meta/id :orders :created-at)]))))+ upgraded-query (source-swap.mbql/upgrade-field-refs-in-mbql-stages query)+ swapped-query (source-swap.mbql/swap-source-in-mbql-stages upgraded-query+ [:table (meta/id :orders)]+ [:table (meta/id :reviews)])]+ (testing "upgrade should heal the broken ref to a name-based ref without join-alias"+ (is (=? {:stages [{:source-table (meta/id :products)+ :joins [{:alias "Orders"+ :stages [{:source-table (meta/id :orders)}]}]}+ {:filters [[:not-null {} [:field (complement :join-alias) "Orders__CREATED_AT"]]]}]}+ upgraded-query)))+ (testing "should return an identical query if upgrade is not needed"+ (is (= upgraded-query (source-swap.mbql/upgrade-field-refs-in-mbql-stages upgraded-query))))+ (testing "should preserve second-stage ref and swap join source to reviews"+ (is (=? {:stages [{:source-table (meta/id :products)+ :joins [{:alias "Orders"+ :stages [{:source-table (meta/id :reviews)}]}]}+ {:filters [[:not-null {} [:field (complement :join-alias) "Orders__CREATED_AT"]]]}]}+ swapped-query)))))++(deftest ^:parallel swap-source-in-query-join-missing-alias-test+ (let [query (-> (lib/query meta/metadata-provider (meta/table-metadata :products))+ (lib/join (meta/table-metadata :orders))+ (lib/filter (lib/not-null (lib/ensure-uuid [:field {:base-type :type/Integer}+ (meta/id :orders :product-id)]))))+ upgraded-query (source-swap.mbql/upgrade-field-refs-in-mbql-stages query)+ swapped-query (source-swap.mbql/swap-source-in-mbql-stages upgraded-query+ [:table (meta/id :orders)]+ [:table (meta/id :reviews)])]+ (testing "upgrade should heal the missing join-alias"+ (is (=? {:stages [{:source-table (meta/id :products)+ :filters [[:not-null {} [:field {:join-alias "Orders"}+ (meta/id :orders :product-id)]]]}]}+ upgraded-query)))+ (testing "should return an identical query if upgrade is not needed"+ (is (= upgraded-query (source-swap.mbql/upgrade-field-refs-in-mbql-stages upgraded-query))))+ (testing "should swap joined column ref after healing"+ (is (=? {:stages [{:source-table (meta/id :products)+ :joins [{:stages [{:source-table (meta/id :reviews)}]}]+ :filters [[:not-null {} [:field {:join-alias "Orders"}+ (meta/id :reviews :product-id)]]]}]}+ swapped-query)))))++(deftest ^:parallel swap-source-in-query-join-filter-card->table-test+ (let [reviews-query (lib/query meta/metadata-provider (meta/table-metadata :reviews))+ mp (lib.tu/metadata-provider-with-card-from-query 1 reviews-query)+ query (as-> (lib/query mp (meta/table-metadata :products)) q+ (lib/join q (lib.metadata/card mp 1))+ (lib/filter q (lib/not-null (find-column (lib/filterable-columns q) "PRODUCT_ID"))))+ join-alias (-> query lib/joins first lib/current-join-alias)+ upgraded-query (source-swap.mbql/upgrade-field-refs-in-mbql-stages query)+ swapped-query (source-swap.mbql/swap-source-in-mbql-stages upgraded-query+ [:card 1]+ [:table (meta/id :reviews)])]+ (testing "should preserve id-based base refs and name-based card refs when upgrading"+ (is (=? {:stages [{:source-table (meta/id :products)+ :joins [{:stages [{:source-card 1}]+ :alias join-alias+ :conditions [[:= {}+ [:field {} (meta/id :products :id)]+ [:field {:join-alias join-alias} "PRODUCT_ID"]]]}]+ :filters [[:not-null {} [:field {:join-alias join-alias} "PRODUCT_ID"]]]}]}+ upgraded-query)))+ (testing "should return an identical query if upgrade is not needed"+ (is (= upgraded-query (source-swap.mbql/upgrade-field-refs-in-mbql-stages upgraded-query))))+ (testing "should swap card join to table with id-based refs and preserve products refs"+ (is (=? {:stages [{:source-table (meta/id :products)+ :joins [{:stages [{:source-table (meta/id :reviews)}]+ :alias join-alias+ :conditions [[:= {}+ [:field {} (meta/id :products :id)]+ [:field {:join-alias join-alias} (meta/id :reviews :product-id)]]]}]+ :filters [[:not-null {} [:field {:join-alias join-alias} (meta/id :reviews :product-id)]]]}]}+ swapped-query)))))++(deftest ^:parallel upgrade-field-refs-multiple-identity-expressions-test+ (testing "Multiple identity expressions referencing the same field should all be preserved"+ (let [created-at-id (meta/id :orders :created-at)+ tax-id (meta/id :orders :tax)+ total-id (meta/id :orders :total)+ query (-> (lib/query meta/metadata-provider (meta/table-metadata :orders))+ (lib/with-fields [(meta/field-metadata :orders :tax)+ (meta/field-metadata :orders :total)+ (meta/field-metadata :orders :created-at)])+ (lib/expression "Big Endian Time" (meta/field-metadata :orders :created-at))+ (lib/expression "Date Only Time" (meta/field-metadata :orders :created-at))+ (lib/expression "Tax Rate" (lib// (meta/field-metadata :orders :tax)+ (meta/field-metadata :orders :total)))+ (lib/expression "Original Time Copy" (meta/field-metadata :orders :created-at)))+ upgraded (source-swap.mbql/upgrade-field-refs-in-mbql-stages query)]+ (testing "before upgrade: all four expressions present"+ (is (=? {:stages [{:expressions [[:field {:lib/expression-name "Big Endian Time"} created-at-id]+ [:field {:lib/expression-name "Date Only Time"} created-at-id]+ [:/ {:lib/expression-name "Tax Rate"}+ [:field {} tax-id]+ [:field {} total-id]]+ [:field {:lib/expression-name "Original Time Copy"} created-at-id]]}]}+ query)))+ (testing "after upgrade: all four expressions still present"+ (is (= 4 (count (get-in upgraded [:stages 0 :expressions]))))+ (is (=? {:stages [{:expressions [[:field {:lib/expression-name "Big Endian Time"} created-at-id]+ [:field {:lib/expression-name "Date Only Time"} created-at-id]+ [:/ {:lib/expression-name "Tax Rate"}+ [:field {} tax-id]+ [:field {} total-id]]+ [:field {:lib/expression-name "Original Time Copy"} created-at-id]]}]}+ upgraded))))))++(deftest ^:parallel swap-source-in-query-identity-expression-test+ (let [query (-> (lib/query meta/metadata-provider (meta/table-metadata :orders))+ (lib/expression "created-at-expr" (meta/field-metadata :orders :created-at)))+ upgraded-query (source-swap.mbql/upgrade-field-refs-in-mbql-stages query)+ swapped-query (source-swap.mbql/swap-source-in-mbql-stages upgraded-query+ [:table (meta/id :orders)]+ [:table (meta/id :reviews)])]+ (testing "should preserve id-based ref and :lib/expression-name when upgrading"+ (is (=? {:stages [{:expressions [[:field {:lib/expression-name "created-at-expr"}+ (meta/id :orders :created-at)]]}]}+ upgraded-query)))+ (testing "should return an identical query if upgrade is not needed"+ (is (= upgraded-query (source-swap.mbql/upgrade-field-refs-in-mbql-stages upgraded-query))))+ (testing "should convert to id-based ref and preserve :lib/expression-name when swapping"+ (is (=? {:stages [{:source-table (meta/id :reviews)+ :expressions [[:field {:lib/expression-name "created-at-expr"}+ (meta/id :reviews :created-at)]]}]}+ swapped-query)))))++(deftest ^:parallel swap-source-identity-with-implicit-join-filters-table-test+ (let [query (as-> (lib/query meta/metadata-provider (meta/table-metadata :orders)) q+ (let [cols (filter #(= (:name %) "CREATED_AT") (lib/filterable-columns q))]+ (reduce (fn [q col] (lib/filter q (lib/not-null col))) q cols)))+ upgraded-query (source-swap.mbql/upgrade-field-refs-in-mbql-stages query)+ swapped-query (source-swap.mbql/swap-source-in-mbql-stages upgraded-query+ [:table (meta/id :orders)]+ [:table (meta/id :orders)])]+ (testing "should preserve source-field for implicit joins after upgrade"+ (is (=? {:stages [{:source-table (meta/id :orders)+ :filters [[:not-null {} [:field (complement :source-field) (meta/id :orders :created-at)]]+ [:not-null {} [:field {:source-field (meta/id :orders :user-id)}+ (meta/id :people :created-at)]]+ [:not-null {} [:field {:source-field (meta/id :orders :product-id)}+ (meta/id :products :created-at)]]]}]}+ upgraded-query)))+ (testing "should return an identical query if upgrade is not needed"+ (is (= upgraded-query (source-swap.mbql/upgrade-field-refs-in-mbql-stages upgraded-query))))+ (testing "identity swap should preserve all source-field options"+ (is (= upgraded-query swapped-query)))))++(deftest ^:parallel swap-source-identity-with-implicit-join-filters-card-test+ (let [orders-query (lib/query meta/metadata-provider (meta/table-metadata :orders))+ mp (lib.tu/metadata-provider-with-card-from-query 1 orders-query)+ query (as-> (lib/query mp (lib.metadata/card mp 1)) q+ (let [cols (filter #(= (:name %) "CREATED_AT") (lib/filterable-columns q))]+ (reduce (fn [q col] (lib/filter q (lib/not-null col))) q cols)))+ upgraded-query (source-swap.mbql/upgrade-field-refs-in-mbql-stages query)+ swapped-query (source-swap.mbql/swap-source-in-mbql-stages upgraded-query+ [:card 1]+ [:card 1])]+ (testing "should upgrade card column to name-based ref and preserve implicit join refs"+ (is (=? {:stages [{:source-card 1+ :filters [[:not-null {} [:field (complement :source-field) "CREATED_AT"]]+ [:not-null {} [:field {:source-field (meta/id :orders :product-id)}+ (meta/id :products :created-at)]]+ [:not-null {} [:field {:source-field (meta/id :orders :user-id)}+ (meta/id :people :created-at)]]]}]}+ upgraded-query)))+ (testing "should return an identical query if upgrade is not needed"+ (is (= upgraded-query (source-swap.mbql/upgrade-field-refs-in-mbql-stages upgraded-query))))+ (testing "identity swap should preserve all refs"+ (is (= upgraded-query swapped-query)))))++(deftest ^:parallel swap-source-in-query-implicit-join-test+ (let [query (as-> (lib/query meta/metadata-provider (meta/table-metadata :orders)) q+ (lib/filter q (lib/= (find-column (lib/filterable-columns q) "CATEGORY") "Widget")))+ upgraded-query (source-swap.mbql/upgrade-field-refs-in-mbql-stages query)+ swapped-query (source-swap.mbql/swap-source-in-mbql-stages upgraded-query+ [:table (meta/id :orders)]+ [:table (meta/id :reviews)])]+ (testing "should preserve IDs for implicit join columns when upgrading"+ (is (=? {:stages [{:source-table (meta/id :orders)+ :filters [[:= {}+ [:field {:source-field (meta/id :orders :product-id)}+ (meta/id :products :category)]+ "Widget"]]}]}+ upgraded-query)))+ (testing "should return an identical query if upgrade is not needed"+ (is (= upgraded-query (source-swap.mbql/upgrade-field-refs-in-mbql-stages upgraded-query))))+ (testing "should change source-field when swapping"+ (is (=? {:stages [{:source-table (meta/id :reviews)+ :filters [[:= {}+ [:field {:source-field (meta/id :reviews :product-id)}+ (meta/id :products :category)]+ "Widget"]]}]}+ swapped-query)))))++(deftest ^:parallel swap-source-in-query-implicit-join-table->card-test+ (let [reviews-query (lib/query meta/metadata-provider (meta/table-metadata :reviews))+ mp (lib.tu/metadata-provider-with-card-from-query 1 reviews-query)+ query (as-> (lib/query mp (meta/table-metadata :orders)) q+ (lib/filter q (lib/= (find-column (lib/filterable-columns q) "CATEGORY") "Widget")))+ upgraded-query (source-swap.mbql/upgrade-field-refs-in-mbql-stages query)+ swapped-query (source-swap.mbql/swap-source-in-mbql-stages upgraded-query+ [:table (meta/id :orders)]+ [:card 1])]+ (testing "should preserve IDs for implicit join columns when upgrading"+ (is (=? {:stages [{:source-table (meta/id :orders)+ :filters [[:= {}+ [:field {:source-field (meta/id :orders :product-id)}+ (meta/id :products :category)]+ "Widget"]]}]}+ upgraded-query)))+ (testing "should return an identical query if upgrade is not needed"+ (is (= upgraded-query (source-swap.mbql/upgrade-field-refs-in-mbql-stages upgraded-query))))+ (testing "should swap source-table to source-card and change source-field"+ (is (=? {:stages [{:source-card 1+ :filters [[:= {}+ [:field {:source-field (meta/id :reviews :product-id)}+ (meta/id :products :category)]+ "Widget"]]}]}+ swapped-query)))))++(deftest ^:parallel swap-source-in-query-implicit-join-card-swap-underlying-test+ (let [orders-query (lib/query meta/metadata-provider (meta/table-metadata :orders))+ mp (lib.tu/metadata-provider-with-card-from-query 1 orders-query)+ query (as-> (lib/query mp (lib.metadata/card mp 1)) q+ (lib/filter q (lib/= (find-column (lib/filterable-columns q) "CATEGORY") "Widget")))+ upgraded-query (source-swap.mbql/upgrade-field-refs-in-mbql-stages query)+ swapped-query (source-swap.mbql/swap-source-in-mbql-stages upgraded-query+ [:table (meta/id :orders)]+ [:table (meta/id :reviews)])]+ (testing "should preserve IDs for implicit join columns when upgrading"+ (is (=? {:stages [{:source-card 1+ :filters [[:= {}+ [:field {:source-field (meta/id :orders :product-id)}+ (meta/id :products :category)]+ "Widget"]]}]}+ upgraded-query)))+ (testing "should return an identical query if upgrade is not needed"+ (is (= upgraded-query (source-swap.mbql/upgrade-field-refs-in-mbql-stages upgraded-query))))+ (testing "should keep source-card and only change source-field in the ref"+ (is (=? {:stages [{:source-card 1+ :filters [[:= {}+ [:field {:source-field (meta/id :reviews :product-id)}+ (meta/id :products :category)]+ "Widget"]]}]}+ swapped-query)))))++(deftest ^:parallel swap-source-in-query-implicit-join-card->table-test+ (let [orders-query (lib/query meta/metadata-provider (meta/table-metadata :orders))+ mp (lib.tu/metadata-provider-with-card-from-query 1 orders-query)+ query (as-> (lib/query mp (lib.metadata/card mp 1)) q+ (lib/filter q (lib/= (find-column (lib/filterable-columns q) "CATEGORY") "Widget")))+ upgraded-query (source-swap.mbql/upgrade-field-refs-in-mbql-stages query)+ swapped-query (source-swap.mbql/swap-source-in-mbql-stages upgraded-query+ [:card 1]+ [:table (meta/id :reviews)])]+ (testing "should preserve IDs for implicit join columns when upgrading"+ (is (=? {:stages [{:source-card 1+ :filters [[:= {}+ [:field {:source-field (meta/id :orders :product-id)}+ (meta/id :products :category)]+ "Widget"]]}]}+ upgraded-query)))+ (testing "should return an identical query if upgrade is not needed"+ (is (= upgraded-query (source-swap.mbql/upgrade-field-refs-in-mbql-stages upgraded-query))))+ (testing "should swap source-card to source-table and change source-field"+ (is (=? {:stages [{:source-table (meta/id :reviews)+ :filters [[:= {}+ [:field {:source-field (meta/id :reviews :product-id)}+ (meta/id :products :category)]+ "Widget"]]}]}+ swapped-query)))))++(mu/defn- filter-all-columns :- ::lib.schema/query+ "Add a not-null filter for every filterable column in the query."+ [query :- ::lib.schema/query]+ (reduce (fn [q col] (lib/filter q (lib/not-null col)))+ query+ (lib/filterable-columns query)))++(deftest ^:parallel swap-source-in-query-multi-stage-test+ (let [query (as-> (lib/query meta/metadata-provider (meta/table-metadata :orders)) q+ (lib/aggregate q (lib/sum (meta/field-metadata :orders :id)))+ (lib/breakout q (meta/field-metadata :orders :created-at))+ (lib/append-stage q)+ (filter-all-columns q))+ upgraded-query (source-swap.mbql/upgrade-field-refs-in-mbql-stages query)+ swapped-query (source-swap.mbql/swap-source-in-mbql-stages upgraded-query+ [:table (meta/id :orders)]+ [:table (meta/id :products)])]+ (testing "should preserve id-based refs in first stage and name-based refs in second stage"+ (is (=? {:stages [{:source-table (meta/id :orders)+ :aggregation [[:sum {} [:field {} (meta/id :orders :id)]]]+ :breakout [[:field {} (meta/id :orders :created-at)]]}+ {:filters [[:not-null {} [:field {} "CREATED_AT"]]+ [:not-null {} [:field {} "sum"]]]}]}+ upgraded-query)))+ (testing "should swap first stage to new table id-based refs and preserve name-based refs in second stage"+ (is (=? {:stages [{:source-table (meta/id :products)+ :aggregation [[:sum {} [:field {} (meta/id :products :id)]]]+ :breakout [[:field {} (meta/id :products :created-at)]]}+ {:filters [[:not-null {} [:field {} "CREATED_AT"]]+ [:not-null {} [:field {} "sum"]]]}]}+ swapped-query)))))++(deftest ^:parallel swap-source-in-query-multi-stage-temporal-breakouts-test+ (let [query (as-> (lib/query meta/metadata-provider (meta/table-metadata :orders)) q+ (lib/aggregate q (lib/sum (meta/field-metadata :orders :id)))+ (lib/breakout q (lib/with-temporal-bucket (meta/field-metadata :orders :created-at) :year))+ (lib/breakout q (lib/with-temporal-bucket (meta/field-metadata :orders :created-at) :month))+ (lib/append-stage q)+ (filter-all-columns q))+ upgraded-query (source-swap.mbql/upgrade-field-refs-in-mbql-stages query)+ swapped-query (source-swap.mbql/swap-source-in-mbql-stages upgraded-query+ [:table (meta/id :orders)]+ [:table (meta/id :products)])]+ (testing "should preserve id-based refs in first stage and name-based refs in second stage"+ (is (=? {:stages [{:source-table (meta/id :orders)+ :aggregation [[:sum {} [:field {} (meta/id :orders :id)]]]+ :breakout [[:field {:temporal-unit :year} (meta/id :orders :created-at)]+ [:field {:temporal-unit :month} (meta/id :orders :created-at)]]}+ {:filters [[:not-null {} [:field {} "CREATED_AT"]]+ [:not-null {} [:field {} "CREATED_AT_2"]]+ [:not-null {} [:field {} "sum"]]]}]}+ upgraded-query)))+ (testing "should swap first stage to new table id-based refs and preserve name-based refs in second stage"+ (is (=? {:stages [{:source-table (meta/id :products)+ :aggregation [[:sum {} [:field {} (meta/id :products :id)]]]+ :breakout [[:field {:temporal-unit :year} (meta/id :products :created-at)]+ [:field {:temporal-unit :month} (meta/id :products :created-at)]]}+ {:filters [[:not-null {} [:field {} "CREATED_AT"]]+ [:not-null {} [:field {} "CREATED_AT_2"]]+ [:not-null {} [:field {} "sum"]]]}]}+ swapped-query)))))++(deftest ^:parallel swap-source-in-query-deduplicated-name-test+ (let [query (-> (lib/query meta/metadata-provider (meta/table-metadata :orders))+ (lib/join (meta/table-metadata :products))+ lib/append-stage+ (lib/filter (lib/not-null (lib/ensure-uuid [:field {:base-type :type/BigInteger} "ID_2"]))))+ upgraded-query (source-swap.mbql/upgrade-field-refs-in-mbql-stages query)+ swapped-query (source-swap.mbql/swap-source-in-mbql-stages upgraded-query+ [:table (meta/id :orders)]+ [:table (meta/id :reviews)])]+ (testing "should upgrade second stage deduplicated ref to canonical name"+ (is (=? {:stages [{:source-table (meta/id :orders)}+ {:filters [[:not-null {} [:field {} "Products__ID"]]]}]}+ upgraded-query)))+ (testing "should return an identical query if upgrade is not needed"+ (is (= upgraded-query (source-swap.mbql/upgrade-field-refs-in-mbql-stages upgraded-query))))+ (testing "should swap orders refs to reviews refs and preserve deduplicated names"+ (is (=? {:stages [{:source-table (meta/id :reviews)}+ {:filters [[:not-null {} [:field {} "Products__ID"]]]}]}+ swapped-query)))))++(deftest ^:parallel swap-source-in-query-nonexistent-source-field-test+ (let [field-ref (lib/ensure-uuid [:field {:base-type :type/Text+ :source-field Integer/MAX_VALUE}+ (meta/id :products :category)])+ query (-> (lib/query meta/metadata-provider (meta/table-metadata :orders))+ (lib/filter (lib/ensure-uuid [:= {} field-ref "Widget"])))+ upgraded-query (source-swap.mbql/upgrade-field-refs-in-mbql-stages query)+ swapped-query (source-swap.mbql/swap-source-in-mbql-stages upgraded-query+ [:table (meta/id :orders)]+ [:table (meta/id :reviews)])]+ (testing "should preserve non-existent source-field when upgrading"+ (is (=? {:stages [{:filters [[:= {}+ [:field {:source-field Integer/MAX_VALUE}+ (meta/id :products :category)]+ "Widget"]]}]}+ upgraded-query)))+ (testing "should return an identical query if upgrade is not needed"+ (is (= upgraded-query (source-swap.mbql/upgrade-field-refs-in-mbql-stages upgraded-query))))+ (testing "should preserve non-existent source-field when swapping"+ (is (=? {:stages [{:source-table (meta/id :reviews)+ :filters [[:= {}+ [:field {:source-field Integer/MAX_VALUE}+ (meta/id :products :category)]+ "Widget"]]}]}+ swapped-query)))))++(deftest ^:parallel swap-source-in-query-field-id-wrong-table-test+ (let [field-ref (lib/ensure-uuid [:field {:base-type :type/DateTimeWithLocalTZ}+ (meta/id :people :created-at)])+ query (-> (lib/query meta/metadata-provider (meta/table-metadata :products))+ (lib/filter (lib/not-null field-ref)))+ upgraded-query (source-swap.mbql/upgrade-field-refs-in-mbql-stages query)+ swapped-query (source-swap.mbql/swap-source-in-mbql-stages upgraded-query+ [:table (meta/id :products)]+ [:table (meta/id :orders)])]+ (testing "should preserve the wrong table field id when upgrading"+ (is (=? {:stages [{:source-table (meta/id :products)+ :filters [[:not-null {} [:field {} (meta/id :people :created-at)]]]}]}+ upgraded-query)))+ (testing "should return an identical query if upgrade is not needed"+ (is (= upgraded-query (source-swap.mbql/upgrade-field-refs-in-mbql-stages upgraded-query))))+ (testing "should preserve the wrong table field id when swapping"+ (is (=? {:stages [{:source-table (meta/id :orders)+ :filters [[:not-null {} [:field {} (meta/id :people :created-at)]]]}]}+ swapped-query)))))++(deftest ^:parallel swap-source-in-query-nonexistent-field-id-test+ (let [query (-> (lib/query meta/metadata-provider (meta/table-metadata :orders))+ (lib/with-fields [(lib/ensure-uuid [:field {:base-type :type/Integer} Integer/MAX_VALUE])]))+ upgraded-query (source-swap.mbql/upgrade-field-refs-in-mbql-stages query)+ swapped-query (source-swap.mbql/swap-source-in-mbql-stages upgraded-query+ [:table (meta/id :orders)]+ [:table (meta/id :reviews)])]+ (testing "should preserve non-existent field id when upgrading"+ (is (=? {:stages [{:source-table (meta/id :orders)+ :fields [[:field {} Integer/MAX_VALUE]]}]}+ upgraded-query)))+ (testing "should return an identical query if upgrade is not needed"+ (is (= upgraded-query (source-swap.mbql/upgrade-field-refs-in-mbql-stages upgraded-query))))+ (testing "should preserve non-existent field id when swapping"+ (is (=? {:stages [{:source-table (meta/id :reviews)+ :fields [[:field {} Integer/MAX_VALUE]]}]}+ swapped-query)))))++(deftest ^:parallel swap-source-in-query-nonexistent-field-name-test+ (let [query (-> (lib/query meta/metadata-provider (meta/table-metadata :orders))+ (lib/with-fields [(lib/ensure-uuid [:field {:base-type :type/Text} "DOES_NOT_EXIST"])]))+ upgraded-query (source-swap.mbql/upgrade-field-refs-in-mbql-stages query)+ swapped-query (source-swap.mbql/swap-source-in-mbql-stages upgraded-query+ [:table (meta/id :orders)]+ [:table (meta/id :reviews)])]+ (testing "should preserve non-existent field name when upgrading"+ (is (=? {:stages [{:source-table (meta/id :orders)+ :fields [[:field {} "DOES_NOT_EXIST"]]}]}+ upgraded-query)))+ (testing "should return an identical query if upgrade is not needed"+ (is (= upgraded-query (source-swap.mbql/upgrade-field-refs-in-mbql-stages upgraded-query))))+ (testing "should preserve non-existent field name when swapping"+ (is (=? {:stages [{:source-table (meta/id :reviews)+ :fields [[:field {} "DOES_NOT_EXIST"]]}]}+ swapped-query)))))++(deftest ^:parallel swap-source-in-query-field-ref-without-base-type-test+ (let [query (-> (lib/query meta/metadata-provider (meta/table-metadata :orders))+ (lib/filter (lib/not-null (lib/ensure-uuid [:field {} (meta/id :orders :id)]))))+ upgraded-query (source-swap.mbql/upgrade-field-refs-in-mbql-stages query)+ swapped-query (source-swap.mbql/swap-source-in-mbql-stages upgraded-query+ [:table (meta/id :orders)]+ [:table (meta/id :products)])]+ (testing "upgrade should not modify the query when base-type is missing"+ (is (= query upgraded-query)))+ (testing "should swap the field id to the new table"+ (is (=? {:stages [{:source-table (meta/id :products)+ :filters [[:not-null {} [:field {} (meta/id :products :id)]]]}]}+ swapped-query)))))++(deftest ^:parallel swap-source-in-parameter-target-implicit-join-test+ (let [query (lib/query meta/metadata-provider (meta/table-metadata :orders))+ category-col (find-column (lib/filterable-columns query) "CATEGORY")+ target [:dimension (lib/->legacy-MBQL (lib/ref category-col))]+ upgraded-target (source-swap.mbql/upgrade-field-ref-in-parameter-mbql-target query target)+ swapped-target (source-swap.mbql/swap-source-in-parameter-mbql-target query target+ [:table (meta/id :orders)]+ [:table (meta/id :reviews)])]+ (testing "should not change implicit join target when upgrading"+ (is (=? [:dimension [:field (meta/id :products :category) {:source-field (meta/id :orders :product-id)}]]+ upgraded-target)))+ (testing "should return an identical target if upgrade is not needed"+ (is (= upgraded-target (source-swap.mbql/upgrade-field-ref-in-parameter-mbql-target query upgraded-target))))+ (testing "should swap :source-field to reviews.product-id"+ (is (=? [:dimension [:field (meta/id :products :category) {:source-field (meta/id :reviews :product-id)}]]+ swapped-target)))))++(deftest ^:parallel swap-source-in-parameter-target-card-implicit-join-test+ (let [orders-query (lib/query meta/metadata-provider (meta/table-metadata :orders))+ mp (lib.tu/metadata-provider-with-card-from-query 1 orders-query)+ query (lib/query mp (lib.metadata/card mp 1))+ category-col (find-column (lib/filterable-columns query) "CATEGORY")+ target [:dimension (lib/->legacy-MBQL (lib/ref category-col))]+ upgraded-target (source-swap.mbql/upgrade-field-ref-in-parameter-mbql-target query target)+ swapped-target (source-swap.mbql/swap-source-in-parameter-mbql-target query target+ [:card 1]+ [:table (meta/id :reviews)])]+ (testing "should not change implicit join target when upgrading"+ (is (=? [:dimension [:field (meta/id :products :category) {:source-field (meta/id :orders :product-id)}]]+ upgraded-target)))+ (testing "should return an identical target if upgrade is not needed"+ (is (= upgraded-target (source-swap.mbql/upgrade-field-ref-in-parameter-mbql-target query upgraded-target))))+ (testing "should swap :source-field to reviews.product-id"+ (is (=? [:dimension [:field (meta/id :products :category) {:source-field (meta/id :reviews :product-id)}]]+ swapped-target)))))++(deftest ^:parallel swap-source-in-parameter-target-table->card-implicit-join-test+ (let [reviews-query (lib/query meta/metadata-provider (meta/table-metadata :reviews))+ mp (lib.tu/metadata-provider-with-card-from-query 1 reviews-query)+ query (lib/query mp (meta/table-metadata :orders))+ category-col (find-column (lib/filterable-columns query) "CATEGORY")+ target [:dimension (lib/->legacy-MBQL (lib/ref category-col))]+ upgraded-target (source-swap.mbql/upgrade-field-ref-in-parameter-mbql-target query target)+ swapped-target (source-swap.mbql/swap-source-in-parameter-mbql-target query target+ [:table (meta/id :orders)]+ [:card 1])]+ (testing "should not change implicit join target when upgrading"+ (is (=? [:dimension [:field (meta/id :products :category) {:source-field (meta/id :orders :product-id)}]]+ upgraded-target)))+ (testing "should return an identical target if upgrade is not needed"+ (is (= upgraded-target (source-swap.mbql/upgrade-field-ref-in-parameter-mbql-target query upgraded-target))))+ (testing "should swap :source-field to reviews.product-id"+ (is (=? [:dimension [:field (meta/id :products :category) {:source-field (meta/id :reviews :product-id)}]]+ swapped-target)))))++(deftest ^:parallel swap-source-in-parameter-target-card->table-implicit-join-test+ (let [orders-query (lib/query meta/metadata-provider (meta/table-metadata :orders))+ mp (lib.tu/metadata-provider-with-card-from-query 1 orders-query)+ query (lib/query mp (lib.metadata/card mp 1))+ category-col (find-column (lib/filterable-columns query) "CATEGORY")+ target [:dimension (lib/->legacy-MBQL (lib/ref category-col))]+ upgraded-target (source-swap.mbql/upgrade-field-ref-in-parameter-mbql-target query target)+ swapped-target (source-swap.mbql/swap-source-in-parameter-mbql-target query target+ [:card 1]+ [:table (meta/id :reviews)])]+ (testing "should not change implicit join target when upgrading"+ (is (=? [:dimension [:field (meta/id :products :category) {:source-field (meta/id :orders :product-id)}]]+ upgraded-target)))+ (testing "should return an identical target if upgrade is not needed"+ (is (= upgraded-target (source-swap.mbql/upgrade-field-ref-in-parameter-mbql-target query upgraded-target))))+ (testing "should swap :source-field to reviews.product-id"+ (is (=? [:dimension [:field (meta/id :products :category) {:source-field (meta/id :reviews :product-id)}]]+ swapped-target)))))++(deftest ^:parallel upgrade-deduplicates-all-clauses-test+ (let [orders-query (lib/query meta/metadata-provider (meta/table-metadata :orders))+ products-query (lib/query meta/metadata-provider (meta/table-metadata :products))+ reviews-query (lib/query meta/metadata-provider (meta/table-metadata :reviews))+ mp (-> meta/metadata-provider+ (lib.tu/metadata-provider-with-card-from-query 1 orders-query)+ (lib.tu/metadata-provider-with-card-from-query 2 products-query)+ (lib.tu/metadata-provider-with-card-from-query 3 reviews-query))+ query (as-> (lib/query mp (lib.metadata/card mp 1)) q+ (lib/with-fields q [(meta/field-metadata :orders :created-at)+ (find-column (lib/fieldable-columns q) "CREATED_AT")])+ (lib/join q (-> (lib/join-clause (lib.metadata/card mp 2))+ (lib/with-join-fields [(meta/field-metadata :products :id)+ (find-column (lib/join-condition-rhs-columns q (lib.metadata/card mp 2) nil nil) "ID")])))+ (lib/expression q "expr_1" (lib/+ (meta/field-metadata :orders :id)+ (find-column (lib/expressionable-columns q nil) "ID")))+ (lib/expression q "expr_2" (lib/+ (meta/field-metadata :orders :id)+ (find-column (lib/expressionable-columns q nil) "ID")))+ (lib/filter q (lib/not-null (meta/field-metadata :orders :created-at)))+ (lib/filter q (lib/not-null (find-column (lib/filterable-columns q) "CREATED_AT")))+ (lib/aggregate q (lib/count))+ (lib/aggregate q (lib/count))+ (lib/breakout q (meta/field-metadata :orders :created-at))+ (lib/breakout q (find-column (lib/breakoutable-columns q) "CREATED_AT"))+ (lib/order-by q (meta/field-metadata :orders :created-at))+ (lib/order-by q (find-column (lib/orderable-columns q) "CREATED_AT")))+ join-alias (-> query lib/joins first lib/current-join-alias)+ upgraded-query (source-swap.mbql/upgrade-field-refs-in-mbql-stages query)+ swapped-query (source-swap.mbql/swap-source-in-mbql-stages upgraded-query+ [:card 1]+ [:card 3])]+ (testing "before upgrade: 2 entries in each clause differing only in ref style"+ (is (=? {:stages [{:fields [[:field {} (meta/id :orders :created-at)]+ [:field {} "CREATED_AT"]+ [:expression {} "expr_1"]+ [:expression {} "expr_2"]]+ :joins [{:fields [[:field {:join-alias join-alias} (meta/id :products :id)]+ [:field {:join-alias join-alias} "ID"]]}]+ :expressions [[:+ {:lib/expression-name "expr_1"}+ [:field {} (meta/id :orders :id)]+ [:field {} "ID"]]+ [:+ {:lib/expression-name "expr_2"}+ [:field {} (meta/id :orders :id)]+ [:field {} "ID"]]]+ :filters [[:not-null {} [:field {} (meta/id :orders :created-at)]]+ [:not-null {} [:field {} "CREATED_AT"]]]+ :aggregation [[:count {}] [:count {}]]+ :breakout [[:field {} (meta/id :orders :created-at)]+ [:field {} "CREATED_AT"]]+ :order-by [[:asc {} [:field {} (meta/id :orders :created-at)]]+ [:asc {} [:field {} "CREATED_AT"]]]}]}+ query)))+ (testing "upgrade deduplicates :fields, :breakout, :order-by, but not :expressions, :filters, or :aggregation"+ (is (=? {:stages [{:source-card 1+ :fields [[:field {} "CREATED_AT"]+ [:expression {} "expr_1"]+ [:expression {} "expr_2"]]+ :joins [{:stages [{:source-card 2}]+ :fields [[:field {:join-alias join-alias} "ID"]]}]+ :expressions [[:+ {:lib/expression-name "expr_1"}+ [:field {} "ID"]+ [:field {} "ID"]]+ [:+ {:lib/expression-name "expr_2"}+ [:field {} "ID"]+ [:field {} "ID"]]]+ :filters [[:not-null {} [:field {} "CREATED_AT"]]+ [:not-null {} [:field {} "CREATED_AT"]]]+ :aggregation [[:count {}] [:count {}]]+ :breakout [[:field {} "CREATED_AT"]]+ :order-by [[:asc {} [:field {} "CREATED_AT"]]]}]}+ upgraded-query)))+ (testing "swap to reviews card preserves name-based refs"+ (is (=? {:stages [{:source-card 3+ :fields [[:field {} "CREATED_AT"]+ [:expression {} "expr_1"]+ [:expression {} "expr_2"]]+ :joins [{:stages [{:source-card 2}]+ :fields [[:field {:join-alias join-alias} "ID"]]}]+ :expressions [[:+ {:lib/expression-name "expr_1"}+ [:field {} "ID"]+ [:field {} "ID"]]+ [:+ {:lib/expression-name "expr_2"}+ [:field {} "ID"]+ [:field {} "ID"]]]+ :filters [[:not-null {} [:field {} "CREATED_AT"]]+ [:not-null {} [:field {} "CREATED_AT"]]]+ :aggregation [[:count {}] [:count {}]]+ :breakout [[:field {} "CREATED_AT"]]+ :order-by [[:asc {} [:field {} "CREATED_AT"]]]}]}+ swapped-query)))))++(deftest ^:parallel swap-source-in-query-field-ref-to-expression-test+ (let [query (-> (lib/query meta/metadata-provider (meta/table-metadata :orders))+ (lib/expression "expr" (lib/+ (meta/field-metadata :orders :id) 1))+ (lib/filter (lib/not-null (lib/ensure-uuid [:field {:base-type :type/Integer} "expr"]))))+ upgraded-query (source-swap.mbql/upgrade-field-refs-in-mbql-stages query)+ swapped-query (source-swap.mbql/swap-source-in-mbql-stages upgraded-query+ [:table (meta/id :orders)]+ [:table (meta/id :products)])]+ (testing "upgrade should convert :field ref pointing to expression into :expression ref"+ (is (=? {:stages [{:source-table (meta/id :orders)+ :expressions [[:+ {:lib/expression-name "expr"}+ [:field {} (meta/id :orders :id)]+ 1]]+ :filters [[:not-null {} [:expression {} "expr"]]]}]}+ upgraded-query)))+ (testing "should return an identical query if upgrade is not needed"+ (is (= upgraded-query (source-swap.mbql/upgrade-field-refs-in-mbql-stages upgraded-query))))+ (testing "should swap source to products and update field refs inside expression"+ (is (=? {:stages [{:source-table (meta/id :products)+ :expressions [[:+ {:lib/expression-name "expr"}+ [:field {} (meta/id :products :id)]+ 1]]+ :filters [[:not-null {} [:expression {} "expr"]]]}]}+ swapped-query)))))++(deftest ^:parallel upgrade-field-ref-in-parameter-target-expression-test+ (let [query (-> (lib/query meta/metadata-provider (meta/table-metadata :orders))+ (lib/expression "expr" (lib/+ (meta/field-metadata :orders :id) 1)))+ target [:dimension (lib/->legacy-MBQL (lib/ensure-uuid [:field {:base-type :type/Integer} "expr"]))]+ upgraded-target (source-swap.mbql/upgrade-field-ref-in-parameter-mbql-target query target)+ swapped-target (source-swap.mbql/swap-source-in-parameter-mbql-target query target+ [:table (meta/id :orders)]+ [:table (meta/id :products)])]+ (testing "upgrade should convert :field ref pointing to expression into :expression ref"+ (is (=? [:dimension [:expression "expr" {:base-type :type/Integer}]]+ upgraded-target)))+ (testing "should return an identical target if upgrade is not needed"+ (is (= upgraded-target (source-swap.mbql/upgrade-field-ref-in-parameter-mbql-target query upgraded-target))))+ (testing "swap should also convert the :field ref pointing to expression"+ (is (=? [:dimension [:expression "expr" {:base-type :type/Integer}]]+ swapped-target)))))diff --git a/test/metabase/source_swap/native_test.clj b/test/metabase/source_swap/native_test.cljnew file mode 100644index 00000000000..305c8a75b25--- /dev/null+++ b/test/metabase/source_swap/native_test.clj@@ -0,0 +1,469 @@+(ns metabase.source-swap.native-test+ (:require+ [clojure.string :as str]+ [clojure.test :refer [deftest is testing]]+ [metabase.lib.core :as lib]+ [metabase.lib.metadata :as lib.metadata]+ [metabase.lib.test-metadata :as meta]+ [metabase.lib.test-util :as lib.tu]+ [metabase.source-swap.native :as source-swap.native]))++(defn- field-id-ref+ [mp field-id]+ (lib/ref (lib.metadata/field mp field-id)))++(defn- metadata-provider-with-cards []+ (lib.tu/mock-metadata-provider meta/metadata-provider+ {:cards [{:id 1 :name "Card 1" :database-id (meta/id)}+ {:id 2 :name "Card 2" :database-id (meta/id)}+ {:id 3 :name "Card 3" :database-id (meta/id)}]}))++;;; ----------------------------------------- swap-card-in-native-query (pure) ------------------------------------------++(deftest ^:parallel swap-card-in-native-query-basic-test+ (let [mp (metadata-provider-with-cards)]+ (testing "Simple card tag replacement"+ (let [query (-> (lib/native-query mp "SELECT * FROM {{#1}}")+ (lib/with-template-tags {"#1" {:type :card :card-id 1 :name "#1" :display-name "#1"}}))+ result (source-swap.native/swap-source-in-native-stages query [:card 1] [:card 2])]+ (is (= "SELECT * FROM {{#2}}"+ (lib/raw-native-query result)))+ (is (= 2 (get-in (lib/template-tags result) ["#2" :card-id])))))++ (testing "Multiple card tags, only matching one is replaced"+ (let [query (-> (lib/native-query mp "SELECT * FROM {{#1}} JOIN {{#3}} ON 1=1")+ (lib/with-template-tags {"#1" {:type :card :card-id 1 :name "#1" :display-name "#1"}+ "#3" {:type :card :card-id 3 :name "#3" :display-name "#3"}}))+ result (source-swap.native/swap-source-in-native-stages query [:card 1] [:card 2])]+ (is (= "SELECT * FROM {{#2}} JOIN {{#3}} ON 1=1"+ (lib/raw-native-query result)))+ (is (= 2 (get-in (lib/template-tags result) ["#2" :card-id])))+ (is (= 3 (get-in (lib/template-tags result) ["#3" :card-id])))))))++(deftest ^:parallel swap-card-in-native-query-with-field-filters-test+ (testing "Card tag with field filter tags present"+ (let [mp (metadata-provider-with-cards)+ query (-> (lib/native-query mp "SELECT * FROM {{#1}} WHERE {{created_at}}")+ (lib/with-template-tags {"#1" {:type :card :card-id 1 :name "#1" :display-name "#1"}+ "created_at" {:type :dimension :name "created_at" :display-name "Created At" :widget-type :date/single}}))+ result (source-swap.native/swap-source-in-native-stages query [:card 1] [:card 2])]+ (is (= "SELECT * FROM {{#2}} WHERE {{created_at}}"+ (lib/raw-native-query result)))+ (is (= 2 (get-in (lib/template-tags result) ["#2" :card-id])))+ (is (= "created_at"+ (get-in (lib/template-tags result) ["created_at" :name]))))))++(deftest ^:parallel swap-card-in-native-query-with-optional-clauses-test+ (let [mp (metadata-provider-with-cards)]+ (testing "Card tag with optional clause containing field filter"+ (let [query (-> (lib/native-query mp "SELECT * FROM {{#1}} [[WHERE {{created_at}}]]")+ (lib/with-template-tags {"#1" {:type :card :card-id 1 :name "#1" :display-name "#1"}+ "created_at" {:type :dimension :name "created_at" :display-name "Created At" :widget-type :date/single}}))+ result (source-swap.native/swap-source-in-native-stages query [:card 1] [:card 2])]+ (is (= "SELECT * FROM {{#2}} [[WHERE {{created_at}}]]"+ (lib/raw-native-query result)))+ (is (= 2 (get-in (lib/template-tags result) ["#2" :card-id])))))++ (testing "Card tag inside optional clause"+ (let [query (-> (lib/native-query mp "SELECT * FROM foo [[JOIN {{#1}} ON 1=1]]")+ (lib/with-template-tags {"#1" {:type :card :card-id 1 :name "#1" :display-name "#1"}}))+ result (source-swap.native/swap-source-in-native-stages query [:card 1] [:card 2])]+ (is (= "SELECT * FROM foo [[JOIN {{#2}} ON 1=1]]"+ (lib/raw-native-query result)))+ (is (= 2 (get-in (lib/template-tags result) ["#2" :card-id])))))))++(deftest ^:parallel swap-card-in-native-query-comment-test+ (let [mp (metadata-provider-with-cards)]+ (testing "Card tag inside a line comment should NOT be replaced"+ (let [query (-> (lib/native-query mp "SELECT * FROM {{#1}}\n-- old: {{#1}}")+ (lib/with-template-tags {"#1" {:type :card :card-id 1 :name "#1" :display-name "#1"}}))+ result (source-swap.native/swap-source-in-native-stages query [:card 1] [:card 2])]+ (is (= "SELECT * FROM {{#2}}\n-- old: {{#1}}"+ (lib/raw-native-query result))+ "The tag in the comment should be left alone")))++ (testing "Card tag inside a block comment should NOT be replaced"+ (let [query (-> (lib/native-query mp "SELECT * FROM {{#1}} /* see also {{#1}} */")+ (lib/with-template-tags {"#1" {:type :card :card-id 1 :name "#1" :display-name "#1"}}))+ result (source-swap.native/swap-source-in-native-stages query [:card 1] [:card 2])]+ (is (= "SELECT * FROM {{#2}} /* see also {{#1}} */"+ (lib/raw-native-query result))+ "The tag in the block comment should be left alone")))))++(deftest ^:parallel swap-card-in-native-query-string-literal-test+ (testing "Card tag inside a SQL string literal is also replaced (parser does not distinguish string literals)"+ (let [mp (metadata-provider-with-cards)+ query (-> (lib/native-query mp "SELECT * FROM {{#1}} WHERE col = '{{#1}}'")+ (lib/with-template-tags {"#1" {:type :card :card-id 1 :name "#1" :display-name "#1"}}))+ result (source-swap.native/swap-source-in-native-stages query [:card 1] [:card 2])]+ (is (= "SELECT * FROM {{#2}} WHERE col = '{{#2}}'"+ (lib/raw-native-query result))+ "Both tags are replaced since the parser treats string literal tags as params too"))))++(deftest ^:parallel swap-card-in-native-query-multiple-cards-test+ (testing "Multiple different card tags, replace only the target"+ (let [mp (metadata-provider-with-cards)+ query (-> (lib/native-query mp "SELECT a.* FROM {{#1}} a JOIN {{#3}} b ON a.id = b.id JOIN {{#1}} c ON a.id = c.id")+ (lib/with-template-tags {"#1" {:type :card :card-id 1 :name "#1" :display-name "#1"}+ "#3" {:type :card :card-id 3 :name "#3" :display-name "#3"}}))+ result (source-swap.native/swap-source-in-native-stages query [:card 1] [:card 2])]+ (is (= "SELECT a.* FROM {{#2}} a JOIN {{#3}} b ON a.id = b.id JOIN {{#2}} c ON a.id = c.id"+ (lib/raw-native-query result)))+ (is (= 2 (get-in (lib/template-tags result) ["#2" :card-id])))+ (is (= 3 (get-in (lib/template-tags result) ["#3" :card-id]))))))++;;; ------------------------------------------------ Native Query Card Reference Tests ------------------------------------------------+;;; These tests cover card→card replacement specifically in native SQL queries using {{#id}} syntax++(deftest ^:parallel swap-native-card-ref-with-whitespace-test+ (testing "Card reference with whitespace ({{ #1 }}) is replaced correctly"+ (let [mp (metadata-provider-with-cards)+ query (lib/native-query mp "SELECT * FROM {{ #1 }} WHERE x > 1")+ result (source-swap.native/swap-source-in-native-stages query [:card 1] [:card 2])]+ (is (str/includes? (lib/raw-native-query result) "{{#2}}"))+ (is (not (str/includes? (lib/raw-native-query result) "#1"))))))++(deftest ^:parallel swap-native-card-ref-multiple-test+ (testing "Multiple card references to the same card are all replaced"+ (let [mp (metadata-provider-with-cards)+ query (lib/native-query mp "SELECT * FROM {{#1}} a JOIN {{#1}} b ON a.id = b.id")+ result (source-swap.native/swap-source-in-native-stages query [:card 1] [:card 2])]+ (is (= 2 (count (re-seq #"\{\{#2\}\}" (lib/raw-native-query result)))))+ (is (not (str/includes? (lib/raw-native-query result) "{{#1}}"))))))++(deftest ^:parallel swap-native-card-ref-in-cte-test+ (testing "Card reference in CTE is replaced correctly"+ (let [mp (metadata-provider-with-cards)+ query (lib/native-query mp "WITH base AS {{#1}} SELECT * FROM base WHERE x > 1")+ result (source-swap.native/swap-source-in-native-stages query [:card 1] [:card 2])]+ (is (str/includes? (lib/raw-native-query result) "WITH base AS {{#2}}"))+ (is (not (str/includes? (lib/raw-native-query result) "{{#1}}"))))))++(deftest ^:parallel swap-native-card-ref-with-alias-test+ (testing "Card reference with alias ({{#1}} AS t) is replaced correctly"+ (let [mp (metadata-provider-with-cards)+ query (lib/native-query mp "SELECT t.* FROM {{#1}} AS t WHERE t.x > 1")+ result (source-swap.native/swap-source-in-native-stages query [:card 1] [:card 2])]+ (is (str/includes? (lib/raw-native-query result) "{{#2}} AS t"))+ (is (not (str/includes? (lib/raw-native-query result) "{{#1}}"))))))++(deftest ^:parallel swap-native-card-ref-preserves-other-params-test+ (testing "Other template params are preserved when replacing card reference"+ (let [mp (metadata-provider-with-cards)+ query (lib/native-query mp "SELECT * FROM {{#1}} WHERE status = {{status}} AND total > {{min_total}}")+ result (source-swap.native/swap-source-in-native-stages query [:card 1] [:card 2])]+ (is (str/includes? (lib/raw-native-query result) "{{#2}}"))+ (is (str/includes? (lib/raw-native-query result) "{{status}}"))+ (is (str/includes? (lib/raw-native-query result) "{{min_total}}"))+ (is (contains? (lib/template-tags result) "status"))+ (is (contains? (lib/template-tags result) "min_total")))))++(deftest ^:parallel swap-native-card-ref-different-cards-test+ (testing "Only the specified card reference is replaced, others are preserved"+ (let [mp (metadata-provider-with-cards)+ query (lib/native-query mp "SELECT * FROM {{#1}} a JOIN {{#3}} b ON a.id = b.id")+ result (source-swap.native/swap-source-in-native-stages query [:card 1] [:card 2])]+ (is (str/includes? (lib/raw-native-query result) "{{#2}}"))+ (is (str/includes? (lib/raw-native-query result) "{{#3}}"))+ (is (not (str/includes? (lib/raw-native-query result) "{{#1}}"))))))++(deftest ^:parallel swap-native-card-ref-in-subquery-test+ (testing "Card reference in subquery is replaced correctly"+ (let [mp (metadata-provider-with-cards)+ query (lib/native-query mp "SELECT * FROM orders WHERE product_id IN (SELECT id FROM {{#1}})")+ result (source-swap.native/swap-source-in-native-stages query [:card 1] [:card 2])]+ (is (str/includes? (lib/raw-native-query result) "FROM {{#2}}"))+ (is (not (str/includes? (lib/raw-native-query result) "{{#1}}"))))))++(deftest ^:parallel swap-native-card-ref-in-optional-clause-test+ (testing "Card reference inside optional clause [[...{{#1}}...]] is replaced correctly"+ (let [mp (metadata-provider-with-cards)+ query (lib/native-query mp "SELECT * FROM orders WHERE 1=1 [[AND product_id IN (SELECT id FROM {{#1}})]]\n")+ result (source-swap.native/swap-source-in-native-stages query [:card 1] [:card 2])]+ (is (str/includes? (lib/raw-native-query result) "{{#2}}"))+ (is (not (str/includes? (lib/raw-native-query result) "{{#1}}"))))))++;;; ------------------------------------------------ Native Query Table→Table Tests ------------------------------------------------+;;; These tests cover table→table replacement in native SQL queries using sql-tools++(deftest ^:parallel replace-table-in-native-sql-basic-test+ (testing "Basic table rename in native SQL"+ (let [result (#'source-swap.native/replace-table-in-native-sql :h2+ "SELECT * FROM ORDERS"+ "ORDERS" "NEW_ORDERS")]+ (is (str/includes? result "NEW_ORDERS"))+ (is (not (str/includes? result "ORDERS "))))))++(deftest ^:parallel replace-table-in-native-sql-with-template-tags-test+ (testing "Table rename preserves template tags"+ (let [result (#'source-swap.native/replace-table-in-native-sql :h2+ "SELECT * FROM ORDERS WHERE status = {{status}}"+ "ORDERS" "NEW_ORDERS")]+ (is (str/includes? result "NEW_ORDERS"))+ (is (str/includes? result "{{status}}")))))++(deftest ^:parallel replace-table-in-native-sql-with-optional-clause-test+ (testing "Table rename works with optional clauses"+ (let [result (#'source-swap.native/replace-table-in-native-sql :h2+ "SELECT * FROM ORDERS WHERE 1=1 [[AND status = {{status}}]]"+ "ORDERS" "NEW_ORDERS")]+ (is (str/includes? result "NEW_ORDERS"))+ (is (str/includes? result "[["))+ (is (str/includes? result "]]"))+ (is (str/includes? result "{{status}}")))))++(deftest ^:parallel replace-table-in-native-sql-table-in-optional-test+ (testing "Table inside optional clause is renamed"+ ;; Note: [[...]] must contain at least one {{param}} per Metabase parser rules+ (let [result (#'source-swap.native/replace-table-in-native-sql :h2+ "SELECT * FROM ORDERS WHERE 1=1 [[AND product_id IN (SELECT id FROM PRODUCTS WHERE cat = {{cat}})]]"+ "PRODUCTS" "NEW_PRODUCTS")]+ (is (str/includes? result "NEW_PRODUCTS"))+ (is (not (str/includes? result "FROM PRODUCTS"))))))++(deftest ^:parallel replace-table-in-native-sql-with-join-test+ (testing "Table rename in JOIN"+ (let [result (#'source-swap.native/replace-table-in-native-sql :h2+ "SELECT * FROM ORDERS o JOIN PRODUCTS p ON o.product_id = p.id"+ "PRODUCTS" "NEW_PRODUCTS")]+ (is (str/includes? result "NEW_PRODUCTS"))+ (is (str/includes? result "ORDERS")))))++(deftest ^:parallel replace-table-in-native-sql-with-cte-test+ (testing "Table inside CTE is renamed"+ (let [result (#'source-swap.native/replace-table-in-native-sql :h2+ "WITH recent AS (SELECT * FROM ORDERS WHERE created > '2024-01-01') SELECT * FROM recent"+ "ORDERS" "NEW_ORDERS")]+ (is (str/includes? result "NEW_ORDERS"))+ (is (str/includes? result "recent")))))++(deftest ^:parallel replace-table-in-native-sql-multiple-tags-test+ (testing "Table rename with multiple template tags"+ (let [result (#'source-swap.native/replace-table-in-native-sql :h2+ "SELECT * FROM ORDERS WHERE status = {{status}} AND total > {{min_total}}"+ "ORDERS" "NEW_ORDERS")]+ (is (str/includes? result "NEW_ORDERS"))+ (is (str/includes? result "{{status}}"))+ (is (str/includes? result "{{min_total}}")))))++(deftest ^:parallel replace-table-in-native-sql-nested-optionals-test+ (testing "Table rename with nested optional clauses"+ (let [result (#'source-swap.native/replace-table-in-native-sql :h2+ "SELECT * FROM ORDERS WHERE 1=1 [[AND total > {{min}} [[AND status = {{status}}]]]]"+ "ORDERS" "NEW_ORDERS")]+ (is (str/includes? result "NEW_ORDERS"))+ (is (str/includes? result "[["))+ (is (str/includes? result "{{min}}"))+ (is (str/includes? result "{{status}}")))))++(deftest ^:parallel replace-table-in-native-sql-comment-with-bracket-markers-test+ (testing "SQL comments containing bracket markers are not modified"+ (let [result (#'source-swap.native/replace-table-in-native-sql :h2+ "SELECT * FROM\n-- /*]]*/ \nORDERS LIMIT 5"+ "ORDERS" "NEW_ORDERS")]+ (is (str/includes? result "NEW_ORDERS"))+ (is (str/includes? result "/*]]*/") "Comment with bracket marker should be preserved"))))++;;; ------------------------------------------------ Schema-Qualified Native SQL Tests ------------------------------------------------++(deftest ^:parallel replace-table-in-native-sql-schema-qualified-test+ (testing "Schema-qualified table reference is matched and renamed"+ (let [result (#'source-swap.native/replace-table-in-native-sql :h2+ "SELECT * FROM PUBLIC.ORDERS"+ {:schema "PUBLIC" :table "ORDERS"}+ {:schema "PUBLIC" :table "NEW_ORDERS"})]+ (is (str/includes? result "NEW_ORDERS"))+ (is (not (str/includes? result "ORDERS ")))))++ (testing "Cross-schema rename: PUBLIC.ORDERS → ANALYTICS.NEW_ORDERS"+ (let [result (#'source-swap.native/replace-table-in-native-sql :h2+ "SELECT * FROM PUBLIC.ORDERS"+ {:schema "PUBLIC" :table "ORDERS"}+ {:schema "ANALYTICS" :table "NEW_ORDERS"})]+ (is (str/includes? result "ANALYTICS"))+ (is (str/includes? result "NEW_ORDERS"))+ (is (not (str/includes? result "PUBLIC")))))++ (testing "Unqualified SQL still matches when old-table has schema"+ (let [result (#'source-swap.native/replace-table-in-native-sql :h2+ "SELECT * FROM ORDERS"+ {:schema "PUBLIC" :table "ORDERS"}+ {:schema "PUBLIC" :table "NEW_ORDERS"})]+ (is (str/includes? result "NEW_ORDERS"))))++ (testing "Schema-qualified table→card clears the schema (no PUBLIC.{{#card}} in output)"+ ;; Just a plain string — replace-table-in-native-sql infers schema clearing+ ;; because old-table has a schema and new-table doesn't+ (let [result (#'source-swap.native/replace-table-in-native-sql :h2+ "SELECT * FROM PUBLIC.ORDERS"+ {:schema "PUBLIC" :table "ORDERS"}+ "{{#123-my-card}}")]+ (is (str/includes? result "{{#123-my-card}}"))+ (is (not (str/includes? result "PUBLIC.{{"))+ "Schema must be cleared, not left as PUBLIC.{{#card}}")+ (is (not (str/includes? result "PUBLIC")))))++ (testing "Card reference must not be quoted in SQL output"+ (let [result (#'source-swap.native/replace-table-in-native-sql :h2+ "SELECT * FROM ORDERS"+ {:table "ORDERS"}+ "{{#123-my-card}}")]+ (is (= "SELECT * FROM {{#123-my-card}}" result)+ "Card reference should not be wrapped in double quotes"))))++;;; ------------------------------------------------ table→table for native queries ------------------------------------------------++(deftest ^:parallel replace-table-in-native-query-test+ (testing "table→table: SQL table reference is updated"+ (let [query (lib/native-query meta/metadata-provider "SELECT * FROM PRODUCTS")+ result (source-swap.native/swap-source-in-native-stages query+ [:table (meta/id :products)]+ [:table (meta/id :orders)])]+ (is (str/includes? (lib/raw-native-query result) "ORDERS"))+ (is (not (str/includes? (lib/raw-native-query result) "PRODUCTS")))))++ (testing "table→table: template tags are preserved"+ (let [query (lib/native-query meta/metadata-provider "SELECT * FROM PRODUCTS WHERE category = {{category}}")+ result (source-swap.native/swap-source-in-native-stages query+ [:table (meta/id :products)]+ [:table (meta/id :orders)])]+ (is (str/includes? (lib/raw-native-query result) "ORDERS"))+ (is (not (str/includes? (lib/raw-native-query result) "PRODUCTS")))+ (is (str/includes? (lib/raw-native-query result) "{{category}}"))))++ (testing "table→table: only the target table is renamed in a JOIN"+ (let [query (lib/native-query meta/metadata-provider "SELECT o.*, p.title FROM ORDERS o JOIN PRODUCTS p ON o.product_id = p.id")+ result (source-swap.native/swap-source-in-native-stages query+ [:table (meta/id :orders)]+ [:table (meta/id :reviews)])]+ (is (str/includes? (lib/raw-native-query result) "REVIEWS"))+ (is (str/includes? (lib/raw-native-query result) "PRODUCTS"))+ (is (not (str/includes? (lib/raw-native-query result) "ORDERS")))))++ (testing "table→table: schema-qualified SQL reference is replaced"+ (let [query (lib/native-query meta/metadata-provider "SELECT * FROM PUBLIC.PRODUCTS")+ result (source-swap.native/swap-source-in-native-stages query+ [:table (meta/id :products)]+ [:table (meta/id :orders)])]+ (is (str/includes? (lib/raw-native-query result) "ORDERS"))+ (is (not (str/includes? (lib/raw-native-query result) "PRODUCTS"))))))++;;; ------------------------------------------------ table→card for native queries ------------------------------------------------++(deftest ^:parallel replace-table-with-card-in-native-test+ (let [mp (metadata-provider-with-cards)]+ (testing "table→card: SQL gets card template tag"+ (let [query (lib/native-query mp "SELECT * FROM PRODUCTS")+ result (source-swap.native/swap-source-in-native-stages query+ [:table (meta/id :products)]+ [:card 1])]+ (is (str/includes? (lib/raw-native-query result) "{{#1-card-1}}"))+ (is (not (str/includes? (lib/raw-native-query result) "PRODUCTS")))+ (is (= 1 (get-in (lib/template-tags result) ["#1-card-1" :card-id])))))++ (testing "table→card: existing template tags are preserved"+ (let [query (lib/native-query mp "SELECT * FROM PRODUCTS WHERE category = {{category}}")+ result (source-swap.native/swap-source-in-native-stages query+ [:table (meta/id :products)]+ [:card 1])]+ (is (str/includes? (lib/raw-native-query result) "{{#1-card-1}}"))+ (is (str/includes? (lib/raw-native-query result) "{{category}}"))+ (is (contains? (lib/template-tags result) "category"))+ (is (= 1 (get-in (lib/template-tags result) ["#1-card-1" :card-id])))))++ (testing "table→card: schema-qualified SQL gets card ref without schema prefix"+ (let [query (lib/native-query mp "SELECT * FROM PUBLIC.PRODUCTS")+ result (source-swap.native/swap-source-in-native-stages query+ [:table (meta/id :products)]+ [:card 1])]+ (is (str/includes? (lib/raw-native-query result) "{{#1-card-1}}"))+ (is (not (str/includes? (lib/raw-native-query result) "PUBLIC.{{")))+ (is (not (str/includes? (lib/raw-native-query result) "PRODUCTS")))))))++;;; ------------------------------------------------ card→table for native queries ------------------------------------------------++(deftest ^:parallel replace-card-with-table-in-native-test+ (testing "card→table: card ref becomes direct table reference"+ (let [query (-> (lib/native-query meta/metadata-provider "SELECT * FROM {{#1-card-1}}")+ (lib/with-template-tags {"#1-card-1" {:type :card :card-id 1 :name "#1-card-1" :display-name "#1-card-1"}}))+ result (source-swap.native/swap-source-in-native-stages query+ [:card 1]+ [:table (meta/id :orders)])]+ (is (str/includes? (lib/raw-native-query result) "ORDERS"))+ (is (not (str/includes? (lib/raw-native-query result) "{{#1")))+ (is (empty? (filter #(= (:card-id %) 1) (vals (lib/template-tags result)))))))++ (testing "card→table: other template tags are preserved"+ (let [query (-> (lib/native-query meta/metadata-provider "SELECT * FROM {{#1-card-1}} WHERE status = {{status}}")+ (lib/with-template-tags {"#1-card-1" {:type :card :card-id 1 :name "#1-card-1" :display-name "#1-card-1"}}))+ result (source-swap.native/swap-source-in-native-stages query+ [:card 1]+ [:table (meta/id :orders)])]+ (is (str/includes? (lib/raw-native-query result) "ORDERS"))+ (is (str/includes? (lib/raw-native-query result) "{{status}}"))+ (is (contains? (lib/template-tags result) "status"))+ (is (empty? (filter #(= (:card-id %) 1) (vals (lib/template-tags result))))))))++;;; ------------------------------------------------ Table Tag Tests ------------------------------------------------++(deftest ^:parallel swap-table-to-table-with-table-tag-test+ (testing "swap-source table → table: {{table}} tag's :table-id is updated"+ (let [query (-> (lib/native-query meta/metadata-provider "SELECT * FROM {{my_table}}")+ (lib/with-template-tags {"my_table" {:type :table :table-id 1 :name "my_table" :display-name "My Table"}}))+ result (#'source-swap.native/update-table-tags-for-table-swap+ (lib/template-tags query)+ 1 2)]+ (is (= 2 (get-in result ["my_table" :table-id]))))))++(deftest ^:parallel swap-table-to-card-with-table-tag-test+ (testing "swap-source table → card: {{my_table}} becomes {{#card-id-slug}}"+ (let [sql "SELECT * FROM {{my_table}}"+ tags {"my_table" {:type :table :table-id 1 :name "my_table" :display-name "My Table"}}+ {:keys [sql template-tags]} (#'source-swap.native/update-table-tags-for-card-swap sql tags 1 2 "New Card")]+ ;; SQL should have card reference+ (is (str/includes? sql "{{#2-new-card}}"))+ (is (not (str/includes? sql "{{my_table}}")))+ ;; Template tag should be :type :card now+ (is (= :card (get-in template-tags ["#2-new-card" :type])))+ (is (= 2 (get-in template-tags ["#2-new-card" :card-id])))+ ;; Old table tag should be gone+ (is (not (contains? template-tags "my_table"))))))++(deftest ^:parallel swap-table-to-card-preserves-required-flag-test+ (testing "swap-source table → card: :required flag is preserved"+ (let [sql "SELECT * FROM {{my_table}}"+ tags {"my_table" {:type :table+ :table-id 1+ :name "my_table"+ :display-name "My Table"+ :required true+ :default "fallback"}}+ {:keys [template-tags]} (#'source-swap.native/update-table-tags-for-card-swap sql tags 1 2 "New Card")]+ ;; :required and :default should be preserved+ (is (true? (get-in template-tags ["#2-new-card" :required])))+ (is (= "fallback" (get-in template-tags ["#2-new-card" :default]))))))++;;; ------------------------------------------------ Dimension Tag Tests ------------------------------------------------++(deftest ^:parallel update-dimension-tags-test+ (testing "dimension tag field ref is remapped to new table's field"+ (let [query (lib/native-query meta/metadata-provider "SELECT 1")+ tags {"filter" {:type :dimension+ :name "filter"+ :dimension (field-id-ref meta/metadata-provider (meta/id :products :id))}}+ result (#'source-swap.native/update-dimension-tags query tags (meta/id :products) (meta/id :orders))]+ (is (=? {"filter" {:dimension [:field {} (meta/id :orders :id)]}}+ result)))))++(deftest ^:parallel update-dimension-tags-no-match-test+ (testing "dimension tag left unchanged when no matching field on new table"+ (let [query (lib/native-query meta/metadata-provider "SELECT 1")+ dimension (field-id-ref meta/metadata-provider (meta/id :products :ean))+ tags {"filter" {:type :dimension+ :name "filter"+ :dimension dimension}}+ ;; Orders table doesn't have an EAN field+ result (#'source-swap.native/update-dimension-tags query tags (meta/id :products) (meta/id :orders))]+ ;; Should be unchanged since no matching field+ (is (= dimension+ (get-in result ["filter" :dimension]))))))