Skip to content

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

Revert data_permissions migration (#77864)

Alexander Polyankin1921483a0daa
diff --git a/resources/migrations/062/20260707_collapse_uniform_table_permissions.yaml b/resources/migrations/062/20260707_collapse_uniform_table_permissions.yaml
index 0f2ddef2d46..f861862e18b 100644
--- a/resources/migrations/062/20260707_collapse_uniform_table_permissions.yaml
+++ b/resources/migrations/062/20260707_collapse_uniform_table_permissions.yaml
@@ -1,198 +1,141 @@
-## Collapse redundant table-level data_permissions rows into db-level rows (#76077).
+## Retired collapse of redundant table-level data_permissions rows (#76077).
##
## A bug wrote one 'blocked' view-data row per table even when the db-level row was already 'blocked',
-## ballooning data_permissions into millions of redundant rows. Two steps:
-## 1. For each (group, db) with no db-level view-data row, whose table-level view-data rows are all
-## 'blocked' and cover every active table: insert the equivalent db-level row.
-## 2. Drop every table-level view-data row made redundant by an equal-value db-level row.
-## Only view-data/blocked is safe to collapse; all other combos are left untouched.
+## ballooning data_permissions into tens of millions of redundant rows on some instances. Two shipped
+## attempts to collapse those rows during the upgrade (v62.2026-07-07T*, then the v62.2026-07-14T*
+## rewrite) both timed out on the largest instances — the ones that needed the collapse most. The
+## collapse is an optimization, not a correctness fix (the redundant representation is understood by all
+## versions >= 49), so the changeSet below retires the whole migration rather than iterating on it again.
+## Instances that already ran it keep the collapsed form; everyone else keeps the redundant rows.
+##
+## Operators who want the collapse can run the SQL at the end of this file manually.
databaseChangeLog:
- logicalFilePath: migrations/062_update_migrations.yaml
- # Step 1 originally shipped as v62.2026-07-07T00:00:00, which timed out on large instances (its plan
- # was O(rows x databases) on postgres). The changeSets below replace it under new ids; this DELETE just
- # removes the retired changeSet's stale changelog entry from instances that already ran it. For them the
- # replacements re-collapse already-collapsed data, which is a cheap no-op.
- - changeSet:
- id: v62.2026-07-14T00:00:00
- author: ranquild
- comment: Retire the timeout-prone original of the collapse changeSets below (#76077)
- changes:
- - sql:
- sql: DELETE FROM DATABASECHANGELOG WHERE id = 'v62.2026-07-07T00:00:00';
- rollback: # not necessary
-
- # Step 1. Aggregate to (group_id, db_id) first, then join. Coverage is counted in rows, not DISTINCT
- # tables — equivalent unless duplicate rows exist, which the app never writes. Postgres runs the
- # staged, parallel version in the changeSets below instead.
- - changeSet:
- id: v62.2026-07-14T00:00:01
- author: ranquild
- dbms: mysql,mariadb,h2
- comment: Insert db-level data_permissions rows for uniform full-coverage blocked table-level view-data permissions (#76077)
- changes:
- - sql:
- sql: >-
- INSERT INTO data_permissions (group_id, perm_type, db_id, perm_value)
- SELECT agg.group_id, 'perms/view-data', agg.db_id, 'blocked'
- FROM (
- SELECT dp.group_id, dp.db_id,
- MIN(dp.perm_value) AS min_value,
- MAX(dp.perm_value) AS max_value,
- COUNT(*) AS covered_rows,
- COUNT(it.id) AS inactive_rows
- FROM data_permissions dp
- LEFT JOIN metabase_table it ON it.id = dp.table_id AND it.active = FALSE
- WHERE dp.perm_type = 'perms/view-data'
- AND dp.table_id IS NOT NULL
- GROUP BY dp.group_id, dp.db_id
- ) agg
- JOIN (SELECT db_id, COUNT(*) AS n FROM metabase_table WHERE active = TRUE GROUP BY db_id) tc
- ON tc.db_id = agg.db_id
- WHERE agg.min_value = 'blocked'
- AND agg.max_value = 'blocked'
- AND agg.covered_rows - agg.inactive_rows = tc.n
- AND NOT EXISTS (
- SELECT 1 FROM data_permissions dbp
- WHERE dbp.group_id = agg.group_id
- AND dbp.db_id = agg.db_id
- AND dbp.perm_type = 'perms/view-data'
- AND dbp.table_id IS NULL);
- rollback: # not necessary; the collapsed representation is understood by all versions >= 49
-
- # Step 1 for postgres. CREATE TABLE AS gets a parallel plan (INSERT ... SELECT and DISTINCT
- # aggregates do not), so the one heavy pass over data_permissions runs on all available workers.
+ # Removes the stale changelog entries of every retired changeSet from instances that already ran them
+ # (fully or partially); nothing in this file re-runs any of that work.
- changeSet:
- id: v62.2026-07-14T00:00:02
+ id: v62.2026-07-15T00:00:00
author: ranquild
- dbms: postgresql
- comment: Stage per group-and-database aggregates of table-level view-data permissions (#76077)
+ comment: Retire the timeout-prone collapse changeSets that previously lived in this file (#76077)
changes:
- sql:
sql: >-
- SET LOCAL max_parallel_workers_per_gather = 8;
- CREATE TABLE data_permissions_collapse_agg AS
- SELECT dp.group_id, dp.db_id,
- COUNT(*) AS covered_rows,
- COUNT(it.id) AS inactive_rows,
- MIN(dp.perm_value) AS min_value,
- MAX(dp.perm_value) AS max_value
- FROM data_permissions dp
- LEFT JOIN metabase_table it ON it.id = dp.table_id AND it.active = FALSE
- WHERE dp.perm_type = 'perms/view-data'
- AND dp.table_id IS NOT NULL
- GROUP BY dp.group_id, dp.db_id;
- rollback: # not necessary; the staging table is dropped at the end of this migration
-
- - changeSet:
- id: v62.2026-07-14T00:00:03
- author: ranquild
- dbms: postgresql
- comment: Analyze the staged collapse aggregates (#76077)
- changes:
- - sql:
- sql: ANALYZE data_permissions_collapse_agg;
+ DELETE FROM DATABASECHANGELOG WHERE id IN (
+ 'v62.2026-07-07T00:00:00',
+ 'v62.2026-07-07T00:00:01',
+ 'v62.2026-07-07T00:00:02',
+ 'v62.2026-07-07T00:00:03',
+ 'v62.2026-07-07T00:00:04',
+ 'v62.2026-07-07T00:00:05',
+ 'v62.2026-07-14T00:00:00',
+ 'v62.2026-07-14T00:00:01',
+ 'v62.2026-07-14T00:00:02',
+ 'v62.2026-07-14T00:00:03',
+ 'v62.2026-07-14T00:00:04',
+ 'v62.2026-07-14T00:00:05');
rollback: # not necessary
- # NOT IN evaluates as one hashed subplan instead of an index probe per staged row.
- - changeSet:
- id: v62.2026-07-14T00:00:04
- author: ranquild
- dbms: postgresql
- comment: Insert db-level data_permissions rows for uniform full-coverage blocked table-level view-data permissions (#76077)
- changes:
- - sql:
- sql: >-
- INSERT INTO data_permissions (group_id, perm_type, db_id, perm_value)
- SELECT agg.group_id, 'perms/view-data', agg.db_id, 'blocked'
- FROM data_permissions_collapse_agg agg
- JOIN (SELECT db_id, COUNT(*) AS n FROM metabase_table WHERE active = TRUE GROUP BY db_id) tc
- ON tc.db_id = agg.db_id
- WHERE agg.min_value = 'blocked'
- AND agg.max_value = 'blocked'
- AND agg.covered_rows - agg.inactive_rows = tc.n
- AND (agg.group_id, agg.db_id) NOT IN (
- SELECT dbp.group_id, dbp.db_id FROM data_permissions dbp
- WHERE dbp.perm_type = 'perms/view-data'
- AND dbp.table_id IS NULL);
- rollback: # not necessary; the collapsed representation is understood by all versions >= 49
-
- - changeSet:
- id: v62.2026-07-14T00:00:05
- author: ranquild
- dbms: postgresql
- comment: Drop the staged collapse aggregates (#76077)
- changes:
- - sql:
- sql: DROP TABLE data_permissions_collapse_agg;
- rollback: # not necessary
-
- # Step 2. A row-by-row DELETE of millions of rows is too slow, so: stage the surviving rows,
- # truncate data_permissions, reload. One changeSet per step, so a failed run resumes where it stopped.
- - changeSet:
- id: v62.2026-07-07T00:00:01
- author: ranquild
- comment: Create the staging table that will hold the surviving data_permissions rows (#76077)
- changes:
- # createTable (not raw SQL) so Liquibase adds MySQL/MariaDB's ENGINE/charset clauses.
- - createTable:
- tableName: data_permissions_collapse_keep
- remarks: Transient staging table holding the data_permissions rows to keep while the table is collapsed; dropped at the end of this migration
- columns:
- - column: {name: id, type: int, remarks: Copy of data_permissions.id}
- - column: {name: group_id, type: int, remarks: Copy of data_permissions.group_id}
- - column: {name: perm_type, type: varchar(64), remarks: Copy of data_permissions.perm_type}
- - column: {name: db_id, type: int, remarks: Copy of data_permissions.db_id}
- - column: {name: schema_name, type: varchar(254), remarks: Copy of data_permissions.schema_name}
- - column: {name: table_id, type: int, remarks: Copy of data_permissions.table_id}
- - column: {name: perm_value, type: varchar(64), remarks: Copy of data_permissions.perm_value}
- rollback: # not necessary; the collapsed representation is understood by all versions >= 49
-
- - changeSet:
- id: v62.2026-07-07T00:00:02
- author: ranquild
- comment: Stage the surviving data_permissions rows before collapsing the redundant ones (#76077)
- changes:
- - sql:
- sql: >-
- INSERT INTO data_permissions_collapse_keep (id, group_id, perm_type, db_id, schema_name, table_id, perm_value)
- SELECT dp.id, dp.group_id, dp.perm_type, dp.db_id, dp.schema_name, dp.table_id, dp.perm_value
- FROM data_permissions dp
- LEFT JOIN data_permissions dbp
- ON dp.perm_type = 'perms/view-data' AND dp.table_id IS NOT NULL
- AND dbp.group_id = dp.group_id AND dbp.db_id = dp.db_id
- AND dbp.perm_type = dp.perm_type AND dbp.perm_value = dp.perm_value
- AND dbp.table_id IS NULL
- WHERE dbp.id IS NULL;
- rollback: # not necessary; the collapsed representation is understood by all versions >= 49
-
- - changeSet:
- id: v62.2026-07-07T00:00:03
- author: ranquild
- comment: Empty data_permissions before reloading only the surviving rows (#76077)
- changes:
- - sql:
- sql: TRUNCATE TABLE data_permissions;
- rollback: # not necessary; the collapsed representation is understood by all versions >= 49
-
- - changeSet:
- id: v62.2026-07-07T00:00:04
- author: ranquild
- comment: Reload the surviving data_permissions rows (#76077)
- changes:
- - sql:
- sql: >-
- INSERT INTO data_permissions (id, group_id, perm_type, db_id, schema_name, table_id, perm_value)
- SELECT id, group_id, perm_type, db_id, schema_name, table_id, perm_value
- FROM data_permissions_collapse_keep;
- rollback: # not necessary; the collapsed representation is understood by all versions >= 49
-
- - changeSet:
- id: v62.2026-07-07T00:00:05
- author: ranquild
- comment: Drop the staging table used to collapse data_permissions (#76077)
- changes:
- - sql:
- sql: DROP TABLE data_permissions_collapse_keep;
- rollback: # not necessary; the collapsed representation is understood by all versions >= 49
+## Manual collapse (PostgreSQL). One statement, so it is atomic; it is also idempotent, safe to run (and
+## to cancel) while Metabase is up, and strictly semantics-preserving: for every (group, database) whose
+## table-level view-data rows are all 'blocked' and cover every active table, it inserts the equivalent
+## db-level row (unless any db-level view-data row already exists) and deletes the table-level rows it
+## makes redundant; it also deletes table-level view-data rows that duplicate the value of a pre-existing
+## db-level row. Everything else is left untouched. Run it from psql — there it is not subject to the
+## JDBC socket timeout that killed the migration; expect minutes on tens of millions of rows.
+##
+## 1. Current size of data_permissions:
+##
+## SELECT COUNT(*) AS rows_now FROM data_permissions;
+##
+## 2. Dry run (read-only): what the row count would be after the collapse. The CTEs mirror the collapse
+## statement below exactly.
+##
+## WITH active_counts AS (
+## SELECT db_id, COUNT(*) AS n
+## FROM metabase_table
+## WHERE active = TRUE
+## GROUP BY db_id
+## ),
+## collapsible AS (
+## SELECT dp.group_id, dp.db_id
+## FROM data_permissions dp
+## JOIN active_counts ac ON ac.db_id = dp.db_id
+## LEFT JOIN metabase_table t ON t.id = dp.table_id AND t.active = TRUE
+## WHERE dp.perm_type = 'perms/view-data'
+## AND dp.table_id IS NOT NULL
+## GROUP BY dp.group_id, dp.db_id, ac.n
+## HAVING MIN(dp.perm_value) = 'blocked'
+## AND MAX(dp.perm_value) = 'blocked'
+## AND COUNT(DISTINCT t.id) = ac.n
+## ),
+## insertable AS (
+## SELECT c.group_id, c.db_id
+## FROM collapsible c
+## WHERE NOT EXISTS (
+## SELECT 1 FROM data_permissions dbp
+## WHERE dbp.group_id = c.group_id
+## AND dbp.db_id = c.db_id
+## AND dbp.perm_type = 'perms/view-data'
+## AND dbp.table_id IS NULL)
+## ),
+## deletable AS (
+## SELECT dp.id
+## FROM data_permissions dp
+## WHERE dp.perm_type = 'perms/view-data'
+## AND dp.table_id IS NOT NULL
+## AND (dp.group_id, dp.db_id, dp.perm_value) IN (
+## SELECT group_id, db_id, 'blocked' FROM insertable
+## UNION ALL
+## SELECT group_id, db_id, perm_value
+## FROM data_permissions
+## WHERE perm_type = 'perms/view-data'
+## AND table_id IS NULL)
+## )
+## SELECT (SELECT COUNT(*) FROM data_permissions)
+## + (SELECT COUNT(*) FROM insertable)
+## - (SELECT COUNT(*) FROM deletable) AS rows_after_collapse;
+##
+## 3. The collapse itself:
+##
+## WITH active_counts AS (
+## SELECT db_id, COUNT(*) AS n
+## FROM metabase_table
+## WHERE active = TRUE
+## GROUP BY db_id
+## ),
+## collapsible AS (
+## SELECT dp.group_id, dp.db_id
+## FROM data_permissions dp
+## JOIN active_counts ac ON ac.db_id = dp.db_id
+## LEFT JOIN metabase_table t ON t.id = dp.table_id AND t.active = TRUE
+## WHERE dp.perm_type = 'perms/view-data'
+## AND dp.table_id IS NOT NULL
+## GROUP BY dp.group_id, dp.db_id, ac.n
+## HAVING MIN(dp.perm_value) = 'blocked'
+## AND MAX(dp.perm_value) = 'blocked'
+## AND COUNT(DISTINCT t.id) = ac.n
+## ),
+## inserted AS (
+## INSERT INTO data_permissions (group_id, perm_type, db_id, perm_value)
+## SELECT c.group_id, 'perms/view-data', c.db_id, 'blocked'
+## FROM collapsible c
+## WHERE NOT EXISTS (
+## SELECT 1 FROM data_permissions dbp
+## WHERE dbp.group_id = c.group_id
+## AND dbp.db_id = c.db_id
+## AND dbp.perm_type = 'perms/view-data'
+## AND dbp.table_id IS NULL)
+## RETURNING group_id, db_id, perm_value
+## )
+## DELETE FROM data_permissions dp
+## WHERE dp.perm_type = 'perms/view-data'
+## AND dp.table_id IS NOT NULL
+## AND (dp.group_id, dp.db_id, dp.perm_value) IN (
+## SELECT group_id, db_id, perm_value FROM inserted
+## UNION ALL
+## SELECT group_id, db_id, perm_value
+## FROM data_permissions
+## WHERE perm_type = 'perms/view-data'
+## AND table_id IS NULL);
diff --git a/test/metabase/app_db/schema_migrations_test.clj b/test/metabase/app_db/schema_migrations_test.clj
index c0f1ce2245a..025a5181fc6 100644
--- a/test/metabase/app_db/schema_migrations_test.clj
+++ b/test/metabase/app_db/schema_migrations_test.clj
@@ -2888,115 +2888,6 @@
:created_at :%now}
attrs)))
-(deftest collapse-uniform-table-permissions-migration-test
- (testing "Migrations v62.2026-07-14T00:00:00 through v62.2026-07-07T00:00:05:
- uniform full-coverage :blocked view-data table rows collapse to a db-level row (#76077)"
- (impl/test-migrations ["v62.2026-07-14T00:00:00" "v62.2026-07-07T00:00:05"] [migrate!]
- (let [group-id (t2/insert-returning-pk! :permissions_group {:name "collapse-test-group"})
- db! (fn [db-name]
- (t2/insert-returning-pk! :metabase_database
- {:details "{}"
- :created_at :%now
- :updated_at :%now
- :engine "h2"
- :is_sample false
- :name db-name}))
- table! (fn [db-id active?]
- (t2/insert-returning-pk! :metabase_table
- {:db_id db-id
- :name (mt/random-name)
- :schema "PUBLIC"
- :created_at :%now
- :updated_at :%now
- :active active?}))
- perm! (fn [db-id table-id perm-type perm-value]
- (t2/insert! :data_permissions
- {:group_id group-id
- :db_id db-id
- :table_id table-id
- :schema_name (when table-id "PUBLIC")
- :perm_type perm-type
- :perm_value perm-value}))
- rows (fn [db-id perm-type]
- (t2/select-fn-set (juxt :table_id :perm_value) :data_permissions
- :group_id group-id :db_id db-id :perm_type perm-type))
- ;; db-1: uniform :blocked covering all active tables (+ a :blocked row for an inactive table)
- ;; → collapses
- db-1 (db! "collapse-uniform")
- t1a (table! db-1 true)
- t1b (table! db-1 true)
- t1-off (table! db-1 false)
- ;; db-2: partial coverage → untouched
- db-2 (db! "collapse-partial")
- t2a (table! db-2 true)
- _t2b (table! db-2 true)
- ;; db-3: mixed values → untouched
- db-3 (db! "collapse-mixed")
- t3a (table! db-3 true)
- t3b (table! db-3 true)
- ;; db-4: uniform but :unrestricted → untouched
- db-4 (db! "collapse-unrestricted")
- t4a (table! db-4 true)
- ;; db-5: uniform :no create-queries (not view-data) → untouched
- db-5 (db! "collapse-other-type")
- t5a (table! db-5 true)
- ;; db-6: active tables uniformly :blocked, but an INACTIVE table's row differs → untouched;
- ;; the differing row must survive so a reactivated table picks its value back up
- db-6 (db! "collapse-inactive-differs")
- t6a (table! db-6 true)
- t6-off (table! db-6 false)
- ;; db-7: table row duplicating an equal-value db-level row → duplicate deleted
- db-7 (db! "collapse-dup-of-db-level")
- t7a (table! db-7 true)
- ;; db-8: duplicate row alongside full coverage → conservatively NOT collapsed: coverage is
- ;; row-counted, so the duplicate makes the count exceed the active-table count (the app never
- ;; writes duplicate rows; leaving them uncollapsed preserves the current state)
- db-8 (db! "collapse-dup-full-coverage")
- t8a (table! db-8 true)
- t8b (table! db-8 true)
- ;; db-9: duplicate row exactly offsetting a MISSING table row → collapsed; lossless because a
- ;; table with no row in a group with no db-level row already reads as the least permissive
- ;; value, blocked, which is what the inserted db-level row says
- db-9 (db! "collapse-dup-masks-missing")
- t9a (table! db-9 true)
- _t9b (table! db-9 true)]
- (perm! db-1 t1a "perms/view-data" "blocked")
- (perm! db-1 t1b "perms/view-data" "blocked")
- (perm! db-1 t1-off "perms/view-data" "blocked")
- (perm! db-2 t2a "perms/view-data" "blocked")
- (perm! db-3 t3a "perms/view-data" "blocked")
- (perm! db-3 t3b "perms/view-data" "unrestricted")
- (perm! db-4 t4a "perms/view-data" "unrestricted")
- (perm! db-5 t5a "perms/create-queries" "no")
- (perm! db-6 t6a "perms/view-data" "blocked")
- (perm! db-6 t6-off "perms/view-data" "unrestricted")
- (perm! db-7 nil "perms/view-data" "unrestricted")
- (perm! db-7 t7a "perms/view-data" "unrestricted")
- (perm! db-8 t8a "perms/view-data" "blocked")
- (perm! db-8 t8a "perms/view-data" "blocked")
- (perm! db-8 t8b "perms/view-data" "blocked")
- (perm! db-9 t9a "perms/view-data" "blocked")
- (perm! db-9 t9a "perms/view-data" "blocked")
- (migrate!)
- (testing "uniform :blocked collapses to a single db-level row, dropping the inactive table's row too"
- (is (= #{[nil "blocked"]} (rows db-1 "perms/view-data"))))
- (testing "partial coverage untouched"
- (is (= #{[t2a "blocked"]} (rows db-2 "perms/view-data"))))
- (testing "mixed values untouched"
- (is (= #{[t3a "blocked"] [t3b "unrestricted"]} (rows db-3 "perms/view-data"))))
- (testing "uniform non-blocked value untouched"
- (is (= #{[t4a "unrestricted"]} (rows db-4 "perms/view-data"))))
- (testing "non-view-data perm types untouched"
- (is (= #{[t5a "no"]} (rows db-5 "perms/create-queries"))))
- (testing "a differing value on an inactive table's row blocks the collapse"
- (is (= #{[t6a "blocked"] [t6-off "unrestricted"]} (rows db-6 "perms/view-data"))))
- (testing "table rows duplicating an equal-value db-level row are deleted"
- (is (= #{[nil "unrestricted"]} (rows db-7 "perms/view-data"))))
- (testing "a duplicate row alongside full coverage conservatively blocks the collapse"
- (is (= #{[t8a "blocked"] [t8b "blocked"]} (rows db-8 "perms/view-data"))))
- (testing "a duplicate row offsetting a missing table row collapses losslessly"
- (is (= #{[nil "blocked"]} (rows db-9 "perms/view-data"))))))))
-
(defn- collection-entity-id
[collection-id]
(str/trim (t2/select-one-fn :entity_id :collection :id collection-id)))