Skip to content

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

Index data_app.resource_collection_id on Postgres in a version-less changeset (#85405)

John Swanson52754e69a7dd
diff --git a/resources/migrations/065/20261006_data_app_resource_collection.yaml b/resources/migrations/065/20261006_data_app_resource_collection.yaml
index 2dc44153453..759c731e12c 100644
--- a/resources/migrations/065/20261006_data_app_resource_collection.yaml
+++ b/resources/migrations/065/20261006_data_app_resource_collection.yaml
@@ -3,25 +3,8 @@
## was deleted has none: give the first the namespace and the second a collection.
databaseChangeLog:
- ## MySQL, MariaDB, and H2 index a foreign key's column themselves; Postgres doesn't. On MySQL a second index
- ## would take over the constraint and refuse to be dropped on rollback, so this one is Postgres-only.
- - changeSet:
- id: v65.2026-10-06T00:00:00
- author: timofei
- comment: Index data apps by their resource collection (PostgreSQL)
- dbms: postgresql
- changes:
- - createIndex:
- tableName: data_app
- indexName: idx_data_app_resource_collection_id
- columns:
- - column:
- name: resource_collection_id
- rollback:
- - dropIndex:
- tableName: data_app
- indexName: idx_data_app_resource_collection_id
-
+ ## The Postgres index on data_app.resource_collection_id moved to 2026/20261023_data_app_resource_collection_index,
+ ## which retires its old v65.2026-10-06T00:00:00 row.
- changeSet:
id: v65.2026-10-06T00:00:01
author: timofei
diff --git a/resources/migrations/065/20261022_data_app_unique_resource_collection.yaml b/resources/migrations/065/20261022_data_app_unique_resource_collection.yaml
index 0045131dde1..06be63e67c4 100644
--- a/resources/migrations/065/20261022_data_app_unique_resource_collection.yaml
+++ b/resources/migrations/065/20261022_data_app_unique_resource_collection.yaml
@@ -1,7 +1,8 @@
## Two data apps can't share one resource collection. A plain index on the column comes first, on every database: on
## MySQL and MariaDB a unique index would otherwise take the place of the index behind the foreign key, and a rollback
-## could not drop it. Postgres has that index already (20261006_data_app_resource_collection). Apps that share a
-## collection keep it for the first app only; the others are left without one, which an app may be given again.
+## could not drop it. On Postgres an instance upgraded from 64 has the index already, from
+## 2026/20261023_data_app_resource_collection_index. Apps that share a collection keep it for the first app only; the
+## others are left without one, which an app may be given again.
databaseChangeLog:
- changeSet:
diff --git a/resources/migrations/2026/20261023_data_app_resource_collection_index.yaml b/resources/migrations/2026/20261023_data_app_resource_collection_index.yaml
new file mode 100644
index 00000000000..c4b2d0cbc47
--- /dev/null
+++ b/resources/migrations/2026/20261023_data_app_resource_collection_index.yaml
@@ -0,0 +1,42 @@
+## MySQL, MariaDB, and H2 index a foreign key's column themselves; Postgres doesn't. On MySQL a second index
+## would take over the constraint and refuse to be dropped on rollback, so the index is Postgres-only.
+##
+## The index changeSet first shipped as v65.2026-10-06T00:00:00 in migrations/065/, but the column it indexes ships
+## in 64. The first changeSet retires that row. The index is guarded because an instance may have it already: from the
+## retired changeSet, or from 065/20261022_data_app_unique_resource_collection, which runs before this file on 65.
+## DATABASECHANGELOG is unquoted on purpose: it folds to the table's actual case on postgres (lowercase) and h2
+## (uppercase), and matches it exactly on mysql/mariadb.
+
+databaseChangeLog:
+ - changeSet:
+ id: dyc49r
+ author: johnswanson
+ comment: Retire the misnumbered v65 changelog row for the data_app.resource_collection_id index
+ changes:
+ - sql:
+ sql: >-
+ DELETE FROM DATABASECHANGELOG WHERE id = 'v65.2026-10-06T00:00:00'
+ rollback: []
+
+ - changeSet:
+ id: p8k2nw
+ author: timofei
+ comment: Index data apps by their resource collection (PostgreSQL), where no index exists yet
+ dbms: postgresql
+ preConditions:
+ - onFail: MARK_RAN
+ - not:
+ - indexExists:
+ tableName: data_app
+ indexName: idx_data_app_resource_collection_id
+ changes:
+ - createIndex:
+ tableName: data_app
+ indexName: idx_data_app_resource_collection_id
+ columns:
+ - column:
+ name: resource_collection_id
+ rollback:
+ - dropIndex:
+ tableName: data_app
+ indexName: idx_data_app_resource_collection_id
diff --git a/test/metabase/app_db/general_schema_tests.clj b/test/metabase/app_db/general_schema_tests.clj
index dbe4c00f246..d735aa011ee 100644
--- a/test/metabase/app_db/general_schema_tests.clj
+++ b/test/metabase/app_db/general_schema_tests.clj
@@ -18,7 +18,10 @@
(deftest ^:parallel fks-are-indexed-test
(when (= (mdb/db-type) :postgres)
(let [excluded-fks #{{:table_name "data_app"
- :column_name "creator_id"}}
+ :column_name "creator_id"}
+ ;; this column only exists on release 64: a v65 migration drops it, so it doesn't need an index
+ {:table_name "data_app"
+ :column_name "permission_group_id"}}
indexed-fks (t2/query
"SELECT
conrelid::regclass::text AS table_name,