Skip to content

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

GHY-4016: Replace Field YAMLs with FieldUserSettings in git sync (PR … (#76843)

dpsutton648273795773
diff --git a/.clj-kondo/config/modules/config.edn b/.clj-kondo/config/modules/config.edn
index 19d4a54f271..0957c494973 100644
--- a/.clj-kondo/config/modules/config.edn
+++ b/.clj-kondo/config/modules/config.edn
@@ -2716,6 +2716,7 @@
warehouses}
:model-exports #{:model/Dimension
:model/Field
+ :model/FieldUserSettings
:model/FieldValues
:model/Table}
:model-imports #{:model/Card
@@ -3454,6 +3455,7 @@
:model/Dashboard
:model/Document
:model/Field
+ :model/FieldUserSettings
:model/Measure
:model/NativeQuerySnippet
:model/PythonLibrary
diff --git a/enterprise/backend/src/metabase_enterprise/remote_sync/events.clj b/enterprise/backend/src/metabase_enterprise/remote_sync/events.clj
index 1f5511b7f07..770b79e7c7d 100644
--- a/enterprise/backend/src/metabase_enterprise/remote_sync/events.clj
+++ b/enterprise/backend/src/metabase_enterprise/remote_sync/events.clj
@@ -217,7 +217,7 @@
;;; --------------------------------- Spec-based Event Registration (Non-Collection) -----------------------------------
-(doseq [[_model-key model-spec] (dissoc spec/remote-sync-specs :model/Collection)]
+(doseq [[_model-key model-spec] (dissoc spec/remote-sync-specs :model/Collection :model/Field)]
(register-events-for-spec! model-spec))
;;; ----------------------------------------- Collection Event Handler -------------------------------------------------
@@ -272,3 +272,28 @@
(do
(log/infof "Collection %s no longer needs syncing, marking as removed" (:id object))
(create-or-update-remote-sync-object-entry! "Collection" (:id object) "removed" hydrate-collection-details)))))
+
+;;; ----------------------------------------- FieldUserSettings Tracking -----------------------------------------------
+;; When a field is updated in a published table, also track any FieldUserSettings row for that field.
+;; FieldUserSettings has no separate event; it piggybacks on :event/field-update.
+
+(def ^:private field-spec (get spec/remote-sync-specs :model/Field))
+
+(derive :event/field-update ::field-update-event)
+(derive ::field-update-event :metabase/event)
+
+(methodical/defmethod events/publish-event! ::field-update-event
+ [_topic {:keys [object]}]
+ (let [field-id (:id object)
+ eligible? (spec/check-eligibility field-spec object)]
+ (cond
+ (and eligible? (t2/exists? :model/FieldUserSettings :field_id field-id))
+ (create-or-update-remote-sync-object-entry!
+ "FieldUserSettings" field-id "update"
+ (fn [id] (spec/hydrate-model-details field-spec id)))
+
+ (and (not eligible?)
+ (t2/exists? :model/RemoteSyncObject :model_type "FieldUserSettings" :model_id field-id))
+ (create-or-update-remote-sync-object-entry!
+ "FieldUserSettings" field-id "removed"
+ (fn [id] (spec/hydrate-model-details field-spec id))))))
diff --git a/enterprise/backend/src/metabase_enterprise/remote_sync/impl.clj b/enterprise/backend/src/metabase_enterprise/remote_sync/impl.clj
index 42b05e54a47..eb82e819d0d 100644
--- a/enterprise/backend/src/metabase_enterprise/remote_sync/impl.clj
+++ b/enterprise/backend/src/metabase_enterprise/remote_sync/impl.clj
@@ -845,11 +845,12 @@
Return:
- [[row entity]] (if no entity, then omit)"
[{:keys [model_type rows]}]
- (let [id->row (u/index-by :model_id rows)
- opts {:where [:in :id (mapv :model_id rows)] :skip-archived true}]
+ (let [pk-col (spec/pk-col model_type)
+ id->row (u/index-by :model_id rows)
+ opts {:where [:in pk-col (mapv :model_id rows)] :skip-archived true}]
;; extract-one must run inside the extract-query reduction, while its ResultSet is open
(into [] (keep (fn [instance]
- (when-let [row (id->row (:id instance))]
+ (when-let [row (id->row (get instance pk-col))]
[row (serdes/extract-one model_type opts instance)])))
(serdes/extract-query model_type opts))))
diff --git a/enterprise/backend/src/metabase_enterprise/remote_sync/spec.clj b/enterprise/backend/src/metabase_enterprise/remote_sync/spec.clj
index 6f04c7eb82f..cdf1d57f04f 100644
--- a/enterprise/backend/src/metabase_enterprise/remote_sync/spec.clj
+++ b/enterprise/backend/src/metabase_enterprise/remote_sync/spec.clj
@@ -1216,22 +1216,32 @@
(defmethod query-export-roots :default [_] nil)
+(def ^:private git-sync-extract-opts
+ "Serdes extraction opts for git sync. :user-edits-only restricts field-level export to
+ FieldUserSettings (user-curated metadata only), omitting the full Field YAML for every column."
+ {:include-field-values false
+ :include-database-secrets false
+ :continue-on-error false
+ :skip-archived true
+ :user-edits-only true})
+
(defn exportable-entities
"What a full export would serialize: a map of {model-name [id ...]} — the export roots plus their transitive
`serdes/descendants`/`required` closure — or `{}` when there is no remote-syncable content."
[]
- (let [opts {:include-field-values false
- :include-database-secrets false
- :continue-on-error false
- :skip-archived true}
- root-targets (into []
+ (let [root-targets (into []
(mapcat query-export-roots)
(vals (enabled-specs)))
targets (-> #{}
- (into (keys (u/traverse root-targets #(serdes/descendants (first %) (second %) opts))))
+ (into (keys (u/traverse root-targets #(serdes/descendants (first %) (second %) git-sync-extract-opts))))
(into (keys (u/traverse root-targets #(serdes/required (first %) (second %))))))]
(u/group-by first second targets)))
+(defn pk-col
+ "Returns the PK column keyword for `model`. FieldUserSettings uses :field_id; all others use :id."
+ [model]
+ (if (= model "FieldUserSettings") :field_id :id))
+
(defn extract-entities-for-export
"Extracts all entities for remote-sync export based on enabled specs.
@@ -1243,7 +1253,7 @@
3. Are in one of the provided collections (or descendants)"
[]
(eduction (map (fn [[model ids]]
- (serdes/extract-all model {:where [:in :id ids]
+ (serdes/extract-all model {:where [:in (pk-col model) ids]
:skip-archived true})))
cat
(exportable-entities)))
@@ -1255,7 +1265,7 @@
[rows]
(let [by-model (u/group-by :model_type :model_id conj #{} rows)]
(eduction (map (fn [[model ids]]
- (serdes/extract-all model {:where [:in :id ids]
+ (serdes/extract-all model {:where [:in (pk-col model) ids]
:skip-archived true})))
cat
by-model)))
diff --git a/enterprise/backend/src/metabase_enterprise/serialization/v2/extract.clj b/enterprise/backend/src/metabase_enterprise/serialization/v2/extract.clj
index b33d51f78d2..c64db7cc1a0 100644
--- a/enterprise/backend/src/metabase_enterprise/serialization/v2/extract.clj
+++ b/enterprise/backend/src/metabase_enterprise/serialization/v2/extract.clj
@@ -159,9 +159,11 @@
;; so cards that reference them can still be exported and imported correctly
(and analytics-cards (contains? by-model "Card"))
(update "Card" (fn [ids] (vec (remove analytics-cards ids)))))
+ ;; FieldUserSettings has a non-standard PK (field_id, not id) — use the right column.
+ pk-col (fn [model] (if (= model "FieldUserSettings") :field_id :id))
extract-by-ids (fn [[model ids]]
(serdes/extract-all model (merge opts {:collection-set coll-set
- :where [:in :id ids]})))
+ :where [:in (pk-col model) ids]})))
extract-all (fn [model]
(serdes/extract-all model (assoc opts :collection-set coll-set)))]
(eduction cat
diff --git a/enterprise/backend/src/metabase_enterprise/serialization/v2/models.clj b/enterprise/backend/src/metabase_enterprise/serialization/v2/models.clj
index 7dfe45dbdc4..dc8f69a68ba 100644
--- a/enterprise/backend/src/metabase_enterprise/serialization/v2/models.clj
+++ b/enterprise/backend/src/metabase_enterprise/serialization/v2/models.clj
@@ -13,9 +13,11 @@
(def data-model-in-collection
"Data model types that can be found in collections (via published tables).
- These are extracted by ID when discovered via descendants, even if no-data-model is set."
+ These are extracted by ID when discovered via descendants, even if no-data-model is set.
+ Includes both Field (full serdes) and FieldUserSettings (user-edits-only / git sync)."
["Table"
"Field"
+ "FieldUserSettings"
"Segment"])
(def content
diff --git a/enterprise/backend/test/metabase_enterprise/remote_sync/content_hash_test.clj b/enterprise/backend/test/metabase_enterprise/remote_sync/content_hash_test.clj
index 09c23cea9ed..74cc21f5d77 100644
--- a/enterprise/backend/test/metabase_enterprise/remote_sync/content_hash_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/remote_sync/content_hash_test.clj
@@ -210,12 +210,13 @@
:model/NativeQuerySnippet snip {:name "Snip" :content "SELECT 1" :collection_id (:id snips)}
:model/Table table {:name "T" :schema "PUBLIC" :db_id (:id db) :is_published true :collection_id (:id data)}
:model/Field field {:name "F" :table_id (:id table) :base_type :type/Integer}
+ :model/FieldUserSettings _ {:field_id (:id field) :description "curated"}
:model/Measure measure {:name "M" :table_id (:id table)}]
(mt/with-model-cleanup [:model/RemoteSyncTask]
(let [task-id (t2/insert-returning-pk! :model/RemoteSyncTask {:sync_task_type "export" :initiated_by (mt/user->id :rasta)})
rows (mapv (fn [[mt id]] {:model_type mt :model_id id})
[["Card" (:id card)] ["Collection" (:id rs)] ["NativeQuerySnippet" (:id snip)]
- ["Field" (:id field)] ["Measure" (:id measure)]])]
+ ["FieldUserSettings" (:id field)] ["Measure" (:id measure)]])]
(t2/delete! :model/RemoteSyncObject)
(doseq [row rows]
(t2/insert! :model/RemoteSyncObject (merge row {:model_name "x" :status "synced"
diff --git a/enterprise/backend/test/metabase_enterprise/remote_sync/events_test.clj b/enterprise/backend/test/metabase_enterprise/remote_sync/events_test.clj
index 8b3fb93d230..af6d91c3b5c 100644
--- a/enterprise/backend/test/metabase_enterprise/remote_sync/events_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/remote_sync/events_test.clj
@@ -1115,28 +1115,13 @@
;;; Field Event Tests
-(deftest field-update-event-creates-entry-test
- (testing "field-update event creates remote sync object entry for field in published table in remote-synced collection"
- (mt/with-temp [:model/Collection remote-sync-collection {:is_remote_synced true :name "Remote-Sync"}
- :model/Table table {:name "Test Table"
- :is_published true
- :collection_id (:id remote-sync-collection)}
- :model/Field field {:name "test_field"
- :table_id (:id table)
- :base_type :type/Text}]
- (t2/delete! :model/RemoteSyncObject)
- (events/publish-event! :event/field-update
- {:object field :user-id (mt/user->id :rasta)})
- (let [entries (t2/select :model/RemoteSyncObject)]
- (is (= 1 (count entries)))
- (is (=? {:model_type "Field"
- :model_id (:id field)
- :status "update"}
- (first entries)))))))
+;; Field RSO tracking has been replaced by FieldUserSettings RSO tracking.
+;; field-update events now track FieldUserSettings (user-curated metadata) rather than raw Field rows.
+;; See the FieldUserSettings RSO Tracking section below for the current behavior tests.
(deftest field-update-event-no-entry-for-unpublished-table-test
- (testing "field-update event doesn't create entry for unpublished table"
- (mt/with-temp [:model/Collection remote-sync-collection {:is_remote_synced true :name "Remote-Sync"}
+ (testing "field-update event creates no RSOs for an unpublished table"
+ (mt/with-temp [:model/Collection remote-sync-collection {:is_remote_synced true :name "Remote-Sync" :type "library-data"}
:model/Table table {:name "Test Table"
:is_published false
:collection_id (:id remote-sync-collection)}
@@ -1146,12 +1131,11 @@
(t2/delete! :model/RemoteSyncObject)
(events/publish-event! :event/field-update
{:object field :user-id (mt/user->id :rasta)})
- (let [entries (t2/select :model/RemoteSyncObject)]
- (is (= 0 (count entries)))))))
+ (is (empty? (t2/select :model/RemoteSyncObject))))))
(deftest field-update-event-no-entry-for-normal-collection-test
- (testing "field-update event doesn't create entry for table in non-remote-synced collection"
- (mt/with-temp [:model/Collection normal-collection {:name "Normal"}
+ (testing "field-update event creates no RSOs for a table in a non-remote-synced collection"
+ (mt/with-temp [:model/Collection normal-collection {:name "Normal" :type "library-data"}
:model/Table table {:name "Test Table"
:is_published true
:collection_id (:id normal-collection)}
@@ -1161,41 +1145,75 @@
(t2/delete! :model/RemoteSyncObject)
(events/publish-event! :event/field-update
{:object field :user-id (mt/user->id :rasta)})
+ (is (empty? (t2/select :model/RemoteSyncObject))))))
+
+;;; ----------------------------------------- FieldUserSettings RSO Tracking ------------------------------------------
+;; In git-sync mode, field updates should track FieldUserSettings RSOs only — not Field RSOs.
+;; Field YAMLs are replaced by FieldUserSettings YAMLs (user-curated metadata only).
+
+(deftest field-update-creates-only-fus-rso-when-fus-exists-test
+ (testing "field-update on an eligible field with a FUS row creates ONLY a FieldUserSettings RSO — no Field RSO"
+ (mt/with-temp [:model/Collection coll {:is_remote_synced true :name "Remote-Sync" :type "library-data"}
+ :model/Table table {:name "T" :is_published true :collection_id (:id coll)}
+ :model/Field field {:name "f" :table_id (:id table) :base_type :type/Text}]
+ (t2/insert! :model/FieldUserSettings {:field_id (:id field) :description "curated"})
+ (t2/delete! :model/RemoteSyncObject)
+ (events/publish-event! :event/field-update {:object field :user-id (mt/user->id :rasta)})
(let [entries (t2/select :model/RemoteSyncObject)]
- (is (= 0 (count entries)))))))
+ (is (= 1 (count entries)) "exactly one RSO — no Field RSO alongside the FUS RSO")
+ (is (=? {:model_type "FieldUserSettings"
+ :model_id (:id field)
+ :status "update"
+ :model_table_id (:id table)}
+ (first entries)))))))
-(deftest existing-field-table-unpublished-marks-as-removed-test
- (testing "existing field is marked as removed when its table is no longer in sync scope"
- (mt/with-temp [:model/Collection remote-sync-collection {:is_remote_synced true :name "Remote-Sync"}
- :model/Collection normal-collection {:name "Normal"}
- :model/Table table {:name "Test Table"
- :is_published true
- :collection_id (:id remote-sync-collection)}
- :model/Field field {:name "test_field"
- :table_id (:id table)
- :base_type :type/Text}]
+(deftest field-update-no-rso-when-no-fus-row-test
+ (testing "field-update on an eligible field with NO FUS row creates no RSOs at all"
+ (mt/with-temp [:model/Collection coll {:is_remote_synced true :name "Remote-Sync" :type "library-data"}
+ :model/Table table {:name "T" :is_published true :collection_id (:id coll)}
+ :model/Field field {:name "f" :table_id (:id table) :base_type :type/Text}]
(t2/delete! :model/RemoteSyncObject)
- ;; First create a synced entry
- (t2/insert! :model/RemoteSyncObject {:model_type "Field"
- :model_id (:id field)
- :model_name "test_field"
- :model_table_id (:id table)
- :model_table_name "Test Table"
- :status "synced"
+ (events/publish-event! :event/field-update {:object field :user-id (mt/user->id :rasta)})
+ (is (empty? (t2/select :model/RemoteSyncObject))
+ "no RSOs created — field has no user-curated metadata to track"))))
+
+(deftest field-update-no-rso-for-unpublished-table-test
+ (testing "field-update creates no RSOs when the table is not published"
+ (mt/with-temp [:model/Collection coll {:is_remote_synced true :name "Remote-Sync" :type "library-data"}
+ :model/Table table {:name "T" :is_published false :collection_id (:id coll)}
+ :model/Field field {:name "f" :table_id (:id table) :base_type :type/Text}]
+ (t2/insert! :model/FieldUserSettings {:field_id (:id field) :description "curated"})
+ (t2/delete! :model/RemoteSyncObject)
+ (events/publish-event! :event/field-update {:object field :user-id (mt/user->id :rasta)})
+ (is (empty? (t2/select :model/RemoteSyncObject))))))
+
+(deftest field-update-no-rso-for-non-synced-collection-test
+ (testing "field-update creates no RSOs when the collection is not remote-synced"
+ (mt/with-temp [:model/Collection coll {:name "Normal" :type "library-data"}
+ :model/Table table {:name "T" :is_published true :collection_id (:id coll)}
+ :model/Field field {:name "f" :table_id (:id table) :base_type :type/Text}]
+ (t2/insert! :model/FieldUserSettings {:field_id (:id field) :description "curated"})
+ (t2/delete! :model/RemoteSyncObject)
+ (events/publish-event! :event/field-update {:object field :user-id (mt/user->id :rasta)})
+ (is (empty? (t2/select :model/RemoteSyncObject))))))
+
+(deftest field-update-fus-rso-marks-removed-when-table-leaves-sync-scope-test
+ (testing "existing FieldUserSettings RSO is marked removed when table leaves sync scope — no Field RSO created"
+ (mt/with-temp [:model/Collection synced {:is_remote_synced true :name "Synced" :type "library-data"}
+ :model/Collection normal {:name "Normal" :type "library-data"}
+ :model/Table table {:name "T" :is_published true :collection_id (:id synced)}
+ :model/Field field {:name "f" :table_id (:id table) :base_type :type/Text}]
+ (t2/insert! :model/FieldUserSettings {:field_id (:id field) :description "curated"})
+ (t2/delete! :model/RemoteSyncObject)
+ (t2/insert! :model/RemoteSyncObject {:model_type "FieldUserSettings"
+ :model_id (:id field)
+ :model_name "f"
+ :model_table_id (:id table)
+ :status "synced"
:status_changed_at (t/offset-date-time)})
- (let [initial-entry (t2/select-one :model/RemoteSyncObject :model_type "Field" :model_id (:id field))]
- (is (= "synced" (:status initial-entry)))
- ;; Move table to normal collection
- (t2/update! :model/Table (:id table) {:collection_id (:id normal-collection)})
- ;; Now trigger field update event - field's table is no longer in sync scope
- (events/publish-event! :event/field-update
- {:object field :user-id (mt/user->id :rasta)})
- (let [update-entry (t2/select-one :model/RemoteSyncObject :model_type "Field" :model_id (:id field))]
- (is (= "removed" (:status update-entry))))))))
-
-(deftest ^:parallel field-event-derivation-test
- (testing "field events properly derive from :metabase/event"
- (is (isa? ::remote-sync.events/field-change-event :metabase/event))
- (is (isa? :event/field-create ::remote-sync.events/field-change-event))
- (is (isa? :event/field-update ::remote-sync.events/field-change-event))
- (is (isa? :event/field-delete ::remote-sync.events/field-change-event))))
+ (t2/update! :model/Table (:id table) {:collection_id (:id normal)})
+ (events/publish-event! :event/field-update {:object field :user-id (mt/user->id :rasta)})
+ (let [entries (t2/select :model/RemoteSyncObject)]
+ (is (= 1 (count entries)) "still only one RSO — no Field RSO added")
+ (is (= "removed"
+ (:status (t2/select-one :model/RemoteSyncObject :model_type "FieldUserSettings" :model_id (:id field)))))))))
diff --git a/enterprise/backend/test/metabase_enterprise/serialization/v2/extract_test.clj b/enterprise/backend/test/metabase_enterprise/serialization/v2/extract_test.clj
index 2680fad3cb0..794dd9e58c9 100644
--- a/enterprise/backend/test/metabase_enterprise/serialization/v2/extract_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/serialization/v2/extract_test.clj
@@ -1132,6 +1132,69 @@
(t2/count :model/FieldUserSettings)
(count (filter #{"FieldUserSettings"} models)))))))))
+(deftest table-descendants-user-edits-only-test
+ (mt/with-empty-h2-app-db!
+ (ts/with-temp-dpc [:model/Database {db-id :id} {:name "DB"}
+ :model/Table {table-id :id} {:name "T" :db_id db-id}
+ :model/Field {f1-id :id} {:name "F1" :table_id table-id}
+ :model/Field {f2-id :id} {:name "F2" :table_id table-id}
+ :model/Field {f3-id :id} {:name "F3" :table_id table-id}]
+ (testing "without user-edits-only: all fields returned as Field descendants"
+ (let [desc (serdes/descendants "Table" table-id {})]
+ (is (= #{["Field" f1-id] ["Field" f2-id] ["Field" f3-id]}
+ (set (keys desc))))))
+ (testing "with user-edits-only and no FieldUserSettings rows: no field descendants"
+ (let [desc (serdes/descendants "Table" table-id {:user-edits-only true})]
+ (is (empty? (filter (fn [[model _]] (#{"Field" "FieldUserSettings"} model)) (keys desc))))))
+ (testing "with user-edits-only and one FieldUserSettings row: only that field appears as FieldUserSettings"
+ (t2/insert! :model/FieldUserSettings {:field_id f2-id :description "edited"})
+ (let [desc (serdes/descendants "Table" table-id {:user-edits-only true})]
+ (is (= #{["FieldUserSettings" f2-id]}
+ (set (filter (fn [[model _]] (#{"Field" "FieldUserSettings"} model)) (keys desc))))))
+ (t2/delete! :model/FieldUserSettings :field_id f2-id))
+ (testing "with user-edits-only and all fields edited: all appear as FieldUserSettings, not Field"
+ (t2/insert! :model/FieldUserSettings {:field_id f1-id})
+ (t2/insert! :model/FieldUserSettings {:field_id f2-id})
+ (t2/insert! :model/FieldUserSettings {:field_id f3-id})
+ (let [desc (serdes/descendants "Table" table-id {:user-edits-only true})]
+ (is (= #{["FieldUserSettings" f1-id] ["FieldUserSettings" f2-id] ["FieldUserSettings" f3-id]}
+ (set (filter (fn [[model _]] (#{"Field" "FieldUserSettings"} model)) (keys desc)))))))
+ (testing "Field and FieldUserSettings are leaf nodes in the descendants graph"
+ ;; Table's descendants method is the only source of field-level entries; if Field ever
+ ;; grows its own descendants (e.g. Field -> FieldUserSettings), traversal would visit
+ ;; every field and full exports would change shape. Cement the leaf-ness here.
+ (doseq [opts [{} {:user-edits-only true}]]
+ (is (empty? (serdes/descendants "Field" f1-id opts)))
+ (is (empty? (serdes/descendants "FieldUserSettings" f1-id opts))))))))
+
+(deftest user-edits-only-extract-test
+ (mt/with-empty-h2-app-db!
+ (ts/with-temp-dpc [:model/Database {db-id :id} {:name "DB"}
+ :model/Collection {coll-id :id} {:name "Library" :type "library-data"}
+ :model/Table {table-id :id} {:name "T" :db_id db-id
+ :is_published true
+ :collection_id coll-id}
+ :model/Field _ {:name "F1" :table_id table-id}
+ :model/Field {f2-id :id} {:name "F2" :table_id table-id}
+ :model/FieldUserSettings _ {:field_id f2-id :description "curated"}]
+ (testing "targeting the collection with user-edits-only: produces FieldUserSettings, not Field"
+ (let [entities (into [] (extract/extract {:targets [["Collection" coll-id]]
+ :user-edits-only true
+ :no-data-model true}))
+ by-model (group-by (comp :model last :serdes/meta) entities)]
+ (is (contains? by-model "FieldUserSettings") "should include FieldUserSettings")
+ (is (not (contains? by-model "Field")) "should not include Field")
+ (is (= #{"F2"}
+ (set (map #(-> % :serdes/meta (nth 2) :id) (by-model "FieldUserSettings"))))
+ "only the edited field's FieldUserSettings (identified by field name in path)")
+ (is (some #(= "T" (:name %)) (by-model "Table")) "the table itself is included")))
+ (testing "without user-edits-only: produces Field, not FieldUserSettings"
+ (let [entities (into [] (extract/extract {:targets [["Collection" coll-id]]
+ :no-data-model true}))
+ by-model (group-by (comp :model last :serdes/meta) entities)]
+ (is (contains? by-model "Field") "should include Field")
+ (is (not (contains? by-model "FieldUserSettings")) "should not include FieldUserSettings"))))))
+
(deftest cards-test
(mt/with-empty-h2-app-db!
(ts/with-temp-dpc
diff --git a/src/metabase/warehouse_schema/models/table.clj b/src/metabase/warehouse_schema/models/table.clj
index dea1aa1da01..93625f58725 100644
--- a/src/metabase/warehouse_schema/models/table.clj
+++ b/src/metabase/warehouse_schema/models/table.clj
@@ -588,19 +588,27 @@
collection_id (conj [{:model "Collection" :id collection_id}])
transform_id (conj [{:model "Transform" :id transform_id}])))
-(defmethod serdes/descendants "Table" [_model-name id {:keys [skip-archived]}]
- (let [fields (into {} (for [field-id (t2/select-pks-set :model/Field {:where [:= :table_id id]})]
- {["Field" field-id] {"Table" id}}))
+(defmethod serdes/descendants "Table" [_model-name id {:keys [skip-archived user-edits-only]}]
+ (let [;; When user-edits-only, emit only fields that have user-authored metadata (FieldUserSettings rows).
+ ;; On import the parent Field row is synthesized if missing, so FieldUserSettings is self-sufficient.
+ ;; Otherwise emit all Field ids for a full serdes backup/restore.
+ fields (if user-edits-only
+ (into {} (for [fus-field-id (t2/select-fn-set :field_id :model/FieldUserSettings
+ {:join [[:metabase_field :f] [:= :f.id :field_id]]
+ :where [:= :f.table_id id]})]
+ [["FieldUserSettings" fus-field-id] {"Table" id}]))
+ (into {} (for [field-id (t2/select-pks-set :model/Field {:where [:= :table_id id]})]
+ [["Field" field-id] {"Table" id}])))
segments (into {} (for [segment-id (t2/select-pks-set :model/Segment
{:where [:and
[:= :table_id id]
(when skip-archived [:not :archived])]})]
- {["Segment" segment-id] {"Table" id}}))
+ [["Segment" segment-id] {"Table" id}]))
measures (into {} (for [measure-id (t2/select-pks-set :model/Measure
{:where [:and
[:= :table_id id]
(when skip-archived [:not :archived])]})]
- {["Measure" measure-id] {"Table" id}}))]
+ [["Measure" measure-id] {"Table" id}]))]
(merge fields segments measures)))
(defmethod serdes/generate-path "Table" [_ table]