Skip to content

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

Fix transform target table hydration (#64999)

metambenbb6d7b1cbccb
diff --git a/enterprise/backend/src/metabase_enterprise/transforms/models/transform.clj b/enterprise/backend/src/metabase_enterprise/transforms/models/transform.clj
index 488f13ae9b5..3c87d77355d 100644
--- a/enterprise/backend/src/metabase_enterprise/transforms/models/transform.clj
+++ b/enterprise/backend/src/metabase_enterprise/transforms/models/transform.clj
@@ -187,13 +187,16 @@
(when-not schema
[db-id table-name]))
table-keys)
- tables (-> (t2/select :model/Table
- {:where [:or
- [:in [:composite :db_id :schema :name] table-keys-with-schema]
- [:and
- [:= :schema nil]
- [:in [:composite :db_id :name] table-keys-without-schema]]]})
- (t2/hydrate :db :fields))
+ tables (when (or (seq table-keys-with-schema) (seq table-keys-without-schema))
+ (-> (t2/select :model/Table
+ {:where [:or
+ (when (seq table-keys-with-schema)
+ [:in [:composite :db_id :schema :name] table-keys-with-schema])
+ (when (seq table-keys-without-schema)
+ [:and
+ [:= :schema nil]
+ [:in [:composite :db_id :name] table-keys-without-schema]])]})
+ (t2/hydrate :db :fields)))
table-keys->table (m/index-by (juxt :db_id :schema :name) tables)]
(for [transform transforms]
(assoc transform :table (get table-keys->table (table-key-fn transform))))))
diff --git a/enterprise/backend/test/metabase_enterprise/transforms/models_test.clj b/enterprise/backend/test/metabase_enterprise/transforms/models_test.clj
index b83d3e99b8b..7d349c23d18 100644
--- a/enterprise/backend/test/metabase_enterprise/transforms/models_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/transforms/models_test.clj
@@ -156,3 +156,67 @@
(let [hydrated (t2/hydrate (t2/select-one :model/TransformJob :id job-id) :tag_ids)]
(is (= [(:id tag1) (:id tag3) (:id tag2)] (:tag_ids hydrated))
"Tags should be reordered with new tag added"))))))))
+
+(deftest table-with-db-and-fields-hydration-test
+ (testing "table-with-db-and-fields hydration handles NULL schemas correctly"
+ (mt/with-temp [:model/Database {db-id :id} {}
+ ;; Create tables with different schema values
+ :model/Table {table-with-schema-id :id} {:db_id db-id
+ :schema "public"
+ :name "test_table_with_schema"}
+ :model/Table {table-null-schema-id :id} {:db_id db-id
+ :schema nil
+ :name "test_table_null_schema"}
+ :model/Table {other-table-id :id} {:db_id db-id
+ :schema "other"
+ :name "other_table"}
+ ;; Create transforms targeting these tables
+ :model/Transform transform1 {:name "Transform with schema"
+ :source {:type "query"
+ :query {:database db-id
+ :type "native"
+ :native {:query "SELECT 1"
+ :template-tags {}}}}
+ :target {:type "table"
+ :schema "public"
+ :name "test_table_with_schema"
+ :db_id db-id}}
+ :model/Transform transform2 {:name "Transform with NULL schema"
+ :source {:type "query"
+ :query {:database db-id
+ :type "native"
+ :native {:query "SELECT 2"
+ :template-tags {}}}}
+ :target {:type "table"
+ :schema nil
+ :name "test_table_null_schema"
+ :db_id db-id}}]
+
+ (testing "Hydrates table with non-NULL schema"
+ (let [hydrated (t2/hydrate transform1 :table-with-db-and-fields)]
+ (is (some? (:table hydrated))
+ "Transform should have hydrated table")
+ (is (= table-with-schema-id (-> hydrated :table :id))
+ "Should hydrate correct table with schema")))
+
+ (testing "Hydrates table with NULL schema"
+ (let [hydrated (t2/hydrate transform2 :table-with-db-and-fields)]
+ (is (some? (:table hydrated))
+ "Transform with NULL schema should have hydrated table")
+ (is (= table-null-schema-id (-> hydrated :table :id))
+ "Should hydrate correct table with NULL schema")))
+
+ (testing "Hydrates multiple transforms with mixed schemas in single batch"
+ (let [hydrated (t2/hydrate [transform1 transform2] :table-with-db-and-fields)]
+ (is (= 2 (count hydrated))
+ "Should hydrate both transforms")
+ (is (every? (comp some? :table) hydrated)
+ "Both transforms should have hydrated tables")
+ (is (= #{table-with-schema-id table-null-schema-id}
+ (set (map (comp :id :table) hydrated)))
+ "Should hydrate both tables correctly")))
+
+ (testing "Does not hydrate unrelated tables"
+ (let [hydrated (t2/hydrate transform1 :table-with-db-and-fields)]
+ (is (not= other-table-id (-> hydrated :table :id))
+ "Should not hydrate unrelated table"))))))