Skip to content

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

GHY-3856: Surface the underlying sync failure cause in the UI, not just "Sync failed" (#83042)

Eric Normand76934b3c8429
diff --git a/frontend/src/metabase-types/api/database.ts b/frontend/src/metabase-types/api/database.ts
index 37cd55bbde8..1a87f2d973a 100644
--- a/frontend/src/metabase-types/api/database.ts
+++ b/frontend/src/metabase-types/api/database.ts
@@ -69,6 +69,7 @@ export interface Database extends DatabaseData {
native_permissions: "write" | "none";
transforms_permissions?: "write" | "none";
initial_sync_status: InitialSyncStatus;
+ initial_sync_error?: string | null;
description?: string;
caveats?: string;
points_of_interest?: string;
diff --git a/frontend/src/metabase/admin/databases/components/DatabaseConnectionInfoSection/DatabaseConnectionInfoSection.tsx b/frontend/src/metabase/admin/databases/components/DatabaseConnectionInfoSection/DatabaseConnectionInfoSection.tsx
index 5491750b6dd..c97ff3eaa16 100644
--- a/frontend/src/metabase/admin/databases/components/DatabaseConnectionInfoSection/DatabaseConnectionInfoSection.tsx
+++ b/frontend/src/metabase/admin/databases/components/DatabaseConnectionInfoSection/DatabaseConnectionInfoSection.tsx
@@ -15,8 +15,8 @@ import {
} from "metabase/common/utils/database";
import { useDispatch } from "metabase/redux";
import { useNavigate } from "metabase/router";
-import { Button, Flex, Tooltip } from "metabase/ui";
-import { isSyncCompleted } from "metabase/utils/syncing";
+import { Alert, Button, Flex, Icon, Tooltip } from "metabase/ui";
+import { isSyncAborted, isSyncCompleted } from "metabase/utils/syncing";
import type { Database } from "metabase-types/api";
import { DatabaseConnectionHealthInfo } from "../DatabaseConnectionHealthInfo";
@@ -78,6 +78,18 @@ export const DatabaseConnectionInfoSection = ({
<DatabaseInfoSectionDivider condensed />
+ {isSyncAborted(database) && database.initial_sync_error && (
+ <Alert
+ size="compact"
+ color="error"
+ icon={<Icon name="warning" />}
+ title={t`Sync failed`}
+ mb="md"
+ >
+ {database.initial_sync_error}
+ </Alert>
+ )}
+
{!database.is_attached_dwh && (
<Flex gap="sm" wrap="wrap">
{!isSynced && <Button disabled>{t`Syncing database…`}</Button>}
diff --git a/frontend/src/metabase/admin/databases/components/DatabaseConnectionInfoSection/DatabaseConnectionInfoSection.unit.spec.tsx b/frontend/src/metabase/admin/databases/components/DatabaseConnectionInfoSection/DatabaseConnectionInfoSection.unit.spec.tsx
index a6cc0311b44..be390c969cc 100644
--- a/frontend/src/metabase/admin/databases/components/DatabaseConnectionInfoSection/DatabaseConnectionInfoSection.unit.spec.tsx
+++ b/frontend/src/metabase/admin/databases/components/DatabaseConnectionInfoSection/DatabaseConnectionInfoSection.unit.spec.tsx
@@ -178,6 +178,33 @@ describe("DatabaseConnectionInfoSection", () => {
});
});
+ describe("sync failure cause", () => {
+ it("shows why the initial sync aborted (GHY-3856)", () => {
+ setup({
+ database: createMockDatabase({
+ initial_sync_status: "aborted",
+ initial_sync_error:
+ "User is not authorized to perform: glue:GetDatabases",
+ }),
+ });
+
+ expect(screen.getByText("Sync failed")).toBeInTheDocument();
+ expect(
+ screen.getByText(
+ "User is not authorized to perform: glue:GetDatabases",
+ ),
+ ).toBeInTheDocument();
+ });
+
+ it("isn't shown when the database has no recorded sync error", () => {
+ setup({
+ database: createMockDatabase({ initial_sync_status: "aborted" }),
+ });
+
+ expect(screen.queryByText("Sync failed")).not.toBeInTheDocument();
+ });
+ });
+
describe("Cloud-attached databases", () => {
it("should not show the sync actions", async () => {
setup({ database: createMockDatabase({ is_attached_dwh: true }) });
diff --git a/frontend/src/metabase/status/components/DatabaseStatusLarge/DatabaseStatusLarge.tsx b/frontend/src/metabase/status/components/DatabaseStatusLarge/DatabaseStatusLarge.tsx
index a0fe051fc08..9be406c1aa2 100644
--- a/frontend/src/metabase/status/components/DatabaseStatusLarge/DatabaseStatusLarge.tsx
+++ b/frontend/src/metabase/status/components/DatabaseStatusLarge/DatabaseStatusLarge.tsx
@@ -56,7 +56,9 @@ const getDescription = (database: Database): string => {
const isError = isSyncAborted(database);
if (isError) {
- return t`Sync failed`;
+ return database.initial_sync_error
+ ? t`Sync failed: ${database.initial_sync_error}`
+ : t`Sync failed`;
} else if (isDone) {
return t`Syncing completed`;
} else {
diff --git a/frontend/src/metabase/status/components/DatabaseStatusLarge/DatabaseStatusLarge.unit.spec.tsx b/frontend/src/metabase/status/components/DatabaseStatusLarge/DatabaseStatusLarge.unit.spec.tsx
index 314a2077c4c..385e7f4bbfd 100644
--- a/frontend/src/metabase/status/components/DatabaseStatusLarge/DatabaseStatusLarge.unit.spec.tsx
+++ b/frontend/src/metabase/status/components/DatabaseStatusLarge/DatabaseStatusLarge.unit.spec.tsx
@@ -6,10 +6,13 @@ import { DatabaseStatusLarge } from "./DatabaseStatusLarge";
interface SetupOpts {
databases: Database[];
+ isActive?: boolean;
}
-const setup = ({ databases }: SetupOpts) => {
- renderWithProviders(<DatabaseStatusLarge databases={databases} />);
+const setup = ({ databases, isActive }: SetupOpts) => {
+ renderWithProviders(
+ <DatabaseStatusLarge databases={databases} isActive={isActive} />,
+ );
};
describe("DatabaseStatusLarge", () => {
@@ -30,4 +33,37 @@ describe("DatabaseStatusLarge", () => {
expect(screen.getByText("Syncing…")).toBeInTheDocument();
expect(screen.getByText("Syncing tables…")).toBeInTheDocument();
});
+
+ // A card that mounts already aborted renders only while the panel is active
+ it("should render the cause of an aborted sync (GHY-3856)", () => {
+ setup({
+ isActive: true,
+ databases: [
+ createMockDatabase({
+ id: 1,
+ initial_sync_status: "aborted",
+ initial_sync_error:
+ "User is not authorized to perform: glue:GetDatabases",
+ }),
+ ],
+ });
+
+ expect(screen.getByText("Error syncing")).toBeInTheDocument();
+ expect(
+ screen.getByText(
+ "Sync failed: User is not authorized to perform: glue:GetDatabases",
+ ),
+ ).toBeInTheDocument();
+ });
+
+ it("should render a generic message for an aborted sync with no recorded cause", () => {
+ setup({
+ isActive: true,
+ databases: [
+ createMockDatabase({ id: 1, initial_sync_status: "aborted" }),
+ ],
+ });
+
+ expect(screen.getByText("Sync failed")).toBeInTheDocument();
+ });
});
diff --git a/resources/migrations/065/20260925_database_initial_sync_error.yaml b/resources/migrations/065/20260925_database_initial_sync_error.yaml
new file mode 100644
index 00000000000..b8025c64b89
--- /dev/null
+++ b/resources/migrations/065/20260925_database_initial_sync_error.yaml
@@ -0,0 +1,21 @@
+## Keeps the reason an initial sync aborted, so admins can diagnose it without reading the server logs.
+
+databaseChangeLog:
+ - changeSet:
+ id: v65.2026-09-25T00:00:00
+ author: ericnormand
+ comment: Add initial_sync_error to metabase_database
+ changes:
+ - addColumn:
+ tableName: metabase_database
+ columns:
+ - column:
+ name: initial_sync_error
+ type: ${text.type}
+ remarks: Message of the error that aborted the initial sync; null unless initial_sync_status is aborted
+ constraints:
+ nullable: true
+ rollback:
+ - dropColumn:
+ tableName: metabase_database
+ columnName: initial_sync_error
diff --git a/src/metabase/sync/sync_metadata.clj b/src/metabase/sync/sync_metadata.clj
index 30d9a165148..1eabacb2d7a 100644
--- a/src/metabase/sync/sync_metadata.clj
+++ b/src/metabase/sync/sync_metadata.clj
@@ -73,25 +73,25 @@
(tracing/with-span :sync "sync.metadata.fetch-metadata" {:db/id (:id database)}
(fetch-metadata/db-metadata database))
(catch Throwable e
- (sync-util/set-initial-database-sync-aborted! database)
+ (sync-util/set-initial-database-sync-aborted! database e)
(throw e)))
;; holder the `sync-tables` step fills with any `_metabase_metadata` table(s) as it streams
;; `:tables`, so the later `sync-metabase-metadata` step doesn't have to re-scan them
db-metadata (assoc db-metadata :metabase-metadata-tables (volatile! []))
steps (make-sync-steps db-metadata)
essential-steps (into #{} (comp (filter :essential?) (map :step-name)) steps)
- results (sync-util/run-sync-operation "sync" database steps)]
- (cond
- (some sync-util/abandon-sync? (map second (:steps results)))
- (sync-util/set-initial-database-sync-aborted! database)
-
- (some (fn [[step-name step-results]]
- (and (contains? essential-steps step-name)
- (sync-util/step-failed? step-results)))
- (:steps results))
- (sync-util/set-initial-database-sync-aborted! database)
-
- :else
+ results (sync-util/run-sync-operation "sync" database steps)
+ abort-cause (or (some (fn [[_ step-results]]
+ (when (sync-util/abandon-sync? step-results)
+ (:throwable step-results)))
+ (:steps results))
+ (some (fn [[step-name step-results]]
+ (when (and (contains? essential-steps step-name)
+ (sync-util/step-failed? step-results))
+ (:throwable step-results)))
+ (:steps results)))]
+ (if abort-cause
+ (sync-util/set-initial-database-sync-aborted! database abort-cause)
(sync-util/set-initial-database-sync-complete! database))
results))
diff --git a/src/metabase/sync/sync_metadata/fields.clj b/src/metabase/sync/sync_metadata/fields.clj
index d2e3b445f31..c3816e7e321 100644
--- a/src/metabase/sync/sync_metadata/fields.clj
+++ b/src/metabase/sync/sync_metadata/fields.clj
@@ -108,9 +108,11 @@
[(not= (:schema item) target-schema)
(not= (:name item) target-name)]))
-(mu/defn sync-fields! :- [:map
- [:updated-fields ms/IntGreaterThanOrEqualToZero]
- [:total-fields ms/IntGreaterThanOrEqualToZero]]
+(mu/defn sync-fields! :- [:or
+ [:map
+ [:updated-fields ms/IntGreaterThanOrEqualToZero]
+ [:total-fields ms/IntGreaterThanOrEqualToZero]]
+ (ms/InstanceOfClass Throwable)]
"Sync the Fields in the Metabase application database for all the Tables in a `database`.
`fields-metadata` is a *reducible* of per-column metadata, ordered so a table's columns are contiguous. It is passed
diff --git a/src/metabase/sync/util.clj b/src/metabase/sync/util.clj
index 9c05ce2a9eb..05f87e1f74b 100644
--- a/src/metabase/sync/util.clj
+++ b/src/metabase/sync/util.clj
@@ -452,13 +452,16 @@
"Marks initial sync as complete for this database so that this is reflected in the UI, if not already set"
[database]
(when (not= (:initial_sync_status database) "complete")
- (sync.db/update-database! (u/the-id database) {:initial_sync_status "complete"})))
+ (sync.db/update-database! (u/the-id database) {:initial_sync_status "complete"
+ :initial_sync_error nil})))
(defn set-initial-database-sync-aborted!
- "Marks initial sync as aborted for this database so that an error can be displayed on the UI"
- [database]
+ "Marks initial sync as aborted for this database, recording the message of `error` (the Throwable that aborted it)
+ so that the cause can be displayed on the UI"
+ [database error]
(when (not= (:initial_sync_status database) "complete")
- (sync.db/update-database! (u/the-id database) {:initial_sync_status "aborted"})))
+ (sync.db/update-database! (u/the-id database) {:initial_sync_status "aborted"
+ :initial_sync_error (ex-message error)})))
;;; +----------------------------------------------------------------------------------------------------------------+
;;; | OTHER SYNC UTILITY FUNCTIONS |
diff --git a/src/metabase/util/jvm.clj b/src/metabase/util/jvm.clj
index 1092ff4b971..f446ad4b980 100644
--- a/src/metabase/util/jvm.clj
+++ b/src/metabase/util/jvm.clj
@@ -86,9 +86,8 @@
(concat
(map str frames-after-last-mb)
;; add a little arrow to the frame so it stands out more
- (cons
- (some->> last-mb-frame (str "--> "))
- frames-before-last-mb)))))})
+ (when last-mb-frame
+ (cons (str "--> " last-mb-frame) frames-before-last-mb))))))})
(defn ip-address?
"Whether string `s` is a valid IP (v4 or v6) address."
diff --git a/src/metabase/warehouses/models/database.clj b/src/metabase/warehouses/models/database.clj
index 1e6cfeda6b5..55a61ac423b 100644
--- a/src/metabase/warehouses/models/database.clj
+++ b/src/metabase/warehouses/models/database.clj
@@ -643,8 +643,8 @@
driver.u/default-sensitive-fields))
(methodical/defmethod mi/to-json :model/Database
- "When encoding a Database as JSON remove the `details`, `write_data_details`, and `admin_details` for any User
- without write perms for the DB. Users with write perms can see the details but remove anything resembling a
+ "When encoding a Database as JSON remove the `details`, `write_data_details`, `admin_details`, and
+ `initial_sync_error` for any User without write perms for the DB. Users with write perms can see the details but remove anything resembling a
password. No one gets to see this in an API response!
Also remove settings that the User doesn't have read perms for."
@@ -657,7 +657,7 @@
(next-method
(let [db (if (not (mi/can-write? db))
(do (log/debug "Fully redacting database details during json encoding.")
- (dissoc db :details :write_data_details :admin_details))
+ (dissoc db :details :write_data_details :admin_details :initial_sync_error))
(do (log/debug "Redacting sensitive fields within database details during json encoding.")
(-> db
(secret/to-json-hydrate-redacted-secrets)
@@ -698,7 +698,9 @@
:default_schema :metadata_sync_schedule :name :points_of_interest :provider_name :refingerprint :settings :timezone :uploads_enabled
:uploads_schema_name :uploads_table_prefix]
:skip [;; deprecated field
- :cache_ttl]
+ :cache_ttl
+ ;; describes a sync on the source instance, and may name its connection details
+ :initial_sync_error]
:transform {:created_at (serdes/date)
:details details-transform
:write_data_details details-transform
diff --git a/src/metabase/warehouses/schema.clj b/src/metabase/warehouses/schema.clj
index 63ca5f8ed1d..98704a7da9d 100644
--- a/src/metabase/warehouses/schema.clj
+++ b/src/metabase/warehouses/schema.clj
@@ -76,6 +76,7 @@
[:refingerprint {:optional true} [:maybe :boolean]]
[:cache_ttl {:optional true} [:maybe :int]]
[:initial_sync_status {:optional true} [:maybe [:or :keyword :string]]]
+ [:initial_sync_error {:optional true} [:maybe :string]]
[:creator_id {:optional true} [:maybe ::lib.schema.id/user]]
[:settings {:optional true} [:maybe ::database.settings]]
[:dbms_version {:optional true} [:maybe ::database.dbms-version]]
diff --git a/test/metabase/sync/sync_metadata_test.clj b/test/metabase/sync/sync_metadata_test.clj
index 8cd778e0d1c..ffce5c14137 100644
--- a/test/metabase/sync/sync_metadata_test.clj
+++ b/test/metabase/sync/sync_metadata_test.clj
@@ -46,7 +46,10 @@
(is (thrown? Throwable
(#'sync-metadata/sync-db-metadata!* db))))
(is (= "aborted"
- (t2/select-one-fn :initial_sync_status :model/Database (:id db))))))))
+ (t2/select-one-fn :initial_sync_status :model/Database (:id db))))
+ (testing "and the message of the error is recorded as the cause (GHY-3856)"
+ (is (= "boom"
+ (t2/select-one-fn :initial_sync_error :model/Database (:id db)))))))))
(deftest field-sync-failure-aborts-initial-sync-test
(testing "When an essential sync step fails non-transiently mid-initial-sync (e.g. MariaDB <10.2 whose"
@@ -61,4 +64,16 @@
(.setStackTrace (into-array StackTraceElement [])))))]
(#'sync-metadata/sync-db-metadata!* (t2/select-one :model/Database :id (mt/id))))
(is (= "aborted"
- (t2/select-one-fn :initial_sync_status :model/Database (mt/id)))))))))
+ (t2/select-one-fn :initial_sync_status :model/Database (mt/id))))
+ (testing "and the step's own error message is recorded as the cause, not a schema-validation error (GHY-3856)"
+ (is (= "Unknown column 'generation_expression' in 'field list'"
+ (t2/select-one-fn :initial_sync_error :model/Database (mt/id))))))))))
+
+(deftest completed-sync-clears-initial-sync-error-test
+ (testing "GHY-3856: a sync that completes after an aborted one clears the recorded cause, so the UI shows no stale error"
+ (mt/with-temp-copy-of-db
+ (t2/update! :model/Database (mt/id) {:initial_sync_status "aborted"
+ :initial_sync_error "boom"})
+ (#'sync-metadata/sync-db-metadata!* (t2/select-one :model/Database :id (mt/id)))
+ (is (= {:initial_sync_status "complete", :initial_sync_error nil}
+ (t2/select-one [:model/Database :initial_sync_status :initial_sync_error] :id (mt/id)))))))
diff --git a/test/metabase/sync/util_test.clj b/test/metabase/sync/util_test.clj
index c23255f9d35..38b967f1d87 100644
--- a/test/metabase/sync/util_test.clj
+++ b/test/metabase/sync/util_test.clj
@@ -320,12 +320,15 @@
(mt/with-dynamic-fn-redefs [sync-metadata/make-sync-steps (fn [_]
[(sync-util/create-sync-step
"fake-step"
- (fn [_] (throw (java.net.ConnectException.))))])]
+ (fn [_] (throw (java.net.ConnectException. "Connection refused"))))])]
(sync/sync-database! db)
- (is (= "aborted" (t2/select-one-fn :initial_sync_status :model/Database :id (:id db)))))))
+ (is (= "aborted" (t2/select-one-fn :initial_sync_status :model/Database :id (:id db))))
+ (testing "and the step's error message is recorded as the cause (GHY-3856)"
+ (is (= "Connection refused" (t2/select-one-fn :initial_sync_error :model/Database :id (:id db))))))))
(testing "If `initial-sync-status` is `aborted` for a database, it is set to `complete` the next time sync finishes
without error"
- (let [_ (t2/update! :model/Database (mt/id) {:initial_sync_status "complete"})
+ (let [_ (t2/update! :model/Database (mt/id) {:initial_sync_status "complete"
+ :initial_sync_error nil})
db (t2/select-one :model/Database :id (mt/id))]
(sync/sync-database! db)
(is (= "complete" (t2/select-one-fn :initial_sync_status :model/Database :id (:id db))))))))
diff --git a/test/metabase/util/jvm_test.clj b/test/metabase/util/jvm_test.clj
index 97e1a5bcd10..8c24f2870b3 100644
--- a/test/metabase/util/jvm_test.clj
+++ b/test/metabase/util/jvm_test.clj
@@ -6,6 +6,8 @@
[clojure.test.check.properties :as prop]
[metabase.util :as u]))
+(set! *warn-on-reflection* true)
+
(deftest ^:parallel host-up?-test
(testing "host-up?"
(are [s expected] (= expected
@@ -16,6 +18,22 @@
(is (= false
(u/host-port-up? "nosuchhost" 8005)))))
+(defn- exception-with-frames [& class-names]
+ (doto (Exception. "boom")
+ (.setStackTrace (into-array StackTraceElement
+ (for [class-name class-names]
+ (StackTraceElement. class-name "invoke" "File.java" 1))))))
+
+(deftest ^:parallel filtered-stacktrace-test
+ (testing "a trace with a Metabase frame keeps the frames before it, marks it, and drops non-Metabase frames after it"
+ (is (= ["org.h2.Driver.invoke(File.java:1)"
+ "--> sync.fields.invoke(File.java:1)"]
+ (u/filtered-stacktrace (exception-with-frames "org.h2.Driver" "metabase.sync.fields" "clojure.core")))))
+ (testing "a trace with no Metabase frame has no nil entry, so it satisfies [:sequential :string] (GHY-3856)"
+ (is (= [] (u/filtered-stacktrace (exception-with-frames))))
+ (is (= ["org.h2.Driver.invoke(File.java:1)"]
+ (u/filtered-stacktrace (exception-with-frames "org.h2.Driver"))))))
+
(deftest ^:parallel ip-address?-test
(are [x expected] (= expected
(u/ip-address? x))
diff --git a/test/metabase/warehouse_schema_rest/api/table_test.clj b/test/metabase/warehouse_schema_rest/api/table_test.clj
index f7b695ae656..cbe5ef2895b 100644
--- a/test/metabase/warehouse_schema_rest/api/table_test.clj
+++ b/test/metabase/warehouse_schema_rest/api/table_test.clj
@@ -470,7 +470,7 @@
(is (= (merge
(-> (table-defaults)
(dissoc :segments :field_values :metrics :measures :updated_at)
- (update :db merge (select-keys (mt/db) [:details :write_data_details :admin_details])))
+ (update :db merge (select-keys (mt/db) [:details :write_data_details :admin_details :initial_sync_error])))
(t2/hydrate (t2/select-one [:model/Table :id :schema :name :created_at :initial_sync_status] :id (u/the-id table))
:pk_field :collection)
{:description "What a nice table!"
diff --git a/test/metabase/warehouses_rest/api_test.clj b/test/metabase/warehouses_rest/api_test.clj
index 10a456963a6..9d6ebe5bc1b 100644
--- a/test/metabase/warehouses_rest/api_test.clj
+++ b/test/metabase/warehouses_rest/api_test.clj
@@ -26,6 +26,7 @@
[metabase.settings.core :as setting :refer [defsetting]]
[metabase.sync.analyze :as analyze]
[metabase.sync.core :as sync]
+ [metabase.sync.fetch-metadata :as fetch-metadata]
[metabase.sync.field-values :as sync.field-values]
[metabase.sync.sync-metadata :as sync-metadata]
[metabase.sync.task.sync-databases :as task.sync-databases]
@@ -163,7 +164,7 @@
(testing "DB details visibility"
(testing "Regular users should not see DB details"
(is (= (-> (db-details)
- (dissoc :details :write_data_details :admin_details :schedules))
+ (dissoc :details :write_data_details :admin_details :initial_sync_error :schedules))
(-> (mt/user-http-request :rasta :get 200 (format "database/%d" (mt/id)))
(dissoc :schedules :can_upload)))))
(testing "Superusers should see DB details"
@@ -397,10 +398,13 @@
(testing "create a db with default scan options"
(with-db-scheduler-setup!
(with-test-driver-available!
- (let [resp (mt/user-http-request :crowberto :post 200 "database"
- {:name (mt/random-name)
- :engine (u/qualified-name ::test-driver)
- :details {:db "my_db"}})
+ ;; The create event starts a sync on another thread. If it gets as far as the dbms-version step before the
+ ;; row is read back, `dbms_version` is no longer its default, so keep it from running.
+ (let [resp (mt/with-dynamic-fn-redefs [quick-task/submit-task! (constantly nil)]
+ (mt/user-http-request :crowberto :post 200 "database"
+ {:name (mt/random-name)
+ :engine (u/qualified-name ::test-driver)
+ :details {:db "my_db"}}))
db (t2/select-one :model/Database (:id resp))]
(is (malli= [:merge
(into [:map] (m/map-vals (fn [v] [:= {} v]) (mt/object-defaults :model/Database)))
@@ -873,7 +877,7 @@
(deftest ^:parallel fetch-database-metadata-test
(testing "GET /api/database/:id/metadata"
- (is (= (merge (dissoc (db-details) :details :write_data_details :admin_details :router_user_attribute)
+ (is (= (merge (dissoc (db-details) :details :write_data_details :admin_details :initial_sync_error :router_user_attribute)
{:engine "h2"
:name "test-data (h2)"
:features (map u/qualified-name (driver.u/features :h2 (mt/db)))
@@ -1200,7 +1204,7 @@
(testing "Database details/settings *should not* come back for Rasta since she's not a superuser"
(let [expected-keys (-> #{:features :native_permissions :can_upload :router_user_attribute :transforms_permissions}
(into (keys (t2/select-one :model/Database :id (mt/id))))
- (disj :details :write_data_details :admin_details))]
+ (disj :details :write_data_details :admin_details :initial_sync_error))]
(doseq [db (:data (mt/user-http-request :rasta :get 200 "database"))]
(testing (format "Database %s %d %s" (:engine db) (u/the-id db) (pr-str (:name db)))
(is (= expected-keys
@@ -1813,6 +1817,41 @@
(is (pos? (t2/count :model/Table :db_id db-id))
"Sync-now must populate tables even when disable-auto-sync is on")))))))
+(deftest sync-schema-failure-cause-is-readable-through-api-test
+ (testing (str "GHY-3856: when Sync-now fails because the metadata fetch throws (e.g. Athena credentials lacking "
+ "glue:GetDatabases), GET /api/database/:id tells the admin why, not only that initial sync aborted")
+ (let [details (:details (mt/db))
+ cause "User: arn:aws:iam::123:user/x is not authorized to perform: glue:GetDatabases"]
+ (mt/with-temporary-setting-values [disable-auto-sync true]
+ (mt/with-temp [:model/Database {db-id :id} {:engine "h2"
+ :details details
+ :initial_sync_status "incomplete"}]
+ (mt/with-dynamic-fn-redefs [fetch-metadata/db-metadata (fn [_] (throw (ex-info cause {})))
+ quick-task/submit-task! (fn [f]
+ (binding [driver.settings/*allow-testing-h2-connections* true]
+ (try (f) (catch Throwable _))))]
+ (mt/user-http-request :crowberto :post 200 (format "database/%d/sync_schema" db-id)))
+ (let [response (mt/user-http-request :crowberto :get 200 (format "database/%d" db-id))]
+ (is (= "aborted" (:initial_sync_status response)))
+ (testing "the admin page reads the failure cause from the database"
+ (is (str/includes? (str (:initial_sync_error response)) "glue:GetDatabases"))))
+ (testing "GET /api/database, which the sync status panel reads, carries the cause too"
+ (is (str/includes? (->> (mt/user-http-request :crowberto :get 200 "database")
+ :data
+ (some #(when (= db-id (:id %)) %))
+ :initial_sync_error
+ str)
+ "glue:GetDatabases"))))))))
+
+(deftest initial-sync-error-hidden-without-write-perms-test
+ (testing "GHY-3856: the sync failure cause can name connection details, so only users who can edit the database see it"
+ (mt/with-temp [:model/Database {db-id :id} {:initial_sync_status "aborted"
+ :initial_sync_error "boom"}]
+ (is (= "boom"
+ (:initial_sync_error (mt/user-http-request :crowberto :get 200 (format "database/%d" db-id)))))
+ (is (not (contains? (mt/user-http-request :rasta :get 200 (format "database/%d" db-id))
+ :initial_sync_error))))))
+
(deftest sync-schema-does-not-refetch-database-per-field-test
(testing (str "GHY-3289: POST /api/database/:id/sync_schema must not read the Database row from the app DB once "
"per column. The Database does not change during a sync, so the number of reads should not grow "