Skip to content

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

Remove `driver/set-use-database` in favour of setting the DB in the jdbc spec (#63047)

Riley Thompsonf4d3359e33bf
diff --git a/docs/developers-guide/driver-changelog.md b/docs/developers-guide/driver-changelog.md
index a507a1e01cb..98799672c15 100644
--- a/docs/developers-guide/driver-changelog.md
+++ b/docs/developers-guide/driver-changelog.md
@@ -20,6 +20,8 @@ title: Driver interface changelog
- `driver/humanize-connection-error-message` now takes a list of all messages in the exception chain,
instead of just the top-level message as a string.
+- Removed `driver/set-database-used`. Drivers should set default databases in their connection specs instead.
+
## Metabase 0.56.3
- Added the driver multi-method `driver/describe-database*` that drivers should now implement instead of `driver/describe-database`.
diff --git a/enterprise/backend/test/metabase_enterprise/impersonation/driver_test.clj b/enterprise/backend/test/metabase_enterprise/impersonation/driver_test.clj
index 7a52131d18d..d190a405362 100644
--- a/enterprise/backend/test/metabase_enterprise/impersonation/driver_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/impersonation/driver_test.clj
@@ -604,7 +604,7 @@
;; (`LIMITED.ROLE` in CI Snowflake has no data access)
(is (thrown-with-msg?
clojure.lang.ExceptionInfo
- #"SQL compilation error:\nObject does not exist, or operation cannot be performed"
+ #"Cannot perform SELECT. This session does not have a current database. Call 'USE DATABASE', or use a qualified name."
(mt/run-mbql-query venues
{:aggregation [[:count]]})))
diff --git a/modules/drivers/snowflake/resources/metabase-plugin.yaml b/modules/drivers/snowflake/resources/metabase-plugin.yaml
index 6281371dcfa..408b7cd72ac 100644
--- a/modules/drivers/snowflake/resources/metabase-plugin.yaml
+++ b/modules/drivers/snowflake/resources/metabase-plugin.yaml
@@ -63,12 +63,6 @@ driver:
- merge:
- additional-options
- placeholder: 'queryTimeout=0'
- - name: quote-db-name
- display-name: Quote DB name (to ensure case sensitive matching)
- default: false
- type: boolean
- visible-if:
- advanced-options: true
- default-advanced-options
init:
- step: load-namespace
diff --git a/modules/drivers/snowflake/src/metabase/driver/snowflake.clj b/modules/drivers/snowflake/src/metabase/driver/snowflake.clj
index 9fbf52370de..bc6d47dce26 100644
--- a/modules/drivers/snowflake/src/metabase/driver/snowflake.clj
+++ b/modules/drivers/snowflake/src/metabase/driver/snowflake.clj
@@ -231,8 +231,7 @@
;; of `db`. If we run across `dbname`, correct our behavior
(set/rename-keys {:dbname :db})
;; see https://github.com/metabase/metabase/issues/27856
- (cond-> (:quote-db-name details)
- (update :db quote-name))
+ (update :db quote-name)
(cond-> use-password
(dissoc :private-key))
;; password takes precedence if `use-password` is missing
@@ -868,11 +867,6 @@
(with-open [rs (.getTables metadata db-name schema-name table-name nil)]
(.next rs)))))))
-(defmethod driver/set-database-used! :snowflake [_driver conn db]
- (let [sql (format "USE DATABASE \"%s\"" (db-name db))]
- (with-open [stmt (.createStatement ^java.sql.Connection conn)]
- (.execute stmt sql))))
-
(defmethod driver/run-transform! [:snowflake :table]
[driver {:keys [connection-details query output-table]} opts]
(let [driver (keyword driver)
@@ -881,7 +875,11 @@
:output-table output-table})]
(:overwrite? opts) (cons (driver/compile-drop-table driver output-table)))
db-name-val (some connection-details [:db :dbname])
+ quoted-db (if (and (= (first db-name-val) \")
+ (= (last db-name-val) \"))
+ db-name-val
+ (quote-name db-name-val))
all-queries (if db-name-val
- (cons [(format "USE DATABASE \"%s\"" db-name-val)] queries)
+ (cons [(str "USE DATABASE " quoted-db)] queries)
queries)]
{:rows-affected (last (driver/execute-raw-queries! driver connection-details all-queries))}))
diff --git a/modules/drivers/snowflake/test/metabase/driver/snowflake_test.clj b/modules/drivers/snowflake/test/metabase/driver/snowflake_test.clj
index 941f37ddc3b..c3ce83824b0 100644
--- a/modules/drivers/snowflake/test/metabase/driver/snowflake_test.clj
+++ b/modules/drivers/snowflake/test/metabase/driver/snowflake_test.clj
@@ -140,7 +140,6 @@
:additional-options nil
:db "v3_sample-dataset"
:password "passwd"
- :quote-db-name false
:let-user-control-scheduling false
:private-key-options "uploaded"
:private-key-source nil
@@ -156,12 +155,9 @@
:user "SNOWFLAKE_DEVELOPER"
:private-key-created-at "2024-01-05T19:10:30.861839Z"
:host ""}]
- (testing "Database name is quoted if quoting is requested (#27856)"
- (are [quote? result] (=? {:db result}
- (let [details (assoc details :quote-db-name quote?)]
- (sql-jdbc.conn/connection-details->spec :snowflake details)))
- true "\"v3_sample-dataset\""
- false "v3_sample-dataset"))
+ (testing "Database name is always quoted in jdbc spec"
+ (is (=? "\"v3_sample-dataset\""
+ (:db (sql-jdbc.conn/connection-details->spec :snowflake details)))))
(testing "Subname is replaced if hostname is provided (#22133)"
(are [use-hostname alternative-host expected-subname] (=? expected-subname
(:subname (let [details (-> details
@@ -258,7 +254,6 @@
(qp.store/with-metadata-provider (mt/id)
(let [schema "INFORMATION_SCHEMA"
details (-> (mt/db)
- (assoc-in [:details :quote-db-name] true)
(assoc-in [:details :additional-options] (format "schema=%s" schema))
:details)]
(sql-jdbc.conn/with-connection-spec-for-testing-connection [spec [:snowflake details]]
diff --git a/src/metabase/driver.clj b/src/metabase/driver.clj
index 18b53e08bda..2339eac2466 100644
--- a/src/metabase/driver.clj
+++ b/src/metabase/driver.clj
@@ -1471,11 +1471,3 @@
(if (table-known-to-not-exist? driver e)
false
(throw e)))))
-
-(defmulti set-database-used!
- "Sets the database to be used on a connection. Called prior to query execution for drivers that support USE DATABASE like commands."
- {:added "0.56.0" :arglists '([driver conn db])}
- dispatch-on-initialized-driver
- :hierarchy #'hierarchy)
-
-(defmethod set-database-used! ::driver [_driver _conn _db] nil)
diff --git a/src/metabase/driver/sql_jdbc/execute.clj b/src/metabase/driver/sql_jdbc/execute.clj
index 57e5494177b..9b2af7dc513 100644
--- a/src/metabase/driver/sql_jdbc/execute.clj
+++ b/src/metabase/driver/sql_jdbc/execute.clj
@@ -371,8 +371,7 @@
(u/id db-or-id-or-spec) db-or-id-or-spec
;; otherwise it's a spec and we can't get the db
:else nil)]
- (set-role-if-supported! driver conn db)
- (driver/set-database-used! driver conn db))
+ (set-role-if-supported! driver conn db))
(when-not (recursive-connection?)
(log/tracef "Setting default connection options with options %s" (pr-str options))
(set-best-transaction-level! driver conn)