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
Ignore FKs from inactive tables when checking source replacement (#82618)
Eric Normandefbd247394bb
diff --git a/enterprise/backend/src/metabase_enterprise/replacement/db.clj b/enterprise/backend/src/metabase_enterprise/replacement/db.cljindex 2c8154595b8..a657fc412e2 100644--- a/enterprise/backend/src/metabase_enterprise/replacement/db.clj+++ b/enterprise/backend/src/metabase_enterprise/replacement/db.clj@@ -195,9 +195,15 @@(t2/select-pks-set :model/Field :table_id table-id :active true {:from [(warehouse-schema-overlay/field-query {:user-settings? false})]}))(mu/defn active-fk-to-fields-exists?- "Whether an active Field points at one of the Fields with `field-ids`."+ "Whether an active Field on an active Table points at one of the Fields with `field-ids`."[field-ids :- [:set ::lib.schema.id/field]]- (t2/exists? :model/Field :fk_target_field_id [:in field-ids] :active true {:from [(warehouse-schema-overlay/field-query)]}))+ (t2/exists? :model/Field {:from [(warehouse-schema-overlay/field-query)]+ :join [(warehouse-schema-overlay/table-query {:user-settings? false})+ [:= :metabase_field.table_id :metabase_table.id]]+ :where [:and+ [:in :metabase_field.fk_target_field_id field-ids]+ [:= :metabase_field.active true]+ [:= :metabase_table.active true]]}))(mu/defn sandbox-exists-for-table?"Whether a Sandbox is defined on the Table with `table-id`."diff --git a/enterprise/backend/src/metabase_enterprise/replacement/source_check.clj b/enterprise/backend/src/metabase_enterprise/replacement/source_check.cljindex d2e3f302b8c..b56ece1d476 100644--- a/enterprise/backend/src/metabase_enterprise/replacement/source_check.clj+++ b/enterprise/backend/src/metabase_enterprise/replacement/source_check.clj@@ -24,7 +24,7 @@(replacement.db/card-with-table-exists? sandbox-card-ids table-id))))))(mu/defn- has-incoming-fks? :- :boolean- "Returns true if any active field has a FK pointing to a field in `table-id`."+ "Returns true if any active field on an active table has a FK pointing to a field in `table-id`."[table-id :- ::lib.schema.id/table](if-let [field-ids (not-empty (replacement.db/active-field-ids-of-table table-id))](replacement.db/active-fk-to-fields-exists? field-ids)diff --git a/enterprise/backend/test/metabase_enterprise/replacement/source_check_test.clj b/enterprise/backend/test/metabase_enterprise/replacement/source_check_test.cljindex 9af9f2d0ce1..f501ad485a9 100644--- a/enterprise/backend/test/metabase_enterprise/replacement/source_check_test.clj+++ b/enterprise/backend/test/metabase_enterprise/replacement/source_check_test.clj@@ -1,9 +1,15 @@(ns metabase-enterprise.replacement.source-check-test(:require+ [clojure.java.jdbc :as jdbc][clojure.test :refer :all][metabase-enterprise.replacement.source-check :as replacement.source-check][metabase-enterprise.replacement.usages :as replacement.usages]- [metabase.test :as mt]))+ [metabase.driver :as driver]+ [metabase.driver.sql-jdbc.connection :as sql-jdbc.conn]+ [metabase.sync.core :as sync]+ [metabase.test :as mt]+ [metabase.test.data.interface :as tx]+ [toucan2.core :as t2]))(set! *warn-on-reflection* true)@@ -361,3 +367,31 @@[:card c1-id] [:card c2-id])](is (true? (:success result)))(is (nil? (some #{:database-mismatch} (:errors result))))))))++(deftest no-implicit-joins-from-dropped-table-test+ (testing "#82615: an FK on a table that was dropped from the warehouse does not block replacing the table it targets"+ (mt/test-driver :postgres+ (tx/drop-if-exists-and-create-db! driver/*driver* "replace-dropped-fk-test")+ (let [details (mt/dbdef->connection-details :postgres :db {:database-name "replace-dropped-fk-test"})+ spec (sql-jdbc.conn/connection-details->spec :postgres details)]+ (doseq [statement ["CREATE TABLE table_a (id integer PRIMARY KEY, name text NOT NULL);"+ "CREATE TABLE table_b (id integer PRIMARY KEY, name text NOT NULL);"+ "CREATE TABLE table_c (fk_id integer NOT NULL REFERENCES table_a (id));"+ "CREATE TABLE table_d (fk_id integer NOT NULL REFERENCES table_a (id));"]]+ (jdbc/execute! spec [statement]))+ (mt/with-temp [:model/Database database {:engine :postgres, :details details}]+ (sync/sync-database! database {:scan :schema})+ (let [table-id (fn [table-name] (t2/select-one-pk :model/Table :db_id (:id database) :name table-name))+ fk-field (fn [table-name] (t2/select-one :model/Field :table_id (table-id table-name) :name "fk_id"))+ table-a-id (table-id "table_a")+ table-b-id (table-id "table_b")]+ (jdbc/execute! spec ["DROP TABLE table_d;"])+ (sync/sync-database! database {:scan :schema})+ (testing "precondition: the dropped table is inactive but its FK field is still active"+ (is (false? (t2/select-one-fn :active :model/Table :id (table-id "table_d"))))+ (is (=? {:active true, :fk_target_field_id some?} (fk-field "table_d"))))+ (mt/user-http-request :crowberto :put 200 (format "field/%d" (:id (fk-field "table_c")))+ {:semantic_type nil})+ (is (=? {:success true}+ (replacement.source-check/check-replace-source+ [:table table-a-id] [:table table-b-id])))))))))