Skip to content

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

move do-with-resilient-connection to wrapping describe-database function (#62062)

Nicola Mometto7c0714c7e6ca
diff --git a/docs/developers-guide/driver-changelog.md b/docs/developers-guide/driver-changelog.md
index d71afb24b53..1afa5ec0d50 100644
--- a/docs/developers-guide/driver-changelog.md
+++ b/docs/developers-guide/driver-changelog.md
@@ -17,6 +17,13 @@ title: Driver interface changelog
metabase.driver/connection-details, metabase.driver/table-exists?, metabase.driver.sql/normalize-name,
metabase.driver.sql/default-schema, and metabase.driver.sql/find-table to implement sql transforms.
+## Metabase 0.56.3
+
+- Added the driver multi-method `driver/describe-database*` that drivers should now implement instead of `driver/describe-database`.
+ This provides automatic resilient connection handling for better error recovery when database connections are closed
+ during metadata sync operations. Existing drivers implementing `describe-database` will continue to work but should
+ migrate to `describe-database*` to benefit from the resilient connection handling.
+
## Metabase 0.56.0
- Add the testing multi-method `tx/track-dataset` for shared cloud dbs to track loaded datasets for more efficient sharing.
diff --git a/enterprise/backend/test/metabase_enterprise/impersonation/driver_test.clj b/enterprise/backend/test/metabase_enterprise/impersonation/driver_test.clj
index 7f3ed7e23b4..e7d4eb15f4d 100644
--- a/enterprise/backend/test/metabase_enterprise/impersonation/driver_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/impersonation/driver_test.clj
@@ -684,10 +684,9 @@
(sync/sync-database! database {:scan :schema})
- (let [tables-set #(->> (driver/do-with-resilient-connection
+ (let [tables-set #(->> (driver/describe-database
driver/*driver*
- (t2/select-one :model/Database (mt/id))
- driver/describe-database)
+ (t2/select-one :model/Database (mt/id)))
:tables
set)
default-table-set (tables-set)
diff --git a/modules/drivers/athena/src/metabase/driver/athena.clj b/modules/drivers/athena/src/metabase/driver/athena.clj
index aefdac838aa..5e35c8747f3 100644
--- a/modules/drivers/athena/src/metabase/driver/athena.clj
+++ b/modules/drivers/athena/src/metabase/driver/athena.clj
@@ -509,7 +509,7 @@
; #{"database_name"})
; If we want to limit the initial connection to a specific database/schema, I think we'd have to do that here...
-(defmethod driver/describe-database :athena
+(defmethod driver/describe-database* :athena
[driver {details :details, :as database}]
(sql-jdbc.execute/do-with-connection-with-options
driver
diff --git a/modules/drivers/bigquery-cloud-sdk/src/metabase/driver/bigquery_cloud_sdk.clj b/modules/drivers/bigquery-cloud-sdk/src/metabase/driver/bigquery_cloud_sdk.clj
index e263b4623b1..7ad74b6a071 100644
--- a/modules/drivers/bigquery-cloud-sdk/src/metabase/driver/bigquery_cloud_sdk.clj
+++ b/modules/drivers/bigquery-cloud-sdk/src/metabase/driver/bigquery_cloud_sdk.clj
@@ -195,7 +195,7 @@
(->> (list-datasets (:details database) :logging-schema-exclusions? true)
(eduction (mapcat (fn [dataset-id] (eduction (map #(table-info dataset-id %)) (query-dataset dataset-id))))))))
-(defmethod driver/describe-database :bigquery-cloud-sdk
+(defmethod driver/describe-database* :bigquery-cloud-sdk
[driver database]
{:tables (into #{} (describe-database-tables driver database))})
diff --git a/modules/drivers/clickhouse/src/metabase/driver/clickhouse_introspection.clj b/modules/drivers/clickhouse/src/metabase/driver/clickhouse_introspection.clj
index 07cb403f445..e2ef5a83f91 100644
--- a/modules/drivers/clickhouse/src/metabase/driver/clickhouse_introspection.clj
+++ b/modules/drivers/clickhouse/src/metabase/driver/clickhouse_introspection.clj
@@ -149,7 +149,7 @@
(filter not-inner-mv-table?)
(tables-set)))
-(defmethod driver/describe-database :clickhouse
+(defmethod driver/describe-database* :clickhouse
[driver {{:keys [scan-all-databases]}
:details :as db}]
{:tables
diff --git a/modules/drivers/databricks/src/metabase/driver/databricks.clj b/modules/drivers/databricks/src/metabase/driver/databricks.clj
index f64ae971743..c317b9b6fc4 100644
--- a/modules/drivers/databricks/src/metabase/driver/databricks.clj
+++ b/modules/drivers/databricks/src/metabase/driver/databricks.clj
@@ -120,7 +120,7 @@
[:not [:startswith :t.table_catalog [:inline "__databricks"]]]]}
:dialect (sql.qp/quote-style driver)))
-(defmethod driver/describe-database :databricks
+(defmethod driver/describe-database* :databricks
[driver database]
(try
{:tables
diff --git a/modules/drivers/druid/src/metabase/driver/druid.clj b/modules/drivers/druid/src/metabase/driver/druid.clj
index 9e0efade2ab..3df5e88d0a0 100644
--- a/modules/drivers/druid/src/metabase/driver/druid.clj
+++ b/modules/drivers/druid/src/metabase/driver/druid.clj
@@ -39,7 +39,7 @@
[_ database]
(druid.sync/dbms-version database))
-(defmethod driver/describe-database :druid
+(defmethod driver/describe-database* :druid
[_ database]
(druid.sync/describe-database database))
diff --git a/modules/drivers/mongo/src/metabase/driver/mongo.clj b/modules/drivers/mongo/src/metabase/driver/mongo.clj
index 4f14c26334d..fe0ddcae274 100644
--- a/modules/drivers/mongo/src/metabase/driver/mongo.clj
+++ b/modules/drivers/mongo/src/metabase/driver/mongo.clj
@@ -110,7 +110,7 @@
{:version (get build-info "version")
:semantic-version sanitized-version-array})))
-(defmethod driver/describe-database :mongo
+(defmethod driver/describe-database* :mongo
[_ database]
(mongo.connection/with-mongo-database [^MongoDatabase db database]
{:tables (set (for [collection (mongo.util/list-collection-names db)
diff --git a/modules/drivers/oracle/test/metabase/test/data/oracle.clj b/modules/drivers/oracle/test/metabase/test/data/oracle.clj
index 53caa600580..f5893d557f5 100644
--- a/modules/drivers/oracle/test/metabase/test/data/oracle.clj
+++ b/modules/drivers/oracle/test/metabase/test/data/oracle.clj
@@ -305,10 +305,10 @@
(defonce ^:private ^{:arglists '([driver database])}
original-describe-database
- (get-method driver/describe-database :oracle))
+ (get-method driver/describe-database* :oracle))
;; For test databases, only sync the tables that are qualified by the db name
-(defmethod driver/describe-database :oracle
+(defmethod driver/describe-database* :oracle
[driver database]
(if *override-describe-database-to-filter-by-db-name?*
(let [r (original-describe-database driver database)
diff --git a/modules/drivers/presto-jdbc/src/metabase/driver/presto_jdbc.clj b/modules/drivers/presto-jdbc/src/metabase/driver/presto_jdbc.clj
index 607be94db78..3dfa6abb911 100644
--- a/modules/drivers/presto-jdbc/src/metabase/driver/presto_jdbc.clj
+++ b/modules/drivers/presto-jdbc/src/metabase/driver/presto_jdbc.clj
@@ -595,7 +595,7 @@
(describe-schema driver conn catalog schema))))
(jdbc/reducible-query {:connection conn} sql))))
-(defmethod driver/describe-database :presto-jdbc
+(defmethod driver/describe-database* :presto-jdbc
[driver {{:keys [catalog schema] :as _details} :details :as database}]
(sql-jdbc.execute/do-with-connection-with-options
driver
diff --git a/modules/drivers/redshift/src/metabase/driver/redshift.clj b/modules/drivers/redshift/src/metabase/driver/redshift.clj
index 17cb772bb4a..bdbfecc8812 100644
--- a/modules/drivers/redshift/src/metabase/driver/redshift.clj
+++ b/modules/drivers/redshift/src/metabase/driver/redshift.clj
@@ -113,7 +113,7 @@
(map #(dissoc % :type)))
(sql-jdbc.execute/reducible-query database get-tables-sql))))
-(defmethod driver/describe-database :redshift
+(defmethod driver/describe-database* :redshift
[driver database]
;; TODO: change this to return a reducible so we don't have to hold 100k tables in memory in a set like this
;;
diff --git a/modules/drivers/redshift/test/metabase/test/data/redshift.clj b/modules/drivers/redshift/test/metabase/test/data/redshift.clj
index d05aabd17b6..79a6cd3285b 100644
--- a/modules/drivers/redshift/test/metabase/test/data/redshift.clj
+++ b/modules/drivers/redshift/test/metabase/test/data/redshift.clj
@@ -246,10 +246,10 @@
(defonce ^:private ^{:arglists '([driver database])}
original-describe-database
- (get-method driver/describe-database :redshift))
+ (get-method driver/describe-database* :redshift))
;; For test databases, only sync the tables that are qualified by the db name
-(defmethod driver/describe-database :redshift
+(defmethod driver/describe-database* :redshift
[driver database]
(if *override-describe-database-to-filter-by-db-name?*
(let [r (original-describe-database driver database)
diff --git a/modules/drivers/snowflake/src/metabase/driver/snowflake.clj b/modules/drivers/snowflake/src/metabase/driver/snowflake.clj
index 03bcd5fc65d..3ec81ca3a97 100644
--- a/modules/drivers/snowflake/src/metabase/driver/snowflake.clj
+++ b/modules/drivers/snowflake/src/metabase/driver/snowflake.clj
@@ -593,7 +593,7 @@
:from [[table-identifier]]}]
(sql-jdbc/query driver database query))))
-(defmethod driver/describe-database :snowflake
+(defmethod driver/describe-database* :snowflake
[driver database]
(let [db-name (db-name database)
excluded-schemas (set (sql-jdbc.sync/excluded-schemas driver))]
diff --git a/modules/drivers/sparksql/src/metabase/driver/sparksql.clj b/modules/drivers/sparksql/src/metabase/driver/sparksql.clj
index 2a9103c8309..bcd64a5bb38 100644
--- a/modules/drivers/sparksql/src/metabase/driver/sparksql.clj
+++ b/modules/drivers/sparksql/src/metabase/driver/sparksql.clj
@@ -107,7 +107,7 @@
(str/replace s #"-" "_")))
;; workaround for SPARK-9686 Spark Thrift server doesn't return correct JDBC metadata
-(defmethod driver/describe-database :sparksql
+(defmethod driver/describe-database* :sparksql
[driver database]
{:tables
(sql-jdbc.execute/do-with-connection-with-options
diff --git a/modules/drivers/starburst/src/metabase/driver/starburst.clj b/modules/drivers/starburst/src/metabase/driver/starburst.clj
index f2b2fa5a2c2..2f025eef164 100644
--- a/modules/drivers/starburst/src/metabase/driver/starburst.clj
+++ b/modules/drivers/starburst/src/metabase/driver/starburst.clj
@@ -469,7 +469,7 @@
(describe-schema driver conn catalog schema))))
(jdbc/reducible-result-set rs {})))))
-(defmethod driver/describe-database :starburst
+(defmethod driver/describe-database* :starburst
[driver {{:keys [catalog schema] :as _details} :details :as database}]
(sql-jdbc.execute/do-with-connection-with-options
driver
diff --git a/modules/drivers/vertica/src/metabase/driver/vertica.clj b/modules/drivers/vertica/src/metabase/driver/vertica.clj
index 696cbfe51ff..fbd7bef075f 100644
--- a/modules/drivers/vertica/src/metabase/driver/vertica.clj
+++ b/modules/drivers/vertica/src/metabase/driver/vertica.clj
@@ -292,9 +292,9 @@
(catch Throwable e
(log/error e "Failed to fetch materialized views for this database"))))
-(defmethod driver/describe-database :vertica
+(defmethod driver/describe-database* :vertica
[driver database]
- (-> ((get-method driver/describe-database :sql-jdbc) driver database)
+ (-> ((get-method driver/describe-database* :sql-jdbc) driver database)
(update :tables set/union (materialized-views database))))
(defmethod driver/db-default-timezone :vertica
diff --git a/modules/drivers/vertica/test/metabase/test/data/vertica.clj b/modules/drivers/vertica/test/metabase/test/data/vertica.clj
index 667044f0a23..ae51996ef50 100644
--- a/modules/drivers/vertica/test/metabase/test/data/vertica.clj
+++ b/modules/drivers/vertica/test/metabase/test/data/vertica.clj
@@ -294,10 +294,10 @@
(defonce ^:private ^{:arglists '([driver database])}
original-describe-database
- (get-method driver/describe-database :vertica))
+ (get-method driver/describe-database* :vertica))
;; For test databases, only sync the tables that are qualified by the db name
-(defmethod driver/describe-database :vertica
+(defmethod driver/describe-database* :vertica
[driver database]
(if *override-describe-database-to-filter-by-db-name?*
(let [r (original-describe-database driver database)
diff --git a/src/metabase/driver.clj b/src/metabase/driver.clj
index b118c42d1c4..df0ca194d70 100644
--- a/src/metabase/driver.clj
+++ b/src/metabase/driver.clj
@@ -255,14 +255,38 @@
[_ _]
nil)
+(defmulti do-with-resilient-connection
+ "Execute function `f` within a context that may recover (on-demand) from connection failures.
+ `f` must be eager."
+ {:added "0.55.9" :arglists '([driver database f])}
+ dispatch-on-initialized-driver
+ :hierarchy #'hierarchy)
+
+(defmethod do-with-resilient-connection
+ ::driver
+ [driver database f] (f driver database))
+
+(defmulti describe-database*
+ "Impl multimethod for [[describe-database]]"
+ {:added "0.56.3" :arglists '([driver database])}
+ dispatch-on-initialized-driver
+ :hierarchy #'hierarchy)
+
(defmulti describe-database
"Return a map containing information that describes all of the tables in a `database`, an instance of the `Database`
model. It is expected that this function will be peformant and avoid draining meaningful resources of the database.
- Results should match the [[metabase.sync.interface/DatabaseMetadata]] schema."
+ Results should match the [[metabase.sync.interface/DatabaseMetadata]] schema.
+ Multimethod for backwards compatibility, but should not be extended directly, should instead implement [[describe-database*]].
+ Default impl invokes [[describe-database*]] wrapped in [[do-with-resilient-connection]]"
{:added "0.32.0" :arglists '([driver database])}
dispatch-on-initialized-driver
:hierarchy #'hierarchy)
+(defmethod describe-database
+ ::driver
+ [driver database]
+ (do-with-resilient-connection driver database describe-database*))
+
(defmulti describe-table
"Return a map containing a single field `:fields` that describes the fields in a `table`. `database` will be an
instance of the `Database` model; and `table`, an instance of the `Table` model. It is expected that this function
@@ -1450,14 +1474,3 @@
:hierarchy #'hierarchy)
(defmethod set-database-used! ::driver [_driver _conn _db] nil)
-
-(defmulti do-with-resilient-connection
- "Execute function `f` within a context that may recover (on-demand) from connection failures.
- `f` must be eager."
- {:added "0.55.9" :arglists '([driver database f])}
- dispatch-on-initialized-driver
- :hierarchy #'hierarchy)
-
-(defmethod do-with-resilient-connection
- ::driver
- [driver database f] (f driver database))
diff --git a/src/metabase/driver/postgres.clj b/src/metabase/driver/postgres.clj
index baa833b86a3..2209432d183 100644
--- a/src/metabase/driver/postgres.clj
+++ b/src/metabase/driver/postgres.clj
@@ -284,7 +284,7 @@
(map #(dissoc % :type)))
(get-tables database syncable-schemas nil))))))))))
-(defmethod driver/describe-database :postgres
+(defmethod driver/describe-database* :postgres
[_driver database]
;; TODO: we should figure out how to sync tables using transducer, this way we don't have to hold 100k tables in
;; memory in a set like this
diff --git a/src/metabase/driver/sql_jdbc.clj b/src/metabase/driver/sql_jdbc.clj
index 899e99d67e9..1b446b97156 100644
--- a/src/metabase/driver/sql_jdbc.clj
+++ b/src/metabase/driver/sql_jdbc.clj
@@ -89,7 +89,7 @@
[driver database]
(sql-jdbc.sync/dbms-version driver (sql-jdbc.conn/db->pooled-connection-spec database)))
-(defmethod driver/describe-database :sql-jdbc
+(defmethod driver/describe-database* :sql-jdbc
[driver database]
(sql-jdbc.sync/describe-database driver database))
diff --git a/src/metabase/sync/api/notify.clj b/src/metabase/sync/api/notify.clj
index 39507e2d838..44b3bba980f 100644
--- a/src/metabase/sync/api/notify.clj
+++ b/src/metabase/sync/api/notify.clj
@@ -50,7 +50,7 @@
(defn- find-and-sync-new-table
[database table-name schema-name]
(let [driver (driver.u/database->driver database)
- {db-tables :tables} (driver/do-with-resilient-connection driver database driver/describe-database)]
+ {db-tables :tables} (driver/describe-database driver database)]
(if-let [table (some (fn [table-in-db]
(when (and (= schema-name (:schema table-in-db))
(= table-name (:name table-in-db)))
diff --git a/src/metabase/sync/fetch_metadata.clj b/src/metabase/sync/fetch_metadata.clj
index e99375d3b1f..1ade3a89c64 100644
--- a/src/metabase/sync/fetch_metadata.clj
+++ b/src/metabase/sync/fetch_metadata.clj
@@ -27,7 +27,7 @@
[database :- i/DatabaseInstance]
(log-if-error "db-metadata"
(let [driver (driver.u/database->driver database)]
- (driver/do-with-resilient-connection driver database driver/describe-database))))
+ (driver/describe-database driver database))))
(defn include-nested-fields-for-table
"Add nested-field-columns for table to set of fields."
diff --git a/test/metabase/driver/sql_jdbc/sync/describe_database_test.clj b/test/metabase/driver/sql_jdbc/sync/describe_database_test.clj
index ff624418516..e2c92146ec9 100644
--- a/test/metabase/driver/sql_jdbc/sync/describe_database_test.clj
+++ b/test/metabase/driver/sql_jdbc/sync/describe_database_test.clj
@@ -53,7 +53,7 @@
[]
(conj (set
(filter
- #(identical? (get-method driver/describe-database :sql-jdbc) (get-method driver/describe-database %))
+ #(identical? (get-method driver/describe-database* :sql-jdbc) (get-method driver/describe-database* %))
(descendants driver/hierarchy :sql-jdbc)))
;; redshift wraps the default implementation, but additionally filters tables according to the database name
:redshift))
@@ -254,8 +254,8 @@
(deftest resilient-to-conn-close?-test
(testing "checking sync is resilient to connections being closed during [have-select-privilege?]"
- (let [jdbc-describe-database #(identical? (get-method driver/describe-database :sql-jdbc)
- (get-method driver/describe-database %))]
+ (let [jdbc-describe-database #(identical? (get-method driver/describe-database* :sql-jdbc)
+ (get-method driver/describe-database* %))]
(mt/test-drivers (mt/normal-driver-select {:+parent :sql-jdbc
:+fns [jdbc-describe-database]
:-features [:table-privileges]})
@@ -269,7 +269,7 @@
(.close conn))
(execute-select-probe-query driver conn query))]
(let [table-names #(->> % :tables (map :name) set)
- all-tables-sans-one (table-names (driver/do-with-resilient-connection driver/*driver* (mt/id) driver/describe-database))]
+ all-tables-sans-one (table-names (driver/describe-database driver/*driver* (mt/id)))]
;; there is at maximum one missing table
(is (>= 1 (count (set/difference all-tables all-tables-sans-one)))))))))))
diff --git a/test/metabase/sync/sync_test.clj b/test/metabase/sync/sync_test.clj
index b7687c06133..cfffd2b05e5 100644
--- a/test/metabase/sync/sync_test.clj
+++ b/test/metabase/sync/sync_test.clj
@@ -74,7 +74,7 @@
:database-position 1}}
:description ""}})
-(defmethod driver/describe-database ::sync-test
+(defmethod driver/describe-database* ::sync-test
[& _]
{:tables (set (for [table (vals (sync-test-tables))]
(dissoc table :fields)))})
@@ -324,7 +324,7 @@
(driver/register! ::sync-database-error-test)
-(defmethod driver/describe-database ::sync-database-error-test
+(defmethod driver/describe-database* ::sync-database-error-test
[_driver _database]
(throw (doto (Exception. "OOPS!")
(.setStackTrace (into-array StackTraceElement [])))))
diff --git a/test/metabase/sync/util_test.clj b/test/metabase/sync/util_test.clj
index 33fb5408492..82ea461ad02 100644
--- a/test/metabase/sync/util_test.clj
+++ b/test/metabase/sync/util_test.clj
@@ -28,7 +28,7 @@
(driver/register! ::concurrent-sync-test, :abstract? true)
-(defmethod driver/describe-database ::concurrent-sync-test [& _]
+(defmethod driver/describe-database* ::concurrent-sync-test [& _]
(swap! calls-to-describe-database inc)
(Thread/sleep 1000)
{:tables #{}})
diff --git a/test/metabase/test/mock/moviedb.clj b/test/metabase/test/mock/moviedb.clj
index f67471f1391..97b3c394e0c 100644
--- a/test/metabase/test/mock/moviedb.clj
+++ b/test/metabase/test/mock/moviedb.clj
@@ -72,7 +72,7 @@
(driver/register! ::moviedb, :abstract? true)
-(defmethod driver/describe-database ::moviedb [_ {:keys [exclude-tables]}]
+(defmethod driver/describe-database* ::moviedb [_ {:keys [exclude-tables]}]
(let [tables (for [table (vals moviedb-tables)
:when (not (contains? exclude-tables (:name table)))]
(select-keys table [:schema :name]))]
diff --git a/test/metabase/test/mock/toucanery.clj b/test/metabase/test/mock/toucanery.clj
index eab954fa8d6..0d25b680366 100644
--- a/test/metabase/test/mock/toucanery.clj
+++ b/test/metabase/test/mock/toucanery.clj
@@ -81,7 +81,7 @@
(defmethod driver/database-supports? [::toucanery :nested-fields] [_driver _feature _database] true)
(defmethod driver/database-supports? [::toucanery :schemas] [_driver _feature _database] false)
-(defmethod driver/describe-database ::toucanery
+(defmethod driver/describe-database* ::toucanery
[_ {:keys [exclude-tables]}]
(let [tables (for [table (vals toucanery-tables)
:when (not (contains? exclude-tables (:name table)))]
diff --git a/test/metabase/warehouses/api_test.clj b/test/metabase/warehouses/api_test.clj
index ce46e403594..0d8e783209b 100644
--- a/test/metabase/warehouses/api_test.clj
+++ b/test/metabase/warehouses/api_test.clj
@@ -67,7 +67,7 @@
[_ _]
"1.0")
-(defmethod driver/describe-database ::test-driver
+(defmethod driver/describe-database* ::test-driver
[_ _]
{:tables []})