Skip to content

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

GDGT-2597 Order Schema Viewer fields to match the table metadata view (#75716)

Stas Gavrylov19cd3a9b25d1
diff --git a/.clj-kondo/config/modules/config.edn b/.clj-kondo/config/modules/config.edn
index d0d2e474211..409eb41cc60 100644
--- a/.clj-kondo/config/modules/config.edn
+++ b/.clj-kondo/config/modules/config.edn
@@ -3112,7 +3112,8 @@
:api #{metabase-enterprise.erd.api}
:uses #{api
models
- util}
+ util
+ warehouse-schema}
:model-imports #{:model/Database
:model/Field
:model/Table}}
diff --git a/enterprise/backend/src/metabase_enterprise/erd/impl.clj b/enterprise/backend/src/metabase_enterprise/erd/impl.clj
index d355f475c23..b41cc4f24ef 100644
--- a/enterprise/backend/src/metabase_enterprise/erd/impl.clj
+++ b/enterprise/backend/src/metabase_enterprise/erd/impl.clj
@@ -9,6 +9,7 @@
[metabase.util.i18n :refer [tru]]
[metabase.util.malli.registry :as mr]
[metabase.util.malli.schema :as ms]
+ [metabase.warehouse-schema.models.table :as schema.table]
[toucan2.core :as t2]))
;;; -------------------------------------------------- Schema --------------------------------------------------
@@ -118,13 +119,15 @@
(defn- fetch-fields-for-tables
"Fetch all active fields for the given table IDs.
- Returns a vector of field maps."
+ Returns a vector of field maps ordered by the same ordering
+ as table metadata view (`/api/table/:id/query_metadata`)."
[table-ids]
(when (seq table-ids)
(t2/select :model/Field
- {:where [:and
- [:in :table_id table-ids]
- [:= :active true]]})))
+ {:where [:and
+ [:in :table_id table-ids]
+ [:= :active true]]
+ :order-by schema.table/field-order-rule})))
(defn- fetch-fields-by-ids
"Fetch active fields by ID."
diff --git a/enterprise/backend/test/metabase_enterprise/erd/erd_test.clj b/enterprise/backend/test/metabase_enterprise/erd/erd_test.clj
index c0f9bc0f612..eddaebb4ee5 100644
--- a/enterprise/backend/test/metabase_enterprise/erd/erd_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/erd/erd_test.clj
@@ -90,6 +90,29 @@
(is (nil? (:fk_target_field_id pk-field)))
(is (nil? (:fk_target_table_id pk-field)))))))
+(deftest erd-graph-field-order-test
+ (mt/with-premium-features #{:schema-viewer}
+ (testing "fields come back ordered by [position asc, lower(name) asc] — same as /api/table/:id/query_metadata"
+ ;; Insertion order deliberately scrambled relative to the expected order so
+ ;; an unsorted DB read would not accidentally pass. `position` is the primary
+ ;; key; `lower(name)` breaks ties (and proves the sort is case-insensitive).
+ (mt/with-temp [:model/Database {db-id :id} {}
+ :model/Table {tid :id} {:db_id db-id :name "t" :schema "PUBLIC"}
+ :model/Field _ {:table_id tid :name "zulu" :position 2
+ :database_type "INTEGER" :base_type :type/Integer}
+ :model/Field _ {:table_id tid :name "Bravo" :position 1
+ :database_type "INTEGER" :base_type :type/Integer}
+ :model/Field _ {:table_id tid :name "alpha" :position 1
+ :database_type "INTEGER" :base_type :type/Integer}
+ :model/Field _ {:table_id tid :name "yankee" :position 0
+ :database_type "INTEGER" :base_type :type/Integer}]
+ (let [response (mt/user-http-request :rasta :get 200 "ee/erd"
+ :database-id db-id
+ :table-ids tid)
+ node (first (filter #(= tid (:table_id %)) (:nodes response)))]
+ (is (= ["yankee" "alpha" "Bravo" "zulu"]
+ (mapv :name (:fields node)))))))))
+
(deftest erd-external-fk-target-resolution-test
(mt/with-premium-features #{:schema-viewer}
(testing "FK field pointing to a readable table beyond the hop budget still carries the target IDs"