diff --git a/src/metabase/api/card.clj b/src/metabase/api/card.clj index 8885f13f3e0404a2379a747409a920c1032864c7..f8e2427905288e64b9831e212b9dc191c3cc2f23 100644 --- a/src/metabase/api/card.clj +++ b/src/metabase/api/card.clj @@ -1153,6 +1153,10 @@ saved later when it is ready." _ (upload/load-from-csv driver db-id schema+table-name csv-file) _ (sync/sync-database! database) table-id (t2/select-one-fn :id Table :db_id db-id :%lower.name table-name)] + (when (nil? table-id) + (driver/drop-table driver db-id table-name) + (throw (ex-info (tru "The CSV file was uploaded to {0} but the table could not be found on sync." schema+table-name) + {:status-code 422}))) (create-card! {:collection_id collection-id, :dataset true diff --git a/test/metabase/api/card_test.clj b/test/metabase/api/card_test.clj index 85738e4324412ac371ad0d4654aaf96008f48765..dc918a89f27896fbf394a9f6a7733099d8e9b618 100644 --- a/test/metabase/api/card_test.clj +++ b/test/metabase/api/card_test.clj @@ -16,6 +16,7 @@ [metabase.driver :as driver] [metabase.driver.sql-jdbc.connection :as sql-jdbc.conn] [metabase.driver.sql-jdbc.execute :as sql-jdbc.execute] + [metabase.driver.util :as driver.u] [metabase.http-client :as client] [metabase.models :refer [CardBookmark @@ -2926,3 +2927,36 @@ java.lang.Exception #"^You do not have curate permissions for this Collection\.$" (upload-example-csv! nil))))))))))) + +(defn- find-schema-filters-prop [driver] + (first (filter (fn [conn-prop] + (= :schema-filters (keyword (:type conn-prop)))) + (driver/connection-properties driver)))) + +(deftest upload-csv!-schema-doesnt-sync-test + ;; Just test with postgres because failure should be independent of the driver + (mt/test-driver :postgres + (mt/with-empty-db + (let [driver (driver.u/database->driver (mt/db)) + schema-filter-prop (find-schema-filters-prop driver) + filter-type-prop (keyword (str (:name schema-filter-prop) "-type")) + patterns-type-prop (keyword (str (:name schema-filter-prop) "-patterns"))] + (t2/update! Database (mt/id) {:details (-> (mt/db) + :details + (assoc filter-type-prop "exclusion" + patterns-type-prop "public"))}) + (mt/with-temporary-setting-values [uploads-enabled true + uploads-database-id (mt/id) + uploads-schema-name "public"] + (testing "Upload should fail if table can't be found after sync, for example because of schema filters" + (try (upload-example-csv! nil) + (catch Exception e + (is (= {:status-code 422} + (.getData e))) + (is (re-matches #"^The CSV file was uploaded to public\.example(.*) but the table could not be found on sync\.$" + (.getMessage e)))))) + (testing "\nThe table should be deleted" + (is (false? (let [details (mt/dbdef->connection-details driver/*driver* :db {:database-name (:name (mt/db))})] + (-> (jdbc/query (sql-jdbc.conn/connection-details->spec driver/*driver* details) + ["SELECT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_schema = 'public')"]) + first :exists))))))))))