Skip to content

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

Skip the redundant audit DB sync after a fresh install (#83681)

bryand6b2903ccd7c
diff --git a/enterprise/backend/src/metabase_enterprise/audit_app/audit.clj b/enterprise/backend/src/metabase_enterprise/audit_app/audit.clj
index d2d7e173801..37fb0d35b0d 100644
--- a/enterprise/backend/src/metabase_enterprise/audit_app/audit.clj
+++ b/enterprise/backend/src/metabase_enterprise/audit_app/audit.clj
@@ -262,6 +262,15 @@
(audit/last-analytics-checksum! current-checksum))
(not= original-engine (mdb/db-type))))))))))
+(defn- views-checksum
+ "Checksum of the `instance_analytics_views` SQL files. Stored in the `last-analytics-views-checksum`
+ setting; when it changes, `ensure-audit-db-installed!` triggers a one-shot audit DB schema sync
+ so that newly added or renamed views become discoverable without waiting for the next scheduled sync."
+ []
+ (when (io/resource "migrations/instance_analytics_views")
+ (u.files/with-open-path-to-resource [views-dir "migrations/instance_analytics_views"]
+ (directory-content-checksum views-dir ".sql"))))
+
(defn- maybe-install-audit-db!
[]
(let [audit-db (audit-app.db/audit-database)
@@ -285,18 +294,11 @@
(when (contains? #{::installed ::updated} result)
(when-let [db (audit-app.db/audit-database)]
(log/info "Syncing Audit DB")
- (log/with-no-logs (sync/sync-database! db {:scan :schema}))))
+ (log/with-no-logs (sync/sync-database! db {:scan :schema}))
+ ;; this sync saw every current view, so `maybe-sync-audit-db!` need not sync again for stale views
+ (audit-app.settings/last-analytics-views-checksum! (views-checksum))))
result))
-(defn- views-checksum
- "Checksum of the `instance_analytics_views` SQL files. Stored in the `last-analytics-views-checksum`
- setting; when it changes, `ensure-audit-db-installed!` triggers a one-shot audit DB schema sync
- so that newly added or renamed views become discoverable without waiting for the next scheduled sync."
- []
- (when (io/resource "migrations/instance_analytics_views")
- (u.files/with-open-path-to-resource [views-dir "migrations/instance_analytics_views"]
- (directory-content-checksum views-dir ".sql"))))
-
(defn- maybe-sync-audit-db!
"One-shot synchronous `:scan :schema` sync of the audit DB. Fires when any trigger is true:
- `engine-changed?` — `maybe-load-analytics-content!` swapped the audit DB engine and
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 1d452ebb7d3..096e8505c8b 100644
--- a/enterprise/backend/test/metabase_enterprise/audit_app/audit_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/audit_app/audit_test.clj
@@ -7,6 +7,7 @@
[metabase-enterprise.audit-app.audit :as ee-audit]
[metabase-enterprise.audit-app.settings :as ee.audit.settings]
[metabase-enterprise.serialization.cmd :as serialization.cmd]
+ [metabase.app-db.core :as mdb]
[metabase.audit-app.core :as audit]
[metabase.core.core :as mbc]
[metabase.lib.core :as lib]
@@ -225,6 +226,21 @@
(is (nil? (#'ee-audit/maybe-sync-audit-db! audit-db false)))
(is (= 0 (ee.audit.settings/last-analytics-views-checksum)))))))
+(deftest install-sync-records-views-checksum-test
+ (testing "the sync after install/update records the views checksum, so the views-stale sync does not repeat it"
+ (with-audit-db-restoration!
+ (let [audit-db (t2/select-one :model/Database :is_audit true)
+ syncs (atom 0)]
+ (ee.audit.settings/last-analytics-views-checksum! 0)
+ ;; any engine other than the host's takes the ::updated path
+ (t2/update! :model/Database (:id audit-db) {:engine (if (= :postgres (mdb/db-type)) "h2" "postgres")})
+ (mt/with-dynamic-fn-redefs [ee-audit/views-checksum (constantly 12345)
+ sync.core/sync-database! (fn [& _] (swap! syncs inc))]
+ (is (= ::ee-audit/updated (#'ee-audit/maybe-install-audit-db!)))
+ (is (= 12345 (ee.audit.settings/last-analytics-views-checksum)))
+ (#'ee-audit/maybe-sync-audit-db! audit-db false)
+ (is (= 1 @syncs)))))))
+
(deftest adjust-audit-db-to-source-test
(testing "adjust-audit-db-to-source! correctly handles tables and fields with mixed case"
(mt/with-temp [:model/Database {audit-db-id :id} {:engine "h2"}