Skip to content

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

feat: support remote-syncing measures (#68392)

Edward Paget0933b7378f7a
diff --git a/.clj-kondo/config/modules/config.edn b/.clj-kondo/config/modules/config.edn
index 72758dd1599..887c6f4fa4c 100644
--- a/.clj-kondo/config/modules/config.edn
+++ b/.clj-kondo/config/modules/config.edn
@@ -808,6 +808,7 @@
lib-be
models
permissions
+ remote-sync
search
util}}
diff --git a/enterprise/backend/src/metabase_enterprise/remote_sync/impl.clj b/enterprise/backend/src/metabase_enterprise/remote_sync/impl.clj
index b27c79cbc70..985ff5ef896 100644
--- a/enterprise/backend/src/metabase_enterprise/remote_sync/impl.clj
+++ b/enterprise/backend/src/metabase_enterprise/remote_sync/impl.clj
@@ -92,6 +92,9 @@
(some-> e ex-cause ex-message (str/includes? "Can't create a tenant collection without tenants enabled"))
"This repository contains tenant collections, but the tenants feature is disabled on your instance."
+ (str/includes? (ex-message e) "Missing commit")
+ "Repository cache is stale: the remote repository may have been force-pushed. Please retry the operation."
+
:else
(format "Failed to reload from git repository: %s" (ex-message e))))
@@ -343,7 +346,14 @@
(u.jvm/in-virtual-thread*
(dh/with-timeout {:interrupt? true
:timeout-ms (* (settings/remote-sync-task-time-limit-ms) 10)}
- (handle-task-result! (sync-fn task-id) task-id branch)))
+ (handle-task-result!
+ (try
+ (sync-fn task-id)
+ (catch Exception e
+ (log/error e "Remote sync task failed")
+ {:status :error
+ :message (source-error-message e)}))
+ task-id branch)))
task))
(defn async-import!
diff --git a/enterprise/backend/src/metabase_enterprise/remote_sync/source/git.clj b/enterprise/backend/src/metabase_enterprise/remote_sync/source/git.clj
index 43048cb86f7..729f1edad61 100644
--- a/enterprise/backend/src/metabase_enterprise/remote_sync/source/git.clj
+++ b/enterprise/backend/src/metabase_enterprise/remote_sync/source/git.clj
@@ -419,7 +419,32 @@
(version [this]
(:version this)))
-(defn- snapshot
+(def ^:private jgit (atom {}))
+
+(defn- stale-cache-error?
+ "Returns true if the exception indicates a stale git cache (e.g., after a force-push on the remote)."
+ [^Exception e]
+ (some-> (ex-message e) (str/includes? "Missing commit")))
+
+(defn- clear-cached-repo!
+ "Clears a cached git repository from memory and disk."
+ [^File repo-path]
+ (log/info "Clearing stale git cache" {:repo-path (str repo-path)})
+ (swap! jgit dissoc (.getPath repo-path))
+ (FileUtils/deleteDirectory repo-path))
+
+(defn- get-jgit [^File path {:keys [remote-url token] :as args}]
+ (if-let [obj (get @jgit (.getPath path))]
+ obj
+ (get (swap! jgit assoc (.getPath path) (u/prog1 (open-jgit path {:remote-url remote-url
+ :token token})
+ (when-not (has-data? (assoc args :git <>))
+ (FileUtils/deleteDirectory path)
+ (throw (ex-info "Cannot connect to uninitialized repository" {:url remote-url})))))
+ (.getPath path))))
+
+(defn- snapshot*
+ "Internal snapshot implementation. Returns a GitSnapshot or throws."
[source]
(fetch! source)
(let [version (commit-sha source (:branch source))]
@@ -427,6 +452,21 @@
(->GitSnapshot (:git source) (:remote-url source) (:branch source) version (:token source))
(throw (ex-info (str "Invalid branch: " (:branch source)) {})))))
+(defn- snapshot
+ "Creates a snapshot, recovering from stale cache errors by re-cloning."
+ [{:keys [remote-url token] :as source}]
+ (try
+ (snapshot* source)
+ (catch Exception e
+ (if (stale-cache-error? e)
+ (let [path (repo-path {:remote-url remote-url :token token})]
+ (clear-cached-repo! path)
+ (let [fresh-git (get-jgit path {:remote-url remote-url :token token})
+ fresh-source (assoc source :git fresh-git)]
+ (log/info "Retrying snapshot after clearing stale cache")
+ (snapshot* fresh-source)))
+ (throw e)))))
+
(defrecord GitSource [git remote-url branch token]
source.p/Source
(branches [source] (branches source))
@@ -440,18 +480,6 @@
(snapshot [this]
(snapshot this)))
-(def ^:private jgit (atom {}))
-
-(defn- get-jgit [^File path {:keys [remote-url token] :as args}]
- (if-let [obj (get @jgit (.getPath path))]
- obj
- (get (swap! jgit assoc (.getPath path) (u/prog1 (open-jgit path {:remote-url remote-url
- :token token})
- (when-not (has-data? (assoc args :git <>))
- (FileUtils/deleteDirectory path)
- (throw (ex-info "Cannot connect to uninitialized repository" {:url remote-url})))))
- (.getPath path))))
-
(defn git-source
"Creates a new GitSource instance for a git repository.
diff --git a/enterprise/backend/src/metabase_enterprise/remote_sync/spec.clj b/enterprise/backend/src/metabase_enterprise/remote_sync/spec.clj
index b0579d9c20c..50f5fc2379f 100644
--- a/enterprise/backend/src/metabase_enterprise/remote_sync/spec.clj
+++ b/enterprise/backend/src/metabase_enterprise/remote_sync/spec.clj
@@ -225,6 +225,32 @@
:export-path {:type :segment-path}
:enabled? true}
+ :model/Measure
+ {:model-type "Measure"
+ :model-key :model/Measure
+ :identity :hybrid ; entity_id but needs table context for path
+ :path-keys [:database :schema :table]
+ :parent-model :model/Table
+ :events {:prefix :event/measure
+ :types [:create :update :delete]}
+ :eligibility {:type :parent-table}
+ :archived-key :archived
+ ;; Note: hydrate-query uses :s alias because query-entities-for-sync :hybrid
+ ;; hardcodes :s.id and :s.entity_id references
+ :tracking {:hydrate-query {:select [:s.name :s.table_id
+ [:t.collection_id :collection_id]
+ [:t.name :table_name]]
+ :from [[:measure :s]]
+ :join [[:metabase_table :t] [:= :s.table_id :t.id]]
+ :where [:= :s.id :?id]}
+ :field-mappings {:model_name :name
+ :model_collection_id :collection_id
+ :model_table_id :table_id
+ :model_table_name :table_name}}
+ :removal {:statuses #{"removed" "delete"}}
+ :export-path {:type :measure-path}
+ :enabled? true}
+
:model/Transform
{:model-type "Transform"
:model-key :model/Transform
diff --git a/enterprise/backend/test/metabase_enterprise/remote_sync/impl_test.clj b/enterprise/backend/test/metabase_enterprise/remote_sync/impl_test.clj
index 29e1f235a4b..fb168ba1dcd 100644
--- a/enterprise/backend/test/metabase_enterprise/remote_sync/impl_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/remote_sync/impl_test.clj
@@ -954,3 +954,140 @@
"Action should have correct name")
(is (= model-id (:model_id imported-action))
"Action should be attached to the correct model"))))))))
+
+;; Measure tracking tests
+
+(deftest import!-tracks-measures-in-remote-sync-object-test
+ (testing "import! creates RemoteSyncObject entries for imported Measures"
+ (mt/with-model-cleanup [:model/RemoteSyncObject :model/Measure]
+ (let [task-id (t2/insert-returning-pk! :model/RemoteSyncTask {:sync_task_type "import" :initiated_by (mt/user->id :rasta)})]
+ (mt/with-temp [:model/Database {db-id :id} {:name "test-db"}
+ :model/Table {table-id :id} {:name "test-table" :db_id db-id}
+ :model/Field {_field-id :id} {:name "test-field" :table_id table-id :base_type :type/Integer}
+ :model/Collection _ {:name "Test Collection"
+ :is_remote_synced true
+ :entity_id "test-collection-1xxxx"
+ :location "/"}
+ :model/Measure {measure-id :id}
+ {:name "Test Measure"
+ :table_id table-id
+ :entity_id "TNdMrOCMHrQc_UtvCbTC6"}]
+ (let [test-files {"main" {"collections/test-collection-1xxxx-_/test-collection-1xxxx.yaml"
+ (test-helpers/generate-collection-yaml "test-collection-1xxxx" "Test Collection")
+ "databases/test-db/tables/test-table/measures/TNdMrOCMHrQc_UtvCbTC6_test_measure.yaml"
+ (test-helpers/generate-measure-yaml "Test Measure" "test-table" "test-db"
+ :entity-id "TNdMrOCMHrQc_UtvCbTC6"
+ :agg-field-name "test-field")}}
+ mock-source (test-helpers/create-mock-source :initial-files test-files)
+ result (impl/import! (source.p/snapshot mock-source) task-id)]
+ (is (= :success (:status result)))
+ ;; Verify RemoteSyncObject entry exists for the measure
+ (is (t2/exists? :model/RemoteSyncObject
+ :model_type "Measure"
+ :model_id measure-id
+ :status "synced")
+ "Measure should be tracked in RemoteSyncObject")))))))
+
+(deftest export!-deletes-files-for-removed-measures-test
+ (testing "export! deletes files from git source for measures with 'removed' status"
+ (mt/with-model-cleanup [:model/RemoteSyncObject :model/Measure]
+ (mt/with-temporary-setting-values [remote-sync-type :read-write]
+ (let [task-id (t2/insert-returning-pk! :model/RemoteSyncTask {:sync_task_type "export" :initiated_by (mt/user->id :rasta)})]
+ (mt/with-temp [:model/Database {db-id :id} {:name "test-db"}
+ :model/Table {table-id :id} {:name "test-table" :db_id db-id}
+ :model/Collection {coll-id :id}
+ {:name "Active Collection"
+ :is_remote_synced true
+ :entity_id "active-coll-xxxxxxxxx"
+ :location "/"}
+ :model/Measure {measure-id :id}
+ {:name "Test Measure"
+ :table_id table-id
+ :entity_id "test-measure-xxxxxxxx"}]
+ ;; Mark the measure as 'removed' in RemoteSyncObject
+ (t2/insert! :model/RemoteSyncObject
+ [{:model_type "Collection" :model_id coll-id :model_name "Active Collection" :status "synced" :status_changed_at (t/offset-date-time)}
+ {:model_type "Measure" :model_id measure-id :model_name "Test Measure" :model_table_id table-id :model_table_name "test-table" :status "removed" :status_changed_at (t/offset-date-time)}])
+ (let [initial-files {"main" {;; File path uses slugified name to match what serdes/storage-path generates
+ "databases/test-db/tables/test-table/measures/test-measure-xxxxxxxx_test_measure.yaml"
+ (test-helpers/generate-measure-yaml "Test Measure" "test-table" "test-db")}}
+ mock-source (test-helpers/create-mock-source :initial-files initial-files)
+ result (impl/export! (source.p/snapshot mock-source) task-id "Test commit")]
+ (is (= :success (:status result)))
+ (let [files-after-export (get @(:files-atom mock-source) "main")]
+ (is (not (some #(str/includes? % "test-measure-xxxxxxxx") (keys files-after-export)))
+ "Removed measure files should be deleted after export")))))))))
+
+(deftest export!-excludes-archived-measures-test
+ (testing "export! excludes archived measures from export (via skip-archived flag)"
+ (mt/with-model-cleanup [:model/RemoteSyncObject :model/Measure]
+ (mt/with-temporary-setting-values [remote-sync-type :read-write]
+ (let [task-id (t2/insert-returning-pk! :model/RemoteSyncTask {:sync_task_type "export" :initiated_by (mt/user->id :rasta)})]
+ (mt/with-temp [:model/Database {db-id :id} {:name "test-db"}
+ :model/Collection {coll-id :id}
+ {:name "Test Collection"
+ :is_remote_synced true
+ :entity_id "test-collection-1xxxx"
+ :location "/"}
+ ;; Table must have collection_id and is_published to be included as Collection descendant
+ :model/Table {table-id :id} {:name "test-table"
+ :db_id db-id
+ :collection_id coll-id
+ :is_published true}
+ :model/Measure {_active-measure-id :id}
+ {:name "Active Measure"
+ :table_id table-id
+ :entity_id "active-measure-xxxxxx"
+ :archived false}
+ :model/Measure {_archived-measure-id :id}
+ {:name "Archived Measure"
+ :table_id table-id
+ :entity_id "archived-measure-xxxx"
+ :archived true}]
+ ;; Track the collection so export runs
+ (t2/insert! :model/RemoteSyncObject
+ [{:model_type "Collection" :model_id coll-id :model_name "Test Collection" :status "synced" :status_changed_at (t/offset-date-time)}])
+ (let [mock-source (test-helpers/create-mock-source)
+ result (impl/export! (source.p/snapshot mock-source) task-id "Test commit")]
+ (is (= :success (:status result)))
+ (let [files-after-export (get @(:files-atom mock-source) "main")
+ file-keys (keys files-after-export)]
+ ;; File paths use entity_id, e.g. "active-measure-xxxxxx_active_measure.yaml"
+ (is (some #(str/includes? % "active-measure-xxxxxx") file-keys)
+ (str "Active measure should be exported. Keys: " (pr-str file-keys)))
+ (is (not (some #(str/includes? % "archived-measure-xxxx") file-keys))
+ "Archived measure should NOT be exported")))))))))
+
+(deftest export!-deletes-files-for-archived-measures-test
+ (testing "export! deletes files from git source for measures with 'delete' status (archived measures)"
+ (mt/with-model-cleanup [:model/RemoteSyncObject :model/Measure]
+ (mt/with-temporary-setting-values [remote-sync-type :read-write]
+ (let [task-id (t2/insert-returning-pk! :model/RemoteSyncTask {:sync_task_type "export" :initiated_by (mt/user->id :rasta)})]
+ (mt/with-temp [:model/Database {db-id :id} {:name "test-db"}
+ :model/Table {table-id :id} {:name "test-table" :db_id db-id}
+ :model/Collection {coll-id :id}
+ {:name "Active Collection"
+ :is_remote_synced true
+ :entity_id "active-coll-xxxxxxxxx"
+ :location "/"}
+ :model/Measure {measure-id :id}
+ {:name "Archived Measure"
+ :table_id table-id
+ :entity_id "archived-meas-xxxxxxx"
+ :archived true}]
+ ;; Mark the measure as 'delete' in RemoteSyncObject (simulating archival event)
+ (t2/insert! :model/RemoteSyncObject
+ [{:model_type "Collection" :model_id coll-id :model_name "Active Collection" :status "synced" :status_changed_at (t/offset-date-time)}
+ {:model_type "Measure" :model_id measure-id :model_name "Archived Measure" :model_table_id table-id :model_table_name "test-table" :status "delete" :status_changed_at (t/offset-date-time)}])
+ ;; File path uses slugified name to match what serdes/storage-path generates
+ (let [initial-files {"main" {"databases/test-db/tables/test-table/measures/archived-meas-xxxxxxx_archived_measure.yaml"
+ (test-helpers/generate-measure-yaml "Archived Measure" "test-table" "test-db")}}
+ mock-source (test-helpers/create-mock-source :initial-files initial-files)
+ result (impl/export! (source.p/snapshot mock-source) task-id "Test commit")]
+ (is (= :success (:status result)))
+ (let [files-after-export (get @(:files-atom mock-source) "main")]
+ (is (not (some #(str/includes? % "archived-meas-xxxxxxx") (keys files-after-export)))
+ "Archived measure files should be deleted after export"))
+ (testing "RemoteSyncObject entry is cleaned up after export"
+ (is (= "synced" (:status (t2/select-one :model/RemoteSyncObject :model_type "Measure" :model_id measure-id)))
+ "RemoteSyncObject entry for archived measure should have synced status")))))))))
diff --git a/enterprise/backend/test/metabase_enterprise/remote_sync/spec_test.clj b/enterprise/backend/test/metabase_enterprise/remote_sync/spec_test.clj
index 64f63548e25..4d0d719ed15 100644
--- a/enterprise/backend/test/metabase_enterprise/remote_sync/spec_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/remote_sync/spec_test.clj
@@ -81,7 +81,7 @@
(deftest all-specs-have-valid-export-path-test
(testing "Every spec has valid export-path configuration"
(let [valid-path-types #{:collection-entity :table-path :field-path
- :segment-path :transform-path :transform-tag-path}]
+ :segment-path :measure-path :transform-path :transform-tag-path}]
(doseq [[model-key spec] spec/remote-sync-specs]
(testing (str "Spec for " model-key)
(is (contains? valid-path-types (get-in spec [:export-path :type]))
@@ -94,12 +94,14 @@
(is (= :model/Card (:model-key (spec/spec-for-model-type "Card"))))
(is (= :model/Dashboard (:model-key (spec/spec-for-model-type "Dashboard"))))
(is (= :model/Table (:model-key (spec/spec-for-model-type "Table"))))
+ (is (= :model/Measure (:model-key (spec/spec-for-model-type "Measure"))))
(is (nil? (spec/spec-for-model-type "NonExistent")))))
(deftest spec-for-model-key-test
(testing "spec-for-model-key returns correct spec"
(is (= "Card" (:model-type (spec/spec-for-model-key :model/Card))))
(is (= "Dashboard" (:model-type (spec/spec-for-model-key :model/Dashboard))))
+ (is (= "Measure" (:model-type (spec/spec-for-model-key :model/Measure))))
(is (nil? (spec/spec-for-model-key :model/NonExistent)))))
(deftest all-model-types-test
@@ -111,9 +113,10 @@
(is (contains? types "Table"))
(is (contains? types "Field"))
(is (contains? types "Segment"))
+ (is (contains? types "Measure"))
(is (contains? types "Transform"))
(is (contains? types "TransformTag"))
- (is (= 11 (count types))))))
+ (is (= 12 (count types))))))
(deftest specs-by-identity-type-test
(testing "specs-by-identity-type filters correctly"
@@ -133,6 +136,7 @@
(is (not (contains? path-specs :model/Card))))
(testing "hybrid specs"
(is (contains? hybrid-specs :model/Segment))
+ (is (contains? hybrid-specs :model/Measure))
(is (not (contains? hybrid-specs :model/Card))))))))
(deftest excluded-model-types-test
@@ -300,6 +304,8 @@
(testing "query-export-roots with :parent-table eligibility returns nil (derived)"
(let [field-spec (spec/spec-for-model-key :model/Field)
- segment-spec (spec/spec-for-model-key :model/Segment)]
+ segment-spec (spec/spec-for-model-key :model/Segment)
+ measure-spec (spec/spec-for-model-key :model/Measure)]
(is (nil? (spec/query-export-roots field-spec)))
- (is (nil? (spec/query-export-roots segment-spec))))))
+ (is (nil? (spec/query-export-roots segment-spec)))
+ (is (nil? (spec/query-export-roots measure-spec))))))
diff --git a/enterprise/backend/test/metabase_enterprise/remote_sync/test_helpers.clj b/enterprise/backend/test/metabase_enterprise/remote_sync/test_helpers.clj
index 8f7977b34bb..23d071af0f8 100644
--- a/enterprise/backend/test/metabase_enterprise/remote_sync/test_helpers.clj
+++ b/enterprise/backend/test/metabase_enterprise/remote_sync/test_helpers.clj
@@ -554,3 +554,66 @@ serdes/meta:
"")
entity-id
(str/replace (u/lower-case-en name) #"\s+" "_")))
+
+(defn generate-measure-yaml
+ "Generates YAML content for a measure.
+
+ Args:
+ measure-name: The name of the measure.
+ table-name: The name of the table containing this measure.
+ db-name: The name of the database containing this measure.
+ agg-field-name: The name of the field to use in the aggregation clause.
+
+ Returns:
+ A string containing the YAML representation of the measure."
+ [measure-name table-name db-name & {:keys [schema description entity-id agg-field-name]
+ :or {description "Test measure"
+ agg-field-name "Some Field"}}]
+ (let [eid (or entity-id measure-name)]
+ (format "archived: false
+created_at: '2024-08-28T09:46:18.671622Z'
+creator_id: [email protected]
+definition:
+ database: %s
+ query:
+ aggregation:
+ - - sum
+ - - field
+ - - %s
+ - %s
+ - %s
+ - %s
+ - null
+ source-table:
+ - %s
+ - %s
+ - %s
+ type: query
+description: %s
+entity_id: %s
+name: %s
+table_id:
+- %s
+- %s
+- %s
+serdes/meta:
+- id: %s
+ label: %s
+ model: Measure
+"
+ db-name
+ db-name
+ (or schema "null")
+ table-name
+ agg-field-name
+ db-name
+ (or schema "null")
+ table-name
+ description
+ eid
+ measure-name
+ db-name
+ (or schema "null")
+ table-name
+ eid
+ (str/replace (u/lower-case-en measure-name) #"\s+" "_"))))
diff --git a/enterprise/frontend/src/metabase-enterprise/data-studio/measures/pages/NewMeasurePage/NewMeasurePage.tsx b/enterprise/frontend/src/metabase-enterprise/data-studio/measures/pages/NewMeasurePage/NewMeasurePage.tsx
index 3f6d0840d78..8db29bd119b 100644
--- a/enterprise/frontend/src/metabase-enterprise/data-studio/measures/pages/NewMeasurePage/NewMeasurePage.tsx
+++ b/enterprise/frontend/src/metabase-enterprise/data-studio/measures/pages/NewMeasurePage/NewMeasurePage.tsx
@@ -40,6 +40,7 @@ export function NewMeasurePage({
const [name, setName] = useState("");
const [description, setDescription] = useState("");
const [definition, setDefinition] = useState<DatasetQuery | null>(null);
+ const [savedMeasure, setSavedMeasure] = useState<Measure | null>(null);
const isInitialized = useRef(false);
@@ -53,7 +54,10 @@ export function NewMeasurePage({
const { query, aggregations } = useMeasureQuery(definition, metadata);
const isDirty =
- name.trim().length > 0 || description.length > 0 || aggregations.length > 0;
+ !savedMeasure &&
+ (name.trim().length > 0 ||
+ description.length > 0 ||
+ aggregations.length > 0);
const isValid = name.trim().length > 0 && aggregations.length === 1;
const previewUrl =
aggregations.length === 1
@@ -80,8 +84,8 @@ export function NewMeasurePage({
if (error) {
sendErrorToast(t`Failed to create measure`);
} else if (measure) {
+ setSavedMeasure(measure);
sendSuccessToast(t`Measure created`);
- dispatch(push(getSuccessUrl(measure)));
}
}, [
table,
@@ -90,12 +94,16 @@ export function NewMeasurePage({
description,
isValid,
createMeasure,
- dispatch,
- getSuccessUrl,
sendSuccessToast,
sendErrorToast,
]);
+ useEffect(() => {
+ if (savedMeasure) {
+ dispatch(push(getSuccessUrl(savedMeasure)));
+ }
+ }, [savedMeasure, dispatch, getSuccessUrl]);
+
return (
<PageContainer data-testid="new-measure-page" gap="xl">
<NewMeasureHeader
diff --git a/enterprise/frontend/src/metabase-enterprise/remote_sync/components/ChangesLists/AllChangesView.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/remote_sync/components/ChangesLists/AllChangesView.unit.spec.tsx
index 748f0839db6..9775bffac42 100644
--- a/enterprise/frontend/src/metabase-enterprise/remote_sync/components/ChangesLists/AllChangesView.unit.spec.tsx
+++ b/enterprise/frontend/src/metabase-enterprise/remote_sync/components/ChangesLists/AllChangesView.unit.spec.tsx
@@ -244,4 +244,103 @@ describe("AllChangesView", () => {
).toBeInTheDocument();
});
});
+
+ describe("table child models", () => {
+ it("should display measures nested under their parent table", async () => {
+ const tableEntity = createMockRemoteSyncEntity({
+ id: 100,
+ name: "Orders Table",
+ model: "table",
+ collection_id: 1,
+ sync_status: "update",
+ });
+ const measureEntity = createMockRemoteSyncEntity({
+ id: 200,
+ name: "Total Revenue",
+ model: "measure",
+ collection_id: 1,
+ sync_status: "create",
+ table_id: 100,
+ table_name: "Orders Table",
+ });
+
+ setup({
+ entities: [tableEntity, measureEntity],
+ });
+
+ expect(await screen.findByText("Entity Collection")).toBeInTheDocument();
+ expect(screen.getByText("Orders Table")).toBeInTheDocument();
+ expect(screen.getByText("Total Revenue")).toBeInTheDocument();
+ });
+
+ it("should display measures with table name when table is not dirty", async () => {
+ const measureEntity = createMockRemoteSyncEntity({
+ id: 200,
+ name: "Total Revenue",
+ model: "measure",
+ collection_id: 1,
+ sync_status: "update",
+ table_id: 100,
+ table_name: "Orders Table",
+ });
+
+ setup({
+ entities: [measureEntity],
+ });
+
+ expect(await screen.findByText("Entity Collection")).toBeInTheDocument();
+ expect(screen.getByText("Orders Table")).toBeInTheDocument();
+ expect(screen.getByText("Total Revenue")).toBeInTheDocument();
+ });
+
+ it("should display segments nested under their parent table", async () => {
+ const segmentEntity = createMockRemoteSyncEntity({
+ id: 300,
+ name: "Active Users",
+ model: "segment",
+ collection_id: 1,
+ sync_status: "create",
+ table_id: 100,
+ table_name: "Users Table",
+ });
+
+ setup({
+ entities: [segmentEntity],
+ });
+
+ expect(await screen.findByText("Entity Collection")).toBeInTheDocument();
+ expect(screen.getByText("Users Table")).toBeInTheDocument();
+ expect(screen.getByText("Active Users")).toBeInTheDocument();
+ });
+
+ it("should group multiple table children under the same table", async () => {
+ const fieldEntity = createMockRemoteSyncEntity({
+ id: 400,
+ name: "email",
+ model: "field",
+ collection_id: 1,
+ sync_status: "update",
+ table_id: 100,
+ table_name: "Users Table",
+ });
+ const measureEntity = createMockRemoteSyncEntity({
+ id: 500,
+ name: "Total Users",
+ model: "measure",
+ collection_id: 1,
+ sync_status: "create",
+ table_id: 100,
+ table_name: "Users Table",
+ });
+
+ setup({
+ entities: [fieldEntity, measureEntity],
+ });
+
+ expect(await screen.findByText("Entity Collection")).toBeInTheDocument();
+ expect(screen.getByText("Users Table")).toBeInTheDocument();
+ expect(screen.getByText("email")).toBeInTheDocument();
+ expect(screen.getByText("Total Users")).toBeInTheDocument();
+ });
+ });
});
diff --git a/enterprise/frontend/src/metabase-enterprise/remote_sync/constants.ts b/enterprise/frontend/src/metabase-enterprise/remote_sync/constants.ts
index 664d93e5aa4..973bbcc2acf 100644
--- a/enterprise/frontend/src/metabase-enterprise/remote_sync/constants.ts
+++ b/enterprise/frontend/src/metabase-enterprise/remote_sync/constants.ts
@@ -1,6 +1,7 @@
+import type { TagDescription } from "@reduxjs/toolkit/query";
import * as Yup from "yup";
-import { tag } from "../api/tags";
+import { type EnterpriseTagType, tag } from "../api/tags";
export const URL_KEY = "remote-sync-url";
export const TOKEN_KEY = "remote-sync-token";
@@ -25,9 +26,10 @@ export const REMOTE_SYNC_SCHEMA = Yup.object({
[COLLECTIONS_KEY]: Yup.object().nullable().default({}),
});
-export const REMOTE_SYNC_INVALIDATION_TAGS = [
- tag("collection-dirty-entities"),
- tag("collection-is-dirty"),
- tag("remote-sync-current-task"),
- tag("remote-sync-has-remote-changes"),
-];
+export const REMOTE_SYNC_INVALIDATION_TAGS: TagDescription<EnterpriseTagType>[] =
+ [
+ tag("collection-dirty-entities"),
+ tag("collection-is-dirty"),
+ tag("remote-sync-current-task"),
+ tag("remote-sync-has-remote-changes"),
+ ];
diff --git a/enterprise/frontend/src/metabase-enterprise/remote_sync/middleware/model-configs.ts b/enterprise/frontend/src/metabase-enterprise/remote_sync/middleware/model-configs.ts
new file mode 100644
index 00000000000..013ba2bb4f9
--- /dev/null
+++ b/enterprise/frontend/src/metabase-enterprise/remote_sync/middleware/model-configs.ts
@@ -0,0 +1,204 @@
+import type { UnknownAction } from "@reduxjs/toolkit";
+
+import { cardApi } from "metabase/api/card";
+import { collectionApi } from "metabase/api/collection";
+import { dashboardApi } from "metabase/api/dashboard";
+import { documentApi } from "metabase/api/document";
+import { fieldApi } from "metabase/api/field";
+import { measureApi } from "metabase/api/measure";
+import { segmentApi } from "metabase/api/segment";
+import { tableApi as coreTableApi } from "metabase/api/table";
+import { timelineApi } from "metabase/api/timeline";
+import { timelineEventApi } from "metabase/api/timeline-event";
+import { getCollectionFromCollectionsTree } from "metabase/selectors/collection";
+import { tableApi as enterpriseTableApi } from "metabase-enterprise/api/table";
+import { transformApi } from "metabase-enterprise/api/transform";
+import { transformTagApi } from "metabase-enterprise/api/transform-tag";
+import type { CardId, CollectionId, DashboardId } from "metabase-types/api";
+import type { State } from "metabase-types/store";
+
+import {
+ InvalidationType,
+ type ModelMutationConfig,
+} from "./model-invalidation-config";
+
+/**
+ * Helper to extract originalArgs from RTK Query mutation actions.
+ * RTK Query stores the original arguments in action.meta.arg.originalArgs
+ */
+function getOriginalArgs<T>(action: UnknownAction): T | undefined {
+ const meta = (action as { meta?: { arg?: { originalArgs?: unknown } } }).meta;
+ return meta?.arg?.originalArgs as T | undefined;
+}
+
+function getOriginalDocument(originalState: State, id: number) {
+ // RTK Query selector requires RootState type, but our State type is compatible
+ const selector = documentApi.endpoints.getDocument.select({ id });
+ return (
+ selector(originalState as Parameters<typeof selector>[0])?.data ||
+ originalState.entities.documents[id]
+ );
+}
+
+function getOriginalDashboard(originalState: State, id: DashboardId) {
+ const selector = dashboardApi.endpoints.getDashboard.select({ id });
+ return (
+ selector(originalState as Parameters<typeof selector>[0])?.data ||
+ originalState.entities.dashboards[id]
+ );
+}
+
+function getOriginalCard(originalState: State, id: CardId) {
+ const selector = cardApi.endpoints.getCard.select({ id });
+ return (
+ selector(originalState as Parameters<typeof selector>[0])?.data ||
+ originalState.entities.questions[id]
+ );
+}
+
+function getOriginalCollection(originalState: State, id: CollectionId) {
+ return (
+ collectionApi.endpoints.getCollection.select({ id })(originalState)?.data ||
+ getCollectionFromCollectionsTree(originalState, id)
+ );
+}
+
+/**
+ * Configuration for model mutations that trigger remote sync invalidation.
+ *
+ * Models are grouped by invalidation strategy:
+ * - remote_synced_change: Cards, dashboards, documents (check is_remote_synced)
+ * - collection_based: Collections (check collection's is_remote_synced)
+ * - always: Table children (fields, segments, measures) and other models
+ */
+export const MODEL_MUTATION_CONFIGS: ModelMutationConfig[] = [
+ // Models with is_remote_synced tracking
+ {
+ modelType: "card",
+ createEndpoints: [cardApi.endpoints.createCard.matchFulfilled],
+ updateEndpoints: [cardApi.endpoints.updateCard.matchFulfilled],
+ deleteEndpoints: [cardApi.endpoints.deleteCard.matchFulfilled],
+ invalidation: {
+ type: InvalidationType.RemoteSyncedChange,
+ getOriginal: getOriginalCard,
+ },
+ getDeleteId: (action) => getOriginalArgs<number>(action),
+ },
+ {
+ modelType: "dashboard",
+ createEndpoints: [dashboardApi.endpoints.createDashboard.matchFulfilled],
+ updateEndpoints: [dashboardApi.endpoints.updateDashboard.matchFulfilled],
+ deleteEndpoints: [dashboardApi.endpoints.deleteDashboard.matchFulfilled],
+ invalidation: {
+ type: InvalidationType.RemoteSyncedChange,
+ getOriginal: getOriginalDashboard,
+ },
+ getDeleteId: (action) => getOriginalArgs<number>(action),
+ },
+ {
+ modelType: "document",
+ createEndpoints: [documentApi.endpoints.createDocument.matchFulfilled],
+ updateEndpoints: [documentApi.endpoints.updateDocument.matchFulfilled],
+ deleteEndpoints: [documentApi.endpoints.deleteDocument.matchFulfilled],
+ invalidation: {
+ type: InvalidationType.RemoteSyncedChange,
+ getOriginal: getOriginalDocument,
+ },
+ getDeleteId: (action) => getOriginalArgs<number>(action),
+ },
+
+ // Collections have special handling
+ {
+ modelType: "collection",
+ createEndpoints: [collectionApi.endpoints.createCollection.matchFulfilled],
+ updateEndpoints: [collectionApi.endpoints.updateCollection.matchFulfilled],
+ deleteEndpoints: [collectionApi.endpoints.deleteCollection.matchFulfilled],
+ invalidation: {
+ type: InvalidationType.CollectionBased,
+ getOriginalCollection: getOriginalCollection,
+ },
+ getDeleteId: (action) => getOriginalArgs<{ id: number }>(action),
+ },
+
+ // Always-invalidate models (table children and related)
+ {
+ modelType: "timeline",
+ createEndpoints: [timelineApi.endpoints.createTimeline.matchFulfilled],
+ updateEndpoints: [timelineApi.endpoints.updateTimeline.matchFulfilled],
+ deleteEndpoints: [timelineApi.endpoints.deleteTimeline.matchFulfilled],
+ invalidation: { type: InvalidationType.Always },
+ },
+ {
+ modelType: "timelineEvent",
+ createEndpoints: [
+ timelineEventApi.endpoints.createTimelineEvent.matchFulfilled,
+ ],
+ updateEndpoints: [
+ timelineEventApi.endpoints.updateTimelineEvent.matchFulfilled,
+ ],
+ deleteEndpoints: [
+ timelineEventApi.endpoints.deleteTimelineEvent.matchFulfilled,
+ ],
+ invalidation: { type: InvalidationType.Always },
+ },
+ {
+ modelType: "enterpriseTable",
+ createEndpoints: [
+ enterpriseTableApi.endpoints.publishTables.matchFulfilled,
+ ],
+ updateEndpoints: [enterpriseTableApi.endpoints.editTables.matchFulfilled],
+ deleteEndpoints: [
+ enterpriseTableApi.endpoints.unpublishTables.matchFulfilled,
+ ],
+ invalidation: { type: InvalidationType.Always },
+ },
+ {
+ modelType: "coreTable",
+ updateEndpoints: [
+ coreTableApi.endpoints.updateTable.matchFulfilled,
+ coreTableApi.endpoints.updateTableFieldsOrder.matchFulfilled,
+ ],
+ invalidation: { type: InvalidationType.Always },
+ },
+ {
+ modelType: "field",
+ updateEndpoints: [
+ fieldApi.endpoints.updateField.matchFulfilled,
+ fieldApi.endpoints.createFieldDimension.matchFulfilled,
+ fieldApi.endpoints.deleteFieldDimension.matchFulfilled,
+ ],
+ invalidation: { type: InvalidationType.Always },
+ },
+ {
+ modelType: "segment",
+ createEndpoints: [segmentApi.endpoints.createSegment.matchFulfilled],
+ updateEndpoints: [segmentApi.endpoints.updateSegment.matchFulfilled],
+ invalidation: { type: InvalidationType.Always },
+ },
+ {
+ modelType: "measure",
+ createEndpoints: [measureApi.endpoints.createMeasure.matchFulfilled],
+ updateEndpoints: [measureApi.endpoints.updateMeasure.matchFulfilled],
+ invalidation: { type: InvalidationType.Always },
+ },
+ {
+ modelType: "transform",
+ createEndpoints: [transformApi.endpoints.createTransform.matchFulfilled],
+ updateEndpoints: [transformApi.endpoints.updateTransform.matchFulfilled],
+ deleteEndpoints: [transformApi.endpoints.deleteTransform.matchFulfilled],
+ invalidation: { type: InvalidationType.Always },
+ },
+ {
+ modelType: "transformTag",
+ createEndpoints: [
+ transformTagApi.endpoints.createTransformTag.matchFulfilled,
+ ],
+ updateEndpoints: [
+ transformTagApi.endpoints.updateTransformTag.matchFulfilled,
+ ],
+ deleteEndpoints: [
+ transformTagApi.endpoints.deleteTransformTag.matchFulfilled,
+ ],
+ invalidation: { type: InvalidationType.Always },
+ },
+];
diff --git a/enterprise/frontend/src/metabase-enterprise/remote_sync/middleware/model-invalidation-config.ts b/enterprise/frontend/src/metabase-enterprise/remote_sync/middleware/model-invalidation-config.ts
new file mode 100644
index 00000000000..25a40fd90b5
--- /dev/null
+++ b/enterprise/frontend/src/metabase-enterprise/remote_sync/middleware/model-invalidation-config.ts
@@ -0,0 +1,63 @@
+import type { UnknownAction } from "@reduxjs/toolkit";
+
+import type {
+ Card,
+ CardId,
+ Collection,
+ CollectionId,
+ Dashboard,
+ DashboardId,
+ Document,
+} from "metabase-types/api";
+import type { State } from "metabase-types/store";
+
+/**
+ * Invalidation strategies for remote sync model mutations.
+ */
+export enum InvalidationType {
+ /** Always invalidate on create/update/delete (for table children like fields, segments, measures) */
+ Always = "always",
+ /** Only invalidate if is_remote_synced changed or was already true */
+ RemoteSyncedChange = "remote_synced_change",
+ /** Check collection's is_remote_synced status */
+ CollectionBased = "collection_based",
+}
+
+export type InvalidationStrategy =
+ | { type: InvalidationType.Always }
+ | {
+ type: InvalidationType.RemoteSyncedChange;
+ getOriginal: (
+ state: State,
+ id: number,
+ ) => Card | Dashboard | Document | undefined;
+ }
+ | {
+ type: InvalidationType.CollectionBased;
+ getOriginalCollection: (
+ state: State,
+ id: CollectionId,
+ ) => Collection | undefined;
+ };
+
+/**
+ * Type for RTK Query endpoint matchers (matchFulfilled, matchPending, etc.)
+ * These are type guard functions that narrow actions to their specific type.
+ */
+export type EndpointMatcher = (
+ action: UnknownAction,
+) => action is UnknownAction & { payload: unknown; meta: unknown };
+
+export type ModelMutationConfig = {
+ modelType: string;
+ createEndpoints?: EndpointMatcher[];
+ updateEndpoints?: EndpointMatcher[];
+ deleteEndpoints?: EndpointMatcher[];
+ invalidation: InvalidationStrategy;
+ getDeleteId?: (action: UnknownAction) => number | { id: number } | undefined;
+};
+
+export type ModelWithRemoteSynced = {
+ id: number | CardId | DashboardId;
+ is_remote_synced?: boolean;
+};
diff --git a/enterprise/frontend/src/metabase-enterprise/remote_sync/middleware/register-listeners.ts b/enterprise/frontend/src/metabase-enterprise/remote_sync/middleware/register-listeners.ts
new file mode 100644
index 00000000000..09e1c2f2c9d
--- /dev/null
+++ b/enterprise/frontend/src/metabase-enterprise/remote_sync/middleware/register-listeners.ts
@@ -0,0 +1,287 @@
+import type {
+ ListenerEffectAPI,
+ ListenerMiddlewareInstance,
+ ThunkDispatch,
+ TypedStartListening,
+ UnknownAction,
+} from "@reduxjs/toolkit";
+import { isAnyOf } from "@reduxjs/toolkit";
+
+import { Api } from "metabase/api";
+import type { Card, Collection, Dashboard, Document } from "metabase-types/api";
+import type { State } from "metabase-types/store";
+
+import { REMOTE_SYNC_INVALIDATION_TAGS, TRANSFORMS_KEY } from "../constants";
+
+import { MODEL_MUTATION_CONFIGS } from "./model-configs";
+import {
+ type EndpointMatcher,
+ InvalidationType,
+ type ModelMutationConfig,
+ type ModelWithRemoteSynced,
+} from "./model-invalidation-config";
+
+type AppDispatch = ThunkDispatch<State, unknown, UnknownAction>;
+type AppListenerEffectAPI = ListenerEffectAPI<State, AppDispatch>;
+type AppStartListening = TypedStartListening<State, AppDispatch>;
+
+/**
+ * Wrapper type for the listener middleware that provides proper typing
+ */
+type TypedListenerMiddleware = {
+ startListening: AppStartListening;
+};
+
+function invalidateRemoteSyncTags(dispatch: AppDispatch) {
+ // Type assertion needed because Api is typed with base TagType,
+ // but enterprise uses EnterpriseTagType which is a superset
+ dispatch(Api.util.invalidateTags(REMOTE_SYNC_INVALIDATION_TAGS as never));
+}
+
+function shouldInvalidateForRemoteSyncedModel(
+ oldModel: Card | Dashboard | Document | undefined,
+ newModel: Card | Dashboard | Document,
+): boolean {
+ const oldSynced = oldModel?.is_remote_synced ?? false;
+ const newSynced = newModel.is_remote_synced ?? false;
+
+ return oldSynced !== newSynced || newSynced;
+}
+
+function isTransformsSyncEnabled(state: State): boolean {
+ return !!state.settings?.values?.[TRANSFORMS_KEY];
+}
+
+function shouldInvalidateForCollection(
+ oldCollection: Collection | undefined,
+ newCollection: Collection | undefined,
+ state?: State,
+): boolean {
+ if (!newCollection) {
+ return false;
+ }
+
+ const oldSynced = oldCollection?.is_remote_synced ?? false;
+ const newSynced = newCollection.is_remote_synced ?? false;
+
+ // Invalidate if the collection is remote synced (or was)
+ if (oldSynced || newSynced) {
+ return true;
+ }
+
+ // Also invalidate for transforms namespace collections when transforms sync is enabled
+ if (
+ state &&
+ isTransformsSyncEnabled(state) &&
+ newCollection.namespace === "transforms"
+ ) {
+ return true;
+ }
+
+ return false;
+}
+
+/**
+ * Helper to extract payload from an RTK action.
+ * Returns undefined if the action doesn't have a payload property.
+ */
+function getActionPayload<T>(action: UnknownAction): T | undefined {
+ if (action && typeof action === "object" && "payload" in action) {
+ return action.payload as T;
+ }
+ return undefined;
+}
+
+/**
+ * Creates a combined matcher from an array of endpoint matchers.
+ * Type assertion is required because TypeScript cannot infer the union
+ * of all action types when matchers are provided dynamically.
+ */
+function createMatcher(endpoints: EndpointMatcher[]) {
+ // isAnyOf expects at least one argument, and we need to cast because
+ // the dynamic array loses type information about the specific action types
+ return isAnyOf(...(endpoints as [EndpointMatcher, ...EndpointMatcher[]]));
+}
+
+function registerCreateListeners(
+ middleware: TypedListenerMiddleware,
+ config: ModelMutationConfig,
+) {
+ if (!config.createEndpoints?.length) {
+ return;
+ }
+
+ middleware.startListening({
+ matcher: createMatcher(config.createEndpoints),
+ effect: async (
+ action: UnknownAction,
+ { dispatch, getState }: AppListenerEffectAPI,
+ ) => {
+ const { invalidation } = config;
+
+ switch (invalidation.type) {
+ case InvalidationType.Always:
+ invalidateRemoteSyncTags(dispatch);
+ break;
+
+ case InvalidationType.RemoteSyncedChange: {
+ const payload = getActionPayload<ModelWithRemoteSynced>(action);
+ if (payload?.is_remote_synced) {
+ invalidateRemoteSyncTags(dispatch);
+ }
+ break;
+ }
+
+ case InvalidationType.CollectionBased: {
+ const collection = getActionPayload<Collection>(action);
+ if (
+ shouldInvalidateForCollection(undefined, collection, getState())
+ ) {
+ invalidateRemoteSyncTags(dispatch);
+ }
+ break;
+ }
+ }
+ },
+ });
+}
+
+function registerUpdateListeners(
+ middleware: TypedListenerMiddleware,
+ config: ModelMutationConfig,
+) {
+ if (!config.updateEndpoints?.length) {
+ return;
+ }
+
+ middleware.startListening({
+ matcher: createMatcher(config.updateEndpoints),
+ effect: async (
+ action: UnknownAction,
+ { getOriginalState, dispatch }: AppListenerEffectAPI,
+ ) => {
+ const { invalidation } = config;
+
+ switch (invalidation.type) {
+ case InvalidationType.Always:
+ invalidateRemoteSyncTags(dispatch);
+ break;
+
+ case InvalidationType.RemoteSyncedChange: {
+ const newModel = getActionPayload<Card | Dashboard | Document>(
+ action,
+ );
+ if (!newModel) {
+ break;
+ }
+ const oldModel = invalidation.getOriginal(
+ getOriginalState(),
+ newModel.id as number,
+ );
+ if (shouldInvalidateForRemoteSyncedModel(oldModel, newModel)) {
+ invalidateRemoteSyncTags(dispatch);
+ }
+ break;
+ }
+
+ case InvalidationType.CollectionBased: {
+ const newCollection = getActionPayload<Collection>(action);
+ if (!newCollection) {
+ break;
+ }
+ const originalState = getOriginalState();
+ const oldCollection = invalidation.getOriginalCollection(
+ originalState,
+ newCollection.id,
+ );
+ if (
+ shouldInvalidateForCollection(
+ oldCollection,
+ newCollection,
+ originalState,
+ )
+ ) {
+ invalidateRemoteSyncTags(dispatch);
+ }
+ break;
+ }
+ }
+ },
+ });
+}
+
+function registerDeleteListeners(
+ middleware: TypedListenerMiddleware,
+ config: ModelMutationConfig,
+) {
+ if (!config.deleteEndpoints?.length) {
+ return;
+ }
+
+ middleware.startListening({
+ matcher: createMatcher(config.deleteEndpoints),
+ effect: async (
+ action: UnknownAction,
+ { getOriginalState, dispatch }: AppListenerEffectAPI,
+ ) => {
+ const { invalidation, getDeleteId } = config;
+
+ switch (invalidation.type) {
+ case InvalidationType.Always:
+ invalidateRemoteSyncTags(dispatch);
+ break;
+
+ case InvalidationType.RemoteSyncedChange: {
+ const id = getDeleteId?.(action);
+ if (id == null) {
+ break;
+ }
+ const model = invalidation.getOriginal(
+ getOriginalState(),
+ id as number,
+ );
+ if (model?.is_remote_synced) {
+ invalidateRemoteSyncTags(dispatch);
+ }
+ break;
+ }
+
+ case InvalidationType.CollectionBased: {
+ const deleteRequest = getDeleteId?.(action);
+ const id =
+ typeof deleteRequest === "object"
+ ? deleteRequest?.id
+ : deleteRequest;
+ if (id == null) {
+ break;
+ }
+ const originalState = getOriginalState();
+ const collection = invalidation.getOriginalCollection(
+ originalState,
+ id,
+ );
+ if (
+ shouldInvalidateForCollection(undefined, collection, originalState)
+ ) {
+ invalidateRemoteSyncTags(dispatch);
+ }
+ break;
+ }
+ }
+ },
+ });
+}
+
+/**
+ * Registers all model mutation listeners based on the MODEL_MUTATION_CONFIGS.
+ * This replaces the individual listener registrations with a data-driven approach.
+ */
+export function registerModelMutationListeners(
+ middleware: ListenerMiddlewareInstance<State, AppDispatch>,
+) {
+ for (const config of MODEL_MUTATION_CONFIGS) {
+ registerCreateListeners(middleware, config);
+ registerUpdateListeners(middleware, config);
+ registerDeleteListeners(middleware, config);
+ }
+}
diff --git a/enterprise/frontend/src/metabase-enterprise/remote_sync/middleware/remote-sync-listener-middleware.ts b/enterprise/frontend/src/metabase-enterprise/remote_sync/middleware/remote-sync-listener-middleware.ts
index feae708bba7..d062ba0d56b 100644
--- a/enterprise/frontend/src/metabase-enterprise/remote_sync/middleware/remote-sync-listener-middleware.ts
+++ b/enterprise/frontend/src/metabase-enterprise/remote_sync/middleware/remote-sync-listener-middleware.ts
@@ -1,34 +1,11 @@
-import type { PayloadAction } from "@reduxjs/toolkit";
-import { createListenerMiddleware, isAnyOf } from "@reduxjs/toolkit";
+import type { ThunkDispatch, UnknownAction } from "@reduxjs/toolkit";
+import { createListenerMiddleware } from "@reduxjs/toolkit";
import { Api } from "metabase/api";
-import { cardApi } from "metabase/api/card";
-import { collectionApi } from "metabase/api/collection";
-import { dashboardApi } from "metabase/api/dashboard";
-import { documentApi } from "metabase/api/document";
-import { fieldApi } from "metabase/api/field";
-import { segmentApi } from "metabase/api/segment";
-import { tableApi as coreTableApi } from "metabase/api/table";
-import { timelineApi } from "metabase/api/timeline";
-import { timelineEventApi } from "metabase/api/timeline-event";
-import { getCollectionFromCollectionsTree } from "metabase/selectors/collection";
-import { getSetting } from "metabase/selectors/settings";
import { EnterpriseApi } from "metabase-enterprise/api/api";
import { remoteSyncApi } from "metabase-enterprise/api/remote-sync";
-import { tableApi as enterpriseTableApi } from "metabase-enterprise/api/table";
import { tag } from "metabase-enterprise/api/tags";
-import { transformApi } from "metabase-enterprise/api/transform";
-import { transformTagApi } from "metabase-enterprise/api/transform-tag";
-import type {
- Card,
- CardId,
- Collection,
- CollectionId,
- Dashboard,
- DashboardId,
- Document,
- RemoteSyncTaskStatus,
-} from "metabase-types/api";
+import type { RemoteSyncTaskStatus } from "metabase-types/api";
import type { State } from "metabase-types/store";
import { REMOTE_SYNC_INVALIDATION_TAGS } from "../constants";
@@ -39,10 +16,19 @@ import {
taskUpdated,
} from "../sync-task-slice";
+import { registerModelMutationListeners } from "./register-listeners";
+
+type AppDispatch = ThunkDispatch<State, unknown, UnknownAction>;
+
export const remoteSyncListenerMiddleware = createListenerMiddleware<State>();
-function invalidateRemoteSyncTags(dispatch: any) {
- dispatch(Api.util.invalidateTags(REMOTE_SYNC_INVALIDATION_TAGS as any));
+// Register all model mutation listeners using the data-driven configuration
+registerModelMutationListeners(remoteSyncListenerMiddleware);
+
+function invalidateRemoteSyncTags(dispatch: AppDispatch) {
+ // Type assertion needed because Api is typed with base TagType,
+ // but enterprise uses EnterpriseTagType which is a superset
+ dispatch(Api.util.invalidateTags(REMOTE_SYNC_INVALIDATION_TAGS as never));
}
const ALL_INVALIDATION_TAGS = [
@@ -77,356 +63,6 @@ const ALL_INVALIDATION_TAGS = [
tag("python-transform-library"),
];
-function shouldInvalidateForEntity(
- oldEntity: Card | Dashboard | Document | undefined,
- newEntity: Card | Dashboard | Document,
-): boolean {
- const oldSynced = oldEntity?.is_remote_synced ?? false;
- const newSynced = newEntity.is_remote_synced ?? false;
-
- return oldSynced !== newSynced || newSynced;
-}
-
-function isTransformsNamespaceCollection(
- collection: Collection | undefined,
-): boolean {
- return collection?.namespace === "transforms";
-}
-
-function shouldInvalidateForCollection(
- oldCollection: Collection | undefined,
- newCollection: Collection | undefined,
- isTransformsSyncEnabled: boolean | null | undefined,
-): boolean {
- if (!newCollection) {
- return false;
- }
-
- const oldSynced = oldCollection?.is_remote_synced ?? false;
- const newSynced = newCollection.is_remote_synced ?? false;
-
- // Check if either collection is remote-synced
- if (oldSynced || newSynced) {
- return true;
- }
-
- // Check if either collection is in transforms namespace with transforms sync enabled
- if (isTransformsSyncEnabled) {
- if (
- isTransformsNamespaceCollection(oldCollection) ||
- isTransformsNamespaceCollection(newCollection)
- ) {
- return true;
- }
- }
-
- return false;
-}
-
-function getOriginalDocument(originalState: State, id: number) {
- return (
- documentApi.endpoints.getDocument.select({ id })(originalState as any)
- ?.data || originalState.entities.documents[id]
- );
-}
-
-function getOriginalDashboard(originalState: State, id: DashboardId) {
- return (
- dashboardApi.endpoints.getDashboard.select({ id })(originalState as any)
- ?.data || originalState.entities.dashboards[id]
- );
-}
-
-function getOriginalCard(originalState: State, id: CardId) {
- return (
- cardApi.endpoints.getCard.select({ id })(originalState as any)?.data ||
- originalState.entities.questions[id]
- );
-}
-
-function getOriginalCollection(originalState: State, id: CollectionId) {
- return (
- collectionApi.endpoints.getCollection.select({ id })(originalState)?.data ||
- getCollectionFromCollectionsTree(originalState, id)
- );
-}
-
-remoteSyncListenerMiddleware.startListening({
- matcher: isAnyOf(
- cardApi.endpoints.createCard.matchFulfilled,
- dashboardApi.endpoints.createDashboard.matchFulfilled,
- documentApi.endpoints.createDocument.matchFulfilled,
- ),
- effect: async (
- action: PayloadAction<Card | Dashboard | Document>,
- { dispatch },
- ) => {
- const entity = action.payload;
-
- if (entity.is_remote_synced) {
- invalidateRemoteSyncTags(dispatch);
- }
- },
-});
-
-remoteSyncListenerMiddleware.startListening({
- matcher: cardApi.endpoints.updateCard.matchFulfilled,
- effect: async (
- action: PayloadAction<Card>,
- { getOriginalState, dispatch },
- ) => {
- const newCard = action.payload;
- const oldCard = getOriginalCard(getOriginalState(), newCard.id);
-
- if (shouldInvalidateForEntity(oldCard, newCard)) {
- invalidateRemoteSyncTags(dispatch);
- }
- },
-});
-
-remoteSyncListenerMiddleware.startListening({
- matcher: dashboardApi.endpoints.updateDashboard.matchFulfilled,
- effect: async (
- action: PayloadAction<Dashboard>,
- { getOriginalState, dispatch },
- ) => {
- const newDashboard = action.payload;
- const oldDashboard = getOriginalDashboard(
- getOriginalState(),
- newDashboard.id,
- );
-
- if (shouldInvalidateForEntity(oldDashboard, newDashboard)) {
- invalidateRemoteSyncTags(dispatch);
- }
- },
-});
-
-remoteSyncListenerMiddleware.startListening({
- matcher: documentApi.endpoints.updateDocument.matchFulfilled,
- effect: async (
- action: PayloadAction<Document>,
- { getOriginalState, dispatch },
- ) => {
- const newDocument = action.payload;
- const oldDocument = getOriginalDocument(getOriginalState(), newDocument.id);
-
- if (shouldInvalidateForEntity(oldDocument, newDocument)) {
- invalidateRemoteSyncTags(dispatch);
- }
- },
-});
-
-remoteSyncListenerMiddleware.startListening({
- matcher: cardApi.endpoints.deleteCard.matchFulfilled,
- effect: async (action, { getOriginalState, dispatch }) => {
- const id = (action.meta as any).arg.originalArgs as number;
-
- if (id) {
- const card = getOriginalCard(getOriginalState(), id);
-
- if (card?.is_remote_synced) {
- invalidateRemoteSyncTags(dispatch);
- }
- }
- },
-});
-
-remoteSyncListenerMiddleware.startListening({
- matcher: dashboardApi.endpoints.deleteDashboard.matchFulfilled,
- effect: async (action, { getOriginalState, dispatch }) => {
- const id = (action.meta as any).arg.originalArgs as number;
-
- if (id) {
- const dashboard = getOriginalDashboard(getOriginalState(), id);
-
- if (dashboard?.is_remote_synced) {
- invalidateRemoteSyncTags(dispatch);
- }
- }
- },
-});
-
-remoteSyncListenerMiddleware.startListening({
- matcher: documentApi.endpoints.deleteDocument.matchFulfilled,
- effect: async (action, { getOriginalState, dispatch }) => {
- const id = (action.meta as any).arg.originalArgs as number;
-
- if (id) {
- const document = getOriginalDocument(getOriginalState(), id);
-
- if (document?.is_remote_synced) {
- invalidateRemoteSyncTags(dispatch);
- }
- }
- },
-});
-
-remoteSyncListenerMiddleware.startListening({
- matcher: collectionApi.endpoints.createCollection.matchFulfilled,
- effect: async (action: PayloadAction<Collection>, { dispatch, getState }) => {
- const collection = action.payload;
- const isTransformsSyncEnabled = getSetting(
- getState(),
- "remote-sync-transforms",
- );
-
- if (
- collection.is_remote_synced ||
- (isTransformsSyncEnabled && isTransformsNamespaceCollection(collection))
- ) {
- invalidateRemoteSyncTags(dispatch);
- }
- },
-});
-
-remoteSyncListenerMiddleware.startListening({
- matcher: collectionApi.endpoints.updateCollection.matchFulfilled,
- effect: async (
- action: PayloadAction<Collection>,
- { getOriginalState, dispatch, getState },
- ) => {
- const newCollection = action.payload;
- const oldCollection = getOriginalCollection(
- getOriginalState(),
- newCollection.id,
- );
- const isTransformsSyncEnabled = getSetting(
- getState(),
- "remote-sync-transforms",
- );
-
- if (
- shouldInvalidateForCollection(
- oldCollection,
- newCollection,
- isTransformsSyncEnabled,
- )
- ) {
- invalidateRemoteSyncTags(dispatch);
- }
- },
-});
-
-remoteSyncListenerMiddleware.startListening({
- matcher: collectionApi.endpoints.deleteCollection.matchFulfilled,
- effect: async (action, { getOriginalState, dispatch, getState }) => {
- const deleteRequest = (action.meta as any).arg.originalArgs as {
- id: number;
- };
-
- if (deleteRequest?.id) {
- const collection = getOriginalCollection(
- getOriginalState(),
- deleteRequest.id,
- );
- const isTransformsSyncEnabled = getSetting(
- getState(),
- "remote-sync-transforms",
- );
-
- if (
- collection?.is_remote_synced ||
- (isTransformsSyncEnabled && isTransformsNamespaceCollection(collection))
- ) {
- invalidateRemoteSyncTags(dispatch);
- }
- }
- },
-});
-
-remoteSyncListenerMiddleware.startListening({
- matcher: isAnyOf(
- timelineApi.endpoints.createTimeline.matchFulfilled,
- timelineApi.endpoints.updateTimeline.matchFulfilled,
- timelineApi.endpoints.deleteTimeline.matchFulfilled,
- ),
- effect: async (_action, { dispatch }) => {
- invalidateRemoteSyncTags(dispatch);
- },
-});
-
-remoteSyncListenerMiddleware.startListening({
- matcher: isAnyOf(
- timelineEventApi.endpoints.createTimelineEvent.matchFulfilled,
- timelineEventApi.endpoints.updateTimelineEvent.matchFulfilled,
- timelineEventApi.endpoints.deleteTimelineEvent.matchFulfilled,
- ),
- effect: async (_action, { dispatch }) => {
- invalidateRemoteSyncTags(dispatch);
- },
-});
-
-// Enterprise table mutations (publish/unpublish/edit)
-remoteSyncListenerMiddleware.startListening({
- matcher: isAnyOf(
- enterpriseTableApi.endpoints.publishTables.matchFulfilled,
- enterpriseTableApi.endpoints.unpublishTables.matchFulfilled,
- enterpriseTableApi.endpoints.editTables.matchFulfilled,
- ),
- effect: async (_action, { dispatch }) => {
- invalidateRemoteSyncTags(dispatch);
- },
-});
-
-// Core table mutations (metadata edits)
-remoteSyncListenerMiddleware.startListening({
- matcher: isAnyOf(
- coreTableApi.endpoints.updateTable.matchFulfilled,
- coreTableApi.endpoints.updateTableFieldsOrder.matchFulfilled,
- ),
- effect: async (_action, { dispatch }) => {
- invalidateRemoteSyncTags(dispatch);
- },
-});
-
-// Field mutations
-remoteSyncListenerMiddleware.startListening({
- matcher: isAnyOf(
- fieldApi.endpoints.updateField.matchFulfilled,
- fieldApi.endpoints.createFieldDimension.matchFulfilled,
- fieldApi.endpoints.deleteFieldDimension.matchFulfilled,
- ),
- effect: async (_action, { dispatch }) => {
- invalidateRemoteSyncTags(dispatch);
- },
-});
-
-// Segment mutations
-remoteSyncListenerMiddleware.startListening({
- matcher: isAnyOf(
- segmentApi.endpoints.createSegment.matchFulfilled,
- segmentApi.endpoints.updateSegment.matchFulfilled,
- ),
- effect: async (_action, { dispatch }) => {
- invalidateRemoteSyncTags(dispatch);
- },
-});
-
-// Transform mutations
-remoteSyncListenerMiddleware.startListening({
- matcher: isAnyOf(
- transformApi.endpoints.createTransform.matchFulfilled,
- transformApi.endpoints.updateTransform.matchFulfilled,
- transformApi.endpoints.deleteTransform.matchFulfilled,
- ),
- effect: async (_action, { dispatch }) => {
- invalidateRemoteSyncTags(dispatch);
- },
-});
-
-// Transform tag mutations
-remoteSyncListenerMiddleware.startListening({
- matcher: isAnyOf(
- transformTagApi.endpoints.createTransformTag.matchFulfilled,
- transformTagApi.endpoints.updateTransformTag.matchFulfilled,
- transformTagApi.endpoints.deleteTransformTag.matchFulfilled,
- ),
- effect: async (_action, { dispatch }) => {
- invalidateRemoteSyncTags(dispatch);
- },
-});
-
remoteSyncListenerMiddleware.startListening({
matcher: remoteSyncApi.endpoints.exportChanges.matchPending,
effect: async (_action, { dispatch }) => {
diff --git a/enterprise/frontend/src/metabase-enterprise/remote_sync/utils.ts b/enterprise/frontend/src/metabase-enterprise/remote_sync/utils.ts
index 4fa5eaeaac6..56833cb08d3 100644
--- a/enterprise/frontend/src/metabase-enterprise/remote_sync/utils.ts
+++ b/enterprise/frontend/src/metabase-enterprise/remote_sync/utils.ts
@@ -184,10 +184,11 @@ export const getCollectionPathSegments = (
const TABLE_CHILD_MODELS: Set<RemoteSyncEntityModel> = new Set([
"field",
"segment",
+ "measure",
]);
/**
- * Check if a model type is a child of a table (field or segment)
+ * Check if a model type is a child of a table (field, segment, or measure)
*/
export const isTableChildModel = (model: RemoteSyncEntityModel): boolean => {
return TABLE_CHILD_MODELS.has(model);
diff --git a/enterprise/frontend/src/metabase-enterprise/remote_sync/utils.unit.spec.ts b/enterprise/frontend/src/metabase-enterprise/remote_sync/utils.unit.spec.ts
index 61dae16b2cb..2acb0b203a2 100644
--- a/enterprise/frontend/src/metabase-enterprise/remote_sync/utils.unit.spec.ts
+++ b/enterprise/frontend/src/metabase-enterprise/remote_sync/utils.unit.spec.ts
@@ -1,6 +1,10 @@
import { createMockCollection } from "metabase-types/api/mocks";
-import { buildCollectionMap, getCollectionPathSegments } from "./utils";
+import {
+ buildCollectionMap,
+ getCollectionPathSegments,
+ isTableChildModel,
+} from "./utils";
describe("remote_sync utils", () => {
describe("buildCollectionMap", () => {
@@ -188,4 +192,30 @@ describe("remote_sync utils", () => {
expect(segments[2]).toEqual({ id: 3, name: "Grandchild" });
});
});
+
+ describe("isTableChildModel", () => {
+ it("should return true for field model", () => {
+ expect(isTableChildModel("field")).toBe(true);
+ });
+
+ it("should return true for segment model", () => {
+ expect(isTableChildModel("segment")).toBe(true);
+ });
+
+ it("should return true for measure model", () => {
+ expect(isTableChildModel("measure")).toBe(true);
+ });
+
+ it("should return false for card model", () => {
+ expect(isTableChildModel("card")).toBe(false);
+ });
+
+ it("should return false for table model", () => {
+ expect(isTableChildModel("table")).toBe(false);
+ });
+
+ it("should return false for dashboard model", () => {
+ expect(isTableChildModel("dashboard")).toBe(false);
+ });
+ });
});
diff --git a/frontend/src/metabase-types/api/remote-sync.ts b/frontend/src/metabase-types/api/remote-sync.ts
index 933f40f3a84..78e7bc8f361 100644
--- a/frontend/src/metabase-types/api/remote-sync.ts
+++ b/frontend/src/metabase-types/api/remote-sync.ts
@@ -13,6 +13,7 @@ export type RemoteSyncEntityModel =
| "table"
| "field"
| "segment"
+ | "measure"
| "transform"
| "transformtag"
| "transformjob";
diff --git a/frontend/src/metabase/lib/urls/modelToUrl.ts b/frontend/src/metabase/lib/urls/modelToUrl.ts
index 3e2b50382d8..27aee4679a2 100644
--- a/frontend/src/metabase/lib/urls/modelToUrl.ts
+++ b/frontend/src/metabase/lib/urls/modelToUrl.ts
@@ -10,6 +10,7 @@ import { action } from "./actions";
import { browseDatabase } from "./browse";
import { collection } from "./collections";
import { dashboard } from "./dashboards";
+import { dataStudioPublishedTableMeasure } from "./data-studio";
import { document } from "./documents";
import { indexedEntity } from "./indexed-entities";
import { metric, model } from "./models";
@@ -80,6 +81,11 @@ export function modelToUrl(item: UrlableModel): string {
return tableRowsQuery(databaseId, item.table_id, undefined, item.id);
}
return NOT_FOUND_URL;
+ case "measure":
+ if (item.table_id != null) {
+ return dataStudioPublishedTableMeasure(item.table_id, item.id);
+ }
+ return NOT_FOUND_URL;
default:
return NOT_FOUND_URL;
}
diff --git a/frontend/src/metabase/lib/urls/modelToUrl.unit.spec.ts b/frontend/src/metabase/lib/urls/modelToUrl.unit.spec.ts
index 713e33ffe09..8209f9bdaf0 100644
--- a/frontend/src/metabase/lib/urls/modelToUrl.unit.spec.ts
+++ b/frontend/src/metabase/lib/urls/modelToUrl.unit.spec.ts
@@ -95,4 +95,25 @@ describe("urls > modelToUrl", () => {
}),
).toBe("/question#?db=456&table=789&segment=123");
});
+
+ it("should return a measure URL for a measure", () => {
+ expect(
+ modelToUrl({
+ model: "measure",
+ name: "My Cool Measure",
+ id: 123,
+ table_id: 456,
+ }),
+ ).toBe("/data-studio/library/tables/456/measures/123");
+ });
+
+ it("should return 404 for a measure without table_id", () => {
+ expect(
+ modelToUrl({
+ model: "measure",
+ name: "My Cool Measure",
+ id: 123,
+ }),
+ ).toBe("/404");
+ });
});
diff --git a/frontend/src/metabase/plugins/oss/remote-sync.ts b/frontend/src/metabase/plugins/oss/remote-sync.ts
index a566ecb3e0e..c4b4790f33c 100644
--- a/frontend/src/metabase/plugins/oss/remote-sync.ts
+++ b/frontend/src/metabase/plugins/oss/remote-sync.ts
@@ -54,7 +54,7 @@ export const PLUGIN_REMOTE_SYNC: {
GitSyncSetupMenuItem: ComponentType<GitSyncSetupMenuItemProps>;
CollectionsNavTree: ComponentType<CollectionsNavTreeProps> | null;
CollectionSyncStatusBadge: ComponentType | null;
- REMOTE_SYNC_INVALIDATION_TAGS: TagDescription<any>[] | null;
+ REMOTE_SYNC_INVALIDATION_TAGS: TagDescription<string>[] | null;
useSyncStatus: () => {
isIdle: boolean;
taskType: any;
diff --git a/src/metabase/measures/models/measure.clj b/src/metabase/measures/models/measure.clj
index 9f96b811562..7c544d9c531 100644
--- a/src/metabase/measures/models/measure.clj
+++ b/src/metabase/measures/models/measure.clj
@@ -11,6 +11,7 @@
[metabase.models.interface :as mi]
[metabase.models.serialization :as serdes]
[metabase.permissions.core :as perms]
+ [metabase.remote-sync.core :as remote-sync]
[metabase.search.core :as search]
[metabase.util :as u]
[metabase.util.i18n :refer [tru]]
@@ -46,9 +47,7 @@
(doto :model/Measure
(derive :metabase/model)
(derive :hook/timestamped?)
- (derive :hook/entity-id)
- (derive ::mi/write-policy.superuser)
- (derive ::mi/create-policy.superuser))
+ (derive :hook/entity-id))
(defmethod mi/can-read? :model/Measure
([instance]
@@ -62,6 +61,57 @@
([model pk]
(mi/can-read? (t2/select-one model pk))))
+;; Measures can be written by superusers, but only if the parent table is editable
+;; (not in a remote-synced collection in read-only mode).
+(defmethod mi/can-write? :model/Measure
+ ([instance]
+ (let [table (or (:table instance)
+ (t2/select-one :model/Table :id (:table_id instance)))]
+ (and (mi/superuser?)
+ (remote-sync/table-editable? table))))
+ ([model pk]
+ (mi/can-write? (t2/select-one model pk))))
+
+;; Measures can be created by superusers, but only if the parent table is editable
+;; (not in a remote-synced collection in read-only mode).
+(defmethod mi/can-create? :model/Measure
+ [_model instance]
+ (let [table (or (:table instance)
+ (t2/select-one :model/Table :id (:table_id instance)))]
+ (and (mi/superuser?)
+ (remote-sync/table-editable? table))))
+
+(methodical/defmethod t2/batched-hydrate [:model/Measure :can_write]
+ "Batched hydration for :can_write on measures. First hydrates :table for all measures,
+ then pre-fetches collection is_remote_synced values for those tables, and calls can-write?
+ on each measure. This avoids N+1 queries when checking permissions for multiple measures."
+ [_model k measures]
+ (let [measures-with-tables (t2/hydrate (remove nil? measures) :table)
+ ;; Get all unique collection IDs from the hydrated tables
+ collection-ids (->> measures-with-tables
+ (keep (comp :collection_id :table))
+ distinct)
+ ;; Batch fetch is_remote_synced for all collections
+ collection-synced-map (if (seq collection-ids)
+ (into {}
+ (map (juxt :id :is_remote_synced))
+ (t2/select :model/Collection :id [:in collection-ids]))
+ {})
+ ;; Associate collection info with each measure's table
+ measures-with-collection (for [measure measures-with-tables
+ :let [table (:table measure)
+ coll-id (:collection_id table)]]
+ (if (and table coll-id)
+ (assoc-in measure [:table :collection]
+ {:id coll-id
+ :is_remote_synced (get collection-synced-map coll-id false)})
+ measure))]
+ (mi/instances-with-hydrated-data
+ measures k
+ #(u/index-by :id mi/can-write? measures-with-collection)
+ :id
+ {:default false})))
+
(t2/define-before-insert :model/Measure
[{:keys [definition] :as measure}]
(validate-mbql5-definition definition)
diff --git a/src/metabase/warehouse_schema/models/table.clj b/src/metabase/warehouse_schema/models/table.clj
index 9f2f45dd4f4..540813627de 100644
--- a/src/metabase/warehouse_schema/models/table.clj
+++ b/src/metabase/warehouse_schema/models/table.clj
@@ -524,8 +524,13 @@
{:where [:and
[:= :table_id id]
(when skip-archived [:not :archived])]})]
- {["Segment" segment-id] {"Table" id}}))]
- (merge fields segments)))
+ {["Segment" segment-id] {"Table" id}}))
+ measures (into {} (for [measure-id (t2/select-pks-set :model/Measure
+ {:where [:and
+ [:= :table_id id]
+ (when skip-archived [:not :archived])]})]
+ {["Measure" measure-id] {"Table" id}}))]
+ (merge fields segments measures)))
(defmethod serdes/generate-path "Table" [_ table]
(let [db-name (t2/select-one-fn :name :model/Database :id (:db_id table))]