Skip to content

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

Copy old sample DB permissions to new sample DB on upgrade (#76561)

Ben Grabowfe593bdcf13b
diff --git a/.clj-kondo/config/modules/config.edn b/.clj-kondo/config/modules/config.edn
index 297b0c2824c..9dc51872f44 100644
--- a/.clj-kondo/config/modules/config.edn
+++ b/.clj-kondo/config/modules/config.edn
@@ -1931,6 +1931,7 @@
metabase.sample-data.init}
:uses #{config
models
+ permissions
plugins
settings
sync
@@ -1938,6 +1939,7 @@
:model-imports #{:model/Card
:model/Dashboard
:model/DashboardCard
+ :model/DataPermissions
:model/Database
:model/Field
:model/Table}}
diff --git a/src/metabase/sample_data/impl.clj b/src/metabase/sample_data/impl.clj
index a0d12a65380..678b61945be 100644
--- a/src/metabase/sample_data/impl.clj
+++ b/src/metabase/sample_data/impl.clj
@@ -4,6 +4,7 @@
[clojure.java.io :as io]
[clojure.string :as str]
[metabase.config.core :as config]
+ [metabase.permissions.core :as perms]
[metabase.plugins.core :as plugins]
[metabase.sample-data.example-content :as example-content]
[metabase.sync.core :as sync]
@@ -131,6 +132,44 @@
(when (seq empty-ids)
(t2/delete! :model/Dashboard :id [:in empty-ids])))))
+(defn- capture-permissions-snapshot
+ "Snapshot a database's data-permission rows plus the names of the tables referenced by any table-level
+ rows, so the same access can be re-applied to a replacement database whose tables have new IDs. Must run
+ before the old database (and its cascading permission rows) is deleted."
+ [db-id]
+ (let [permissions (t2/select :model/DataPermissions :db_id db-id)
+ table-ids (into #{} (keep :table_id) permissions)]
+ {:permissions permissions
+ :table-id->name (if (seq table-ids)
+ (t2/select-pk->fn :name :model/Table :id [:in table-ids])
+ {})}))
+
+(defn- apply-permissions-snapshot!
+ "Re-apply a permission snapshot (see [[capture-permissions-snapshot]]) to the replacement database
+ `new-db-id`, replacing the defaults granted when it was created. Table-level rows are remapped onto the
+ new database's tables by name; since the new tables exist only after the first sync, this must run
+ post-sync. A table-level row whose table has no name match in the new database is dropped."
+ [new-db-id {:keys [permissions table-id->name]}]
+ (let [name->new-table-id (t2/select-fn->pk :name :model/Table :db_id new-db-id)
+ granular? (fn [rows] (some :table_id rows))
+ permission-groups (group-by (juxt :group_id :perm_type) permissions)
+ db-perms (for [[[group-id perm-type] rows] permission-groups
+ :when (not (granular? rows))]
+ [group-id perm-type (:perm_value (first rows))])
+ table-perms (for [[[group-id perm-type] rows] permission-groups
+ :when (granular? rows)
+ :let [remapped (into {}
+ (keep (fn [{:keys [table_id perm_value]}]
+ (when-let [new-table-id (-> table_id table-id->name name->new-table-id)]
+ [new-table-id perm_value]))
+ rows))]
+ :when (seq remapped)]
+ [group-id perm-type remapped])]
+ (doseq [[group-id perm-type perm-value] db-perms]
+ (perms/set-database-permission! group-id new-db-id perm-type perm-value))
+ (doseq [[group-id perm-type one-table-perms] table-perms]
+ (perms/set-table-permissions! group-id perm-type one-table-perms))))
+
(defn- replace-sample-database!
"The bundled sample database's engine changed (e.g. H2 -> SQLite on upgrade). The old sample
Database's tables, fields, and connection details are incompatible with the new engine, so rather
@@ -152,25 +191,34 @@
;; doesn't hold the app-DB transaction open.
(let [new-db-details (when (config/load-sample-content?)
(try-to-extract-sample-database! engine))
- new-db (t2/with-transaction [_conn]
- (let [dashboard-ids (sample-database-dashboard-ids (:id old-sample-db))]
- (t2/delete! :model/Database (:id old-sample-db))
- (delete-emptied-dashboards! dashboard-ids))
- (when new-db-details
- (first (t2/insert-returning-instances! :model/Database
- :name sample-database-name
- :details new-db-details
- :engine engine
- :is_sample true))))]
+ {:keys [new-db permissions-snapshot]}
+ (t2/with-transaction [_conn]
+ (let [dashboard-ids (sample-database-dashboard-ids (:id old-sample-db))
+ snapshot (when new-db-details
+ (capture-permissions-snapshot (:id old-sample-db)))]
+ (t2/delete! :model/Database (:id old-sample-db))
+ (delete-emptied-dashboards! dashboard-ids)
+ {:permissions-snapshot snapshot
+ :new-db (when new-db-details
+ (first (t2/insert-returning-instances! :model/Database
+ :name sample-database-name
+ :details new-db-details
+ :engine engine
+ :is_sample true)))}))]
(when new-db
- (when (and (try
- (sync/sync-database! new-db)
- true
- (catch Throwable e
- (log/error e "Failed to sync the replacement sample database")
- false))
- (not (config/config-bool :mb-enable-test-endpoints)))
- (example-content/recreate-example-content! (:id new-db))))))
+ (let [synced? (try
+ (sync/sync-database! new-db)
+ true
+ (catch Throwable e
+ (log/error e "Failed to sync the replacement sample database")
+ false))]
+ (when synced?
+ ;; The new database's tables exist now, so the old sample DB's access can be restored (replacing
+ ;; the defaults granted at creation) before the bundled example content is recreated.
+ (when permissions-snapshot
+ (apply-permissions-snapshot! (:id new-db) permissions-snapshot))
+ (when-not (config/config-bool :mb-enable-test-endpoints)
+ (example-content/recreate-example-content! (:id new-db))))))))
(defn update-sample-database-if-needed!
"Reconcile the existing sample database with the bundled one. When the bundled engine changed
diff --git a/test/metabase/sample_data/impl_test.clj b/test/metabase/sample_data/impl_test.clj
index a4884c52718..e2fdae0429c 100644
--- a/test/metabase/sample_data/impl_test.clj
+++ b/test/metabase/sample_data/impl_test.clj
@@ -7,6 +7,7 @@
[metabase.app-db.core :as mdb]
[metabase.config.core :as config]
[metabase.driver.sql-jdbc.connection :as sql-jdbc.conn]
+ [metabase.permissions.core :as perms]
[metabase.plugins.impl :as plugins]
[metabase.sample-data.example-content :as example-content]
[metabase.sample-data.impl :as sample-data]
@@ -267,6 +268,35 @@
(testing "the engine swap itself still completed - a new SQLite sample DB exists"
(is (t2/exists? :model/Database :is_sample true :engine :sqlite))))))))
+(defn- db-level-perms
+ "DB-level data-permission rows for `db-id` as a comparable map {[group-id perm-type] perm-value}."
+ [db-id]
+ (into {}
+ (for [{:keys [group_id perm_type perm_value]} (t2/select :model/DataPermissions :db_id db-id :table_id nil)]
+ [[group_id perm_type] perm_value])))
+
+(deftest sample-database-upgrade-preserves-permissions-test
+ (testing "The H2->SQLite sample-database swap re-applies each group's permissions to the new sample DB"
+ (mt/with-temp-empty-app-db [_conn :h2]
+ (mdb/setup-db! :create-sample-content? false)
+ (mt/with-temp [:model/PermissionsGroup custom-group {}
+ :model/Database old-sample {:engine :h2, :is_sample true, :details {:db "mem:old-sample"}}]
+ ;; Put the old sample DB into a distinctive, non-default permission state, so we test that real custom
+ ;; permissions carry forward - not just that the defaults happen to line up.
+ (perms/set-database-permission! (perms/all-users-group) (:id old-sample) :perms/create-queries :no)
+ (perms/set-database-permission! custom-group (:id old-sample) :perms/create-queries :query-builder)
+ (let [expected-perms (db-level-perms (:id old-sample))]
+ (with-redefs [config/load-sample-content? (constantly true)]
+ (#'sample-data/update-sample-database-if-needed! old-sample))
+ (let [new-sample (t2/select-one :model/Database :is_sample true :engine :sqlite)]
+ (is (some? new-sample) "the swap created a SQLite sample database")
+ (testing "every group's db-level permissions match the old sample DB exactly"
+ (is (= expected-perms (db-level-perms (:id new-sample)))))
+ (testing "the custom permission state specifically carried forward"
+ (let [new-perms (db-level-perms (:id new-sample))]
+ (is (= :no (new-perms [(:id (perms/all-users-group)) :perms/create-queries])))
+ (is (= :query-builder (new-perms [(:id custom-group) :perms/create-queries])))))))))))
+
(deftest sample-database-schedule-sync-test
(testing "Check that the sample database has scheduled sync jobs, just like a newly created database"
(mt/with-temp-empty-app-db [_conn :h2]