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
GHY-3726 merge non-conflicting remote changes on export instead of blocking (#75050)
Eric Normand0376648dc8a3
diff --git a/e2e/support/helpers/e2e-remote-sync-helpers.ts b/e2e/support/helpers/e2e-remote-sync-helpers.tsindex 0e009d5e8a5..6a8802d0039 100644--- a/e2e/support/helpers/e2e-remote-sync-helpers.ts+++ b/e2e/support/helpers/e2e-remote-sync-helpers.ts@@ -77,8 +77,11 @@ export function configureGitAndPullChanges(// Read-only mode automatically triggers an import, just wait for itpollForTask({ taskName: "import" });} else {- // Read-write mode needs manual import trigger- cy.request("POST", "/api/ee/remote-sync/import", {});+ // Read-write mode needs manual import trigger. expected_branch asserts the client's view of the+ // active branch against the setting (configureGit sets it to "main").+ cy.request("POST", "/api/ee/remote-sync/import", {+ expected_branch: "main",+ });pollForTask({ taskName: "import" });}}diff --git a/e2e/test/scenarios/admin/remote-sync.cy.spec.ts b/e2e/test/scenarios/admin/remote-sync.cy.spec.tsindex 7ade305111a..4eb5d04b261 100644--- a/e2e/test/scenarios/admin/remote-sync.cy.spec.ts+++ b/e2e/test/scenarios/admin/remote-sync.cy.spec.ts@@ -166,14 +166,14 @@ describe("Remote Sync", () => {H.clickPushOption();- // Attempt to push changes- cy.findByRole("dialog", { name: "Push to Git" })- .button(/Push changes/)- .click();-- // push local changes to a different branch, because the remote is ahead of us- cy.findByRole("dialog", { name: /branch is behind/ }).within(() => {- cy.findByRole("radio", { name: /Create a new branch/ }).click();+ // The remote has advanced, so pushing runs the preflight and opens the conflict modal directly+ // (no commit-message step first). The modal title mentions the remote branch whether the merge+ // is clean ("The remote branch has new changes…") or conflicting ("…conflict with the remote+ // branch…"). Push our changes to a new branch instead, because the remote is ahead of us.+ cy.findByRole("dialog", { name: /remote branch/ }).within(() => {+ cy.findByRole("radio", {+ name: /Create a new branch and push changes there/,+ }).click();cy.findByPlaceholderText("your-branch-name").type(NEW_BRANCH);cy.button("Push changes").click();});diff --git a/enterprise/backend/src/metabase_enterprise/remote_sync/api.clj b/enterprise/backend/src/metabase_enterprise/remote_sync/api.cljindex 485188c3f98..f3863372eec 100644--- a/enterprise/backend/src/metabase_enterprise/remote_sync/api.clj+++ b/enterprise/backend/src/metabase_enterprise/remote_sync/api.clj@@ -22,6 +22,24 @@(set! *warn-on-reflection* true)+(defn- check-branch-matches-setting!+ "Compare-and-swap guard against the multi-tab staleness hole: the client sends the branch it+ believes it is operating on, and we reject the request when that disagrees with the authoritative+ `remote-sync-branch` setting — e.g. another session switched branches since this client last loaded+ its settings. The setting stays the source of truth; `requested-branch` is only an assertion.++ Throws a 409 carrying `:branch_mismatch true` and the current `:current_branch` so the client can+ refresh its view and retry. Returns the (now-validated) branch on success."+ [requested-branch]+ (let [current (settings/remote-sync-branch)]+ (when-not (= requested-branch current)+ (throw (ex-info (format "The sync branch changed to '%s' in another session. Refresh and try again."+ current)+ {:status-code 409+ :branch_mismatch true+ :current_branch current})))+ requested-branch))+(api.macros/defendpoint :post "/import" :- remote-sync.schema/ImportResponse"Import Metabase content from configured Remote Sync source.@@ -35,15 +53,23 @@Requires superuser permissions."[_route_query- {:keys [branch force]} :- [:map [:branch {:optional true} ms/NonBlankString]- [:force {:optional true} :boolean]]]+ {:keys [branch force merge expected_branch]}+ :- [:map [:branch {:optional true} ms/NonBlankString]+ [:force {:optional true} :boolean]+ [:merge {:optional true} :boolean]+ ;; the branch the client believes is currently active; rejected if it disagrees with the+ ;; remote-sync-branch setting (a pull/switch from a stale tab). `branch` is the operational+ ;; target (it differs from this on a branch switch); `expected_branch` is only the assertion.+ [:expected_branch ms/NonBlankString]]](api/check-superuser)(api/check-400 (settings/remote-sync-enabled) "Remote sync is not configured.")+ (check-branch-matches-setting! expected_branch)(let [branch-name (or branch (settings/remote-sync-branch))user-id api/*current-user-id*{task-id :id}(impl/async-import!branch-name force {}+ :merge? (or merge false):on-success (fn [task-id _result](impl/publish-sync-event! :event/remote-sync-import task-id {:branch branch-name} user-id)))]{:status :success@@ -101,25 +127,51 @@Requires superuser permissions."[_route_query- {:keys [message branch force]}] :- [:map- [:message {:optional true} ms/NonBlankString]- [:branch {:optional true} ms/NonBlankString]- [:force {:optional true} :boolean]]+ {:keys [message branch force merge]} :- [:map+ [:message {:optional true} ms/NonBlankString]+ [:branch ms/NonBlankString]+ [:force {:optional true} :boolean]+ [:merge {:optional true} :boolean]]](api/check-superuser)(api/check-400 (settings/remote-sync-enabled) "Remote sync is not configured.")(api/check-400 (= (settings/remote-sync-type) :read-write) "Exports are only allowed when remote-sync-type is set to 'read-write'")- (let [branch-name (or branch (settings/remote-sync-branch))+ (let [branch-name (check-branch-matches-setting! branch)user-id api/*current-user-id*{task-id :id}(impl/async-export!branch-name(or force false)(or message "Exported from Metabase")+ :merge? (or merge false):on-success (fn [task-id _result](impl/publish-sync-event! :event/remote-sync-export task-id {:branch branch-name} user-id)))]{:message "Export task started":task_id task-id}))+(api.macros/defendpoint :get "/export-preflight" :- remote-sync.schema/ExportPreflightResponse+ "Dry-run preview of what pushing the current state would do given the live remote branch, without+ writing anything. Drives the UI's push decision (force / new branch / merge).++ Returns:+ - has_changes: whether the remote branch has advanced beyond the last synced version+ - clean: whether a 3-way merge would apply with no conflicts+ - conflicts: human-readable labels of the entities that conflict (empty when clean)+ - summary: counts of remote changes a merge would fold in+ - reason: \"history-rewritten\" when the remote was force-pushed/rebased so no merge base exists++ Requires superuser permissions."+ [_route+ {:keys [branch]} :- [:map [:branch ms/NonBlankString]]]+ (api/check-superuser)+ (api/check-400 (settings/remote-sync-enabled) "Remote sync is not configured.")+ (let [branch-name (check-branch-matches-setting! branch)+ {:keys [diverged? clean? conflicts summary reason]} (impl/preview-export-merge branch-name)]+ {:has_changes diverged?+ :clean clean?+ :conflicts conflicts+ :summary summary+ :reason (some-> reason name)}))+(api.macros/defendpoint :get "/current-task" :- [:maybe remote-sync.schema/SyncTask]"Get the current sync task"[]diff --git a/enterprise/backend/src/metabase_enterprise/remote_sync/impl.clj b/enterprise/backend/src/metabase_enterprise/remote_sync/impl.cljindex 3b78050bbee..b8a4d44feb4 100644--- a/enterprise/backend/src/metabase_enterprise/remote_sync/impl.clj+++ b/enterprise/backend/src/metabase_enterprise/remote_sync/impl.clj@@ -4,6 +4,7 @@[diehard.core :as dh][java-time.api :as t][metabase-enterprise.remote-sync.guards :as guards]+ [metabase-enterprise.remote-sync.merge :as remote-sync.merge][metabase-enterprise.remote-sync.models.remote-sync-object :as remote-sync.object][metabase-enterprise.remote-sync.models.remote-sync-task :as remote-sync.task][metabase-enterprise.remote-sync.settings :as settings]@@ -239,11 +240,135 @@(t2/update! :model/RemoteSyncObject :model_type model-type :model_id id{:file_path path})))))+(defn- branch-changed-since-scheduling?+ "Returns true if `pre-task-branch` was captured by the async-* function and the+ `remote-sync-branch` setting has since drifted to a different value. Used as a+ defense-in-depth check against any future code path that bypasses the operation-level+ guards and mutates the setting between scheduling and the work running."+ [pre-task-branch]+ (and (some? pre-task-branch)+ (not= pre-task-branch (settings/remote-sync-branch))))++(defn load-snapshot!+ "Loads a snapshot's serialized entities into the app DB and reconciles local state to match it:+ runs `load-metabase!`, toggles the `remote-sync-transforms` setting based on the snapshot's contents,+ deletes synced content not present in the snapshot, and refreshes the RemoteSyncObject table.++ Shared by [[import!]] (the pull path) and the post-merge reconcile in [[export!]] (where a clean merge+ brought remote changes that must now be applied locally). Returns the imported-data map.++ Does not set the task version — callers own that, but should pass it (and any other version/status+ bookkeeping) as the `:finalize!` thunk, which runs inside the same transaction as the object-table+ reconcile. That keeps the version pointer, RemoteSyncObject statuses, and the reconcile atomic: either+ they all commit or all roll back, so a crash can never leave the version advanced past stale local+ state or drop captured dirty markers (see [[import-merged!]])."+ [snapshot task-id sync-timestamp & {:keys [finalize!]}]+ (let [path-filters (mapv #(re-pattern (str % "/.*")) serialization/legal-top-level-paths)+ base-ingestable (source.p/->ingestable snapshot {:path-filters path-filters})+ has-transforms? (snapshot-has-transforms? base-ingestable)+ ingestable-snapshot (source.ingestable/wrap-progress-ingestable task-id 0.7 base-ingestable)+ load-result (serdes/with-cache+ (serialization/load-metabase! ingestable-snapshot))+ seen-paths (:seen load-result)+ imported-data (spec/extract-imported-entities seen-paths)]+ (remote-sync.task/update-progress! task-id 0.8)+ (when (and has-transforms?+ (not (settings/remote-sync-transforms)))+ (log/info "Detected transforms in remote source, enabling remote-sync-transforms setting")+ (settings/remote-sync-transforms! true))+ (t2/with-transaction [_conn]+ (remove-unsynced! (spec/all-syncable-collection-ids) imported-data)+ (sync-objects! sync-timestamp imported-data)+ ;; Record the actual repo path each entity was read from, so later renames/deletes resolve the+ ;; real file and stay on the incremental export fast-path.+ (record-exported-paths! (source.ingestable/cached-file-paths base-ingestable))+ (when finalize! (finalize!)))+ (when (and (not has-transforms?)+ (settings/remote-sync-transforms))+ (log/info "No transforms in remote source, disabling remote-sync-transforms setting")+ (settings/remote-sync-transforms! false))+ (remote-sync.task/update-progress! task-id 0.95)+ imported-data))++(defn- capture-dirty-objects+ "Returns the current non-synced RemoteSyncObject rows — the local changes that have not been pushed.+ Captured before a local-only merge so they can be restored afterwards (see [[import-merged!]])."+ []+ (t2/select :model/RemoteSyncObject {:where [:not= :status "synced"]}))++(defn- restore-dirty-objects!+ "Re-applies captured dirty statuses after a merge load (which marks everything 'synced'). For each+ captured row, updates the matching freshly-synced row's status, or re-inserts it when no row exists+ (e.g. a pending local deletion, whose entity is absent from the merged set)."+ [dirty-objects timestamp]+ (doseq [{:keys [model_type model_id status] :as row} dirty-objects]+ (if-let [existing (t2/select-one :model/RemoteSyncObject :model_type model_type :model_id model_id)]+ (t2/update! :model/RemoteSyncObject (:id existing)+ {:status status :status_changed_at timestamp})+ (t2/insert! :model/RemoteSyncObject+ (-> row (dissoc :id) (assoc :status_changed_at timestamp))))))++(defn- import-merged!+ "Local-only merge for the pull path. Performs an entity-identity 3-way merge of local state against the+ remote tip and, on a clean merge, applies the merged result to the LOCAL app DB only — it does not push.+ Remote-originated changes end up 'synced'; the un-pushed local changes are preserved as dirty so they+ can be pushed later. Sets the last version to the remote tip (so the branch is no longer 'behind').++ On a genuine same-entity conflict, returns `:conflict` without touching local state.++ `base-snapshot` is nil only when no safe 3-way merge is possible (no prior sync, or the base commit was+ orphaned by a force-push/rebase) — that surfaces as a conflict. When the remote has not advanced past+ the base, there is nothing to fold in, so it is a no-op success that keeps local changes dirty."+ [snapshot base-snapshot task-id sync-timestamp]+ (cond+ (nil? base-snapshot)+ {:status :conflict+ :version (source.p/version snapshot)+ :conflicts ["Remote history was rewritten (force-push or rebase); cannot merge automatically."]+ :message "Cannot merge: the remote branch history was rewritten. Discard local changes and pull, or push to a new branch."}++ ;; The remote hasn't advanced past the merge base — nothing to fold in. Keep local changes as-is (still+ ;; dirty, to be pushed later); a no-op success, not a spurious "history was rewritten" conflict.+ (= (source.p/version base-snapshot) (source.p/version snapshot))+ (u/prog1 {:status :success+ :version (source.p/version snapshot)+ :merge-summary {:added 0 :updated 0 :removed 0}}+ (log/info "Pull merge: remote has not advanced; keeping local changes unchanged"))++ :else+ (let [models (spec/extract-entities-for-export)+ {:keys [merged conflicts summary]}+ (source/compute-merge models snapshot base-snapshot task-id)]+ (if (seq conflicts)+ (u/prog1 {:status :conflict+ :version (source.p/version snapshot)+ :conflicts (mapv remote-sync.merge/conflict-label conflicts)+ :message "Import blocked: the same content was changed both locally and on the remote branch."}+ (log/infof "Pull merge conflict on %d entit(ies): %s"+ (count conflicts) (str/join ", " (:conflicts <>))))+ ;; Capture the local (un-pushed) changes before loading; the clean merge guarantees they are+ ;; disjoint from the remote changes, so restoring them reproduces exactly the local diff vs remote.+ ;; Restore + set-version run inside the load's transaction so that a crash can't leave the dirty+ ;; markers overwritten by the load (which would lose them — a retry captures nothing).+ (let [dirty-objects (capture-dirty-objects)]+ (load-snapshot! (source/specs->snapshot merged) task-id sync-timestamp+ :finalize! (fn []+ (restore-dirty-objects! dirty-objects sync-timestamp)+ (remote-sync.task/set-version! task-id (source.p/version snapshot))))+ (log/infof "Pull merge: folded in %d remote change(s) (added %d, updated %d, removed %d); kept %d local change(s)"+ (apply + (vals summary)) (:added summary) (:updated summary) (:removed summary)+ (count dirty-objects))+ {:status :success+ :version (source.p/version snapshot)+ :merge-summary summary})))))+(defn import!"Imports and reloads Metabase entities from a remote snapshot.Takes a SourceSnapshot instance, a RemoteSyncTask ID for progress tracking, and optional keyword arguments:- :force? - forces import even when the snapshot version matches the last imported version+ - :merge? - perform a local-only 3-way merge (keep un-pushed local changes) instead of overwriting local+ - :base-snapshot - the merge base (last synced version), required when :merge? is set- :pre-task-branch - the value of `remote-sync-branch` at scheduling time; if it differsfrom the current setting at task start, the import aborts with `:error` to protect dataintegrity (the load mutates the app DB, so we refuse to proceed when state has drifted)@@ -253,9 +378,8 @@Returns a map with :status (either :success or :error), :version, and :message keys. Various exceptions may bethrown during import and are caught and converted to error status maps."- [^SourceSnapshot snapshot task-id & {:keys [force? pre-task-branch]}]- (when (and (some? pre-task-branch)- (not= pre-task-branch (settings/remote-sync-branch)))+ [^SourceSnapshot snapshot task-id & {:keys [force? merge? base-snapshot pre-task-branch]}]+ (when (branch-changed-since-scheduling? pre-task-branch)(log/warnf "Aborting import: remote-sync-branch changed from %s to %s since task was scheduled"pre-task-branch (settings/remote-sync-branch))(throw (ex-info "Branch setting changed since task was scheduled; aborting to protect data integrity"@@ -266,57 +390,37 @@(let [sync-timestamp (t/instant)](if snapshot(try- (let [snapshot-version (source.p/version snapshot)- last-imported-version (remote-sync.task/last-version)- first-import? (nil? last-imported-version)- path-filters (mapv #(re-pattern (str % "/.*")) serialization/legal-top-level-paths)- base-ingestable (source.p/->ingestable snapshot {:path-filters path-filters})- has-transforms? (snapshot-has-transforms? base-ingestable)- {:keys [conflicts summary]} (get-conflicts base-ingestable first-import?)- ingestable-snapshot (source.ingestable/wrap-progress-ingestable task-id 0.7 base-ingestable)]- (cond- (and first-import? (not force?) (seq conflicts))- (u/prog1 {:status :conflict- :version (source.p/version snapshot)- :conflicts summary ; Keep backward compatibility: return set of category names- :conflict-details conflicts ; New: detailed conflict info- :message (format "Skipping import: snapshot version %s contains conflicts use force to override" snapshot-version)}- (log/infof (:message <>)))-- (and (not force?) (= last-imported-version snapshot-version))- (u/prog1 {:status :success- :version (source.p/version snapshot)- :message (format "Skipping import: snapshot version %s matches last imported version" snapshot-version)}- (log/infof (:message <>)))+ (if merge?+ (import-merged! snapshot base-snapshot task-id sync-timestamp)+ (let [snapshot-version (source.p/version snapshot)+ last-imported-version (remote-sync.task/last-version)+ first-import? (nil? last-imported-version)+ path-filters (mapv #(re-pattern (str % "/.*")) serialization/legal-top-level-paths)+ base-ingestable (source.p/->ingestable snapshot {:path-filters path-filters})+ {:keys [conflicts summary]} (get-conflicts base-ingestable first-import?)]+ (cond+ (and first-import? (not force?) (seq conflicts))+ (u/prog1 {:status :conflict+ :version (source.p/version snapshot)+ :conflicts summary ; Keep backward compatibility: return set of category names+ :conflict-details conflicts ; New: detailed conflict info+ :message (format "Skipping import: snapshot version %s contains conflicts use force to override" snapshot-version)}+ (log/infof (:message <>)))++ (and (not force?) (= last-imported-version snapshot-version))+ (u/prog1 {:status :success+ :version (source.p/version snapshot)+ :message (format "Skipping import: snapshot version %s matches last imported version" snapshot-version)}+ (log/infof (:message <>)))- :else- (let [load-result (serdes/with-cache- (serialization/load-metabase! ingestable-snapshot))- seen-paths (:seen load-result)- imported-data (spec/extract-imported-entities seen-paths)]- (remote-sync.task/update-progress! task-id 0.8)- (when (and has-transforms?- (not (settings/remote-sync-transforms)))- (log/info "Detected transforms in remote source, enabling remote-sync-transforms setting")- (settings/remote-sync-transforms! true))- (t2/with-transaction [_conn]- (remove-unsynced! (spec/all-syncable-collection-ids) imported-data)- (sync-objects! sync-timestamp imported-data)- ;; Record the actual repo path each entity was read from, so later renames/deletes- ;; resolve the real file and stay on the incremental export fast-path.- (record-exported-paths! (source.ingestable/cached-file-paths base-ingestable)))- (when (and (not has-transforms?)- (settings/remote-sync-transforms))- (log/info "No transforms in remote source, disabling remote-sync-transforms setting")- (settings/remote-sync-transforms! false))- (remote-sync.task/update-progress! task-id 0.95)- (remote-sync.task/set-version!- task-id- (source.p/version snapshot))- (log/info "Successfully reloaded entities from git repository")- {:status :success- :version (source.p/version snapshot)- :message "Successfully reloaded from git repository"})))+ :else+ (do+ (load-snapshot! snapshot task-id sync-timestamp+ :finalize! #(remote-sync.task/set-version! task-id (source.p/version snapshot)))+ (log/info "Successfully reloaded entities from git repository")+ {:status :success+ :version (source.p/version snapshot)+ :message "Successfully reloaded from git repository"}))))(catch Exception e(handle-import-exception e snapshot))(finally@@ -324,6 +428,50 @@{:status :error:message "Remote sync source is not enabled. Please configure MB_GIT_SOURCE_REPO_URL environment variable."})))+(defn- export-merged!+ "Export path taken when the remote branch has advanced beyond the last synced version (`base-snapshot`+ is the merge base). Performs an entity-identity 3-way merge of local state against the remote tip:+ - on a genuine conflict (same entity changed on both sides) returns a `:conflict` result;+ - on a clean merge, writes the merged set (fast-forwarding onto the remote tip), then reconciles the+ local app DB by loading the merged result (the 'pull' half), so local now contains the remote's+ changes. Returns a `:success` result with a `:merge-summary`."+ [source snapshot base-snapshot task-id message sync-timestamp models]+ (let [{:keys [status version conflicts summary]}+ (source/merge-and-store! models snapshot base-snapshot task-id message)]+ (case status+ :conflict+ (u/prog1 {:status :conflict+ :version (source.p/version snapshot)+ :conflicts (mapv remote-sync.merge/conflict-label conflicts)+ :merge-summary summary+ :message "Export blocked: the same content was changed both locally and on the remote branch."}+ (log/infof "Export merge conflict on %d entit(ies): %s"+ (count conflicts) (str/join ", " (:conflicts <>))))++ :success+ ;; Fold-in pull: the merge brought remote changes that aren't in the local app DB yet. Load the+ ;; merged result so local state matches what we just pushed, marking everything synced and advancing+ ;; the version atomically with the reconcile (set-version! runs only after the load, so a crash leaves+ ;; the version at the old base and a retry re-merges — the push is idempotent — rather than advancing+ ;; the pointer past un-reconciled local state).+ (if-let [merged-snapshot (source.p/snapshot-at source version)]+ (do+ (load-snapshot! merged-snapshot task-id sync-timestamp+ :finalize! (fn []+ (t2/update! :model/RemoteSyncObject {:status "synced" :status_changed_at sync-timestamp})+ (remote-sync.task/set-version! task-id version)))+ (log/infof "Exported with merge: folded in %d remote change(s) (added %d, updated %d, removed %d)"+ (apply + (vals summary)) (:added summary) (:updated summary) (:removed summary))+ {:status :success :version version :merge-summary summary})+ ;; The merge was pushed to `version`, but its commit can't be resolved locally (should not happen —+ ;; write-files! updates the local ref before returning). Fail loudly rather than silently advancing+ ;; the version while skipping the reconcile, which would leave local missing the folded-in remote+ ;; changes. The push already landed, so a retry sees the divergence and re-merges + reconciles.+ (throw (ex-info (format (str "Merge pushed to %s but its commit could not be resolved locally to "+ "reconcile the app DB; re-run the export to pull the merged changes.")+ version)+ {:version version}))))))+;;; ------------------------------------------- Incremental Export Fast-Path -------------------------------------------(def ^:private closure-opts@@ -516,6 +664,11 @@Takes a SourceSnapshot instance, a RemoteSyncTask ID for progress tracking, a commit message string, andoptional keyword arguments:+ - :force? - when true, overwrite the remote branch wholesale even if it has advanced (no merge)+ - :merge? - when true and the remote has advanced, perform a 3-way merge (rather than refusing)+ - :source - the Source the snapshot came from, used to resolve the merge base and the merged result+ - :base-snapshot - a snapshot of the last synced version (the merge base), supplied when the remote+ branch has advanced and a 3-way merge is required- :pre-task-branch - the value of `remote-sync-branch` at scheduling time; if it differsfrom the current setting at task start, the export aborts with `:error` (defense-in-depthagainst any path that mutates the setting between scheduling and the work running).@@ -525,43 +678,61 @@full export: re-serializes the entire synced set, writes it, marks all RemoteSyncObject rows synced,and records each entity's `file_path` so future renames/deletes can go incremental.- Returns a map with {:status :success}, {:status :error}, or throws an exception with an appropriate message."- [^SourceSnapshot snapshot task-id message & {:keys [pre-task-branch]}]- ;; Defense-in-depth: refuse to run if the branch setting drifted between scheduling and now.- (when (and (some? pre-task-branch)- (not= pre-task-branch (settings/remote-sync-branch)))+ Behavior when the remote branch has advanced beyond the last sync:+ - `force?` -> overwrite the remote wholesale (full re-serialize).+ - `merge?` -> 3-way merge; non-conflicting remote changes are merged in and reconciled into the+ local app DB; genuine same-entity conflicts return a `:conflict`.+ - neither (default) -> refuse with `:conflict` (the UI's export preflight decides force/branch/merge).++ When the remote has NOT advanced, takes the incremental fast-path (see `incremental-plan`) when+ possible, otherwise a full re-serialize.++ Returns a map with :status (`:success`, `:conflict`, or `:error`), and optionally :message, :version,+ and :merge-summary keys. Various exceptions are caught and converted to error status maps."+ [^SourceSnapshot snapshot task-id message & {:keys [force? merge? base-snapshot pre-task-branch] src :source}]+ (when (branch-changed-since-scheduling? pre-task-branch)(log/warnf "Aborting export: remote-sync-branch changed from %s to %s since task was scheduled"pre-task-branch (settings/remote-sync-branch))(throw (ex-info "Branch setting changed since task was scheduled; aborting to protect data integrity"{:pre-task-branch pre-task-branch:current-branch (settings/remote-sync-branch)})))-(when (not snapshot)(throw (ex-info "Remote sync source is not enabled. Please configure MB_GIT_SOURCE_REPO_URL environment variable." {})))-(let [sync-timestamp (t/instant)](try(analytics/inc! :metabase-remote-sync/exports)(serdes/with-cache- (let [disabled-files (filterv (comp (disabled-content-dirs) path-top-level-dir)- (source.p/list-files snapshot))- dirty-rows (seq (remote-sync.object/dirty-rows))+ (let [base-version (remote-sync.task/last-version)+ remote-version (source.p/version snapshot)+ diverged? (and (some? base-version)+ (not= base-version remote-version))+ ;; Pending-change inputs for the undiverged path; only computed there (incremental-plan can+ ;; be expensive, and they are unused on the force/merge branches).+ normal? (and (not force?) (not diverged?))+ disabled-files (when normal?+ (filterv (comp (disabled-content-dirs) path-top-level-dir)+ (source.p/list-files snapshot)))+ dirty-rows (when normal? (seq (remote-sync.object/dirty-rows)))plan (when dirty-rows (incremental-plan snapshot dirty-rows))](cond- ;; nothing to do: no pending changes and no stale files in now-disabled content dirs- (and (empty? dirty-rows) (empty? disabled-files))+ ;; Forced overwrite — full re-serialize, replacing managed dirs (discards remote divergence).+ force?+ (full-export! snapshot task-id message sync-timestamp)++ ;; Remote hasn't advanced and there's nothing to export: no dirty rows and no stale files in+ ;; now-disabled content dirs.+ (and (not diverged?) (empty? dirty-rows) (empty? disabled-files))(do(log/info "Remote sync export: no changes to export"){:status :success})- ;; a dirty row can't be applied incrementally → full re-serialize- (= plan :remote-sync/unsyncable-batch)+ ;; Remote hasn't advanced but a dirty row can't be applied incrementally → full re-serialize.+ (and (not diverged?) (= plan :remote-sync/unsyncable-batch))(full-export! snapshot task-id message sync-timestamp)- ;; Incremental fast-path: write only the changed entities, and delete their old paths plus- ;; any files left behind in now-disabled content dirs — preserving every other file. Avoids- ;; re-serializing the entire synced set.- :else+ ;; Remote hasn't advanced — incremental fast-path: write only the changed entities, deleting+ ;; their old paths plus files left behind in now-disabled content dirs, preserving every other.+ (not diverged?)(let [{:keys [upserts delete-paths synced removed-ids]} plandelete-paths (into (vec delete-paths) disabled-files)](remote-sync.task/update-progress! task-id 0.3)@@ -574,7 +745,27 @@(t2/delete! :model/RemoteSyncObject :id [:in removed-ids]))(log/infof "Remote sync incremental export: wrote %d, deleted %d"(count upserts) (count delete-paths))- {:status :success}))))+ {:status :success})++ ;; Remote advanced and the caller did not ask to merge — refuse. The UI's export preflight+ ;; drives the choice between force, new branch, and merge.+ (not merge?)+ {:status :conflict+ :version remote-version+ :conflicts []+ :message "The remote branch has changed since your last sync. Choose how to proceed."}++ ;; Merge requested but the merge base is gone (force-push/rebase) — no safe 3-way merge.+ (nil? base-snapshot)+ {:status :conflict+ :version remote-version+ :conflicts ["Remote history was rewritten (force-push or rebase); cannot merge automatically."]+ :message "Cannot merge: the remote branch history was rewritten. Re-import then export, or force the export to overwrite."}++ ;; Merge: 3-way merge, push the result, and reconcile the local app DB.+ :else+ (export-merged! src snapshot base-snapshot task-id message sync-timestamp+ (spec/extract-entities-for-export)))))(catch Exception e;; handle-task-result! records the failure on this result, and skips entirely when the task;; was already cancelled (ended_at set) — so cancellation needs no special case here.@@ -799,52 +990,105 @@callback that receives [task-id result] after a successful import. Checks for dirty changes and throws anexception if force? is false and changes exist.+ When `:merge?` is set, a local-only 3-way merge keeps un-pushed local changes instead of overwriting+ them, so the dirty-changes guard is skipped.+Returns a RemoteSyncTask. Throws ExceptionInfo with status 400 and :conflicts true if there- are unsaved changes and force? is false."- [branch force? import-args & {:keys [on-success]}]+ are unsaved changes and neither force? nor merge? is set."+ [branch force? import-args & {:keys [on-success merge?]}](guards/ensure-no-active-task!)- (let [pre-task-branch (settings/remote-sync-branch)- source (source/source-from-settings branch)- has-dirty? (remote-sync.object/dirty?)]- (when (and has-dirty? (not force?))+ (let [pre-task-branch (settings/remote-sync-branch)+ source (source/source-from-settings branch)+ has-dirty? (remote-sync.object/dirty?)+ snapshot (source.p/snapshot source)+ ;; the merge base for a merge pull. When the remote has not advanced it is the remote tip itself+ ;; (base == theirs, so import-merged! no-ops and keeps local dirty). When the remote has advanced+ ;; it's the last-synced commit, which may be nil if orphaned by a force-push/rebase (→ conflict).+ ;; nil also when there's no prior sync. Resolved only for a merge.+ last-task-version (remote-sync.task/last-version)+ base-snapshot (when (and merge? (some? last-task-version))+ (if (= last-task-version (source.p/version snapshot))+ snapshot+ (source.p/snapshot-at source last-task-version)))]+ (when (and has-dirty? (not force?) (not merge?))(throw (ex-info "There are unsaved changes in the Remote Sync collection which will be overwritten by the import. Force the import to discard these changes."{:status-code 400:conflicts true})))(run-async! "import" branch(fn [task-id]- (import! (source.p/snapshot source) task-id+ (import! snapshot task-id(assoc import-args:force? force?+ :merge? merge?+ :base-snapshot base-snapshot:pre-task-branch pre-task-branch))):on-success on-success)))(defn async-export!"Exports the remote-synced collections to the remote source repository asynchronously.- Takes a branch name to export to, a force? boolean (if true, exports even if there are new changes in the remote- branch), and a commit message string. Optionally accepts an :on-success callback that receives [task-id result]- after a successful export. Checks if the remote branch has changed since the last sync and throws an- exception if force? is false and changes exist.+ Takes a branch name to export to, a force? boolean, and a commit message string. Optionally accepts+ `:merge?` (perform a 3-way merge when the remote has advanced) and an `:on-success` callback that+ receives [task-id result] after a successful export.- Returns a RemoteSyncTask. Throws ExceptionInfo with status 400 and :conflicts true if there- are new remote changes and force? is false."- [branch force? message & {:keys [on-success]}]+ Behavior when the remote branch has advanced beyond the last synced version:+ - `force?` -> overwrite the remote wholesale.+ - `:merge? true` -> entity-identity 3-way merge; non-conflicting remote changes are merged in and+ reconciled into the local app DB; genuine same-entity conflicts surface as a+ `:conflict` task result.+ - neither (default) -> `:conflict` task result; the caller (typically the UI, via the export preflight)+ decides whether to force, branch, or merge.++ Returns a RemoteSyncTask."+ [branch force? message & {:keys [on-success merge?]}](guards/ensure-no-active-task!)(let [pre-task-branch (settings/remote-sync-branch)source (source/source-from-settings branch)last-task-version (remote-sync.task/last-version)snapshot (source.p/snapshot source)- current-source-version (source.p/version snapshot)]- (when (and (not force?) (some? last-task-version) (not= last-task-version current-source-version))- (throw (ex-info "Cannot export changes that will overwrite new changes in the branch."- {:status-code 400- :conflicts true})))+ current-source-version (source.p/version snapshot)+ ;; the merge base, resolved only when the remote has advanced; nil here means a 3-way merge isn't+ ;; possible (no prior sync, or the base commit was orphaned by a force-push/rebase)+ base-snapshot (when (and (some? last-task-version)+ (not= last-task-version current-source-version))+ (source.p/snapshot-at source last-task-version))](run-async! "export" branch(fn [task-id](export! snapshot task-id message+ :force? force?+ :merge? merge?+ :source source+ :base-snapshot base-snapshot:pre-task-branch pre-task-branch)):on-success on-success)))+(defn preview-export-merge+ "Dry-run preview of what exporting the current state would do given the live remote, without writing+ anything. Drives the UI's push decision (force / new branch / merge). Returns a map:+ - `:diverged?` - whether the remote branch has advanced beyond the last synced version+ - `:clean?` - whether a 3-way merge would apply with no conflicts+ - `:conflicts` - human-readable labels of the entities that conflict (empty when clean)+ - `:summary` - `{:added :updated :removed}` counts of remote changes a merge would fold in+ - `:reason` - `:history-rewritten` when the remote was force-pushed/rebased so no merge base exists++ `branch` is the branch to preview against — the caller is responsible for having validated it against+ the `remote-sync-branch` setting."+ [branch]+ (let [no-changes {:diverged? false :clean? true :conflicts [] :summary {:added 0 :updated 0 :removed 0}}+ source (source/source-from-settings branch)+ snapshot (source.p/snapshot source)+ remote-version (source.p/version snapshot)+ base-version (remote-sync.task/last-version)]+ (if (or (nil? base-version) (= base-version remote-version))+ no-changes+ (if-let [base-snapshot (source.p/snapshot-at source base-version)]+ (serdes/with-cache+ (if-let [models (spec/extract-entities-for-export)]+ (assoc (source/preview-merge models snapshot base-snapshot nil) :diverged? true)+ (assoc no-changes :diverged? true)))+ {:diverged? true :clean? false :reason :history-rewritten+ :conflicts [] :summary {:added 0 :updated 0 :removed 0}}))))+(defn create-branch!"Creates a new remote branch from `base-branch` and switches `remote-sync-branch`to the new name. Does not publish events or return a response map; the callerdiff --git a/enterprise/backend/src/metabase_enterprise/remote_sync/merge.clj b/enterprise/backend/src/metabase_enterprise/remote_sync/merge.cljnew file mode 100644index 00000000000..81a68ed2443--- /dev/null+++ b/enterprise/backend/src/metabase_enterprise/remote_sync/merge.clj@@ -0,0 +1,149 @@+(ns metabase-enterprise.remote-sync.merge+ "Three-way merge of serialized remote-sync content, keyed on serdes identity (model + id) rather than+ on-disk file path.++ Why identity and not path: the storage path is derived from entity *names* and the names of the+ collections containing them (see [[metabase-enterprise.serialization.v2.storage.util/resolve-storage-path]]),+ so renaming or moving an entity changes its file path. A path-keyed merge would therefore treat a rename+ as delete+add and could produce two files with the same entity_id (silent corruption) or report false+ conflicts when a collection is renamed on one side. Keying on the stable serdes identity avoids all of+ that and gives true per-entity conflict semantics.++ The merge takes three sides:+ - `base` - the last successfully synced state (the merge base)+ - `ours` - the freshly serialized local state to be exported+ - `theirs` - the current remote tip++ and produces a merged set of file specs plus a list of genuine conflicts (the same entity changed+ differently on both sides)."+ (:require+ [metabase.models.serialization :as serdes]+ [metabase.util.log :as log]+ [metabase.util.yaml :as yaml]))++(set! *warn-on-reflection* true)++(defn- entity-identity+ "Returns a stable, rename-independent identity key for a serialized entity's YAML `content`, or nil if the+ content can't be parsed or has no serdes path. The key is a vector of `[model id]` pairs (the serdes path+ with labels dropped)."+ [content]+ (try+ (some->> (yaml/parse-string content)+ serdes/path+ seq+ (mapv (fn [seg] [(str (:model seg)) (str (:id seg))])))+ (catch Exception e+ ;; A serialized entity that won't parse is abnormal (malformed/partially-written file) and demotes+ ;; this side to a path key while the other side stays identity-keyed — which reads as a phantom+ ;; add+remove rather than an update. Warn so it's visible rather than silently mis-merged.+ (log/warn e "Could not parse serialized content during merge; treating it as a non-serdes (path-keyed) file")+ nil)))++(defn- file-key+ "Identity key for a `{:path :content}` file spec: the serdes identity when available, otherwise a+ path-based fallback so non-serdes files still merge sanely."+ [{:keys [path content]}]+ (or (entity-identity content)+ [::by-path path]))++(defn- index-by-key+ "Builds a map of identity-key -> `{:path :content}` for a sequence of file specs.++ Throws if two specs on the same side share a serdes identity (the same model + entity_id at two+ paths): silently keeping one would drop the other, which is data corruption, so a duplicate entity_id+ is surfaced as an error. A duplicate path-fallback key (two non-serdes files at the same path, which a+ real tree shouldn't contain) only warns and keeps the last."+ [specs]+ (persistent!+ (reduce (fn [acc {:keys [path] :as spec}]+ (let [k (file-key spec)]+ (when (contains? acc k)+ (if (= ::by-path (first k))+ (log/warnf "Duplicate path %s during merge; keeping the last occurrence" path)+ (throw (ex-info (format "Duplicate serdes identity %s during merge: paths %s and %s share the same entity_id"+ k (:path (get acc k)) path)+ {:identity k+ :paths [(:path (get acc k)) path]}))))+ (assoc! acc k (select-keys spec [:path :content]))))+ (transient {})+ specs)))++(defn- same?+ "Two sides are the same when both are absent, or both present with equal path and content. Path equality+ matters so that a rename with otherwise identical content still counts as a change.++ Known conservative edge: the storage path embeds the names of containing collections, so renaming a+ shared parent collection on one side changes the path of every descendant. Those descendants therefore+ read as changed (path differs) even if their content is identical; if the other side also edited one of+ them, that surfaces as a conflict rather than auto-merging the rename with the edit. This is safe (never+ silently wrong) but more conservative than git would be. Revisit by comparing content separately from+ path if it proves noisy in practice."+ [a b]+ (= a b))++(defn conflict-label+ "Renders a single conflict from [[three-way-merge]] into a human-readable string for display, e.g.+ \"Card A (collections/foo/bar.yaml)\". Prefers the entity's name (parsed from the serialized content),+ falling back to its serdes model + id when there's no name."+ [{:keys [key ours theirs]}]+ (let [content (or (:content ours) (:content theirs))+ entity (try (yaml/parse-string content) (catch Exception _ nil))+ path (or (:path ours) (:path theirs))+ ;; identity keys are [[model id] ...]; the path-fallback key is [::by-path path] for a non-serdes+ ;; file — don't destructure the path string into a "model"/"id" (which yields garbage like "s o").+ descriptor (if (= ::by-path (first key))+ (or path "unknown file")+ (let [[model id] (last key)] (str model " " id)))]+ (cond-> (or (:name entity) descriptor)+ (and path (not= descriptor path)) (str " (" path ")"))))++(defn three-way-merge+ "Three-way merge of serialized content keyed on serdes identity.++ `base`, `ours`, `theirs` are each sequences of `{:path :content}` file specs (base/theirs typically read+ from the corresponding git trees, ours freshly serialized from the app DB).++ Returns a map:+ - `:merged` - sequence of winning `{:path :content}` specs to write+ - `:conflicts` - sequence of `{:key :ours :theirs :base}` for entities changed differently on both sides+ - `:summary` - `{:added :updated :removed}` counts of remote-originated changes folded into the result+ (i.e. changes coming from `theirs` that `ours` did not already have)"+ [base ours theirs]+ (let [b (index-by-key base)+ o (index-by-key ours)+ t (index-by-key theirs)+ all-keys (into #{} (concat (keys b) (keys o) (keys t)))]+ (reduce+ (fn [acc k]+ (let [bv (get b k)+ ov (get o k)+ tv (get t k)+ ours-changed? (not (same? ov bv))+ theirs-changed? (not (same? tv bv))]+ (cond+ ;; neither side changed -> keep base (if present)+ (and (not ours-changed?) (not theirs-changed?))+ (cond-> acc bv (update :merged conj bv))++ ;; only ours changed -> take ours+ (and ours-changed? (not theirs-changed?))+ (cond-> acc ov (update :merged conj ov))++ ;; only theirs changed -> take theirs, and record it as folded-in remote change+ (and theirs-changed? (not ours-changed?))+ (-> (cond-> acc tv (update :merged conj tv))+ (update-in [:summary (cond (nil? bv) :added+ (nil? tv) :removed+ :else :updated)]+ inc))++ ;; both changed the same way -> take it (no conflict)+ (same? ov tv)+ (cond-> acc ov (update :merged conj ov))++ ;; both changed differently -> conflict+ :else+ (update acc :conflicts conj {:key k :base bv :ours ov :theirs tv}))))+ {:merged [] :conflicts [] :summary {:added 0 :updated 0 :removed 0}}+ all-keys)))diff --git a/enterprise/backend/src/metabase_enterprise/remote_sync/schema.clj b/enterprise/backend/src/metabase_enterprise/remote_sync/schema.cljindex ef7c298bbf6..de508a676db 100644--- a/enterprise/backend/src/metabase_enterprise/remote_sync/schema.clj+++ b/enterprise/backend/src/metabase_enterprise/remote_sync/schema.clj@@ -102,6 +102,22 @@[:message :string][:task_id pos-int?]])+(def MergeSummary+ "Counts of remote changes a merge would fold into local content."+ [:map+ [:added :int]+ [:updated :int]+ [:removed :int]])++(def ExportPreflightResponse+ "Schema for GET /export-preflight response."+ [:map+ [:has_changes :boolean]+ [:clean :boolean]+ [:conflicts [:sequential :string]]+ [:summary MergeSummary]+ [:reason [:maybe :string]]])+(def SettingsUpdateResponse"Schema for PUT /settings response."[:mapdiff --git a/enterprise/backend/src/metabase_enterprise/remote_sync/source.clj b/enterprise/backend/src/metabase_enterprise/remote_sync/source.cljindex c55aa157276..d570a0fbc18 100644--- a/enterprise/backend/src/metabase_enterprise/remote_sync/source.clj+++ b/enterprise/backend/src/metabase_enterprise/remote_sync/source.clj@@ -1,6 +1,7 @@(ns metabase-enterprise.remote-sync.source(:require[clojure.string :as str]+ [metabase-enterprise.remote-sync.merge :as remote-sync.merge][metabase-enterprise.remote-sync.models.remote-sync-task :as remote-sync.task][metabase-enterprise.remote-sync.source.git :as git][metabase-enterprise.remote-sync.source.ingestable :as ingestable]@@ -61,6 +62,26 @@:content (yaml/generate-string (serialization/serialization-deep-sort entity){:dumper-options {:flow-style :block :split-lines false}})})+(defn serialize-specs+ "Serializes a stream of entities into an eager vector of `{:path :content}` file specs. Reports progress+ via `task-id` as specs are produced; pass nil for `task-id` to serialize without progress reporting+ (e.g. for a dry-run merge preview).++ Throws Exception if any entity in the stream is an Exception instance."+ [stream task-id]+ (let [opts (serdes/storage-base-context)+ stream-count (bounded-count 10000 stream)]+ (into []+ (map-indexed (fn [idx entity]+ (when (instance? Exception entity)+ (throw entity))+ (let [spec (entity->file-spec opts entity)]+ (when task-id+ (remote-sync.task/update-progress!+ task-id (-> (inc idx) (/ stream-count) (* 0.65) (+ 0.3))))+ spec)))+ stream)))+(defn store!"Stores serialized entities from a stream to a remote source and commits the changes.@@ -86,6 +107,74 @@(source.p/write-files! snapshot message))]{:version version :entries @entries}))+(defn- snapshot->specs+ "Reads a snapshot's managed-directory files into a sequence of `{:path :content}` specs, matching the+ shape produced by [[serialize-specs]]. Used to read the merge base and remote-tip trees for merging."+ [snapshot]+ (into []+ (keep (fn [path]+ (when (contains? serialization/legal-top-level-paths+ (when-let [idx (str/index-of path "/")]+ (subs path 0 idx)))+ (when-let [content (source.p/read-file snapshot path)]+ {:path path :content content}))))+ (source.p/list-files snapshot)))++(defn compute-merge+ "Runs the entity-identity 3-way merge of local state against the remote tip, without writing. Returns+ the raw merge result `{:merged :conflicts :summary}` from [[remote-sync.merge/three-way-merge]]:+ - `base-snapshot` - the last successfully synced state (the merge base)+ - `stream` - the local state to serialize (ours)+ - `snapshot` - the current remote tip (theirs)++ `:merged` is the full reconciled set of `{:path :content}` specs. The export path writes it to the+ remote; the local-only pull merge loads it into the app DB via [[specs->snapshot]]."+ [stream snapshot base-snapshot task-id]+ (let [ours (serialize-specs stream task-id)+ base (snapshot->specs base-snapshot)+ theirs (snapshot->specs snapshot)]+ (remote-sync.merge/three-way-merge base ours theirs)))++(defn specs->snapshot+ "Builds an in-memory read-only SourceSnapshot backed by `specs` (a seq of `{:path :content}`), so merged+ content can be loaded into the app DB without writing it to git. `write-files!` is unsupported."+ [specs]+ (let [by-path (into {} (map (juxt :path :content)) specs)]+ (reify source.p/SourceSnapshot+ (list-files [_] (vec (keys by-path)))+ (read-file [_ path] (get by-path path))+ (write-files! [_ _ _] (throw (ex-info "in-memory merge snapshot is read-only" {})))+ (apply-changes! [_ _ _ _] (throw (ex-info "in-memory merge snapshot is read-only" {})))+ (version [_] nil))))++(defn preview-merge+ "Dry-run of [[merge-and-store!]]: computes the 3-way merge without writing anything. Returns+ `{:clean? bool :conflicts [labels] :summary {:added :updated :removed}}`. Pass nil for `task-id` to+ skip progress reporting."+ [stream snapshot base-snapshot task-id]+ (let [{:keys [conflicts summary]} (compute-merge stream snapshot base-snapshot task-id)]+ {:clean? (empty? conflicts)+ :conflicts (mapv remote-sync.merge/conflict-label conflicts)+ :summary summary}))++(defn merge-and-store!+ "Like [[store!]], but reconciles the freshly serialized local state against a remote branch that has+ advanced beyond the last sync. Performs an entity-identity 3-way merge of:+ - the merge base (`base-snapshot`, the last successfully synced state),+ - the local state (serialized from `stream`),+ - the current remote tip (`snapshot`).++ On a clean merge, writes the merged file set (which fast-forwards onto the remote tip) and returns+ `{:status :success :version <sha> :summary {:added :updated :removed}}`. When the same entity changed+ on both sides, returns `{:status :conflict :conflicts [..] :summary {..}}` without writing anything."+ [stream snapshot base-snapshot task-id message]+ (let [{:keys [merged conflicts summary]} (compute-merge stream snapshot base-snapshot task-id)]+ (if (seq conflicts)+ {:status :conflict :conflicts conflicts :summary summary}+ {:status :success+ :version (source.p/write-files! snapshot message merged)+ :summary summary})))+(defn source-from-settings"Creates a git source from the current remote sync settings.diff --git a/enterprise/backend/src/metabase_enterprise/remote_sync/source/git.clj b/enterprise/backend/src/metabase_enterprise/remote_sync/source/git.cljindex 138c544f7ff..5820a7c0ca4 100644--- a/enterprise/backend/src/metabase_enterprise/remote_sync/source/git.clj+++ b/enterprise/backend/src/metabase_enterprise/remote_sync/source/git.clj@@ -450,6 +450,14 @@(snapshot* fresh-source)))(throw e)))))+(defn snapshot-at-version+ "Builds a GitSnapshot for `source` at an already-fetched `version` (commit-ish), or nil if the version+ cannot be resolved against local state (e.g. it was orphaned by a force-push or rebase). Does not fetch."+ [source version]+ (when version+ (when-let [sha (commit-sha source version)]+ (->GitSnapshot (:git source) (:remote-url source) (:branch source) sha (:token source) (:managed-dirs source)))))+(defrecord GitSource [git remote-url branch token managed-dirs]source.p/Source(branches [source] (branches source))@@ -461,7 +469,10 @@(default-branch this))(snapshot [this]- (snapshot this)))+ (snapshot this))++ (snapshot-at [this version]+ (snapshot-at-version this version)))(defn git-source"Creates a new GitSource instance for a git repository.diff --git a/enterprise/backend/src/metabase_enterprise/remote_sync/source/protocol.clj b/enterprise/backend/src/metabase_enterprise/remote_sync/source/protocol.cljindex d5ae7b4b6af..aed07ef26ed 100644--- a/enterprise/backend/src/metabase_enterprise/remote_sync/source/protocol.clj+++ b/enterprise/backend/src/metabase_enterprise/remote_sync/source/protocol.clj@@ -30,7 +30,16 @@Takes a source instance implementing this protocol.- Returns a SourceSnapshot instance representing a point-in-time view of the source."))+ Returns a SourceSnapshot instance representing a point-in-time view of the source.")++ (snapshot-at [source version]+ "Creates a snapshot of the source at a specific historical version.++ Takes a source instance implementing this protocol and a version identifier (e.g. a git SHA).++ Returns a SourceSnapshot for that version, or nil if the version cannot be resolved (e.g. it was+ orphaned by a force-push or rebase). Unlike `snapshot`, this does not fetch from the remote; it+ resolves against already-fetched local state."))(defprotocol SourceSnapshot(list-files [snapshot]diff --git a/enterprise/backend/test/metabase_enterprise/remote_sync/api_test.clj b/enterprise/backend/test/metabase_enterprise/remote_sync/api_test.cljindex f1e2e8f4916..a40d5a6ec4b 100644--- a/enterprise/backend/test/metabase_enterprise/remote_sync/api_test.clj+++ b/enterprise/backend/test/metabase_enterprise/remote_sync/api_test.clj@@ -35,6 +35,8 @@(default-branch [_]"main")(snapshot [_]+ nil)+ (snapshot-at [_ _version]nil)))(use-fixtures :once@@ -199,7 +201,7 @@remote-sync-token "test-token"remote-sync-branch "main"](mt/with-dynamic-fn-redefs [source/source-from-settings (constantly mock-main)]- (let [{:keys [task_id] :as resp} (mt/user-http-request :crowberto :post 200 "ee/remote-sync/import" {})+ (let [{:keys [task_id] :as resp} (mt/user-http-request :crowberto :post 200 "ee/remote-sync/import" {:expected_branch "main"})completed-task (wait-for-task-completion task_id)](is (=? {:status "success" :task_id int?} resp))(is (remote-sync.task/successful? completed-task))))))))@@ -211,11 +213,37 @@remote-sync-token "test-token"remote-sync-branch "main"](mt/with-dynamic-fn-redefs [source/source-from-settings (constantly mock-develop)]- (let [{:as response :keys [task_id]} (mt/user-http-request :crowberto :post 200 "ee/remote-sync/import" {:branch "feature-branch"})+ (let [{:as response :keys [task_id]} (mt/user-http-request :crowberto :post 200 "ee/remote-sync/import" {:branch "feature-branch" :expected_branch "main"})completed-task (wait-for-task-completion task_id)](is (= "success" (:status response)))(is (remote-sync.task/successful? completed-task))))))))+(deftest import-rejects-expected-branch-mismatch-test+ (testing "POST /api/ee/remote-sync/import rejects when expected_branch disagrees with the configured setting"+ (let [mock-main (test-helpers/create-mock-source)]+ (mt/with-temporary-setting-values [remote-sync-url "https://github.com/test/repo.git"+ remote-sync-token "test-token"+ remote-sync-branch "main"]+ (mt/with-dynamic-fn-redefs [source/source-from-settings (constantly mock-main)]+ (testing "stale expected_branch -> 409 branch_mismatch, no task created"+ (let [before (t2/count :model/RemoteSyncTask)+ resp (mt/user-http-request :crowberto :post 409 "ee/remote-sync/import"+ {:branch "main" :expected_branch "stale-branch"})]+ (is (true? (:branch_mismatch resp)))+ (is (= "main" (:current_branch resp)))+ (is (= before (t2/count :model/RemoteSyncTask))+ "no RemoteSyncTask row is created when the guard fires")))+ (testing "matching expected_branch -> pull proceeds"+ (let [{:keys [task_id] :as resp} (mt/user-http-request :crowberto :post 200 "ee/remote-sync/import"+ {:branch "main" :expected_branch "main"})]+ (is (=? {:status "success" :task_id int?} resp))+ (wait-for-task-completion task_id)))+ (testing "a branch switch (operational branch != expected_branch) is allowed when expected_branch matches the setting"+ (let [{:keys [task_id] :as resp} (mt/user-http-request :crowberto :post 200 "ee/remote-sync/import"+ {:branch "feature-branch" :expected_branch "main"})]+ (is (=? {:status "success" :task_id int?} resp))+ (wait-for-task-completion task_id))))))))+(deftest import-creates-audit-log-entry-test(testing "POST /api/ee/remote-sync/import records a remote-sync-import audit log entry (#73335)";; :audit-app is needed for events to actually be recorded to the audit log@@ -226,7 +254,7 @@remote-sync-branch "main"](mt/with-dynamic-fn-redefs [source/source-from-settings (constantly mock-main)](let [before (t2/count :model/AuditLog :topic "remote-sync-import")- {:keys [task_id]} (mt/user-http-request :crowberto :post 200 "ee/remote-sync/import" {})]+ {:keys [task_id]} (mt/user-http-request :crowberto :post 200 "ee/remote-sync/import" {:expected_branch "main"})](wait-for-task-completion task_id);; the audit event publishes after the task's ended_at is set, so poll for the row(let [entry (dh/with-retry {:max-retries 10@@ -244,13 +272,13 @@(testing "POST /api/ee/remote-sync/import requires superuser permissions"(mt/with-temporary-setting-values [remote-sync-enabled true](is (= "You don't have permissions to do that."- (mt/user-http-request :rasta :post 403 "ee/remote-sync/import" {}))))))+ (mt/user-http-request :rasta :post 403 "ee/remote-sync/import" {:expected_branch "main"}))))))(deftest import-errors-when-remote-sync-disabled-test(testing "POST /api/ee/remote-sync/import errors when remote sync is disabled"(mt/with-temporary-setting-values [remote-sync-url nil](is (= "Remote sync is not configured."- (mt/user-http-request :crowberto :post 400 "ee/remote-sync/import" {}))))))+ (mt/user-http-request :crowberto :post 400 "ee/remote-sync/import" {:expected_branch "main"}))))))(deftest import-handles-network-errors-test(testing "POST /api/ee/remote-sync/import handles network errors during import"@@ -259,7 +287,7 @@remote-sync-token "test-token"remote-sync-branch "main"](mt/with-dynamic-fn-redefs [source/source-from-settings (constantly mock-main)]- (let [{:as response :keys [task_id]} (mt/user-http-request :crowberto :post 200 "ee/remote-sync/import" {})+ (let [{:as response :keys [task_id]} (mt/user-http-request :crowberto :post 200 "ee/remote-sync/import" {:expected_branch "main"})completed-task (wait-for-task-completion task_id)](is (= "success" (:status response)))(is (remote-sync.task/failed? completed-task))))))))@@ -273,7 +301,7 @@remote-sync-branch "main"](mt/with-dynamic-fn-redefs [source/source-from-settings (constantly mock-source)](is (= "Remote sync task in progress"- (mt/user-http-request :crowberto :post 400 "ee/remote-sync/import" {})))))))))+ (mt/user-http-request :crowberto :post 400 "ee/remote-sync/import" {:expected_branch "main"})))))))))(deftest import-errors-when-dirty-changes-test(testing "POST /api/ee/remote-sync/import errors when dirty changes exist"@@ -289,13 +317,177 @@:status_changed_at (java.time.OffsetDateTime/now)})(mt/with-dynamic-fn-redefs [source/source-from-settings (constantly mock-main)](is (= "There are unsaved changes in the Remote Sync collection which will be overwritten by the import. Force the import to discard these changes."- (:message (mt/user-http-request :crowberto :post 400 "ee/remote-sync/import" {}))))+ (:message (mt/user-http-request :crowberto :post 400 "ee/remote-sync/import" {:expected_branch "main"}))))(testing "But can force an import"- (let [{:keys [task_id] :as resp} (mt/user-http-request :crowberto :post 200 "ee/remote-sync/import" {:force true})+ (let [{:keys [task_id] :as resp} (mt/user-http-request :crowberto :post 200 "ee/remote-sync/import" {:force true :expected_branch "main"})completed-task (wait-for-task-completion task_id)](is (=? {:status "success" :task_id int?} resp))(is (remote-sync.task/successful? completed-task)))))))))+(deftest import-merge-keeps-local-changes-test+ (testing "POST /api/ee/remote-sync/import with merge=true does a local-only merge instead of erroring on dirty"+ (mt/with-temp [:model/Collection _ {:is_remote_synced true :name "Test Collection" :location "/"}+ :model/RemoteSyncTask _ {:sync_task_type "foo"+ :ended_at :%now+ :version "other-version"}]+ (let [mock-main (test-helpers/create-mock-source)]+ (mt/with-temporary-setting-values [remote-sync-url "https://github.com/test/repo.git"+ remote-sync-token "test-token"+ remote-sync-branch "main"]+ (t2/insert! :model/RemoteSyncObject {:model_type "Card"+ :model_id 1+ :model_name "Test Card"+ :model_collection_id 1+ :status "update"+ :status_changed_at (java.time.OffsetDateTime/now)})+ ;; Stub the app-DB reconcile load — its correctness is covered synchronously by impl-test; here we+ ;; only verify the endpoint/flag wiring and that the async task completes (loading into a real+ ;; warehouse DB inside the task thread is slow/racy on the MySQL/MariaDB app-db matrix).+ (mt/with-dynamic-fn-redefs [source/source-from-settings (constantly mock-main)+ impl/load-snapshot! (constantly nil)]+ (testing "merge=true succeeds even with unsaved local changes"+ (let [{:keys [task_id]} (mt/user-http-request :crowberto :post 200 "ee/remote-sync/import" {:merge true :expected_branch "main"})+ completed-task (wait-for-task-completion task_id)]+ (is (remote-sync.task/successful? completed-task))))))))))++(deftest import-merge-noop-when-remote-not-advanced-test+ (testing "POST /import merge=true when the remote has NOT advanced is a no-op success that keeps local dirty (not a spurious 'history was rewritten' conflict)"+ (mt/with-temp [:model/Collection _ {:is_remote_synced true :name "Test Collection" :location "/"}+ ;; last-synced version == the mock's current version -> not diverged+ :model/RemoteSyncTask _ {:sync_task_type "foo" :ended_at :%now :version "mock-version"}]+ (let [mock-main (test-helpers/create-mock-source)]+ (mt/with-temporary-setting-values [remote-sync-url "https://github.com/test/repo.git"+ remote-sync-token "test-token"+ remote-sync-branch "main"]+ (t2/insert! :model/RemoteSyncObject {:model_type "Card"+ :model_id 1+ :model_name "Test Card"+ :model_collection_id 1+ :status "update"+ :status_changed_at (java.time.OffsetDateTime/now)})+ (mt/with-dynamic-fn-redefs [source/source-from-settings (constantly mock-main)]+ (let [{:keys [task_id]} (mt/user-http-request :crowberto :post 200 "ee/remote-sync/import" {:merge true :expected_branch "main"})+ completed-task (wait-for-task-completion task_id)]+ (is (remote-sync.task/successful? completed-task)+ "a merge pull with nothing new on the remote succeeds instead of failing as a history-rewritten conflict")+ (is (= "update" (t2/select-one-fn :status :model/RemoteSyncObject :model_id 1))+ "the un-pushed local change is left untouched (still dirty)"))))))))++;;; --------------------------- async-import!/async-export! base-snapshot resolution ---------------------------+;;; These drive the async-*! wrappers end-to-end through a version-controllable fake Source, so the real+;;; base-snapshot resolution + 3-way merge run (no hand-built :base-snapshot, no stubbed compute-merge).+;;; load-snapshot! (the slow app-DB reconcile) is stubbed and captured to assert which branch ran.++(defn- card-yaml-fixture []+ {"collections/c1eidaaaaaaaaaaaaaaaa_coll/cards/card1eidaaaaaaaaaaaaa_q.yaml"+ (test-helpers/generate-card-yaml "card1eidaaaaaaaaaaaaa" "Q1" "c1eidaaaaaaaaaaaaaaaa")})++(deftest async-import-not-diverged-noop-test+ (testing "import merge=true with the remote NOT advanced (last-version == current): no-op success, local stays dirty, no reconcile load"+ (mt/with-temporary-setting-values [remote-sync-url "https://github.com/test/repo.git"+ remote-sync-token "test-token"+ remote-sync-branch "main"]+ (let [loaded (atom [])+ src (test-helpers/versioned-source :current "v1" :trees {"v1" {}})]+ (mt/with-dynamic-fn-redefs [source/source-from-settings (constantly src)+ remote-sync.task/last-version (constantly "v1")+ impl/load-snapshot! (fn [snap & _] (swap! loaded conj (source.p/version snap)) nil)]+ (t2/insert! :model/RemoteSyncObject {:model_type "Card" :model_id 9001 :model_name "Local Card"+ :model_collection_id 1 :status "update"+ :status_changed_at (java.time.OffsetDateTime/now)})+ (let [{:keys [task_id]} (mt/user-http-request :crowberto :post 200 "ee/remote-sync/import" {:merge true :expected_branch "main"})+ task (wait-for-task-completion task_id)]+ (is (remote-sync.task/successful? task))+ (is (empty? @loaded) "no reconcile load happens when there is nothing to fold in")+ (is (= "update" (t2/select-one-fn :status :model/RemoteSyncObject :model_id 9001))+ "the un-pushed local change is left dirty")))))))++(deftest async-import-diverged-resolves-base-test+ (testing "import merge=true when diverged resolves the base from last-version and folds in a remote-only entity"+ (mt/with-temporary-setting-values [remote-sync-url "https://github.com/test/repo.git"+ remote-sync-token "test-token"+ remote-sync-branch "main"]+ (let [loaded (atom [])+ src (test-helpers/versioned-source :current "v2" :trees {"v1" {} "v2" (card-yaml-fixture)})]+ (mt/with-dynamic-fn-redefs [source/source-from-settings (constantly src)+ remote-sync.task/last-version (constantly "v1") ; != current -> diverged, base resolvable+ ;; simulate load-snapshot!'s contract (run the in-txn finalize: restore-dirty ++ ;; set-version) without the slow app-DB reconcile+ impl/load-snapshot! (fn [_snap _ _ & {:keys [finalize!]}]+ (swap! loaded conj :loaded)+ (when finalize! (finalize!))+ nil)]+ (let [{:keys [task_id]} (mt/user-http-request :crowberto :post 200 "ee/remote-sync/import" {:merge true :expected_branch "main"})+ task (wait-for-task-completion task_id)]+ (is (remote-sync.task/successful? task))+ (is (= "v2" (:version task)) "version advances to the remote tip")+ (is (seq @loaded) "the real 3-way merge ran and its result was reconciled into the app DB")))))))++(deftest async-import-base-unreachable-test+ (testing "import merge=true when the base commit is unreachable (orphaned) -> conflict, no reconcile load"+ (mt/with-temporary-setting-values [remote-sync-url "https://github.com/test/repo.git"+ remote-sync-token "test-token"+ remote-sync-branch "main"]+ (let [loaded (atom [])+ src (test-helpers/versioned-source :current "v2" :trees {"v2" (card-yaml-fixture)})] ; "gone" absent+ (mt/with-dynamic-fn-redefs [source/source-from-settings (constantly src)+ remote-sync.task/last-version (constantly "gone")+ impl/load-snapshot! (fn [_snap & _] (swap! loaded conj :loaded) nil)]+ (let [{:keys [task_id]} (mt/user-http-request :crowberto :post 200 "ee/remote-sync/import" {:merge true :expected_branch "main"})+ task (wait-for-task-completion task_id)]+ (is (remote-sync.task/conflict? task))+ (is (empty? @loaded) "no load on an unresolvable-base conflict")))))))++(deftest async-import-no-prior-sync-test+ (testing "import merge=true with no prior sync (last-version nil) -> conflict (no base to merge against)"+ (mt/with-temporary-setting-values [remote-sync-url "https://github.com/test/repo.git"+ remote-sync-token "test-token"+ remote-sync-branch "main"]+ (let [src (test-helpers/versioned-source :current "v2" :trees {"v2" (card-yaml-fixture)})]+ (mt/with-dynamic-fn-redefs [source/source-from-settings (constantly src)+ remote-sync.task/last-version (constantly nil)+ impl/load-snapshot! (fn [_snap & _] nil)]+ (let [{:keys [task_id]} (mt/user-http-request :crowberto :post 200 "ee/remote-sync/import" {:merge true :expected_branch "main"})+ task (wait-for-task-completion task_id)]+ (is (remote-sync.task/conflict? task))))))))++(deftest async-export-diverged-resolves-base-test+ (testing "export merge=true when diverged resolves the base, runs the real merge, writes, and reconciles"+ (mt/with-temporary-setting-values [remote-sync-type :read-write]+ (mt/with-temp [:model/Collection _ {:is_remote_synced true :name "Test Collection" :location "/"}]+ (mt/with-temporary-setting-values [remote-sync-url "https://github.com/test/repo.git"+ remote-sync-token "test-token"+ remote-sync-branch "main"]+ (let [loaded (atom [])+ src (test-helpers/versioned-source :current "v2" :trees {"v1" {} "v2" {}})]+ (mt/with-dynamic-fn-redefs [source/source-from-settings (constantly src)+ remote-sync.task/last-version (constantly "v1") ; diverged, base resolvable+ impl/load-snapshot! (fn [_snap _ _ & {:keys [finalize!]}]+ (swap! loaded conj :loaded)+ (when finalize! (finalize!))+ nil)]+ (let [{:keys [task_id]} (mt/user-http-request :crowberto :post 200 "ee/remote-sync/export" {:branch "main" :merge true})+ task (wait-for-task-completion task_id)]+ (is (remote-sync.task/successful? task))+ (is (seq @loaded) "the merged result is reconciled back into the app DB (the pull half)")))))))))++(deftest async-export-base-unreachable-test+ (testing "export merge=true when the base commit is unreachable -> conflict, nothing written"+ (mt/with-temporary-setting-values [remote-sync-type :read-write]+ (mt/with-temp [:model/Collection _ {:is_remote_synced true :name "Test Collection" :location "/"}]+ (mt/with-temporary-setting-values [remote-sync-url "https://github.com/test/repo.git"+ remote-sync-token "test-token"+ remote-sync-branch "main"]+ (let [loaded (atom [])+ src (test-helpers/versioned-source :current "v2" :trees {"v2" {}})] ; "gone" absent+ (mt/with-dynamic-fn-redefs [source/source-from-settings (constantly src)+ remote-sync.task/last-version (constantly "gone")+ impl/load-snapshot! (fn [_snap & _] (swap! loaded conj :loaded) nil)]+ (let [{:keys [task_id]} (mt/user-http-request :crowberto :post 200 "ee/remote-sync/export" {:branch "main" :merge true})+ task (wait-for-task-completion task_id)]+ (is (remote-sync.task/conflict? task))+ (is (empty? @loaded) "no reconcile on an unresolvable-base conflict")))))))))+;;; ------------------------------------------------- Export Endpoint -------------------------------------------------(deftest export-errors-in-read-only-mode-test@@ -308,7 +500,7 @@remote-sync-branch "main"](mt/with-dynamic-fn-redefs [source/source-from-settings (constantly mock-source)](is (= "Exports are only allowed when remote-sync-type is set to 'read-write'"- (mt/user-http-request :crowberto :post 400 "ee/remote-sync/export" {}))))))))))+ (mt/user-http-request :crowberto :post 400 "ee/remote-sync/export" {:branch "main"}))))))))))(deftest export-with-default-settings-test(testing "POST /api/ee/remote-sync/export succeeds with default settings"@@ -319,20 +511,20 @@remote-sync-token "test-token"remote-sync-branch "main"](mt/with-dynamic-fn-redefs [source/source-from-settings (constantly mock-main)]- (let [{:keys [task_id] :as resp} (mt/user-http-request :crowberto :post 200 "ee/remote-sync/export" {})+ (let [{:keys [task_id] :as resp} (mt/user-http-request :crowberto :post 200 "ee/remote-sync/export" {:branch "main"})task (wait-for-task-completion task_id)](is (remote-sync.task/successful? task))(is (=? {:message string? :task_id int?}resp))))))))))(deftest export-with-custom-branch-and-message-test- (testing "POST /api/ee/remote-sync/export succeeds with custom branch and message"+ (testing "POST /api/ee/remote-sync/export succeeds with a non-default branch and message when on that branch"(mt/with-temporary-setting-values [remote-sync-type :read-write](mt/with-temp [:model/Collection _ {:is_remote_synced true :name "Test Collection" :location "/"}](let [mock-main (test-helpers/create-mock-source)](mt/with-temporary-setting-values [remote-sync-url "https://github.com/test/repo.git"remote-sync-token "test-token"- remote-sync-branch "main"]+ remote-sync-branch "feature-branch"](mt/with-dynamic-fn-redefs [source/source-from-settings (constantly mock-main)](let [{:keys [task_id] :as resp} (mt/user-http-request :crowberto :post 200 "ee/remote-sync/export"{:branch "feature-branch" :message "Custom export message"})@@ -346,14 +538,14 @@(mt/with-temporary-setting-values [remote-sync-type :read-writeremote-sync-url "file://repo.git"](is (= "You don't have permissions to do that."- (mt/user-http-request :rasta :post 403 "ee/remote-sync/export" {}))))))+ (mt/user-http-request :rasta :post 403 "ee/remote-sync/export" {:branch "main"}))))))(deftest export-errors-when-remote-sync-disabled-test(testing "POST /api/ee/remote-sync/export errors when remote sync is disabled"(mt/with-temporary-setting-values [remote-sync-type :read-writeremote-sync-url nil](is (= "Remote sync is not configured."- (mt/user-http-request :crowberto :post 400 "ee/remote-sync/export" {}))))))+ (mt/user-http-request :crowberto :post 400 "ee/remote-sync/export" {:branch "main"}))))))(deftest export-handles-write-errors-test(testing "POST /api/ee/remote-sync/export handles write errors"@@ -367,7 +559,7 @@remote-sync-token "test-token"remote-sync-branch "main"](mt/with-dynamic-fn-redefs [source/source-from-settings (constantly mock-main)]- (let [response (mt/user-http-request :crowberto :post 200 "ee/remote-sync/export" {})+ (let [response (mt/user-http-request :crowberto :post 200 "ee/remote-sync/export" {:branch "main"})task (wait-for-task-completion (:task_id response))](is (remote-sync.task/failed? task))))))))))@@ -381,26 +573,85 @@remote-sync-branch "main"](mt/with-dynamic-fn-redefs [source/source-from-settings (constantly mock-source)](is (= "Remote sync task in progress"- (mt/user-http-request :crowberto :post 400 "ee/remote-sync/export" {}))))))))))+ (mt/user-http-request :crowberto :post 400 "ee/remote-sync/export" {:branch "main"}))))))))))-(deftest export-errors-if-external-changes-test- (testing "POST /api/ee/remote-sync/export errors when remote is ahead of the last sync"+(deftest export-merges-if-external-changes-test+ (testing "POST /api/ee/remote-sync/export with merge=true merges when the remote is ahead of the last sync"(mt/with-temporary-setting-values [remote-sync-type :read-write]- (mt/with-temp [:model/RemoteSyncTask _ {:sync_task_type "foo"+ (mt/with-temp [:model/Collection _ {:is_remote_synced true :name "Test Collection" :location "/"}+ :model/RemoteSyncTask _ {:sync_task_type "foo":ended_at :%now:version "other-version"}](let [mock-source (test-helpers/create-mock-source)](mt/with-temporary-setting-values [remote-sync-url "https://github.com/test/repo.git"remote-sync-token "test-token"remote-sync-branch "main"]- (mt/with-dynamic-fn-redefs [source/source-from-settings (constantly mock-source)]- (is (= "Cannot export changes that will overwrite new changes in the branch."- (:message (mt/user-http-request :crowberto :post 400 "ee/remote-sync/export" {}))))+ ;; Stub the app-DB reconcile load — its correctness is covered synchronously by impl-test; here we+ ;; only verify the endpoint/flag wiring and task completion (loading into a real warehouse DB+ ;; inside the task thread is slow/racy on the MySQL/MariaDB app-db matrix).+ (mt/with-dynamic-fn-redefs [source/source-from-settings (constantly mock-source)+ impl/load-snapshot! (constantly nil)]+ (testing "merge=true reconciles non-conflicting remote changes and succeeds"+ (let [response (mt/user-http-request :crowberto :post 200 "ee/remote-sync/export" {:merge true :branch "main"})+ task (wait-for-task-completion (:task_id response))]+ (is (remote-sync.task/successful? task)+ "non-conflicting remote changes are merged in, so the export succeeds")))+ (testing "without the merge flag a diverged export ends in conflict, not success"+ (mt/with-temp [:model/RemoteSyncTask _ {:sync_task_type "bar"+ :ended_at :%now+ :version "other-version"}]+ (let [response (mt/user-http-request :crowberto :post 200 "ee/remote-sync/export" {:branch "main"})+ task (wait-for-task-completion (:task_id response))]+ (is (remote-sync.task/conflict? task)+ "a diverged export without force/merge surfaces a conflict for the UI to resolve"))))(testing "Can export when the versions match"(mt/with-temp [:model/RemoteSyncTask _ {:sync_task_type "foo":ended_at :%now:version "mock-version"}]- (mt/user-http-request :crowberto :post 200 "ee/remote-sync/export" {}))))))))))+ (mt/user-http-request :crowberto :post 200 "ee/remote-sync/export" {:branch "main"}))))))))))++(deftest export-preflight-test+ (testing "GET /api/ee/remote-sync/export-preflight previews a merge without writing"+ (mt/with-temporary-setting-values [remote-sync-type :read-write]+ (mt/with-temp [:model/Collection _ {:is_remote_synced true :name "Test Collection" :location "/"}]+ (let [mock-source (test-helpers/create-mock-source)]+ (mt/with-temporary-setting-values [remote-sync-url "https://github.com/test/repo.git"+ remote-sync-token "test-token"+ remote-sync-branch "main"]+ (mt/with-dynamic-fn-redefs [source/source-from-settings (constantly mock-source)]+ (testing "no prior sync -> not diverged"+ (let [resp (mt/user-http-request :crowberto :get 200 "ee/remote-sync/export-preflight?branch=main")]+ (is (false? (:has_changes resp)))))+ (testing "remote ahead with non-conflicting changes -> clean merge available"+ (mt/with-temp [:model/RemoteSyncTask _ {:sync_task_type "foo"+ :ended_at :%now+ :version "other-version"}]+ (let [resp (mt/user-http-request :crowberto :get 200 "ee/remote-sync/export-preflight?branch=main")]+ (is (true? (:has_changes resp)))+ (is (true? (:clean resp)))+ (is (= [] (:conflicts resp)))))))))))))++(deftest export-rejects-branch-mismatch-test+ (testing "export and export-preflight reject a requested branch that disagrees with the configured setting (multi-tab CAS guard)"+ (mt/with-temporary-setting-values [remote-sync-type :read-write]+ (mt/with-temp [:model/Collection _ {:is_remote_synced true :name "Test Collection" :location "/"}]+ (let [mock-main (test-helpers/create-mock-source)]+ (mt/with-temporary-setting-values [remote-sync-url "https://github.com/test/repo.git"+ remote-sync-token "test-token"+ remote-sync-branch "main"]+ (mt/with-dynamic-fn-redefs [source/source-from-settings (constantly mock-main)]+ (testing "POST /export -> 409 with a branch_mismatch flag and the current branch"+ (let [before (t2/count :model/RemoteSyncTask)+ resp (mt/user-http-request :crowberto :post 409 "ee/remote-sync/export" {:branch "stale-branch"})]+ (is (true? (:branch_mismatch resp)))+ (is (= "main" (:current_branch resp)))+ (is (string? (:message resp)))+ (is (= before (t2/count :model/RemoteSyncTask))+ "no RemoteSyncTask row is created when the guard fires")))+ (testing "GET /export-preflight -> 409 with a branch_mismatch flag and the current branch"+ (let [resp (mt/user-http-request :crowberto :get 409 "ee/remote-sync/export-preflight?branch=stale-branch")]+ (is (true? (:branch_mismatch resp)))+ (is (= "main" (:current_branch resp))))))))))))(deftest export-force-if-external-changes-test(testing "POST /api/ee/remote-sync/export can force sync when remote is ahead of the last sync"@@ -414,7 +665,7 @@remote-sync-branch "main"](mt/with-dynamic-fn-redefs [source/source-from-settings (constantly mock-source)](testing "Can export with force"- (mt/user-http-request :crowberto :post 200 "ee/remote-sync/export" {:force true})))))))))+ (mt/user-http-request :crowberto :post 200 "ee/remote-sync/export" {:force true :branch "main"})))))))));;; ------------------------------------------------- Current Task Endpoint -------------------------------------------------@@ -1143,6 +1394,10 @@(create-branch [_ _ _] nil)(default-branch [_] "main")(snapshot [_]+ (throw (ex-info (str "Invalid branch: " branch)+ {:error-type :missing-branch+ :branch branch})))+ (snapshot-at [_ _version](throw (ex-info (str "Invalid branch: " branch){:error-type :missing-branch:branch branch})))))@@ -1156,7 +1411,7 @@(mt/with-temp [:model/RemoteSyncTask _ {:sync_task_type "import":ended_at :%now:version "some-prior-version"}]- (with-redefs [source/source-from-settings (fn [& _] (missing-branch-source "gone"))]+ (mt/with-dynamic-fn-redefs [source/source-from-settings (fn [& _] (missing-branch-source "gone"))](impl/invalidate-remote-changes-cache!)(let [response (mt/user-http-request :crowberto :get 200 "ee/remote-sync/has-remote-changes")](is (false? (:has_changes response)))@@ -1181,7 +1436,7 @@(mt/with-temp [:model/RemoteSyncTask _ {:sync_task_type "import":ended_at :%now:version "mock-version"}]- (with-redefs [source/source-from-settings source-fn]+ (mt/with-dynamic-fn-redefs [source/source-from-settings source-fn](impl/invalidate-remote-changes-cache!)(let [first-response (mt/user-http-request :crowberto :get 200 "ee/remote-sync/has-remote-changes")](is (true? (:branch_missing first-response)))@@ -1198,12 +1453,13 @@(branches [_] ["main"])(create-branch [_ _ _] nil)(default-branch [_] "main")- (snapshot [_] (throw (RuntimeException. "boom"))))]+ (snapshot [_] (throw (RuntimeException. "boom")))+ (snapshot-at [_ _version] (throw (RuntimeException. "boom"))))](mt/with-temporary-setting-values [remote-sync-url "https://github.com/test/repo.git"remote-sync-token "test-token"remote-sync-branch "main"remote-sync-check-changes-cache-ttl-seconds 60]- (with-redefs [source/source-from-settings (fn [& _] failing-source)]+ (mt/with-dynamic-fn-redefs [source/source-from-settings (fn [& _] failing-source)](impl/invalidate-remote-changes-cache!)(is (thrown-with-msg? RuntimeException #"boom"(impl/has-remote-changes?))))))))@@ -1382,13 +1638,13 @@:progress 0.0}](let [mock-source (test-helpers/create-mock-source)tasks-before (t2/count :model/RemoteSyncTask)]- (with-redefs [source/source-from-settings (constantly mock-source)]+ (mt/with-dynamic-fn-redefs [source/source-from-settings (constantly mock-source)](mt/with-temporary-setting-values [remote-sync-url "https://github.com/test/repo.git"remote-sync-token "test-token"remote-sync-branch "main"remote-sync-type :read-write](is (= "Remote sync task in progress"- (mt/user-http-request :crowberto :post 400 "ee/remote-sync/import" {})))+ (mt/user-http-request :crowberto :post 400 "ee/remote-sync/import" {:expected_branch "main"})))(is (= tasks-before (t2/count :model/RemoteSyncTask))"no NEW RemoteSyncTask row should be created when the guard fires")))))))@@ -1400,14 +1656,14 @@:progress 0.0}](let [mock-source (test-helpers/create-mock-source)tasks-before (t2/count :model/RemoteSyncTask)]- (with-redefs [source/source-from-settings (constantly mock-source)]+ (mt/with-dynamic-fn-redefs [source/source-from-settings (constantly mock-source)](mt/with-temporary-setting-values [remote-sync-url "https://github.com/test/repo.git"remote-sync-token "test-token"remote-sync-branch "main"remote-sync-type :read-write](is (= "Remote sync task in progress"(mt/user-http-request :crowberto :post 400 "ee/remote-sync/export"- {:message "test export"})))+ {:message "test export" :branch "main"})))(is (= tasks-before (t2/count :model/RemoteSyncTask))"no NEW RemoteSyncTask row should be created when the guard fires")))))))@@ -1419,7 +1675,7 @@:progress 0.0}](let [mock-source (test-helpers/create-mock-source)initial-branches @(:branches-atom mock-source)]- (with-redefs [source/source-from-settings (constantly mock-source)]+ (mt/with-dynamic-fn-redefs [source/source-from-settings (constantly mock-source)](mt/with-temporary-setting-values [remote-sync-url "https://github.com/test/repo.git"remote-sync-token "test-token"remote-sync-branch "main"@@ -1442,7 +1698,7 @@(let [mock-source (test-helpers/create-mock-source)initial-branches @(:branches-atom mock-source)tasks-before (t2/count :model/RemoteSyncTask)]- (with-redefs [source/source-from-settings (constantly mock-source)]+ (mt/with-dynamic-fn-redefs [source/source-from-settings (constantly mock-source)](mt/with-temporary-setting-values [remote-sync-url "https://github.com/test/repo.git"remote-sync-token "test-token"remote-sync-branch "main"diff --git a/enterprise/backend/test/metabase_enterprise/remote_sync/impl_test.clj b/enterprise/backend/test/metabase_enterprise/remote_sync/impl_test.cljindex 5abd51b37c8..9be7650780c 100644--- a/enterprise/backend/test/metabase_enterprise/remote_sync/impl_test.clj+++ b/enterprise/backend/test/metabase_enterprise/remote_sync/impl_test.clj@@ -10,6 +10,7 @@[metabase-enterprise.remote-sync.settings :as remote-sync.settings][metabase-enterprise.remote-sync.source :as source][metabase-enterprise.remote-sync.source.protocol :as source.p]+ [metabase-enterprise.remote-sync.spec :as spec][metabase-enterprise.remote-sync.test-helpers :as test-helpers][metabase.app-db.core :as app-db][metabase.collections.models.collection :as collection]@@ -1621,3 +1622,314 @@ serdes/meta:"new task must be active")(is (not (:existing? new-task))"new task must not be marked as existing"))))++;;; ------------------------------------- export! divergence / merge tests -------------------------------------++(defn- export-test-snapshot+ "A minimal SourceSnapshot reporting a fixed `version`."+ [version]+ (reify source.p/SourceSnapshot+ (version [_] version)+ (list-files [_] [])+ (read-file [_ _] nil)+ (write-files! [_ _ _] version)+ (apply-changes! [_ _ _ _] version)))++(defn- export-test-source+ "A minimal Source whose snapshot-at returns a snapshot at the requested version."+ []+ (reify source.p/Source+ (branches [_] ["main"])+ (create-branch [_ _ _] nil)+ (default-branch [_] "main")+ (snapshot [_] (export-test-snapshot "remote-R"))+ (snapshot-at [_ v] (export-test-snapshot v))))++(deftest export!-merges-non-conflicting-remote-changes-test+ (testing "when the remote advanced and the merge is clean, export! merges, advances the version, and reconciles local"+ (mt/with-temp [:model/RemoteSyncTask {task-id :id} {:sync_task_type "export"}]+ (let [reconciled (atom nil)]+ (with-redefs [remote-sync.task/last-version (constantly "base-B")+ spec/extract-entities-for-export (constantly [{:dummy true}])+ source/merge-and-store! (fn [_ _ _ _ _]+ {:status :success :version "merged-M"+ :summary {:added 2 :updated 1 :removed 0}})+ impl/load-snapshot! (fn [snap _ _ & {:keys [finalize!]}]+ (reset! reconciled (source.p/version snap))+ (when finalize! (finalize!)))]+ (let [result (impl/export! (export-test-snapshot "remote-R") task-id "msg"+ :merge? true+ :source (export-test-source)+ :base-snapshot (export-test-snapshot "base-B"))]+ (is (= :success (:status result)))+ (is (= {:added 2 :updated 1 :removed 0} (:merge-summary result)))+ (is (= "merged-M" @reconciled)+ "the merged result is loaded back into the local app DB (the pull half)")+ (is (= "merged-M" (:version (t2/select-one :model/RemoteSyncTask :id task-id))))))))))++(deftest export!-blocks-on-genuine-conflict-test+ (testing "when the same entity changed on both sides, export! returns :conflict without advancing the version or reconciling"+ (mt/with-temp [:model/RemoteSyncTask {task-id :id} {:sync_task_type "export"}]+ (let [reconciled? (atom false)]+ (with-redefs [remote-sync.task/last-version (constantly "base-B")+ spec/extract-entities-for-export (constantly [{:dummy true}])+ source/merge-and-store! (fn [_ _ _ _ _]+ {:status :conflict+ :conflicts [{:key [["Card" "A"]]+ :ours {:path "collections/a.yaml" :content "x"}+ :theirs {:path "collections/a.yaml" :content "y"}}]+ :summary {:added 0 :updated 0 :removed 0}})+ impl/load-snapshot! (fn [_ _ _] (reset! reconciled? true))]+ (let [result (impl/export! (export-test-snapshot "remote-R") task-id "msg"+ :merge? true+ :source (export-test-source)+ :base-snapshot (export-test-snapshot "base-B"))]+ (is (= :conflict (:status result)))+ (is (= ["Card A (collections/a.yaml)"] (:conflicts result)))+ (is (false? @reconciled?) "no reconcile happens on conflict")+ (is (nil? (:version (t2/select-one :model/RemoteSyncTask :id task-id)))+ "the task version is not advanced on conflict")))))))++(deftest export!-merge-fails-when-merged-commit-unresolvable-test+ (testing "if the pushed merge commit can't be resolved locally, export! fails loudly without advancing the version or reconciling"+ (mt/with-temp [:model/RemoteSyncTask {task-id :id} {:sync_task_type "export" :version "base-B"}]+ (let [reconciled? (atom false)+ no-resolve-source (reify source.p/Source+ (branches [_] ["main"])+ (create-branch [_ _ _] nil)+ (default-branch [_] "main")+ (snapshot [_] (export-test-snapshot "remote-R"))+ (snapshot-at [_ _] nil))]+ (with-redefs [remote-sync.task/last-version (constantly "base-B")+ spec/extract-entities-for-export (constantly [{:dummy true}])+ source/merge-and-store! (fn [_ _ _ _ _]+ {:status :success :version "merged-M"+ :summary {:added 1 :updated 0 :removed 0}})+ impl/load-snapshot! (fn [_ _ _ & _] (reset! reconciled? true))]+ (let [result (impl/export! (export-test-snapshot "remote-R") task-id "msg"+ :merge? true+ :source no-resolve-source+ :base-snapshot (export-test-snapshot "base-B"))]+ (is (= :error (:status result)))+ (is (false? @reconciled?) "no reconcile load happens when the merged commit can't be resolved")+ (is (= "base-B" (:version (t2/select-one :model/RemoteSyncTask :id task-id)))+ "the version is not advanced past the un-reconciled state, so a retry re-merges")))))))++(deftest export!-conflict-when-merge-base-unreachable-test+ (testing "when merge is requested but the merge base is gone (force-push/rebase), export! returns :conflict"+ (mt/with-temp [:model/RemoteSyncTask {task-id :id} {:sync_task_type "export"}]+ (with-redefs [remote-sync.task/last-version (constantly "base-B")+ spec/extract-entities-for-export (constantly [{:dummy true}])]+ (let [result (impl/export! (export-test-snapshot "remote-R") task-id "msg"+ :merge? true+ :source (export-test-source)+ :base-snapshot nil)]+ (is (= :conflict (:status result)))+ (is (str/includes? (:message result) "rewritten")))))))++(deftest export!-refuses-when-diverged-without-merge-flag-test+ (testing "when the remote advanced and neither force? nor merge? is set, export! refuses without writing or merging"+ (mt/with-temp [:model/RemoteSyncTask {task-id :id} {:sync_task_type "export"}]+ (let [merged? (atom false)+ stored? (atom false)]+ (with-redefs [remote-sync.task/last-version (constantly "base-B")+ spec/extract-entities-for-export (constantly [{:dummy true}])+ source/merge-and-store! (fn [_ _ _ _ _] (reset! merged? true) {:status :success})+ source/store! (fn [_ _ _ _] (reset! stored? true) "v")]+ (let [result (impl/export! (export-test-snapshot "remote-R") task-id "msg"+ :source (export-test-source)+ :base-snapshot (export-test-snapshot "base-B"))]+ (is (= :conflict (:status result)))+ (is (false? @merged?) "no merge without the merge flag")+ (is (false? @stored?) "nothing written")+ (is (nil? (:version (t2/select-one :model/RemoteSyncTask :id task-id))))))))))++(deftest export!-force-overwrites-without-merging-test+ (testing "force? overwrites the remote wholesale (full export) even when it advanced — no merge"+ (mt/with-temp [:model/RemoteSyncTask {task-id :id} {:sync_task_type "export"}]+ (let [merged? (atom false)]+ (with-redefs [remote-sync.task/last-version (constantly "base-B")+ spec/extract-entities-for-export (constantly [{:dummy true}])+ source/merge-and-store! (fn [& _] (reset! merged? true) {:status :success})+ ;; force? routes through full-export!, which uses store!'s {:version :entries} result.+ source/store! (fn [& _] {:version "forced-version" :entries []})]+ (let [result (impl/export! (export-test-snapshot "remote-R") task-id "msg"+ :force? true+ :source (export-test-source)+ :base-snapshot (export-test-snapshot "base-B"))]+ (is (= :success (:status result)))+ (is (false? @merged?) "force? skips the merge path")+ (is (= "forced-version" (:version (t2/select-one :model/RemoteSyncTask :id task-id))))))))))++(deftest export!-no-merge-when-not-diverged-test+ (testing "when the remote has not advanced, export! takes the normal (non-merge) export path"+ (mt/with-temp [:model/RemoteSyncTask {task-id :id} {:sync_task_type "export"}]+ (let [merged? (atom false)]+ ;; remote-sync.task/last-version equals the snapshot version, so not diverged. No dirty RemoteSyncObject+ ;; rows and the snapshot lists no files, so the normal export path is a no-op success — and crucially+ ;; never reaches the merge path.+ (with-redefs [remote-sync.task/last-version (constantly "remote-R")+ spec/extract-entities-for-export (constantly [{:dummy true}])+ source/merge-and-store! (fn [& _] (reset! merged? true) {:status :success})]+ (let [result (impl/export! (export-test-snapshot "remote-R") task-id "msg"+ :source (export-test-source)+ :base-snapshot (export-test-snapshot "remote-R"))]+ (is (= :success (:status result)))+ (is (false? @merged?) "no merge when not diverged")))))))++;;; ------------------------------------- export preflight tests -------------------------------------++(deftest preview-export-merge-not-diverged-test+ (testing "preview reports no changes when the remote has not advanced"+ (with-redefs [remote-sync.task/last-version (constantly "remote-R") ; == snapshot version+ source/source-from-settings (constantly (export-test-source))]+ (is (= {:diverged? false :clean? true :conflicts [] :summary {:added 0 :updated 0 :removed 0}}+ (impl/preview-export-merge "main"))))))++(deftest preview-export-merge-clean-test+ (testing "preview reports a clean merge with a summary when changes don't conflict"+ (with-redefs [remote-sync.task/last-version (constantly "base-B")+ source/source-from-settings (constantly (export-test-source))+ spec/extract-entities-for-export (constantly [{:dummy true}])+ source/preview-merge (fn [_ _ _ _]+ {:clean? true :conflicts []+ :summary {:added 1 :updated 0 :removed 0}})]+ (is (= {:diverged? true :clean? true :conflicts [] :summary {:added 1 :updated 0 :removed 0}}+ (impl/preview-export-merge "main"))))))++(deftest preview-export-merge-conflict-test+ (testing "preview reports conflicts when the same entity changed on both sides"+ (with-redefs [remote-sync.task/last-version (constantly "base-B")+ source/source-from-settings (constantly (export-test-source))+ spec/extract-entities-for-export (constantly [{:dummy true}])+ source/preview-merge (fn [_ _ _ _]+ {:clean? false :conflicts ["Card A (collections/a.yaml)"]+ :summary {:added 0 :updated 0 :removed 0}})]+ (is (= {:diverged? true :clean? false+ :conflicts ["Card A (collections/a.yaml)"]+ :summary {:added 0 :updated 0 :removed 0}}+ (impl/preview-export-merge "main"))))))++(deftest preview-export-merge-history-rewritten-test+ (testing "preview reports :history-rewritten when the merge base is gone"+ (let [no-base-source (reify source.p/Source+ (branches [_] ["main"])+ (create-branch [_ _ _] nil)+ (default-branch [_] "main")+ (snapshot [_] (export-test-snapshot "remote-R"))+ (snapshot-at [_ _] nil))]+ (with-redefs [remote-sync.task/last-version (constantly "gone-base")+ source/source-from-settings (constantly no-base-source)+ spec/extract-entities-for-export (constantly [{:dummy true}])]+ (let [result (impl/preview-export-merge "main")]+ (is (true? (:diverged? result)))+ (is (false? (:clean? result)))+ (is (= :history-rewritten (:reason result))))))))++;;; ------------------------------------- local-only pull merge tests -------------------------------------++(deftest import!-merge-keeps-local-changes-test+ (testing "a local-only merge loads remote changes, preserves un-pushed local changes as dirty, and sets version to remote"+ (mt/with-temp [:model/RemoteSyncTask {task-id :id} {:sync_task_type "import"}+ ;; a pending local change (un-pushed) and an already-synced object+ :model/RemoteSyncObject _ {:model_type "Card" :model_id 9991 :status "update"+ :model_name "Local Card" :status_changed_at :%now}+ :model/RemoteSyncObject _ {:model_type "Card" :model_id 9992 :status "synced"+ :model_name "Remote Card" :status_changed_at :%now}]+ (with-redefs [source/compute-merge (fn [_ _ _ _]+ {:merged [{:path "collections/x.yaml" :content "y"}]+ :conflicts []+ :summary {:added 1 :updated 0 :removed 0}})+ ;; simulate the load marking everything synced (what sync-objects! does), then running+ ;; the in-transaction finalize (restore-dirty + set-version)+ impl/load-snapshot! (fn [_ _ _ & {:keys [finalize!]}]+ (t2/update! :model/RemoteSyncObject+ :model_id [:in [9991 9992]]+ {:status "synced"})+ (when finalize! (finalize!)))]+ (let [result (impl/import! (export-test-snapshot "remote-R") task-id+ :merge? true+ :base-snapshot (export-test-snapshot "base-B"))]+ (is (= :success (:status result)))+ (is (= {:added 1 :updated 0 :removed 0} (:merge-summary result)))+ (is (= "remote-R" (:version (t2/select-one :model/RemoteSyncTask :id task-id)))+ "version advances to the remote tip; local changes remain to be pushed")+ (testing "the un-pushed local change is restored to dirty after the merge"+ (is (= "update" (t2/select-one-fn :status :model/RemoteSyncObject :model_id 9991))))+ (testing "remote-originated content stays synced"+ (is (= "synced" (t2/select-one-fn :status :model/RemoteSyncObject :model_id 9992)))))))))++(deftest import!-merge-restores-pending-deletion-test+ (testing "a pending local deletion (no app-db row) is re-inserted as dirty after a merge"+ (mt/with-temp [:model/RemoteSyncTask {task-id :id} {:sync_task_type "import"}+ :model/RemoteSyncObject _ {:model_type "Card" :model_id 8881 :status "delete"+ :model_name "Deleted Card" :status_changed_at :%now}]+ (with-redefs [source/compute-merge (fn [_ _ _ _]+ {:merged [] :conflicts [] :summary {:added 0 :updated 0 :removed 0}})+ ;; simulate the load wiping and not re-inserting the deleted entity's row, then the+ ;; in-transaction finalize (restore-dirty + set-version)+ impl/load-snapshot! (fn [_ _ _ & {:keys [finalize!]}]+ (t2/delete! :model/RemoteSyncObject :model_id 8881)+ (when finalize! (finalize!)))]+ (let [result (impl/import! (export-test-snapshot "remote-R") task-id+ :merge? true+ :base-snapshot (export-test-snapshot "base-B"))]+ (is (= :success (:status result)))+ (is (= "delete" (t2/select-one-fn :status :model/RemoteSyncObject :model_id 8881))+ "the pending deletion is preserved so it can be pushed later"))))))++(deftest import!-merge-folds-remote-change-and-keeps-local-deletion-test+ (testing "a single pull merge folds in an unrelated remote change while preserving a pending local deletion as dirty"+ (mt/with-temp [:model/RemoteSyncTask {task-id :id} {:sync_task_type "import"}+ ;; a pending local deletion (no app-db row) and a synced remote-origin card+ :model/RemoteSyncObject _ {:model_type "Card" :model_id 8881 :status "delete"+ :model_name "Locally Deleted" :status_changed_at :%now}+ :model/RemoteSyncObject _ {:model_type "Card" :model_id 9992 :status "synced"+ :model_name "Remote Card" :status_changed_at :%now}]+ (with-redefs [source/compute-merge (fn [_ _ _ _]+ {:merged [{:path "collections/remote.yaml" :content "y"}]+ :conflicts []+ :summary {:added 0 :updated 1 :removed 0}})+ ;; simulate the load: the remote change is applied (9992 stays synced), the deleted+ ;; entity's row is wiped (the load doesn't re-create it), then the finalize runs.+ impl/load-snapshot! (fn [_ _ _ & {:keys [finalize!]}]+ (t2/delete! :model/RemoteSyncObject :model_id 8881)+ (t2/update! :model/RemoteSyncObject :model_id 9992 {:status "synced"})+ (when finalize! (finalize!)))]+ (let [result (impl/import! (export-test-snapshot "remote-R") task-id+ :merge? true+ :base-snapshot (export-test-snapshot "base-B"))]+ (is (= :success (:status result)))+ (is (= {:added 0 :updated 1 :removed 0} (:merge-summary result)))+ (testing "the unrelated remote change is folded in (stays synced)"+ (is (= "synced" (t2/select-one-fn :status :model/RemoteSyncObject :model_id 9992))))+ (testing "the pending local deletion is preserved as dirty (re-inserted by restore-dirty-objects!)"+ (is (= "delete" (t2/select-one-fn :status :model/RemoteSyncObject :model_id 8881))))+ (is (= "remote-R" (:version (t2/select-one :model/RemoteSyncTask :id task-id)))+ "the version advances to the remote tip"))))))++(deftest import!-merge-conflict-test+ (testing "a local-only merge with a genuine conflict returns :conflict and does not load"+ (mt/with-temp [:model/RemoteSyncTask {task-id :id} {:sync_task_type "import"}]+ (with-redefs [source/compute-merge (fn [_ _ _ _]+ {:merged []+ :conflicts [{:key [["Card" "A"]]+ :ours {:path "collections/a.yaml" :content "x"}+ :theirs {:path "collections/a.yaml" :content "z"}}]+ :summary {:added 0 :updated 0 :removed 0}})+ impl/load-snapshot! (fn [_ _ _] (throw (ex-info "should not load on conflict" {})))]+ (let [result (impl/import! (export-test-snapshot "remote-R") task-id+ :merge? true+ :base-snapshot (export-test-snapshot "base-B"))]+ (is (= :conflict (:status result)))+ (is (= ["Card A (collections/a.yaml)"] (:conflicts result)))+ (is (nil? (:version (t2/select-one :model/RemoteSyncTask :id task-id)))))))))++(deftest import!-merge-history-rewritten-test+ (testing "a local-only merge with no reachable base returns :conflict"+ (mt/with-temp [:model/RemoteSyncTask {task-id :id} {:sync_task_type "import"}]+ (let [result (impl/import! (export-test-snapshot "remote-R") task-id+ :merge? true+ :base-snapshot nil)]+ (is (= :conflict (:status result)))+ (is (str/includes? (:message result) "rewritten"))))))diff --git a/enterprise/backend/test/metabase_enterprise/remote_sync/incremental_export_test.clj b/enterprise/backend/test/metabase_enterprise/remote_sync/incremental_export_test.cljindex d4dc1d0bc20..4a7ad72c3ba 100644--- a/enterprise/backend/test/metabase_enterprise/remote_sync/incremental_export_test.clj+++ b/enterprise/backend/test/metabase_enterprise/remote_sync/incremental_export_test.clj@@ -505,3 +505,32 @@(is (entity-exported? mock e2) "second Foo exported")(is (not= (path-for-eid mock e1) (path-for-eid mock e2))"the two Foos get distinct, deduped paths"))))))++(deftest force-export-overwrites-upstream-changes-test+ ;; A force export must be a true overwrite: it has to drop files the remote diverged with, which the+ ;; incremental fast-path (preserving every file it didn't touch) cannot do. This pins force? = full+ ;; re-serialize and guards the GHY-3726 reconciliation against regressing to an incremental force.+ (with-exported-collection!+ (fn [{:keys [mock card-a]}]+ (let [ghost-path "collections/bench/cards/ghost_upstream.yaml"+ a-eid (t2/select-one-fn :entity_id :model/Card :id card-a)]+ ;; Simulate the remote diverging: a new, non-colliding upstream file the local instance never imported.+ (swap! (:files-atom mock) assoc-in ["main" ghost-path]+ "name: Ghost\nentity_id: ghostUpstream00000001\nserdes/meta:\n- id: ghostUpstream00000001\n model: Card\n")+ (t2/update! :model/Card card-a {:description "local edit"})+ (set-status! "Card" card-a "update")+ (testing "an ordinary (incremental) export preserves the upstream file — so it is NOT an overwrite"+ (let [task (new-task!)]+ (is (= :success (:status (impl/export! (source.p/snapshot mock) task "edit"))))+ (is (= "apply-changes-version" (written-version task)) "took the incremental fast-path")+ (is (contains? (files mock) ghost-path)+ "the incremental path leaves the upstream file in place")))+ (testing "a force export re-serializes the whole set and drops the upstream file (true overwrite)"+ (let [task (new-task!)]+ (is (= :success (:status (impl/export! (source.p/snapshot mock) task "force" :force? true))))+ (is (= "write-files-version" (written-version task)) "took the full-export path")+ (is (not (contains? (files mock) ghost-path))+ "force overwrite removes the upstream file the remote diverged with")+ (is (re-find #"local edit"+ (get (files mock) (path-for-eid mock a-eid)))+ "the local edit survives the overwrite")))))))diff --git a/enterprise/backend/test/metabase_enterprise/remote_sync/merge_test.clj b/enterprise/backend/test/metabase_enterprise/remote_sync/merge_test.cljnew file mode 100644index 00000000000..f49858b8052--- /dev/null+++ b/enterprise/backend/test/metabase_enterprise/remote_sync/merge_test.clj@@ -0,0 +1,129 @@+(ns metabase-enterprise.remote-sync.merge-test+ (:require+ [clojure.test :refer :all]+ [metabase-enterprise.remote-sync.merge :as remote-sync.merge]))++(defn- card+ "Builds a `{:path :content}` spec for a Card with the given entity `id`, `name` (which drives the on-disk+ path, as in real serialization) and an optional `extra` content fragment to vary the body."+ ([id name] (card id name ""))+ ([id name extra]+ {:path (str "collections/" name ".yaml")+ :content (str "serdes/meta:\n- model: Card\n id: " id "\n label: " name "\nname: " name "\n" extra)}))++(defn- ids+ "Sorted entity ids present in a merge result's :merged set."+ [result]+ (sort (map (fn [{:keys [content]}]+ (second (re-find #"id: (\w+)" content)))+ (:merged result))))++(deftest ^:parallel clean-merge-disjoint-changes-test+ (testing "local edits A and adds C; remote adds D; B untouched -> all merged, no conflict"+ (let [base [(card "A" "a") (card "B" "b")]+ ours [(card "A" "a" "x: 1\n") (card "B" "b") (card "C" "c")]+ theirs [(card "A" "a") (card "B" "b") (card "D" "d")]+ result (remote-sync.merge/three-way-merge base ours theirs)]+ (is (empty? (:conflicts result)))+ (is (= ["A" "B" "C" "D"] (ids result)))+ (testing "only D is counted as a folded-in remote add"+ (is (= {:added 1 :updated 0 :removed 0} (:summary result)))))))++(deftest ^:parallel conflict-same-entity-edited-both-sides-test+ (testing "A edited differently on both sides -> conflict, nothing merged for A"+ (let [result (remote-sync.merge/three-way-merge+ [(card "A" "a")]+ [(card "A" "a" "x: ours\n")]+ [(card "A" "a" "x: theirs\n")])]+ (is (= 1 (count (:conflicts result))))+ (is (empty? (:merged result))))))++(deftest ^:parallel local-edit-vs-remote-rename-is-conflict-test+ (testing "local edits A's content while remote renames A -> conflict (both changed the same entity)"+ (let [result (remote-sync.merge/three-way-merge+ [(card "A" "a")]+ [(card "A" "a" "x: 1\n")]+ [(card "A" "a2")])]+ (is (= 1 (count (:conflicts result)))))))++(deftest ^:parallel rename-to-different-names-no-duplicate-test+ (testing "both sides rename A to different names -> conflict, and crucially NO duplicate entity files"+ (let [result (remote-sync.merge/three-way-merge+ [(card "A" "a")]+ [(card "A" "bar")]+ [(card "A" "baz")])]+ (is (= 1 (count (:conflicts result))))+ (is (empty? (:merged result))+ "a path-keyed merge would wrongly keep both bar.yaml and baz.yaml (same entity_id)"))))++(deftest ^:parallel remote-only-rename-takes-theirs-test+ (testing "only remote renames A; local untouched -> take remote's path+content, no conflict"+ (let [result (remote-sync.merge/three-way-merge+ [(card "A" "a")]+ [(card "A" "a")]+ [(card "A" "renamed")])]+ (is (empty? (:conflicts result)))+ (is (= ["collections/renamed.yaml"] (map :path (:merged result))))+ (is (= {:added 0 :updated 1 :removed 0} (:summary result))))))++(deftest ^:parallel remote-delete-takes-effect-test+ (testing "remote deletes A; local untouched -> A removed from merged, counted as remote removal"+ (let [result (remote-sync.merge/three-way-merge+ [(card "A" "a") (card "B" "b")]+ [(card "A" "a") (card "B" "b")]+ [(card "B" "b")])]+ (is (empty? (:conflicts result)))+ (is (= ["B"] (ids result)))+ (is (= {:added 0 :updated 0 :removed 1} (:summary result))))))++(deftest ^:parallel local-delete-vs-remote-edit-is-conflict-test+ (testing "local deletes A while remote edits A -> modify/delete conflict"+ (let [result (remote-sync.merge/three-way-merge+ [(card "A" "a")]+ []+ [(card "A" "a" "x: 1\n")])]+ (is (= 1 (count (:conflicts result)))))))++(deftest ^:parallel both-delete-is-clean-test+ (testing "both sides delete A -> clean, A gone, not counted as a remote change"+ (let [result (remote-sync.merge/three-way-merge+ [(card "A" "a")]+ []+ [])]+ (is (empty? (:conflicts result)))+ (is (empty? (:merged result)))+ (is (= {:added 0 :updated 0 :removed 0} (:summary result))))))++(deftest ^:parallel duplicate-entity-id-on-one-side-throws-test+ (testing "two specs on the same side sharing an entity_id is corruption -> throw, not silently drop one"+ (is (thrown-with-msg?+ clojure.lang.ExceptionInfo #"Duplicate serdes identity"+ (remote-sync.merge/three-way-merge+ [(card "A" "a")]+ [(card "A" "bar") (card "A" "baz")] ; same entity_id "A" at two different paths+ [(card "A" "a")]))))+ (testing "the same entity_id appearing once per side is fine (that's the normal rename/edit case)"+ (is (some? (remote-sync.merge/three-way-merge+ [(card "A" "a")]+ [(card "A" "bar")]+ [(card "A" "baz")])))))++(deftest ^:parallel conflict-label-test+ (testing "conflict-label uses the entity's name and path"+ (is (= "bar (collections/bar.yaml)"+ (remote-sync.merge/conflict-label+ {:key [["Card" "A"]]+ :ours (card "A" "bar")+ :theirs (card "A" "baz")}))))+ (testing "falls back to model + id when the content has no name"+ (is (= "Card A (collections/a.yaml)"+ (remote-sync.merge/conflict-label+ {:key [["Card" "A"]]+ :ours {:path "collections/a.yaml" :content "not-an-entity"}+ :theirs {:path "collections/a.yaml" :content "also-not"}}))))+ (testing "a path-fallback key uses the filename, not garbage from destructuring the path string"+ (is (= "collections/x.yaml"+ (remote-sync.merge/conflict-label+ {:key [::remote-sync.merge/by-path "collections/x.yaml"]+ :ours {:path "collections/x.yaml" :content "not-an-entity"}+ :theirs {:path "collections/x.yaml" :content "also-not"}})))))diff --git a/enterprise/backend/test/metabase_enterprise/remote_sync/source/wrapping_source_test.clj b/enterprise/backend/test/metabase_enterprise/remote_sync/source/wrapping_source_test.cljindex 62f84a95265..281b6fb3a6a 100644--- a/enterprise/backend/test/metabase_enterprise/remote_sync/source/wrapping_source_test.clj+++ b/enterprise/backend/test/metabase_enterprise/remote_sync/source/wrapping_source_test.clj@@ -34,6 +34,9 @@"main")(snapshot [_]+ (->MockSourceSnapshot files))++ (snapshot-at [_ _version](->MockSourceSnapshot files)))(deftest wrapping-source-list-files-single-filter-testdiff --git a/enterprise/backend/test/metabase_enterprise/remote_sync/source_test.clj b/enterprise/backend/test/metabase_enterprise/remote_sync/source_test.cljindex 6dbee30f270..20a0eb4955c 100644--- a/enterprise/backend/test/metabase_enterprise/remote_sync/source_test.clj+++ b/enterprise/backend/test/metabase_enterprise/remote_sync/source_test.clj@@ -45,6 +45,9 @@"main")(snapshot [_]+ (->MockSourceSnapshot written-files))++ (snapshot-at [_ _version](->MockSourceSnapshot written-files)))(defn- create-test-entity@@ -54,6 +57,28 @@:name (str "Test " label):entity_id id})+(defn- create-test-entity*+ "Like create-test-entity but with an extra field to vary the serialized content (simulating an edit)."+ [id label model-name extra]+ (assoc (create-test-entity id label model-name) :note extra))++(defn- entities->snapshot+ "Serializes `entities` to file specs and returns a snapshot whose list-files/read-file expose them. The+ snapshot also records writes into `written` (an atom) and reports a fixed version, so it can stand in for+ the remote tip in [[source/merge-and-store!]]."+ [entities task-id written]+ (let [by-path (into {} (map (juxt :path :content)) (source/serialize-specs entities task-id))]+ (reify source.p/SourceSnapshot+ (list-files [_] (vec (keys by-path)))+ (read-file [_ p] (get by-path p))+ (write-files! [_ message files]+ (reset! written {:message message :files (vec files)})+ "merged-version")+ (apply-changes! [_ message upserts _delete-paths]+ (reset! written {:message message :files (vec upserts)})+ "merged-version")+ (version [_] "remote-tip"))))+(deftest store!-basic-test(testing "store! writes files to source with correct structure"(mt/with-temp [:model/User user {:first_name "Test"@@ -174,3 +199,73 @@z-last-pos (str/index-of content "z_last_field:")](is (< a-first-pos z-last-pos)"a_first_field should appear before z_last_field (alphabetical)")))))))++(deftest merge-and-store!-clean-merge-test+ (testing "when local and remote changed different entities, merge-and-store! writes the union with no conflict"+ (mt/with-temp [:model/RemoteSyncTask {task-id :id} {:sync_task_type "export"}]+ (let [written (atom nil)+ base [(create-test-entity "A" "a" "Card") (create-test-entity "B" "b" "Card")]+ ;; local kept A and B, added C+ ours [(create-test-entity "A" "a" "Card") (create-test-entity "B" "b" "Card")+ (create-test-entity "C" "c" "Card")]+ ;; remote kept A and B, added D+ theirs [(create-test-entity "A" "a" "Card") (create-test-entity "B" "b" "Card")+ (create-test-entity "D" "d" "Card")]+ base-snap (entities->snapshot base task-id (atom nil))+ remote-snap (entities->snapshot theirs task-id written)+ result (source/merge-and-store! ours remote-snap base-snap task-id "merge commit")]+ (is (= :success (:status result)))+ (is (= "merged-version" (:version result)))+ (is (= {:added 1 :updated 0 :removed 0} (:summary result))+ "only D is folded in from the remote")+ (testing "the union of all four entities is written"+ (let [ids (->> @written :files+ (keep #(second (re-find #"entity_id: (\w+)" (:content %))))+ sort)]+ (is (= ["A" "B" "C" "D"] ids))))))))++(deftest merge-and-store!-conflict-test+ (testing "when the same entity changed on both sides, merge-and-store! returns :conflict and writes nothing"+ (mt/with-temp [:model/RemoteSyncTask {task-id :id} {:sync_task_type "export"}]+ (let [written (atom nil)+ base [(create-test-entity "A" "a" "Card")]+ ours [(create-test-entity* "A" "a" "Card" "ours")]+ theirs [(create-test-entity* "A" "a" "Card" "theirs")]+ base-snap (entities->snapshot base task-id (atom nil))+ remote-snap (entities->snapshot theirs task-id written)+ result (source/merge-and-store! ours remote-snap base-snap task-id "merge commit")]+ (is (= :conflict (:status result)))+ (is (= 1 (count (:conflicts result))))+ (is (nil? @written) "nothing should be written when there is a conflict")))))++(deftest preview-merge-clean-test+ (testing "preview-merge reports a clean merge and summary without writing"+ (mt/with-temp [:model/RemoteSyncTask {task-id :id} {:sync_task_type "export"}]+ (let [written (atom nil)+ base [(create-test-entity "A" "a" "Card") (create-test-entity "B" "b" "Card")]+ ours [(create-test-entity "A" "a" "Card") (create-test-entity "B" "b" "Card")+ (create-test-entity "C" "c" "Card")]+ theirs [(create-test-entity "A" "a" "Card") (create-test-entity "B" "b" "Card")+ (create-test-entity "D" "d" "Card")]+ base-snap (entities->snapshot base task-id (atom nil))+ remote-snap (entities->snapshot theirs task-id written)+ result (source/preview-merge ours remote-snap base-snap nil)]+ (is (true? (:clean? result)))+ (is (empty? (:conflicts result)))+ (is (= {:added 1 :updated 0 :removed 0} (:summary result)))+ (is (nil? @written) "preview must not write")))))++(deftest preview-merge-conflict-test+ (testing "preview-merge reports conflicts (with labels) without writing"+ (mt/with-temp [:model/RemoteSyncTask {task-id :id} {:sync_task_type "export"}]+ (let [written (atom nil)+ base [(create-test-entity "A" "a" "Card")]+ ours [(create-test-entity* "A" "a" "Card" "ours")]+ theirs [(create-test-entity* "A" "a" "Card" "theirs")]+ base-snap (entities->snapshot base task-id (atom nil))+ remote-snap (entities->snapshot theirs task-id written)+ result (source/preview-merge ours remote-snap base-snap nil)]+ (is (false? (:clean? result)))+ (is (= 1 (count (:conflicts result))))+ (is (every? string? (:conflicts result)))+ (is (nil? @written))))))diff --git a/enterprise/backend/test/metabase_enterprise/remote_sync/test_helpers.clj b/enterprise/backend/test/metabase_enterprise/remote_sync/test_helpers.cljindex b2677576450..cba79d8ad5b 100644--- a/enterprise/backend/test/metabase_enterprise/remote_sync/test_helpers.clj+++ b/enterprise/backend/test/metabase_enterprise/remote_sync/test_helpers.clj@@ -247,6 +247,11 @@ width: fixed"main")(snapshot [_this]+ (->MockSourceSnapshot source-id base-url branch fail-mode files-atom managed-dirs))++ (snapshot-at [_this _version]+ ;; The mock is version-agnostic (always reports "mock-version"), so any historical snapshot is just+ ;; the current view of the files.(->MockSourceSnapshot source-id base-url branch fail-mode files-atom managed-dirs)))(defn create-mock-source@@ -276,6 +281,54 @@ width: fixedbranches-atom (atom #{["main" "main-ref"] ["develop" "develop-ref"]})](->MockSource "test-source" "https://test.example.com" branch fail-mode files-atom branches-atom managed-dirs)))+(defn versioned-source+ "A fake Source for exercising the `async-import!`/`async-export!` base-snapshot resolution end-to-end.++ Unlike [[create-mock-source]] (which always reports \"mock-version\"), this lets a test control+ versions: `:current` is the version `snapshot` reports, and `:trees` maps version -> {path content}.+ `snapshot-at` returns a snapshot for a version present in `trees`, or nil for an unknown version (to+ model a base orphaned by a force-push/rebase). `write-files!` records the written set under a fresh+ version, advances `:current`, and returns the new version so an export can fast-forward onto it."+ [& {:keys [current trees branch managed-dirs]+ :or {current "v-remote" branch "main" managed-dirs ingest/legal-top-level-paths}}]+ (let [managed (set managed-dirs)+ state (atom {:current current :trees (or trees {}) :counter 0})+ mk-snapshot (fn mk-snapshot [version]+ (reify source.p/SourceSnapshot+ (list-files [_] (vec (keys (get-in @state [:trees version] {}))))+ (read-file [_ path] (get-in @state [:trees version path]))+ (write-files! [_ _message files]+ (let [n (:counter (swap! state update :counter inc))+ new-version (str "written-" n)+ kept (into {}+ (remove (fn [[p _]] (contains? managed (top-level-dir p))))+ (get-in @state [:trees version] {}))+ tree (into kept+ (comp (remove #(str/blank? (:path %)))+ (map (juxt :path :content)))+ files)]+ (swap! state #(-> % (assoc-in [:trees new-version] tree) (assoc :current new-version)))+ new-version))+ (apply-changes! [_ _message upserts delete-paths]+ (let [n (:counter (swap! state update :counter inc))+ new-version (str "written-" n)+ delete-set (into #{} (remove str/blank?) delete-paths)+ tree (as-> (get-in @state [:trees version] {}) t+ (apply dissoc t delete-set)+ (into t+ (comp (remove #(str/blank? (:path %)))+ (map (juxt :path :content)))+ upserts))]+ (swap! state #(-> % (assoc-in [:trees new-version] tree) (assoc :current new-version)))+ new-version))+ (version [_] version)))]+ (reify source.p/Source+ (branches [_] [branch])+ (create-branch [_ _ _] nil)+ (default-branch [_] branch)+ (snapshot [_] (mk-snapshot (:current @state)))+ (snapshot-at [_ v] (when (contains? (:trees @state) v) (mk-snapshot v))))))+(defn clean-object"Test fixture that resets the RemoteSyncObject table before running tests to prevent existingentries from affecting dirty state checks."diff --git a/enterprise/frontend/src/metabase-enterprise/api/remote-sync.ts b/enterprise/frontend/src/metabase-enterprise/api/remote-sync.tsindex 4ec16e4dba6..b0c38e04afb 100644--- a/enterprise/frontend/src/metabase-enterprise/api/remote-sync.ts+++ b/enterprise/frontend/src/metabase-enterprise/api/remote-sync.ts@@ -2,6 +2,7 @@ import type {CreateBranchRequest,ExportChangesRequest,ExportChangesResponse,+ ExportPreflightResponse,GetBranchesResponse,HasRemoteChangesResponse,ImportFromBranchRequest,@@ -24,13 +25,14 @@ export const remoteSyncApi = EnterpriseApi.injectEndpoints({ExportChangesResponse,ExportChangesRequest>({- query: ({ message, force, branch }) => ({+ query: ({ message, force, branch, merge }) => ({url: `/api/ee/remote-sync/export`,method: "POST",body: {message,branch,force,+ merge,},}),invalidatesTags: () => [@@ -38,16 +40,29 @@ export const remoteSyncApi = EnterpriseApi.injectEndpoints({tag("session-properties"),],}),+ getExportPreflight: builder.query<+ ExportPreflightResponse,+ { branch: string }+ >({+ query: ({ branch }) => ({+ url: `/api/ee/remote-sync/export-preflight`,+ method: "GET",+ params: { branch },+ }),+ providesTags: () => [tag("remote-sync-has-remote-changes")],+ }),importChanges: builder.mutation<ImportFromBranchResponse,ImportFromBranchRequest>({- query: ({ branch, force }) => ({+ query: ({ branch, force, merge, expected_branch }) => ({url: `/api/ee/remote-sync/import`,method: "POST",body: {branch,force,+ merge,+ expected_branch,},}),/**@@ -161,6 +176,7 @@ export const {useGetHasRemoteChangesQuery,useUpdateRemoteSyncSettingsMutation,useExportChangesMutation,+ useLazyGetExportPreflightQuery,useGetBranchesQuery,useCreateBranchMutation,useImportChangesMutation,diff --git a/enterprise/frontend/src/metabase-enterprise/remote_sync/components/GitSyncControls/GitSyncControls.tsx b/enterprise/frontend/src/metabase-enterprise/remote_sync/components/GitSyncControls/GitSyncControls.tsxindex 1873e3b07f7..4c9ebad9100 100644--- a/enterprise/frontend/src/metabase-enterprise/remote_sync/components/GitSyncControls/GitSyncControls.tsx+++ b/enterprise/frontend/src/metabase-enterprise/remote_sync/components/GitSyncControls/GitSyncControls.tsx@@ -1,17 +1,34 @@import { useDisclosure } from "@mantine/hooks";import cx from "classnames";-import { useCallback, useState } from "react";+import { useCallback, useEffect, useState } from "react";import { t } from "ttag";import { useToast } from "metabase/common/hooks";import { useDispatch, useSelector } from "metabase/redux";-import { Button, Combobox, Icon, Loader, Text, useCombobox } from "metabase/ui";+import {+ Button,+ Combobox,+ Group,+ Icon,+ Loader,+ Modal,+ Text,+ useCombobox,+} from "metabase/ui";import {useGetHasRemoteChangesQuery,useImportChangesMutation,+ useLazyGetExportPreflightQuery,} from "metabase-enterprise/api";-import { getSyncConflictVariant } from "metabase-enterprise/remote_sync/selectors";-import { syncConflictVariantUpdated } from "metabase-enterprise/remote_sync/sync-task-slice";+import {+ getCurrentTask,+ getSyncConflictVariant,+} from "metabase-enterprise/remote_sync/selectors";+import {+ syncConflictVariantUpdated,+ taskCleared,+} from "metabase-enterprise/remote_sync/sync-task-slice";+import type { ExportPreflightResponse } from "metabase-types/api";import { trackBranchSwitched, trackPullChanges } from "../../analytics";import { useGitSyncVisible } from "../../hooks/use-git-sync-visible";@@ -34,9 +51,21 @@ export const GitSyncControls = () => {const [importChanges, { isLoading: isImporting }] =useImportChangesMutation();+ const [runExportPreflight] = useLazyGetExportPreflightQuery();const { isRunning: isSyncTaskRunning } = useSyncStatus();const [nextBranch, setNextBranch] = useState<string | null>(null);+ // Set when a push or pull needs the conflict modal; carries whether a clean merge is available.+ const [conflictPreflight, setConflictPreflight] =+ useState<ExportPreflightResponse | null>(null);+ // Set when the backend rejects an action because the branch changed in another session.+ const [branchMismatch, setBranchMismatch] = useState<{+ message: string;+ currentBranch: string | null;+ } | null>(null);+ // True while the export preflight runs (push, or a dirty pull): it re-serializes the whole library and+ // reads the remote trees, so it can take a few seconds — show the control as busy meanwhile.+ const [isCheckingPreflight, setIsCheckingPreflight] = useState(false);const [showPushModal, { toggle: togglePushModal }] = useDisclosure(false);const [sendToast] = useToast();const [dropdownView, setDropdownView] = useState<DropdownView>("options");@@ -44,6 +73,23 @@ export const GitSyncControls = () => {const { isDirty, refetch: refetchDirty } = useRemoteSyncDirtyState();+ // An export task that ends in conflict (the push lost the preflight->execute race, or fell through a+ // preflight error) is otherwise silent: the middleware can't toast (no hook), so surface it here, then+ // clear the task so it doesn't re-fire on re-render/navigation.+ const currentTask = useSelector(getCurrentTask);+ useEffect(() => {+ if (+ currentTask?.status === "conflict" &&+ currentTask?.sync_task_type === "export"+ ) {+ sendToast({+ icon: "warning",+ message: t`The remote branch changed before your push finished. Pull the latest changes, then push again.`,+ });+ dispatch(taskCleared());+ }+ }, [currentTask, sendToast, dispatch]);+const {currentData: hasRemoteChangesData,isFetching: isFetchingRemoteChanges,@@ -55,7 +101,30 @@ export const GitSyncControls = () => {const { has_changes: hasRemoteChanges } = hasRemoteChangesData || {};const isSwitchingBranch = !!nextBranch;- const isLoading = isSyncTaskRunning || isSwitchingBranch || isImporting;+ const isLoading =+ isSyncTaskRunning ||+ isSwitchingBranch ||+ isImporting ||+ isCheckingPreflight;++ // If `error` is a branch-mismatch rejection (another session switched branches), open the+ // out-of-date modal prompting a refresh and return true so the caller can stop. Returns false for+ // any other error so the caller can handle it normally.+ const showBranchMismatchIfPresent = useCallback((error: unknown): boolean => {+ const {+ hasBranchMismatch,+ errorMessage,+ currentBranch: serverBranch,+ } = parseSyncError(error as SyncError);+ if (hasBranchMismatch) {+ setBranchMismatch({+ message: errorMessage ?? t`The sync branch changed in another session.`,+ currentBranch: serverBranch,+ });+ return true;+ }+ return false;+ }, []);const changeBranch = useCallback(async (branch: string | null, isNewBranch?: boolean) => {@@ -65,7 +134,16 @@ export const GitSyncControls = () => {}if (!isNewBranch) {- await importChanges({ branch });+ if (currentBranch == null) {+ return;+ }+ // expected_branch is the branch we're switching away from: reject if another session already+ // moved off it, so we don't switch from a stale base. unwrap() so a rejection (e.g. the CAS 409)+ // throws and reaches handleBranchSelect's catch instead of resolving silently.+ await importChanges({+ branch,+ expected_branch: currentBranch,+ }).unwrap();trackBranchSwitched({triggeredFrom: "app-bar",@@ -74,7 +152,7 @@ export const GitSyncControls = () => {setNextBranch(null);},- [importChanges],+ [importChanges, currentBranch],);const handleBranchSelect = useCallback(@@ -95,39 +173,119 @@ export const GitSyncControls = () => {await changeBranch(branch, isNewBranch);setNextBranch(null);}- } catch {+ } catch (error) {+ setNextBranch(null);+ if (showBranchMismatchIfPresent(error)) {+ return;+ }sendToast({icon: "warning",toastColor: "error",message: t`Sorry, we were unable to switch branches.`,});- setNextBranch(null);}},- [currentBranch, refetchDirty, dispatch, changeBranch, sendToast],+ [+ currentBranch,+ refetchDirty,+ dispatch,+ changeBranch,+ sendToast,+ showBranchMismatchIfPresent,+ ],);- const handlePushClick = useCallback(() => {- togglePushModal();+ const handlePushClick = useCallback(async () => {+ if (!currentBranch) {+ return;+ }+combobox.closeDropdown();- }, [combobox, togglePushModal]);++ // Find out up front whether the remote has advanced, so we open the right modal directly instead of+ // collecting a commit message and only then discovering the divergence.+ setIsCheckingPreflight(true);+ try {+ const preflight = await runExportPreflight({+ branch: currentBranch,+ }).unwrap();+ if (preflight.has_changes) {+ setConflictPreflight(preflight);+ dispatch(syncConflictVariantUpdated("push"));+ return;+ }+ } catch (error) {+ // Another session switched branches under us, so the branch shown here is stale. Don't fall+ // through to a push that would target the wrong branch — surface it and prompt a refresh.+ if (showBranchMismatchIfPresent(error)) {+ return;+ }+ // fall through to the plain push modal on any other preflight error+ } finally {+ setIsCheckingPreflight(false);+ }+ togglePushModal();+ }, [+ combobox,+ currentBranch,+ dispatch,+ runExportPreflight,+ togglePushModal,+ showBranchMismatchIfPresent,+ ]);const handlePullClick = useCallback(async () => {if (!currentBranch) {return;}+ combobox.closeDropdown();++ // With un-pushed local changes, a straight pull would clobber them. Check whether a clean local+ // merge is possible and let the user choose (merge / force / new branch / discard).+ if (isDirty) {+ setIsCheckingPreflight(true);+ try {+ const preflight = await runExportPreflight({+ branch: currentBranch,+ }).unwrap();+ setConflictPreflight(preflight);+ } catch (error) {+ if (showBranchMismatchIfPresent(error)) {+ return;+ }+ // Couldn't determine mergeability; open the modal without the merge option but tell the user why.+ setConflictPreflight(null);+ sendToast({+ message: t`Couldn't check whether your changes can be merged. You can still force the pull or stash to a new branch.`,+ icon: "warning",+ });+ } finally {+ setIsCheckingPreflight(false);+ }+ dispatch(syncConflictVariantUpdated("pull"));+ return;+ }+try {- await importChanges({ branch: currentBranch }).unwrap();+ await importChanges({+ branch: currentBranch,+ expected_branch: currentBranch,+ }).unwrap();trackPullChanges({triggeredFrom: "app-bar",force: false,});} catch (error) {+ if (showBranchMismatchIfPresent(error)) {+ return;+ }+const { hasConflict, errorMessage } = parseSyncError(error as SyncError);if (hasConflict) {+ setConflictPreflight(null);dispatch(syncConflictVariantUpdated("pull"));return;}@@ -137,13 +295,21 @@ export const GitSyncControls = () => {icon: "warning",});}-- combobox.closeDropdown();- }, [combobox, currentBranch, dispatch, importChanges, sendToast]);+ }, [+ combobox,+ currentBranch,+ dispatch,+ importChanges,+ isDirty,+ runExportPreflight,+ sendToast,+ showBranchMismatchIfPresent,+ ]);const handleCloseSyncConflictModal = useCallback(() => {dispatch(syncConflictVariantUpdated(null));setNextBranch(null);+ setConflictPreflight(null);}, [dispatch]);const handleSwitchBranchClick = useCallback(() => {@@ -233,8 +399,30 @@ export const GitSyncControls = () => {nextBranch={nextBranch}onClose={handleCloseSyncConflictModal}variant={conflictVariant}+ canMerge={conflictPreflight?.clean}+ conflicts={conflictPreflight?.conflicts}/>)}++ {branchMismatch && (+ <Modal+ opened+ padding="xl"+ title={t`This view is out of date`}+ withCloseButton={false}+ onClose={() => setBranchMismatch(null)}+ >+ <Text mt="md">{branchMismatch.message}</Text>+ <Group gap="sm" justify="end" mt="xl">+ <Button variant="subtle" onClick={() => setBranchMismatch(null)}>+ {t`Cancel`}+ </Button>+ <Button variant="filled" onClick={() => window.location.reload()}>+ {t`Refresh`}+ </Button>+ </Group>+ </Modal>+ )}</>);};diff --git a/enterprise/frontend/src/metabase-enterprise/remote_sync/components/GitSyncControls/GitSyncControls.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/remote_sync/components/GitSyncControls/GitSyncControls.unit.spec.tsxindex 4147c5aedfe..e032702a34c 100644--- a/enterprise/frontend/src/metabase-enterprise/remote_sync/components/GitSyncControls/GitSyncControls.unit.spec.tsx+++ b/enterprise/frontend/src/metabase-enterprise/remote_sync/components/GitSyncControls/GitSyncControls.unit.spec.tsx@@ -1,8 +1,17 @@import userEvent from "@testing-library/user-event";import fetchMock from "fetch-mock";+import { setupEnterprisePlugins } from "__support__/enterprise";import { setupRemoteSyncEndpoints } from "__support__/server-mocks";+import { mockSettings } from "__support__/settings";import { renderWithProviders, screen, waitFor, within } from "__support__/ui";+import { createMockState } from "metabase/redux/store/mocks";+import {+ createMockTokenFeatures,+ createMockUser,+} from "metabase-types/api/mocks";++import { taskUpdated } from "../../sync-task-slice";import { GitSyncControls } from "./GitSyncControls";import {@@ -181,6 +190,39 @@ describe("GitSyncControls", () => {expect(screen.getByRole("dialog")).toBeInTheDocument();});});++ it("shows a refresh modal when the branch changed in another session", async () => {+ setup({ dirty: [createMockDirtyEntity()] });++ // Another session switched the branch since this tab loaded; the preflight CAS guard rejects.+ fetchMock.removeRoute("remote-sync-export-preflight");+ fetchMock.get(+ "path:/api/ee/remote-sync/export-preflight",+ {+ status: 409,+ body: {+ message:+ "The sync branch changed to 'develop' in another session. Refresh and try again.",+ branch_mismatch: true,+ current_branch: "develop",+ },+ },+ { name: "remote-sync-export-preflight" },+ );++ await waitFor(() => {+ expect(getBranchButton(/main/)).toBeInTheDocument();+ });+ await userEvent.click(getBranchButton(/main/));+ await userEvent.click(await findOption(/Push changes/));++ expect(+ await screen.findByText(/changed .* in another session/i),+ ).toBeInTheDocument();+ expect(+ screen.getByRole("button", { name: /Refresh/ }),+ ).toBeInTheDocument();+ });});describe("pull option", () => {@@ -200,6 +242,85 @@ describe("GitSyncControls", () => {});});+ it("shows a refresh modal when a pull is rejected for a stale branch", async () => {+ setup();++ // Another session switched the branch; the import CAS guard rejects.+ fetchMock.removeRoute("remote-sync-import");+ fetchMock.post(+ "path:/api/ee/remote-sync/import",+ {+ status: 409,+ body: {+ message:+ "The sync branch changed to 'develop' in another session. Refresh and try again.",+ branch_mismatch: true,+ current_branch: "develop",+ },+ },+ { name: "remote-sync-import" },+ );++ await waitFor(() => {+ expect(getBranchButton(/main/)).toBeInTheDocument();+ });+ await userEvent.click(getBranchButton(/main/));+ // Wait until the dirty state has settled (push disabled, since nothing is dirty) so the pull takes+ // the non-dirty direct-import path deterministically.+ await waitFor(async () => {+ expect(await findOption(/Push changes/)).toHaveAttribute(+ "data-combobox-disabled",+ "true",+ );+ });+ await userEvent.click(await findOption(/Pull changes/));++ expect(+ await screen.findByText(/changed .* in another session/i),+ ).toBeInTheDocument();+ expect(+ screen.getByRole("button", { name: /Refresh/ }),+ ).toBeInTheDocument();+ });++ it("toasts when the mergeability check fails on a dirty pull", async () => {+ // The toaster (UndoListing) isn't mounted in this harness, so assert the dispatched toast via the+ // undo store rather than the DOM.+ const { store } = setup({+ dirty: [createMockDirtyEntity()],+ hasRemoteChanges: true,+ });++ // The preflight fails for a non-branch-mismatch reason.+ fetchMock.removeRoute("remote-sync-export-preflight");+ fetchMock.get(+ "path:/api/ee/remote-sync/export-preflight",+ { status: 500, body: { message: "boom" } },+ { name: "remote-sync-export-preflight" },+ );++ await waitFor(() => {+ expect(getBranchButton(/main/)).toBeInTheDocument();+ });+ await userEvent.click(getBranchButton(/main/));+ // Wait until the dirty state has settled (push enabled) so the pull takes the dirty/merge path.+ await waitFor(async () => {+ expect(await findOption(/Push changes/)).toBeEnabled();+ });+ await userEvent.click(await findOption(/Pull changes/));++ await waitFor(() => {+ const messages = store+ .getState()+ .undo.map((undo) => String(undo.message));+ expect(+ messages.some((m) =>+ /Couldn't check whether your changes can be merged/i.test(m),+ ),+ ).toBe(true);+ });+ });+it("is enabled when there are changes to pull", async () => {setup({ hasRemoteChanges: true });@@ -280,5 +401,96 @@ describe("GitSyncControls", () => {).toBeInTheDocument();});});++ it("shows a refresh modal when a clean branch switch is rejected for a stale branch", async () => {+ setup({ branches: ["main", "develop"] });++ // Another session already switched the branch; the import CAS guard rejects the switch.+ fetchMock.removeRoute("remote-sync-import");+ fetchMock.post(+ "path:/api/ee/remote-sync/import",+ {+ status: 409,+ body: {+ message:+ "The sync branch changed to 'develop' in another session. Refresh and try again.",+ branch_mismatch: true,+ current_branch: "develop",+ },+ },+ { name: "remote-sync-import" },+ );++ await waitFor(() => {+ expect(getBranchButton(/main/)).toBeInTheDocument();+ });+ await userEvent.click(getBranchButton(/main/));+ await userEvent.click(await findOption(/Switch branch/));+ await userEvent.click(await findOption(/develop/));++ expect(+ await screen.findByText(/changed .* in another session/i),+ ).toBeInTheDocument();+ expect(+ screen.getByRole("button", { name: /Refresh/ }),+ ).toBeInTheDocument();+ });+ });++ // Kept last: setupEnterprisePlugins() registers the remote-sync redux reducer globally, which the other+ // tests in this file deliberately don't need (they use local state). Doing it here keeps that contained.+ describe("export conflict toast", () => {+ it("toasts when an export task comes back in conflict", async () => {+ // The plugin reducer (state.plugins.remoteSyncPlugin, read by getCurrentTask) is only registered+ // when the remote_sync premium gate passes, so enable it and initialize the plugin.+ const settings = mockSettings({+ "token-features": createMockTokenFeatures({ remote_sync: true }),+ "remote-sync-enabled": true,+ "remote-sync-branch": "main",+ "remote-sync-type": "read-write",+ });+ setupEnterprisePlugins();+ setupRemoteSyncEndpoints({+ branches: ["main"],+ dirty: [],+ hasRemoteChanges: false,+ });+ setupCollectionEndpoints();+ setupSessionEndpoints({});++ const { store } = renderWithProviders(<GitSyncControls />, {+ storeInitialState: createMockState({+ currentUser: createMockUser({ is_superuser: true }),+ settings,+ }),+ });++ await waitFor(() => {+ expect(getBranchButton(/main/)).toBeInTheDocument();+ });++ // Simulate the polled export task coming back in conflict (what the middleware dispatches when a+ // push loses the preflight->execute race). GitSyncControls observes it and toasts via useToast.+ store.dispatch(+ taskUpdated({+ id: 77,+ sync_task_type: "export",+ status: "conflict",+ progress: 1,+ started_at: "2026-01-01T00:00:00Z",+ ended_at: "2026-01-01T00:00:01Z",+ last_progress_report_at: null,+ error_message: null,+ initiated_by: 0,+ }),+ );++ await waitFor(() => {+ const messages = store.getState().undo.map((u) => String(u.message));+ expect(messages.some((m) => /changed before your push/i.test(m))).toBe(+ true,+ );+ });+ });});});diff --git a/enterprise/frontend/src/metabase-enterprise/remote_sync/components/PushChangesModal/PushChangesModal.tsx b/enterprise/frontend/src/metabase-enterprise/remote_sync/components/PushChangesModal/PushChangesModal.tsxindex e36004a94a6..4822b816f24 100644--- a/enterprise/frontend/src/metabase-enterprise/remote_sync/components/PushChangesModal/PushChangesModal.tsx+++ b/enterprise/frontend/src/metabase-enterprise/remote_sync/components/PushChangesModal/PushChangesModal.tsx@@ -16,7 +16,6 @@ import { useExportChangesMutation } from "metabase-enterprise/api";import { trackPushChanges } from "../../analytics";import { type SyncError, parseSyncError } from "../../utils";import { ChangesLists } from "../ChangesLists";-import { SyncConflictModal } from "../SyncConflictModal";import { CommitMessageSection } from "./CommitMessageSection";@@ -25,6 +24,11 @@ interface PushChangesModalProps {onClose: () => void;}+/**+ * Plain push of local changes. Only shown when the remote has NOT advanced — the caller+ * (GitSyncControls) runs the export preflight first and, when the remote is ahead, opens the+ * SyncConflictModal (push variant) directly instead of this modal.+ */export const PushChangesModal = ({onClose,currentBranch,@@ -36,7 +40,7 @@ export const PushChangesModal = ({{ isLoading: isPushing, error: exportError, isSuccess },] = useExportChangesMutation();- const { errorMessage, hasConflict } = useMemo(+ const { errorMessage } = useMemo(() => parseSyncError(exportError as SyncError),[exportError],);@@ -63,16 +67,6 @@ export const PushChangesModal = ({});}, [commitMessage, exportChanges, currentBranch]);- if (hasConflict) {- return (- <SyncConflictModal- currentBranch={currentBranch}- onClose={onClose}- variant="push"- />- );- }-return (<Modalopeneddiff --git a/enterprise/frontend/src/metabase-enterprise/remote_sync/components/RemoteSyncAdminSettings/PullChangesButton.tsx b/enterprise/frontend/src/metabase-enterprise/remote_sync/components/RemoteSyncAdminSettings/PullChangesButton.tsxindex dccc79ee2e3..c11f0718363 100644--- a/enterprise/frontend/src/metabase-enterprise/remote_sync/components/RemoteSyncAdminSettings/PullChangesButton.tsx+++ b/enterprise/frontend/src/metabase-enterprise/remote_sync/components/RemoteSyncAdminSettings/PullChangesButton.tsx@@ -21,7 +21,12 @@ export const PullChangesButton = (props: PullChangesButtonProps) => {const handlePullChanges = useCallback(async () => {try {- await importChanges({ branch, force: forcePull }).unwrap();+ // In admin settings the configured branch is the operational target and the assertion.+ await importChanges({+ branch,+ force: forcePull,+ expected_branch: branch,+ }).unwrap();trackPullChanges({triggeredFrom: "admin-settings",diff --git a/enterprise/frontend/src/metabase-enterprise/remote_sync/components/SyncConflictModal/ConflictingChangesList.tsx b/enterprise/frontend/src/metabase-enterprise/remote_sync/components/SyncConflictModal/ConflictingChangesList.tsxnew file mode 100644index 00000000000..23378696c28--- /dev/null+++ b/enterprise/frontend/src/metabase-enterprise/remote_sync/components/SyncConflictModal/ConflictingChangesList.tsx@@ -0,0 +1,26 @@+import { t } from "ttag";++import { Box, List, Text } from "metabase/ui";++interface ConflictingChangesListProps {+ conflicts: string[];+}++/**+ * Lists the entities that were changed both locally and on the remote branch and therefore can't be+ * merged automatically. Shown in the push conflict modal when a 3-way merge isn't clean.+ */+export const ConflictingChangesList = ({+ conflicts,+}: ConflictingChangesListProps) => (+ <Box>+ <Text mb="sm">+ {t`These items were changed both here and on the remote branch, so they can't be merged automatically:`}+ </Text>+ <List spacing="xs" size="sm" withPadding>+ {conflicts.map((conflict) => (+ <List.Item key={conflict}>{conflict}</List.Item>+ ))}+ </List>+ </Box>+);diff --git a/enterprise/frontend/src/metabase-enterprise/remote_sync/components/SyncConflictModal/OutOfSyncOptions.tsx b/enterprise/frontend/src/metabase-enterprise/remote_sync/components/SyncConflictModal/OutOfSyncOptions.tsxindex 1aea37b9bbb..380bbef3e72 100644--- a/enterprise/frontend/src/metabase-enterprise/remote_sync/components/SyncConflictModal/OutOfSyncOptions.tsx+++ b/enterprise/frontend/src/metabase-enterprise/remote_sync/components/SyncConflictModal/OutOfSyncOptions.tsx@@ -12,6 +12,8 @@ interface BranchSwitchOptionsProps {isRemoteSyncReadOnly: boolean;optionValue?: OptionValue;variant: RemoteSyncConflictVariant;+ /** When true (push variant only), offer a "Merge changes" option. */+ canMerge?: boolean;}interface OutOfSyncOption {@@ -26,6 +28,7 @@ export const OutOfSyncOptions = (props: BranchSwitchOptionsProps) => {isRemoteSyncReadOnly,optionValue,variant,+ canMerge,} = props;const options = useMemo<OutOfSyncOption[]>(() => {@@ -43,6 +46,14 @@ export const OutOfSyncOptions = (props: BranchSwitchOptionsProps) => {label: c("{0} is the current GitHub branch name").t`Force push to ${currentBranch} (this will overwrite the remote branch)`,};+ // Push merges and pushes the result; pull merges into local only (the push happens later).+ const mergeOption: OutOfSyncOption = {+ value: "merge",+ label:+ variant === "pull"+ ? t`Merge the remote changes into your local content`+ : t`Merge the remote changes with yours and push`,+ };const discardOption: OutOfSyncOption = {value: "discard",label: t`Delete unsynced changes (can’t be undone)`,@@ -50,7 +61,9 @@ export const OutOfSyncOptions = (props: BranchSwitchOptionsProps) => {switch (variant) {case "push":- return [newBranchOption, forcePushOption];+ return canMerge+ ? [mergeOption, newBranchOption, forcePushOption]+ : [newBranchOption, forcePushOption];case "switch-branch":return [pushOption, newBranchOption, discardOption];case "setup":@@ -58,11 +71,14 @@ export const OutOfSyncOptions = (props: BranchSwitchOptionsProps) => {? [discardOption]: [newBranchOption, discardOption];default: // pull- return isRemoteSyncReadOnly- ? [discardOption]+ if (isRemoteSyncReadOnly) {+ return [discardOption];+ }+ return canMerge+ ? [mergeOption, forcePushOption, newBranchOption, discardOption]: [forcePushOption, newBranchOption, discardOption];}- }, [currentBranch, isRemoteSyncReadOnly, variant]);+ }, [currentBranch, isRemoteSyncReadOnly, variant, canMerge]);return (<Box mt="xl">diff --git a/enterprise/frontend/src/metabase-enterprise/remote_sync/components/SyncConflictModal/OutOfSyncOptions.unit.spec.tsx b/enterprise/frontend/src/metabase-enterprise/remote_sync/components/SyncConflictModal/OutOfSyncOptions.unit.spec.tsxindex f795b91c573..ff38694094d 100644--- a/enterprise/frontend/src/metabase-enterprise/remote_sync/components/SyncConflictModal/OutOfSyncOptions.unit.spec.tsx+++ b/enterprise/frontend/src/metabase-enterprise/remote_sync/components/SyncConflictModal/OutOfSyncOptions.unit.spec.tsx@@ -6,15 +6,21 @@ import { OutOfSyncOptions } from "./OutOfSyncOptions";type SetupOpts = {isRemoteSyncReadOnly?: boolean;variant: RemoteSyncConflictVariant;+ canMerge?: boolean;};-const setup = ({ variant, isRemoteSyncReadOnly = false }: SetupOpts) => {+const setup = ({+ variant,+ isRemoteSyncReadOnly = false,+ canMerge,+}: SetupOpts) => {render(<OutOfSyncOptionscurrentBranch="main"handleOptionChange={jest.fn}isRemoteSyncReadOnly={isRemoteSyncReadOnly}variant={variant}+ canMerge={canMerge}/>,);};@@ -33,6 +39,14 @@ describe("OutOfSyncOptions", () => {),).toBeInTheDocument();});++ it("offers a merge option when the merge is clean", () => {+ setup({ variant: "push", canMerge: true });+ expect(screen.getAllByRole("radio")).toHaveLength(3);+ expect(+ screen.getByLabelText(/Merge the remote changes with yours and push/),+ ).toBeInTheDocument();+ });});describe("switch-branch variant", () => {@@ -75,6 +89,24 @@ describe("OutOfSyncOptions", () => {screen.getByLabelText(/Delete unsynced changes/),).toBeInTheDocument();});++ it("offers a local merge option when the merge is clean", () => {+ setup({ variant: "pull", isRemoteSyncReadOnly: false, canMerge: true });+ expect(screen.getAllByRole("radio")).toHaveLength(4);+ expect(+ screen.getByLabelText(+ /Merge the remote changes into your local content/,+ ),+ ).toBeInTheDocument();+ });++ it("does not offer merge when read-only even if clean", () => {+ setup({ variant: "pull", isRemoteSyncReadOnly: true, canMerge: true });+ expect(screen.getAllByRole("radio")).toHaveLength(1);+ expect(+ screen.getByLabelText(/Delete unsynced changes/),+ ).toBeInTheDocument();+ });});describe("setup variant", () => {diff --git a/enterprise/frontend/src/metabase-enterprise/remote_sync/components/SyncConflictModal/SyncConflictModal.tsx b/enterprise/frontend/src/metabase-enterprise/remote_sync/components/SyncConflictModal/SyncConflictModal.tsxindex bc775dccdd0..6612db533c5 100644--- a/enterprise/frontend/src/metabase-enterprise/remote_sync/components/SyncConflictModal/SyncConflictModal.tsx+++ b/enterprise/frontend/src/metabase-enterprise/remote_sync/components/SyncConflictModal/SyncConflictModal.tsx@@ -23,15 +23,19 @@ import type {} from "metabase-types/api";import { ChangesLists } from "../ChangesLists";+import { CommitMessageSection } from "../PushChangesModal/CommitMessageSection";import { BranchNameInput } from "./BranchNameInput";+import { ConflictingChangesList } from "./ConflictingChangesList";import { OutOfSyncOptions } from "./OutOfSyncOptions";import { SetupConflictInfo } from "./SetupConflictInfo";import {useDiscardChangesAndImportAction,+ useMergeChangesAction,+ useMergeImportAction,usePushChangesAction,useStashToNewBranchAction,-} from "./mutation-wrappers";+} from "./hooks";import {type OptionValue,getContinueButtonText,@@ -43,12 +47,19 @@ interface UnsyncedWarningModalProps {nextBranch?: string | null;onClose: VoidFunction;variant: RemoteSyncConflictVariant;+ /** Push variant only: whether a 3-way merge would apply cleanly (offers the Merge option). */+ canMerge?: boolean;+ /** Push variant only: labels of entities that conflict (shown when the merge isn't clean). */+ conflicts?: string[];}export const SyncConflictModal = (props: UnsyncedWarningModalProps) => {- const { onClose, currentBranch, nextBranch, variant } = props;+ const { onClose, currentBranch, nextBranch, variant, canMerge, conflicts } =+ props;const [optionValue, setOptionValue] = useState<OptionValue>();const [newBranchName, setNewBranchName] = useState<string>("");+ // The push variant collects a commit message here, since merge/force/new-branch all push.+ const [commitMessage, setCommitMessage] = useState<string>("");const { sendErrorToast } = useMetadataToasts();const isRemoteSyncEnabled = !!useSetting(REMOTE_SYNC_KEY);const isRemoteSyncReadOnly = useSelector(getIsRemoteSyncReadOnly);@@ -64,6 +75,8 @@ export const SyncConflictModal = (props: UnsyncedWarningModalProps) => {const [updateRemoteSyncSettings, { isLoading: isUpdatingSettings }] =useUpdateRemoteSyncSettingsMutation();const { pushChanges, isPushingChanges } = usePushChangesAction();+ const { mergeChanges, isMerging } = useMergeChangesAction();+ const { mergeImport, isMergingImport } = useMergeImportAction();const { stashToNewBranch, isStashing } =useStashToNewBranchAction(existingBranches);const { discardChangesAndImport, isImporting } =@@ -97,13 +110,29 @@ export const SyncConflictModal = (props: UnsyncedWarningModalProps) => {updateRemoteSyncSettings,]);+ const message = commitMessage.trim() || undefined;+const handleContinueButtonClick = async () => {if (!optionValue) {return;}if (optionValue === "push" || optionValue === "force-push") {- await pushChanges(currentBranch, optionValue === "force-push", onClose);+ await pushChanges(+ currentBranch,+ optionValue === "force-push",+ onClose,+ message,+ );+ }++ if (optionValue === "merge") {+ // Pull merges into local only; push merges and pushes the result.+ if (variant === "pull") {+ await mergeImport(currentBranch, onClose);+ } else {+ await mergeChanges(currentBranch, onClose, message);+ }}if (optionValue === "new-branch") {@@ -111,16 +140,27 @@ export const SyncConflictModal = (props: UnsyncedWarningModalProps) => {await markLibraryAndTransformsAsSynced();}- await stashToNewBranch(newBranchName, onClose);+ await stashToNewBranch(newBranchName, onClose, message);}if (optionValue === "discard") {- await discardChangesAndImport(nextBranch || currentBranch, onClose);+ // nextBranch is set on a switch-branch discard; currentBranch is the branch we're on now and is+ // asserted against the setting to catch a stale tab.+ await discardChangesAndImport(+ nextBranch || currentBranch,+ currentBranch,+ onClose,+ );}};const isProcessing =- isImporting || isPushingChanges || isStashing || isUpdatingSettings;+ isImporting ||+ isPushingChanges ||+ isMerging ||+ isMergingImport ||+ isStashing ||+ isUpdatingSettings;const isButtonDisabled = useMemo(() => {let disabled = !optionValue || isProcessing;@@ -137,11 +177,17 @@ export const SyncConflictModal = (props: UnsyncedWarningModalProps) => {openedpadding="xl"styles={{ title: { lineHeight: "2rem" } }}- title={getModalTitle(variant)}+ title={getModalTitle(variant, canMerge)}withCloseButton={false}><Box pt="md">- {variant === "setup" ? <SetupConflictInfo /> : <ChangesLists />}+ {variant === "setup" ? (+ <SetupConflictInfo />+ ) : conflicts && conflicts.length > 0 ? (+ <ConflictingChangesList conflicts={conflicts} />+ ) : (+ <ChangesLists />+ )}<OutOfSyncOptionscurrentBranch={currentBranch}@@ -149,6 +195,7 @@ export const SyncConflictModal = (props: UnsyncedWarningModalProps) => {isRemoteSyncReadOnly={isRemoteSyncReadOnly}optionValue={optionValue}variant={variant}+ canMerge={canMerge}/>{optionValue === "new-branch" && (@@ -159,6 +206,16 @@ export const SyncConflictModal = (props: UnsyncedWarningModalProps) => {/>)}+ {/* Pushing (merge / force / new branch) needs a commit message; pull/switch/setup don't. */}+ {variant === "push" && optionValue && optionValue !== "discard" && (+ <Box mt="lg">+ <CommitMessageSection+ value={commitMessage}+ onChange={setCommitMessage}+ />+ </Box>+ )}+<Group gap="sm" justify="end" mt="lg"><Button onClick={onClose} variant="subtle">{t`Cancel`}diff --git a/enterprise/frontend/src/metabase-enterprise/remote_sync/components/SyncConflictModal/mutation-wrappers.ts b/enterprise/frontend/src/metabase-enterprise/remote_sync/components/SyncConflictModal/hooks.tssimilarity index 52%rename from enterprise/frontend/src/metabase-enterprise/remote_sync/components/SyncConflictModal/mutation-wrappers.tsrename to enterprise/frontend/src/metabase-enterprise/remote_sync/components/SyncConflictModal/hooks.tsindex e91405cbd06..6e27f8bdca4 100644--- a/enterprise/frontend/src/metabase-enterprise/remote_sync/components/SyncConflictModal/mutation-wrappers.ts+++ b/enterprise/frontend/src/metabase-enterprise/remote_sync/components/SyncConflictModal/hooks.ts@@ -21,11 +21,17 @@ export const usePushChangesAction = () => {return {pushChanges: useCallback(- async (branch: string, force: boolean, closeModal: VoidFunction) => {+ async (+ branch: string,+ force: boolean,+ closeModal: VoidFunction,+ message?: string,+ ) => {try {await exportChanges({branch,force,+ message,}).unwrap();trackPushChanges({@@ -53,6 +59,81 @@ export const usePushChangesAction = () => {};};+export const useMergeChangesAction = () => {+ const [exportChanges, { isLoading: isMerging }] = useExportChangesMutation();+ const [sendToast] = useToast();++ return {+ mergeChanges: useCallback(+ async (branch: string, closeModal: VoidFunction, message?: string) => {+ try {+ await exportChanges({+ branch,+ merge: true,+ message,+ }).unwrap();++ trackPushChanges({+ triggeredFrom: "conflict-modal",+ force: false,+ });++ sendToast({+ message: c("{0} is the GitHub branch name")+ .t`Merging remote changes and pushing to ${branch}...`,+ });+ closeModal();+ } catch (error) {+ const { errorMessage } = parseSyncError(error as SyncError);+ sendToast({+ message: errorMessage || t`Failed to merge changes`,+ icon: "warning",+ timeout: 8000,+ });+ }+ },+ [exportChanges, sendToast],+ ),+ isMerging,+ };+};++export const useMergeImportAction = () => {+ const [importChanges, { isLoading: isMergingImport }] =+ useImportChangesMutation();+ const [sendToast] = useToast();++ return {+ mergeImport: useCallback(+ async (branch: string, closeModal: VoidFunction) => {+ try {+ // Pull merge: the operational branch is the current branch, so it doubles as the+ // expected_branch assertion.+ await importChanges({+ branch,+ merge: true,+ expected_branch: branch,+ }).unwrap();++ sendToast({+ message: t`Merging the latest remote changes into your local content...`,+ });+ closeModal();+ } catch (error) {+ const { errorMessage } = parseSyncError(error as SyncError);+ sendToast({+ message: errorMessage || t`Failed to merge changes`,+ icon: "warning",+ timeout: 8000,+ });+ }+ },+ [importChanges, sendToast],+ ),+ isMergingImport,+ };+};+export const useStashToNewBranchAction = (existingBranches: string[]) => {const [exportChanges] = useExportChangesMutation();const [createBranch] = useCreateBranchMutation();@@ -61,7 +142,11 @@ export const useStashToNewBranchAction = (existingBranches: string[]) => {return {stashToNewBranch: useCallback(- async (newBranchName: string, closeModal: VoidFunction) => {+ async (+ newBranchName: string,+ closeModal: VoidFunction,+ message?: string,+ ) => {if (!newBranchName) {sendToast({message: t`Please enter a valid branch name`,@@ -88,6 +173,7 @@ export const useStashToNewBranchAction = (existingBranches: string[]) => {await exportChanges({branch: newBranchName,+ message,}).unwrap();sendToast({message: c("{0} is the GitHub branch name")@@ -117,14 +203,27 @@ export const useDiscardChangesAndImportAction = () => {return {discardChangesAndImport: useCallback(- async (targetBranch: string, closeModal: VoidFunction) => {+ async (+ targetBranch: string,+ expectedBranch: string,+ closeModal: VoidFunction,+ ) => {try {- await importChanges({ branch: targetBranch, force: true }).unwrap();+ // targetBranch is what we import (may be a switch target); expectedBranch is the branch+ // we believe is currently active, asserted against the setting to catch a stale tab.+ await importChanges({+ branch: targetBranch,+ force: true,+ expected_branch: expectedBranch,+ }).unwrap();closeModal();} catch (error) {+ const { errorMessage } = parseSyncError(error as SyncError);sendToast({- message: c("{0} is the GitHub branch name")- .t`Failed to import from branch ${targetBranch}`,+ message:+ errorMessage ||+ c("{0} is the GitHub branch name")+ .t`Failed to import from branch ${targetBranch}`,icon: "warning",});}diff --git a/enterprise/frontend/src/metabase-enterprise/remote_sync/components/SyncConflictModal/utils.tsx b/enterprise/frontend/src/metabase-enterprise/remote_sync/components/SyncConflictModal/utils.tsxindex 36f80be4f6f..ff39b104154 100644--- a/enterprise/frontend/src/metabase-enterprise/remote_sync/components/SyncConflictModal/utils.tsx+++ b/enterprise/frontend/src/metabase-enterprise/remote_sync/components/SyncConflictModal/utils.tsx@@ -3,7 +3,12 @@ import { t } from "ttag";import type { RemoteSyncConflictVariant } from "metabase-types/api";-export type OptionValue = "push" | "force-push" | "new-branch" | "discard";+export type OptionValue =+ | "push"+ | "force-push"+ | "new-branch"+ | "merge"+ | "discard";export const getContinueButtonText = (optionValue?: OptionValue) => {switch (optionValue) {@@ -11,6 +16,8 @@ export const getContinueButtonText = (optionValue?: OptionValue) => {case "force-push":case "new-branch":return t`Push changes`;+ case "merge":+ return t`Merge changes`;case "discard":return t`Delete unsynced changes`;default:@@ -20,12 +27,19 @@ export const getContinueButtonText = (optionValue?: OptionValue) => {export const getModalTitle = (variant: RemoteSyncConflictVariant,+ canMerge?: boolean,): ReactNode => {switch (variant) {case "push":- return (+ // The push variant is shown when the remote branch has advanced. When the changes can be merged+ // cleanly we frame it as new remote changes; otherwise we call out the conflict.+ return canMerge ? (+ <>+ {t`The remote branch has new changes.`} {t`What do you want to do?`}+ </>+ ) : (<>- {t`Your branch is behind the remote branch.`}{" "}+ {t`Some of your changes conflict with the remote branch.`}{" "}{t`What do you want to do?`}</>);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.tsindex 3ca46a6de0e..02611ab6214 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@@ -120,14 +120,18 @@ remoteSyncListenerMiddleware.startListening({if (task.status === "conflict") {dispatch(modalDismissed());- dispatch(syncConflictVariantUpdated("setup"));+ // The first-import / setup flow surfaces conflicts as a task status. Export conflicts are+ // surfaced as a toast by GitSyncControls (which observes the task), not here — middleware can't+ // use the useToast hook.+ if (task.sync_task_type !== "export") {+ dispatch(syncConflictVariantUpdated("setup"));+ }return;}const isTerminalState = terminalTaskStates.includes(task.status);if (isTerminalState && task.ended_at) {- const isImportTask = task.sync_task_type === "import";const isSuccessful = task.status === "successful";if (isSuccessful) {@@ -135,9 +139,9 @@ remoteSyncListenerMiddleware.startListening({dispatch(modalDismissed());}, 500);- if (isImportTask) {- dispatch(EnterpriseApi.util.invalidateTags(ALL_INVALIDATION_TAGS));- }+ // Both import and a merged export change local content, so refresh everything. A plain push+ // doesn't change local data, but the extra refetch is harmless and keeps this simple.+ dispatch(EnterpriseApi.util.invalidateTags(ALL_INVALIDATION_TAGS));}invalidateRemoteSyncTags(dispatch);diff --git a/enterprise/frontend/src/metabase-enterprise/remote_sync/middleware/remote-sync-listener-middleware.unit.spec.ts b/enterprise/frontend/src/metabase-enterprise/remote_sync/middleware/remote-sync-listener-middleware.unit.spec.tsindex c1b0efc1898..4d0f52a75ff 100644--- a/enterprise/frontend/src/metabase-enterprise/remote_sync/middleware/remote-sync-listener-middleware.unit.spec.ts+++ b/enterprise/frontend/src/metabase-enterprise/remote_sync/middleware/remote-sync-listener-middleware.unit.spec.ts@@ -204,6 +204,7 @@ describe("remote-sync-listener-middleware", () => {store.dispatch(remoteSyncApi.endpoints.importChanges.initiate({branch: "main",+ expected_branch: "main",}),);@@ -232,6 +233,7 @@ describe("remote-sync-listener-middleware", () => {store.dispatch(remoteSyncApi.endpoints.importChanges.initiate({branch: "main",+ expected_branch: "main",}),);@@ -276,6 +278,32 @@ describe("remote-sync-listener-middleware", () => {);});});++ it("does NOT open the setup modal when an export task ends in conflict", async () => {+ // Export conflicts are surfaced as a toast by GitSyncControls (which can use the useToast hook),+ // not by the middleware — so the middleware must not route them to the setup-conflict modal.+ fetchMock.get("path:/api/ee/remote-sync/current-task", {+ status: 200,+ body: { status: "conflict", sync_task_type: "export" },+ });++ const store = createTestStore();++ store.dispatch(+ remoteSyncApi.endpoints.getRemoteSyncCurrentTask.initiate(),+ );++ await waitForCondition(() =>+ fetchMock.callHistory.done("path:/api/ee/remote-sync/current-task"),+ );++ await waitFor(() => {+ expect(store.getState().remoteSyncPlugin?.showModal).toBe(false);+ });+ expect(store.getState().remoteSyncPlugin?.syncConflictVariant).not.toBe(+ "setup",+ );+ });});describe("collection listeners for transforms namespace", () => {diff --git a/enterprise/frontend/src/metabase-enterprise/remote_sync/utils.ts b/enterprise/frontend/src/metabase-enterprise/remote_sync/utils.tsindex e23d3b07c77..fbf0db98ff1 100644--- a/enterprise/frontend/src/metabase-enterprise/remote_sync/utils.ts+++ b/enterprise/frontend/src/metabase-enterprise/remote_sync/utils.ts@@ -19,6 +19,10 @@ export {type ErrorData = {message?: string;conflicts?: boolean;+ /** Set by the backend CAS guard when the requested branch != the configured remote-sync-branch. */+ branch_mismatch?: boolean;+ /** The authoritative branch the instance is actually on, when branch_mismatch is set. */+ current_branch?: string;};export type SyncError = {@@ -29,6 +33,10 @@ export type SyncError = {type ParsedError = {errorMessage: string | null;hasConflict: boolean;+ /** True when the request was rejected because the branch changed in another session. */+ hasBranchMismatch: boolean;+ /** The branch the instance is actually on, when hasBranchMismatch is true. */+ currentBranch: string | null;};export const getSyncStatusIcon = (status: RemoteSyncEntityStatus): IconName => {@@ -75,7 +83,12 @@ const getErrorMessage = (data: ErrorData): string | undefined =>export const parseSyncError = (exportError: SyncError | null): ParsedError => {if (!exportError) {- return { errorMessage: null, hasConflict: false };+ return {+ errorMessage: null,+ hasConflict: false,+ hasBranchMismatch: false,+ currentBranch: null,+ };}if (@@ -87,12 +100,26 @@ export const parseSyncError = (exportError: SyncError | null): ParsedError => {const messageFromData = getErrorMessage(errorData);const hasConflict = hasConflictProperty(errorData);+ if (errorData.branch_mismatch) {+ const currentBranch = errorData.current_branch ?? null;+ return {+ errorMessage:+ messageFromData ||+ t`The sync branch changed in another session. Refresh the page and try again.`,+ hasConflict: false,+ hasBranchMismatch: true,+ currentBranch,+ };+ }+if (hasConflict) {return {errorMessage:messageFromData ||t`Your changes conflict with the remote repository. You can force push to override them.`,hasConflict: true,+ hasBranchMismatch: false,+ currentBranch: null,};}@@ -100,6 +127,8 @@ export const parseSyncError = (exportError: SyncError | null): ParsedError => {errorMessage:messageFromData || t`Something went wrong. Please try again.`,hasConflict: false,+ hasBranchMismatch: false,+ currentBranch: null,};}@@ -108,12 +137,16 @@ export const parseSyncError = (exportError: SyncError | null): ParsedError => {errorMessage:exportError.message || t`Something went wrong. Please try again.`,hasConflict: false,+ hasBranchMismatch: false,+ currentBranch: null,};}return {errorMessage: t`Something went wrong. Please try again.`,hasConflict: false,+ hasBranchMismatch: false,+ currentBranch: null,};};diff --git a/frontend/src/metabase-types/api/remote-sync.ts b/frontend/src/metabase-types/api/remote-sync.tsindex 5a8ec563cfb..2d7b6aec203 100644--- a/frontend/src/metabase-types/api/remote-sync.ts+++ b/frontend/src/metabase-types/api/remote-sync.ts@@ -57,6 +57,8 @@ export type ExportChangesRequest = {message?: string;branch?: string;force?: boolean;+ /** Perform a 3-way merge when the remote branch has advanced (instead of refusing). */+ merge?: boolean;};export type ExportChangesResponse = {@@ -64,9 +66,37 @@ export type ExportChangesResponse = {task_id?: number;};+/** Counts of remote changes a merge would fold into local content. */+export type RemoteSyncMergeSummary = {+ added: number;+ updated: number;+ removed: number;+};++/** Dry-run preview of what pushing the current state would do, given the live remote branch. */+export type ExportPreflightResponse = {+ /** Whether the remote branch has advanced beyond the last synced version. */+ has_changes: boolean;+ /** Whether a 3-way merge would apply with no conflicts. */+ clean: boolean;+ /** Human-readable labels of the entities that conflict (empty when clean). */+ conflicts: string[];+ summary: RemoteSyncMergeSummary;+ /** "history-rewritten" when the remote was force-pushed/rebased so no merge base exists. */+ reason: string | null;+};+export type ImportFromBranchRequest = {branch: string;force?: boolean;+ /** Perform a local-only 3-way merge, keeping un-pushed local changes instead of overwriting them. */+ merge?: boolean;+ /**+ * The branch the client believes is currently active. Rejected (409) if it disagrees with the+ * configured remote-sync-branch — i.e. another session switched branches. Differs from `branch`+ * on a branch switch, where `branch` is the target and this is the branch being switched away from.+ */+ expected_branch: string;};export type ImportFromBranchResponse = {diff --git a/frontend/test/__support__/server-mocks/remote-sync.ts b/frontend/test/__support__/server-mocks/remote-sync.tsindex aa64da3fb81..19f658c0d37 100644--- a/frontend/test/__support__/server-mocks/remote-sync.ts+++ b/frontend/test/__support__/server-mocks/remote-sync.ts@@ -96,6 +96,46 @@ export const setupRemoteSyncImportEndpoint = ({);};+/**+ * Setup the remote-sync export POST endpoint+ */+export const setupRemoteSyncExportEndpoint = ({+ task_id = 789,+}: { task_id?: number } = {}) => {+ fetchMock.removeRoute("remote-sync-export");+ fetchMock.post(+ "path:/api/ee/remote-sync/export",+ { message: "Export task started", task_id },+ { name: "remote-sync-export" },+ );+};++export interface RemoteSyncExportPreflightResponse {+ has_changes: boolean;+ clean: boolean;+ conflicts: string[];+ summary: { added: number; updated: number; removed: number };+ reason: string | null;+}++/**+ * Setup the remote-sync export-preflight endpoint (drives the push decision)+ */+export const setupRemoteSyncExportPreflightEndpoint = ({+ has_changes = false,+ clean = true,+ conflicts = [],+ summary = { added: 0, updated: 0, removed: 0 },+ reason = null,+}: Partial<RemoteSyncExportPreflightResponse> = {}) => {+ fetchMock.removeRoute("remote-sync-export-preflight");+ fetchMock.get(+ "path:/api/ee/remote-sync/export-preflight",+ { has_changes, clean, conflicts, summary, reason },+ { name: "remote-sync-export-preflight" },+ );+};+/*** Setup the remote-sync cancel task endpoint*/@@ -153,6 +193,7 @@ export const setupRemoteSyncEndpoints = ({hasRemoteChanges = false,hasRemoteChangesDelay = 0,hasRemoteChangesError = false,+ exportPreflight,settingsResponse = { success: true },testConnectionError,}: {@@ -162,6 +203,7 @@ export const setupRemoteSyncEndpoints = ({hasRemoteChanges?: boolean;hasRemoteChangesDelay?: number;hasRemoteChangesError?: boolean;+ exportPreflight?: Partial<RemoteSyncExportPreflightResponse>;settingsResponse?: Partial<RemoteSyncSettingsResponse> & {error?: { status: number; message: string };};@@ -171,6 +213,8 @@ export const setupRemoteSyncEndpoints = ({setupRemoteSyncDirtyEndpoint({ dirty, changedCollections });setupRemoteSyncCurrentTaskEndpoint("idle");setupRemoteSyncImportEndpoint();+ setupRemoteSyncExportEndpoint();+ setupRemoteSyncExportPreflightEndpoint(exportPreflight);setupRemoteSyncSettingsEndpoint(settingsResponse);setupRemoteSyncTestConnectionEndpoint({ error: testConnectionError });fetchMock.post("path:/api/ee/remote-sync/create-branch", {});