The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
Update table-level permissions targeting sample DB to use the correct schema on upgrade (#77843)
Ben Grabowba4c6a87c32c
diff --git a/.clj-kondo/config/modules/config.edn b/.clj-kondo/config/modules/config.ednindex eeb7aeaf815..a2c9eb8a637 100644--- a/.clj-kondo/config/modules/config.edn+++ b/.clj-kondo/config/modules/config.edn@@ -2000,8 +2000,7 @@settingssyncutil}- :model-imports #{:model/Database- :model/Table}}+ :model-imports #{:model/DataPermissions :model/Database :model/Table}}search{:team "UX West"diff --git a/src/metabase/sample_data/impl.clj b/src/metabase/sample_data/impl.cljindex 4f7243f2e7e..1f7a7bc0046 100644--- a/src/metabase/sample_data/impl.clj+++ b/src/metabase/sample_data/impl.clj@@ -150,7 +150,16 @@(t2/update! :model/Database (:id old-sample-db)(cond-> {:engine engine, :details details}settings (assoc :settings settings)))- (t2/update! :model/Table :db_id (:id old-sample-db) {:schema (table-schema-for-engine engine)}))+ (t2/update! :model/Table :db_id (:id old-sample-db) {:schema (table-schema-for-engine engine)})+ ;; Table-level permission rows denormalize the table's schema; keep them matching or schema-scoped+ ;; permission checks (e.g. schema visibility in the data picker) stop counting them. Raw table update:+ ;; the model's before-update rejects all updates, and delete+reinsert would churn ids for a rename that+ ;; doesn't change any permission value.+ (t2/query {:update (t2/table-name :model/DataPermissions)+ :set {:schema_name (table-schema-for-engine engine)}+ :where [:and+ [:= :db_id (:id old-sample-db)]+ [:not= :table_id nil]]}))(sync/sync-database! (t2/select-one :model/Database :id (:id old-sample-db)))))(defn update-sample-database-if-needed!diff --git a/test/metabase/sample_data/impl_test.clj b/test/metabase/sample_data/impl_test.cljindex e400a6497b7..fb29d93b187 100644--- a/test/metabase/sample_data/impl_test.clj+++ b/test/metabase/sample_data/impl_test.clj@@ -144,6 +144,50 @@(is (false? (get-in db [:settings :database-enable-actions])))(is (false? (get-in db [:settings :database-enable-table-editing])))))))))+(deftest migrate-sample-database-engine-in-place-preserves-granular-permissions-test+ (testing "Table-level (granular) permissions on the sample DB survive the engine migration. The migration+ changes every table's schema (H2 PUBLIC -> SQLite nil) while data_permissions rows carry a+ denormalized schema_name, so schema-scoped permission checks must still line up."+ (mt/with-model-cleanup [:model/Database]+ (mt/with-temp [:model/PermissionsGroup group {}+ :model/User user {}+ :model/PermissionsGroupMembership _ {:user_id (:id user) :group_id (:id group)}]+ (let [h2-db (t2/insert-returning-instance! :model/Database+ {:name "Sample Database" :engine :h2 :is_sample true+ :details (#'sample-data/try-to-extract-sample-database! :h2)})]+ (sync/sync-database! h2-db)+ (let [db-id (:id h2-db)+ orders-id (t2/select-one-pk :model/Table :db_id db-id :name "ORDERS")+ people-id (t2/select-one-pk :model/Table :db_id db-id :name "PEOPLE")]+ ;; Granular state: All Users sees nothing; the custom group can query everything except ORDERS.+ ;; Blocking a single table splits the group's db-level rows into per-table rows, each carrying the+ ;; table's schema_name ("PUBLIC" pre-migration).+ (perms/set-database-permission! (perms/all-users-group) db-id :perms/view-data :blocked)+ (perms/set-database-permission! (perms/all-users-group) db-id :perms/create-queries :no)+ (perms/set-database-permission! group db-id :perms/view-data :unrestricted)+ (perms/set-database-permission! group db-id :perms/create-queries :query-builder-and-native)+ (perms/set-table-permission! group orders-id :perms/view-data :blocked)+ (perms/set-table-permission! group orders-id :perms/create-queries :no)+ (let [effective (fn [schema]+ {:orders-view (perms/table-permission-for-user (:id user) :perms/view-data db-id orders-id)+ :people-view (perms/table-permission-for-user (:id user) :perms/view-data db-id people-id)+ ;; what GET /api/database/:id/schemas visibility keys on+ :schema-create (perms/schema-permission-for-user (:id user) :perms/create-queries db-id schema)})+ before (effective "PUBLIC")]+ (is (= {:orders-view :blocked+ :people-view :unrestricted+ ;; splitting a DB's create-queries perms per-table caps the value at :query-builder,+ ;; since native access is all-or-nothing per database+ :schema-create :query-builder}+ before)+ "precondition: granular perms are in effect on the H2 sample DB")+ ;; ---- the migration under test ----+ (#'sample-data/migrate-sample-database-engine-in-place! :sqlite (t2/select-one :model/Database :id db-id))+ (testing "permission checks give the same answers under the new (nil) schema"+ (is (= before (effective nil))))+ (testing "no data_permissions rows are left pointing at the departed PUBLIC schema"+ (is (not (t2/exists? :model/DataPermissions :db_id db-id :schema_name "PUBLIC")))))))))))+(def ^:private extracted-db-path-regex #".*plugins/sample-database\.sqlite$")(deftest extract-sample-database-test