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
Fix models to transforms for transform output tables created with nil schema (#72139)
bryan1c92ac2bbc23
diff --git a/src/metabase/transforms_base/util.clj b/src/metabase/transforms_base/util.cljindex b7f9402c373..3297074561a 100644--- a/src/metabase/transforms_base/util.clj+++ b/src/metabase/transforms_base/util.clj@@ -7,6 +7,7 @@[clojure.string :as str][java-time.api :as t][metabase.driver :as driver]+ [metabase.driver.sql.normalize :as sql.normalize][metabase.events.core :as events][metabase.lib-be.core :as lib-be][metabase.lib.core :as lib]@@ -110,6 +111,16 @@;;; ------------------------------------------------- Table Names -------------------------------------------------+(defn- resolve-nil-schema+ "When a table has nil schema, check if the physical table exists under the driver's+ default schema. If so, return that schema. Otherwise return nil.+ This handles the case where transforms create tables without explicit schema+ but the driver needs a schema to find the table during sync."+ [driver database table]+ (when-let [default-schema (try (sql.normalize/default-schema driver) (catch Exception _ nil))]+ (when (driver/table-exists? driver database {:schema default-schema :name (:name table)})+ default-schema)))+(defn qualified-table-name"Return the name of the target table of a transform as a possibly qualified symbol."[_driver {:keys [schema name]}]@@ -357,8 +368,17 @@(when-let [table (or (target-table (:id database) target)(when create?(sync/create-table! database (select-keys target [:schema :name :data_source :data_authority :is_writable]))))]- (sync/sync-table! table)- table)))+ ;; If the table has nil schema, check if the physical table actually lives under+ ;; the driver's default schema. If so, fix the Table record before syncing.+ (let [table (if (nil? (:schema table))+ (if-let [actual-schema (resolve-nil-schema (:engine database) database table)]+ (do (t2/update! :model/Table (:id table) {:schema actual-schema})+ (-> (t2/select-one :model/Table (:id table))+ (t2/hydrate :db)))+ table)+ table)]+ (sync/sync-table! table)+ table))))(defn activate-table-and-mark-computed!"Activate table for `target` in `database` in the app db."diff --git a/test/metabase/transforms/util_test.clj b/test/metabase/transforms/util_test.cljindex bb34305a80b..c2b58350d6a 100644--- a/test/metabase/transforms/util_test.clj+++ b/test/metabase/transforms/util_test.clj@@ -342,7 +342,8 @@:data_authority (:data_authority table-map)})](reset! synced-table created)created))- sync/sync-table! (constantly nil)]+ sync/sync-table! (constantly nil)+ driver/table-exists? (constantly false)](transforms-base.u/activate-table-and-mark-computed! db target)(is (some? @synced-table))(let [table (t2/select-one :model/Table (:id @synced-table))]@@ -409,3 +410,55 @@(is (string? query))(is (not (re-find #"(?i)\bLIMIT\b" query))(str "Expected no LIMIT clause in compiled SQL, got: " query)))))))++(deftest activate-table-resolves-nil-schema-test+ (testing "activate-table-and-mark-computed! resolves nil schema when physical table exists in default schema"+ (let [target {:type "table" :schema nil :name "test_nil_schema_fix"}+ synced-table (atom nil)]+ (mt/with-temp [:model/Database db {:engine :h2}]+ (with-redefs [sync/create-table! (fn [database table-map]+ (let [created (t2/insert-returning-instance!+ :model/Table+ {:db_id (:id database)+ :name (:name table-map)+ :schema (:schema table-map)+ :active true+ :is_writable (:is_writable table-map)+ :data_source (:data_source table-map)+ :data_authority (:data_authority table-map)})]+ (reset! synced-table created)+ created))+ sync/sync-table! (constantly nil)+ ;; Simulate: physical table exists in "PUBLIC" (H2's default schema)+ driver/table-exists? (fn [_driver _db table]+ (= "PUBLIC" (:schema table)))]+ (transforms-base.u/activate-table-and-mark-computed! db target)+ (is (some? @synced-table))+ (let [table (t2/select-one :model/Table (:id @synced-table))]+ (is (= "PUBLIC" (:schema table))+ "Table schema should be updated to the driver's default schema"))))))++ (testing "activate-table-and-mark-computed! leaves nil schema when physical table has no default schema"+ (let [target {:type "table" :schema nil :name "test_nil_schema_no_default"}+ synced-table (atom nil)]+ (mt/with-temp [:model/Database db {:engine :h2}]+ (with-redefs [sync/create-table! (fn [database table-map]+ (let [created (t2/insert-returning-instance!+ :model/Table+ {:db_id (:id database)+ :name (:name table-map)+ :schema (:schema table-map)+ :active true+ :is_writable (:is_writable table-map)+ :data_source (:data_source table-map)+ :data_authority (:data_authority table-map)})]+ (reset! synced-table created)+ created))+ sync/sync-table! (constantly nil)+ ;; Simulate: physical table does NOT exist under default schema+ driver/table-exists? (constantly false)]+ (transforms-base.u/activate-table-and-mark-computed! db target)+ (is (some? @synced-table))+ (let [table (t2/select-one :model/Table (:id @synced-table))]+ (is (nil? (:schema table))+ "Table schema should remain nil when physical table isn't in default schema")))))))