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
Export published tables when their collection is selected (#67141)
metamben93e1a7a60b9c
diff --git a/enterprise/backend/src/metabase_enterprise/serialization/v2/extract.clj b/enterprise/backend/src/metabase_enterprise/serialization/v2/extract.cljindex 3cd881f39fc..1a87c9bc3dd 100644--- a/enterprise/backend/src/metabase_enterprise/serialization/v2/extract.clj+++ b/enterprise/backend/src/metabase_enterprise/serialization/v2/extract.clj@@ -140,14 +140,19 @@escaped (escape-analysis by-model nodes)](if (seq escaped)(log-escape-report! escaped)- (let [models (model-set opts)- coll-set (get by-model "Collection")- by-model (select-keys by-model models)- extract-by-ids (fn [[model ids]]- (serdes/extract-all model (merge opts {:collection-set coll-set- :where [:in :id ids]})))- extract-all (fn [model]- (serdes/extract-all model (assoc opts :collection-set coll-set)))]+ (let [models (model-set opts)+ coll-set (get by-model "Collection")+ ;; When targets are specified, also include Tables found via descendants+ ;; (published tables in target collections). These are extracted by ID, not all.+ targeted-tables (when (seq targets) (get by-model "Table"))+ by-model (cond-> (select-keys by-model models)+ ;; Add Tables back if they were found in descendants+ (seq targeted-tables) (assoc "Table" targeted-tables))+ extract-by-ids (fn [[model ids]]+ (serdes/extract-all model (merge opts {:collection-set coll-set+ :where [:in :id ids]})))+ extract-all (fn [model]+ (serdes/extract-all model (assoc opts :collection-set coll-set)))](eduction cat[(if (seq targets)(eduction (map extract-by-ids) cat by-model)diff --git a/enterprise/backend/test/metabase_enterprise/serialization/v2/extract_test.clj b/enterprise/backend/test/metabase_enterprise/serialization/v2/extract_test.cljindex b07edf89150..2251dffcb51 100644--- a/enterprise/backend/test/metabase_enterprise/serialization/v2/extract_test.clj+++ b/enterprise/backend/test/metabase_enterprise/serialization/v2/extract_test.clj@@ -2309,3 +2309,36 @@(testing "all transform jobs are extracted"(is (= #{hourly-job-eid custom-job-eid}(ids-by-model "TransformJob" (extract/extract {}))))))))))++(deftest collection-export-includes-published-tables-test+ (testing "Exporting a collection includes published tables without requiring data_model flag"+ (mt/with-empty-h2-app-db!+ (ts/with-temp-dpc [:model/Database {db-id :id} {:name "My Database"}+ :model/Collection {coll-id :id} {:name "Export Collection"}+ :model/Table _ {:name "Published Table"+ :db_id db-id+ :is_published true+ :collection_id coll-id}+ :model/Table _ {:name "Unpublished Table"+ :db_id db-id+ :is_published false}+ :model/Table _ {:name "Other Table"+ :db_id db-id+ :is_published true+ :collection_id nil}]+ (let [opts {:targets [["Collection" coll-id]]+ :no-data-model true+ :no-settings true}+ extracted (into [] (extract/extract opts))+ table-names (->> extracted+ (filter #(= "Table" (-> % :serdes/meta last :model)))+ (map :name)+ set)]+ (testing "published table in target collection is exported"+ (is (contains? table-names "Published Table")))+ (testing "unpublished table is not exported"+ (is (not (contains? table-names "Unpublished Table"))))+ (testing "published table in other collection is not exported"+ (is (not (contains? table-names "Other Table"))))+ (testing "database is not exported (no-data-model: true)"+ (is (empty? (filter #(= "Database" (-> % :serdes/meta last :model)) extracted)))))))))diff --git a/enterprise/backend/test/metabase_enterprise/serialization/v2/load_test.clj b/enterprise/backend/test/metabase_enterprise/serialization/v2/load_test.cljindex 75ade58dbdb..93e1b8ba1f1 100644--- a/enterprise/backend/test/metabase_enterprise/serialization/v2/load_test.clj+++ b/enterprise/backend/test/metabase_enterprise/serialization/v2/load_test.clj@@ -1553,3 +1553,77 @@(log.capture/with-log-messages-for-level [messages :warn](serdes.load/load-metabase! (ingestion-in-memory extract))(is (= 0 (count (filter #(str/includes? % "Version mismatch loading") (messages)))))))))))))++(deftest import-published-table-with-existing-database-test+ (testing "Importing a published table works when database already exists on target"+ (let [serialized (atom nil)+ coll-eid (atom nil)]+ (ts/with-dbs [source-db dest-db]+ ;; Export from source (with no-data-model, so Database is not in export)+ (testing "export published table without database"+ (ts/with-db source-db+ (let [db (ts/create! :model/Database :name "shared-db")+ coll (ts/create! :model/Collection :name "My Collection")+ _ (ts/create! :model/Table+ :name "published_table"+ :db_id (:id db)+ :is_published true+ :collection_id (:id coll)+ :description "A published table")]+ (reset! coll-eid (:entity_id coll))+ (reset! serialized+ (into [] (serdes.extract/extract+ {:targets [["Collection" (:id coll)]]+ :no-data-model true+ :no-settings true}))))))++ (testing "serialized data contains table but not database"+ (is (some #(= "Table" (-> % :serdes/meta last :model)) @serialized))+ (is (not-any? #(= "Database" (-> % :serdes/meta last :model)) @serialized)))++ ;; Import to destination (where database already exists)+ (testing "import succeeds when database exists on target"+ (ts/with-db dest-db+ ;; Pre-create the database on target+ (let [target-db (ts/create! :model/Database :name "shared-db")]+ ;; Load the serialized content+ (serdes.load/load-metabase! (ingestion-in-memory @serialized))++ ;; Verify the table was imported correctly+ (let [imported-table (t2/select-one :model/Table :name "published_table")+ imported-coll (t2/select-one :model/Collection :entity_id @coll-eid)]+ (testing "table exists with correct properties"+ (is (some? imported-table))+ (is (= "A published table" (:description imported-table)))+ (is (true? (:is_published imported-table))))+ (testing "table is linked to correct database"+ (is (= (:id target-db) (:db_id imported-table))))+ (testing "table is linked to imported collection"+ (is (= (:id imported-coll) (:collection_id imported-table))))))))))))++(deftest import-published-table-without-database-fails-test+ (testing "Importing a published table fails when database doesn't exist on target"+ (let [serialized (atom nil)]+ (ts/with-dbs [source-db dest-db]+ (testing "export published table"+ (ts/with-db source-db+ (let [db (ts/create! :model/Database :name "source-only-db")+ coll (ts/create! :model/Collection :name "My Collection")+ _ (ts/create! :model/Table+ :name "published_table"+ :db_id (:id db)+ :is_published true+ :collection_id (:id coll))]+ (reset! serialized+ (into [] (serdes.extract/extract+ {:targets [["Collection" (:id coll)]]+ :no-data-model true+ :no-settings true}))))))++ (testing "import fails when database doesn't exist"+ (ts/with-db dest-db+ ;; Don't create the database - import should fail+ (is (thrown-with-msg?+ Exception+ #"source-only-db|not found|Failed"+ (serdes.load/load-metabase! (ingestion-in-memory @serialized))))))))))diff --git a/src/metabase/collections/models/collection.clj b/src/metabase/collections/models/collection.cljindex cf091b8c953..057e5d35a8b 100644--- a/src/metabase/collections/models/collection.clj+++ b/src/metabase/collections/models/collection.clj@@ -1961,8 +1961,13 @@timelines (into {} (for [timeline-id (t2/select-pks-set :model/Timeline {:where [:and[:= :collection_id id](when skip-archived [:not :archived])]})]- {["Timeline" timeline-id] {"Collection" id}}))]- (merge child-colls dashboards cards documents timelines)))+ {["Timeline" timeline-id] {"Collection" id}}))+ tables (into {} (for [table-id (t2/select-pks-set :model/Table {:where [:and+ [:= :collection_id id]+ [:= :is_published true]+ (when skip-archived [:= :archived_at nil])]})]+ {["Table" table-id] {"Collection" id}}))]+ (merge child-colls dashboards cards documents timelines tables)))(defmethod serdes/storage-path "Collection" [coll {:keys [collections]}](let [parental (get collections (:entity_id coll))]diff --git a/test/metabase/collections/models/collection_test.clj b/test/metabase/collections/models/collection_test.cljindex 0702c6a9e32..9880724ecee 100644--- a/test/metabase/collections/models/collection_test.clj+++ b/test/metabase/collections/models/collection_test.clj@@ -6,6 +6,7 @@[clojure.string :as str][clojure.test :refer :all][clojure.walk :as walk]+ [java-time.api :as t][metabase.api.common :as api][metabase.audit-app.impl :as audit][metabase.collections.models.collection :as collection]@@ -3306,3 +3307,49 @@(let [collection (t2/select-one :model/Collection id)](is (nil? (:type collection)))(is (true? (:is_remote_synced collection))))))++(deftest serdes-descendants-includes-published-tables-test+ (testing "Collection descendants includes published tables"+ (mt/with-temp [:model/Database db {:name "Test DB"}+ :model/Collection coll {:name "Test Collection"}+ :model/Table pub-table {:name "Published Table"+ :db_id (:id db)+ :is_published true+ :collection_id (:id coll)}+ :model/Table unpub-table {:name "Unpublished Table"+ :db_id (:id db)+ :is_published false}+ :model/Table other-coll-table {:name "Other Collection Table"+ :db_id (:id db)+ :is_published true+ :collection_id nil}]+ (let [descendants (serdes/descendants "Collection" (:id coll) {})]+ (testing "published table in collection is included"+ (is (contains? descendants ["Table" (:id pub-table)])))+ (testing "unpublished table is not included"+ (is (not (contains? descendants ["Table" (:id unpub-table)]))))+ (testing "published table in different collection is not included"+ (is (not (contains? descendants ["Table" (:id other-coll-table)]))))))))++(deftest serdes-descendants-skip-archived-tables-test+ (testing "Collection descendants with skip-archived handles published tables"+ (mt/with-temp [:model/Database db {:name "Test DB"}+ :model/Collection coll {:name "Test Collection"}+ :model/Table active-table {:name "Active Published Table"+ :db_id (:id db)+ :is_published true+ :collection_id (:id coll)+ :archived_at nil}+ :model/Table archived-table {:name "Archived Published Table"+ :db_id (:id db)+ :is_published true+ :collection_id (:id coll)+ :archived_at (t/instant)}]+ (testing "archived tables are excluded when skip-archived: true"+ (let [descendants (serdes/descendants "Collection" (:id coll) {:skip-archived true})]+ (is (contains? descendants ["Table" (:id active-table)]))+ (is (not (contains? descendants ["Table" (:id archived-table)])))))+ (testing "archived tables are included when skip-archived: false"+ (let [descendants (serdes/descendants "Collection" (:id coll) {:skip-archived false})]+ (is (contains? descendants ["Table" (:id active-table)]))+ (is (contains? descendants ["Table" (:id archived-table)])))))))