Skip to content

Commit

Permalink
CSV Uploads: catch failed connections and fix duplicate inserts (#238)
Browse files Browse the repository at this point in the history
* + catch sql exceptions from `cloud?`

* fix reference to cloud?
  • Loading branch information
calherries authored May 21, 2024
1 parent 409984e commit 47a64ec
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/metabase/driver/clickhouse.clj
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@
(sql-jdbc.common/handle-additional-options details :separator-style :url))))

(def ^:private ^{:arglists '([db-details])} cloud?
"Is this a cloud DB?"
"Returns true if the `db-details` are for a ClickHouse Cloud instance, and false otherwise. If it fails to connect
to the database, it throws a java.sql.SQLException."
(memoize/ttl
(fn [db-details]
(sql-jdbc.execute/do-with-connection-with-options
Expand All @@ -85,13 +86,17 @@
(defmethod sql-jdbc.conn/connection-details->spec :clickhouse
[_ details]
(cond-> (connection-details->spec* details)
(cloud? details)
(try (cloud? details)
(catch java.sql.SQLException _e
false))
;; select_sequential_consistency guarantees that we can query data from any replica in CH Cloud
;; immediately after it is written
(assoc :select_sequential_consistency true)))

(defmethod driver/database-supports? [:clickhouse :uploads] [_driver _feature db]
(cloud? (:details db)))
(try (cloud? (:details db))
(catch java.sql.SQLException _e
false)))

(defmethod driver/can-connect? :clickhouse
[driver details]
Expand Down Expand Up @@ -179,7 +184,9 @@
:quoted true
:dialect (sql.qp/quote-style driver)))
"ENGINE = MergeTree"
(format "ORDER BY (%s)" (str/join ", " (map quote-name primary-key)))]))
(format "ORDER BY (%s)" (str/join ", " (map quote-name primary-key)))
;; disable insert idempotency to allow duplicate inserts
"SETTINGS replicated_deduplication_window = 0"]))

(defmethod driver/create-table! :clickhouse
[driver db-id table-name column-definitions & {:keys [primary-key]}]
Expand Down
11 changes: 11 additions & 0 deletions test/metabase/driver/clickhouse_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,17 @@
:clickhouse
{}))))))

(deftest clickhouse-connection-fails-test
(mt/test-driver :clickhouse
(mt/with-temp [:model/Database db {:details (assoc (mt/db) :password "wrongpassword") :engine :clickhouse}]
(testing "Sense check that checking the cloud mode fails with a SQLException."
(is (thrown? java.sql.SQLException (#'clickhouse/cloud? (:details db)))))
(testing "`driver/database-supports?` succeeds even if the connection fails."
(is (false? (driver/database-supports? :clickhouse :uploads db))))
(testing (str "`sql-jdbc.conn/connection-details->spec` succeeds even if the connection fails, "
"and doesn't include the `select_sequential_consistency` parameter.")
(is (nil? (:select_sequential_consistency (sql-jdbc.conn/connection-details->spec :clickhouse (:details db)))))))))

(deftest ^:parallel clickhouse-tls
(mt/test-driver
:clickhouse
Expand Down

0 comments on commit 47a64ec

Please sign in to comment.