Skip to content

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

fix: add metabase_version to serdes extract (#74476)

Nicola Mometto08854a40becc
diff --git a/enterprise/backend/src/metabase_enterprise/serialization/v2/extract.clj b/enterprise/backend/src/metabase_enterprise/serialization/v2/extract.clj
index 2cffe861da5..0b00603a1d8 100644
--- a/enterprise/backend/src/metabase_enterprise/serialization/v2/extract.clj
+++ b/enterprise/backend/src/metabase_enterprise/serialization/v2/extract.clj
@@ -8,6 +8,7 @@
[metabase-enterprise.serialization.v2.backfill-ids :as serdes.backfill]
[metabase-enterprise.serialization.v2.models :as serdes.models]
[metabase.collections.models.collection :as collection]
+ [metabase.config.core :as config]
[metabase.models.serialization :as serdes]
[metabase.util :as u]
[metabase.util.log :as log]
@@ -210,11 +211,22 @@
;; extract all non-content entities like data model and settings if necessary
(eduction (map #(serdes/extract-all % opts)) cat (remove (set serdes.models/content) models))])))))
+(defn- needs-version?
+ "True for extracted entities that should carry a `:metabase_version` stamp."
+ [entity]
+ (and (not (instance? Exception entity))
+ (not= "Setting" (-> entity :serdes/meta last :model))))
+
+(defn- stamp-version [entity]
+ (if (needs-version? entity)
+ (assoc entity :metabase_version config/mb-version-string)
+ entity))
+
(defn extract
"Returns a reducible stream of entities to serialize"
[opts]
(serdes.backfill/backfill-ids!)
- (extract-subtrees opts))
+ (eduction (map stamp-version) (extract-subtrees opts)))
(comment
(def nodes (let [colls (mapv vector (repeat "Collection") (collection-set-for-user nil))]
diff --git a/enterprise/backend/src/metabase_enterprise/serialization/v2/load.clj b/enterprise/backend/src/metabase_enterprise/serialization/v2/load.clj
index 09d5e3ca2e3..f6ebed38890 100644
--- a/enterprise/backend/src/metabase_enterprise/serialization/v2/load.clj
+++ b/enterprise/backend/src/metabase_enterprise/serialization/v2/load.clj
@@ -122,12 +122,14 @@
(serdes.backfill/has-entity-id? model))))
(defn- warn-if-version-mismatch
- "Checks if the version in the exported entity's serdes/meta differs from the current Metabase version.
- Logs a warning if there is a mismatch."
+ "Checks if the version in the exported entity differs from the current Metabase version.
+ Logs a warning if there is a mismatch. Entities without a `:metabase_version` (eg. Settings,
+ which are bundled into settings.yaml without per-entity metadata) are skipped."
[ingested path]
- (when (or (nil? *warned-version-mismatch*) (not @*warned-version-mismatch*))
- (let [current-version config/mb-version-string
- exported-version (or (:metabase_version ingested) "UNKNOWN")]
+ (when (and (or (nil? *warned-version-mismatch*) (not @*warned-version-mismatch*))
+ (:metabase_version ingested))
+ (let [current-version config/mb-version-string
+ exported-version (:metabase_version ingested)]
(when (not= exported-version current-version)
(log/warnf "Version mismatch loading %s: exported with: %s, current version: %s"
path
diff --git a/enterprise/backend/test/metabase_enterprise/serialization/v2/e2e_test.clj b/enterprise/backend/test/metabase_enterprise/serialization/v2/e2e_test.clj
index a3d35c9653f..f8f9edb8148 100644
--- a/enterprise/backend/test/metabase_enterprise/serialization/v2/e2e_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/serialization/v2/e2e_test.clj
@@ -105,7 +105,8 @@
(defn- clean-entity
"Removes any comparison-confounding fields, like `:created_at`."
[entity]
- (dissoc entity :created_at :result_metadata :metadata_sync_schedule :cache_field_values_schedule))
+ (dissoc entity :created_at :result_metadata :metadata_sync_schedule :cache_field_values_schedule
+ :metabase_version))
#_{:clj-kondo/ignore [:metabase/i-like-making-cams-eyes-bleed-with-horrifically-long-tests]}
(deftest e2e-storage-ingestion-test
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 a8d8ed8a9d2..7f4afad8576 100644
--- a/enterprise/backend/test/metabase_enterprise/serialization/v2/extract_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/serialization/v2/extract_test.clj
@@ -12,6 +12,7 @@
[metabase-enterprise.serialization.v2.round-trip-test :as round-trip-test]
[metabase.actions.models :as action]
[metabase.audit-app.core :as audit]
+ [metabase.config.core :as config]
[metabase.core.core :as mbc]
[metabase.lib-be.core :as lib-be]
[metabase.lib.core :as lib]
@@ -2972,3 +2973,20 @@
(testing "all embedding themes are extracted"
(is (= #{light-eid dark-eid}
(ids-by-model "EmbeddingTheme" (extract/extract {}))))))))
+
+(deftest stamp-metabase-version-test
+ (testing "extract stamps :metabase_version on entities (so load can detect version mismatches)"
+ (mt/with-empty-h2-app-db!
+ (ts/with-temp-dpc [:model/Collection {coll-id :id} {:name "My Collection"}
+ :model/Card _ {:name "My Card" :collection_id coll-id}]
+ (let [extracted (into [] (extract/extract {}))
+ by-model (group-by (comp :model last :serdes/meta) extracted)]
+ (testing "Collections and Cards get the current Metabase version"
+ (doseq [m ["Collection" "Card"]
+ entity (get by-model m)]
+ (is (= config/mb-version-string (:metabase_version entity))
+ (str m " should be stamped with the current version"))))
+ (testing "Settings are not stamped — settings.yaml only persists :key and :value"
+ (doseq [setting (get by-model "Setting")]
+ (is (not (contains? setting :metabase_version))
+ "Setting should not be stamped"))))))))
diff --git a/enterprise/backend/test/metabase_enterprise/serialization/v2/load_test.clj b/enterprise/backend/test/metabase_enterprise/serialization/v2/load_test.clj
index 5dfe919c0c9..8409d065519 100644
--- a/enterprise/backend/test/metabase_enterprise/serialization/v2/load_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/serialization/v2/load_test.clj
@@ -1865,7 +1865,7 @@
(is (= 4 (coll-count)))))))
(deftest warn-if-version-mismatch-test
- (ts/with-dbs [source-db dest-db dest-db2]
+ (ts/with-dbs [source-db dest-db dest-db2 dest-db3]
(ts/with-db source-db
(mt/with-temp [:model/Collection _ {:name "col-1"}]
(let [extract (into [] (serdes.extract/extract {:no-settings true}))]
@@ -1879,9 +1879,16 @@
"Should log a version mismatch warning only once per load")))))
(ts/with-db dest-db2
(testing "No warnings when version in serdes/meta matches current version"
- (log.capture/with-log-messages-for-level [messages :warn]
+ (log.capture/with-log-messages-for-level [messages [metabase-enterprise.serialization.v2.load :warn]]
(serdes.load/load-metabase! (ingestion-in-memory extract))
- (is (= 0 (count (filter #(str/includes? % "Version mismatch loading") (messages)))))))))))))
+ (is (= 0 (count (filter #(str/includes? % "Version mismatch loading") (messages))))))))
+ (ts/with-db dest-db3
+ (testing "No warnings when entities have no :metabase_version (eg. legacy exports or Settings)"
+ (let [no-version-extract (map #(dissoc % :metabase_version) extract)]
+ (log.capture/with-log-messages-for-level [messages [metabase-enterprise.serialization.v2.load :warn]]
+ (serdes.load/load-metabase! (ingestion-in-memory no-version-extract))
+ (is (= 0 (count (filter #(str/includes? % "Version mismatch loading") (messages))))
+ "Missing :metabase_version should be treated as unknown, not as a mismatch"))))))))))
(deftest import-published-table-with-existing-database-test
(testing "Importing a published table works when database already exists on target"
diff --git a/enterprise/backend/test/metabase_enterprise/serialization/v2/storage_test.clj b/enterprise/backend/test/metabase_enterprise/serialization/v2/storage_test.clj
index a23f71119cf..555c160fa74 100644
--- a/enterprise/backend/test/metabase_enterprise/serialization/v2/storage_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/serialization/v2/storage_test.clj
@@ -43,11 +43,11 @@
(testing "the Collections properly exported"
(let [yaml-parent (-> (yaml/from-file (io/file dump-dir "collections" "main"
"some_collection.yaml"))
- (dissoc :serdes/meta)
+ (dissoc :serdes/meta :metabase_version)
(update :created_at t/offset-date-time))
yaml-child (-> (yaml/from-file (io/file dump-dir "collections" "main"
"some_collection" "child_collection.yaml"))
- (dissoc :serdes/meta)
+ (dissoc :serdes/meta :metabase_version)
(update :created_at t/offset-date-time))]
(is (= (-> (into {} (t2/select-one :model/Collection :id (:id parent)))
(dissoc :id :location)
@@ -145,6 +145,7 @@
"databases" "my_company_data"
"tables" "customers"
"fields" "company__SLASH__organization_website.yaml"))
+ (dissoc :metabase_version)
(update :visibility_type keyword)
(update :base_type keyword))))))))))