Skip to content

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

Fix audit inferring result_metadata (#73169)

Alexander Polyankine8676763ff04
diff --git a/enterprise/backend/src/metabase_enterprise/audit_app/audit.clj b/enterprise/backend/src/metabase_enterprise/audit_app/audit.clj
index 642218014ea..ee290c9acce 100644
--- a/enterprise/backend/src/metabase_enterprise/audit_app/audit.clj
+++ b/enterprise/backend/src/metabase_enterprise/audit_app/audit.clj
@@ -266,43 +266,34 @@
(do
(log/info (str "Loading Analytics Content Complete (" (count (:seen report)) ") entities loaded."))
(audit/last-analytics-checksum! current-checksum))))
- (when-let [{:keys [engine] :as audit-db} (t2/select-one :model/Database :is_audit true)]
- (let [original-engine engine]
- (adjust-audit-db-to-host! audit-db)
- ;; Only sync if we actually changed the engine type
- (when (not= original-engine (mdb/db-type))
- (when-let [updated-audit-db (t2/select-one :model/Database :is_audit true)]
- ;; Sync the audit database to update field metadata to match the host database engine
- ;; This ensures fields with PostgreSQL-specific types (like timestamptz) get updated
- ;; to the correct types for the host database (e.g., datetime for MySQL)
- (log/info "Starting Sync of Audit DB fields to update metadata for host engine")
- (let [sync-future (future
- (log/with-no-logs (sync/sync-database! updated-audit-db {:scan :schema}))
- (log/info "Audit DB field sync complete."))]
- (when config/is-test?
- ;; Tests need the sync to complete before they run
- @sync-future))))))))))
+ (when-let [audit-db (t2/select-one :model/Database :is_audit true)]
+ (adjust-audit-db-to-host! audit-db))))))
(defn- maybe-install-audit-db!
[]
- (let [audit-db (t2/select-one :model/Database :is_audit true)]
- (cond
- (not (audit-app.settings/install-analytics-database))
- (u/prog1 ::blocked
- (log/info "Not installing Audit DB - install-analytics-database setting is false"))
+ (let [audit-db (t2/select-one :model/Database :is_audit true)
+ result (cond
+ (not (audit-app.settings/install-analytics-database))
+ (u/prog1 ::blocked
+ (log/info "Not installing Audit DB - install-analytics-database setting is false"))
- (nil? audit-db)
- (u/prog1 ::installed
- (log/info "Installing Audit DB...")
- (install-database! (mdb/db-type) audit/audit-db-id))
+ (nil? audit-db)
+ (u/prog1 ::installed
+ (log/info "Installing Audit DB...")
+ (install-database! (mdb/db-type) audit/audit-db-id))
- (not= (mdb/db-type) (:engine audit-db))
- (u/prog1 ::updated
- (log/infof "App DB change detected. Changing Audit DB source to match: %s." (name (mdb/db-type)))
- (adjust-audit-db-to-host! audit-db))
+ (not= (mdb/db-type) (:engine audit-db))
+ (u/prog1 ::updated
+ (log/infof "App DB change detected. Changing Audit DB source to match: %s." (name (mdb/db-type)))
+ (adjust-audit-db-to-host! audit-db))
- :else
- ::no-op)))
+ :else
+ ::no-op)]
+ (when (contains? #{::installed ::updated} result)
+ (when-let [db (t2/select-one :model/Database :is_audit true)]
+ (log/info "Syncing Audit DB")
+ (log/with-no-logs (sync/sync-database! db {:scan :schema}))))
+ result))
(defenterprise ensure-audit-db-installed!
"EE implementation of `ensure-db-installed!`. Installs audit db if it does not already exist, and loads audit
diff --git a/enterprise/backend/test/metabase_enterprise/audit_app/audit_test.clj b/enterprise/backend/test/metabase_enterprise/audit_app/audit_test.clj
index 51762b98256..cde7c33105b 100644
--- a/enterprise/backend/test/metabase_enterprise/audit_app/audit_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/audit_app/audit_test.clj
@@ -59,6 +59,15 @@
(is (not= 0 (t2/count :model/Card {:where [:= :database_id audit/audit-db-id]}))
"Cards should be created for Audit DB when the content is there."))
+ (testing "Cards in the audit collection have non-empty :result_metadata after installation"
+ (let [audit-cards (t2/select [:model/Card :id :name :result_metadata :card_schema]
+ :database_id audit/audit-db-id)
+ audit-cards-no-metadata (filter (comp empty? :result_metadata) audit-cards)]
+ (is (seq audit-cards))
+ (is (empty? audit-cards-no-metadata)
+ (str "Cards without :result_metadata: "
+ (pr-str (mapv :name audit-cards-no-metadata))))))
+
(testing "Audit DB starts with no permissions for all users"
(is (= {:perms/manage-database :no
:perms/download-results :one-million-rows
diff --git a/enterprise/backend/test/metabase_enterprise/audit_app/permissions_test.clj b/enterprise/backend/test/metabase_enterprise/audit_app/permissions_test.clj
index 52e9f28cde7..d822a7d7602 100644
--- a/enterprise/backend/test/metabase_enterprise/audit_app/permissions_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/audit_app/permissions_test.clj
@@ -46,7 +46,9 @@
:query {:source-table (str "card__" (u/the-id audit-card))}})))))
(testing "A non-native query can be run on views in the audit DB"
- (let [audit-view (t2/select-one :model/Table :db_id audit/audit-db-id {:where [:like [:lower :name] "v_%"]})]
+ (let [audit-view (t2/select-one :model/Table
+ :db_id audit/audit-db-id
+ {:where [:in [:lower :name] audit-app.permissions/audit-db-view-names]})]
(when-not (some-> audit-view :name u/lower-case-en (str/starts-with? "v_"))
(sync/sync-database! (t2/select-one :model/Database audit/audit-db-id)))
(is (partial=