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
Add missing FKs (#77713)
Alexander Polyankin4c9497c75cdb
diff --git a/bin/check-migration-versions b/bin/check-migration-versionsindex 9a68e22b788..2bfa0ea9578 100755--- a/bin/check-migration-versions+++ b/bin/check-migration-versions@@ -40,7 +40,7 @@ elif [[ "$BASE_REF" == "master" ]]; thenecho "CURRENT_VERSION: ${CURRENT_VERSION}"matches=0- for l in backport double-backport triple-backport; do+ for l in backport single-backport double-backport triple-backport; doif [[ ",$labels," == *",$l,"* ]]; then# N.B. not ((matches++)) — that exits 1 when matches=0 under set -ematches=$((matches + 1))@@ -51,6 +51,7 @@ elif [[ "$BASE_REF" == "master" ]]; thenversion=$((CURRENT_VERSION + 1))# Backport labels shift the target version back so migrations land on the correct release branch.[[ ",$labels," == *",backport,"* ]] && version=$((CURRENT_VERSION))+ [[ ",$labels," == *",single-backport,"* ]] && version=$((CURRENT_VERSION))[[ ",$labels," == *",double-backport,"* ]] && version=$((CURRENT_VERSION - 1))[[ ",$labels," == *",triple-backport,"* ]] && version=$((CURRENT_VERSION - 2))@@ -104,7 +105,7 @@ if [[ "$errors" -gt 0 ]]; thenfiecho "" >&2echo "If this PR will be backported, ensure the correct backport label is applied" >&2- echo "(backport, double-backport, or triple-backport) and re-run CI, even if you" >&2+ echo "(backport, single-backport, double-backport, or triple-backport) and re-run CI, even if you" >&2echo "plan to create the backport PRs manually. Base the label on the oldest" >&2echo "release you're targeting, and simply close any unwanted PRs." >&2echo "" >&2diff --git a/resources/migrations/063/20260714_field_fk_target_field_id_fk.yaml b/resources/migrations/063/20260714_field_fk_target_field_id_fk.yamlnew file mode 100644index 00000000000..5c9d4b2bf63--- /dev/null+++ b/resources/migrations/063/20260714_field_fk_target_field_id_fk.yaml@@ -0,0 +1,316 @@+## Adds a real FK constraint and an index on metabase_field.fk_target_field_id. Nothing enforced+## this reference before, so dangling values (targets hard-deleted via database/table/field+## cascades) are nulled out first.++databaseChangeLog:+ - changeSet:+ id: v63.2026-07-14T00:00:00+ author: ranquild+ comment: Null out metabase_field.fk_target_field_id values that point to deleted fields+ changes:+ - sql:+ dbms: mysql,mariadb+ sql: >-+ UPDATE metabase_field AS a+ LEFT JOIN metabase_field AS b ON b.id = a.fk_target_field_id+ SET a.fk_target_field_id = NULL+ WHERE a.fk_target_field_id IS NOT NULL AND b.id IS NULL;+ - sql:+ dbms: postgresql,h2+ sql: >-+ UPDATE metabase_field+ SET fk_target_field_id = NULL+ WHERE fk_target_field_id IS NOT NULL+ AND fk_target_field_id NOT IN (SELECT id FROM metabase_field);+ rollback: # no change++ ## The index is created before the FK constraint below: MySQL/MariaDB require an index to back+ ## every FK and reuse an existing one, so this keeps rollback working (the FK is dropped before+ ## the index it would otherwise still need). Postgres doesn't auto-index FKs, which is what+ ## fks-are-indexed-test checks for.+ - changeSet:+ id: v63.2026-07-14T00:00:01+ author: ranquild+ comment: Index metabase_field.fk_target_field_id+ changes:+ - createIndex:+ tableName: metabase_field+ indexName: idx_field_fk_target_field_id+ columns:+ - column:+ name: fk_target_field_id++ - changeSet:+ id: v63.2026-07-14T00:00:02+ author: ranquild+ comment: Add FK constraint on metabase_field.fk_target_field_id+ changes:+ - addForeignKeyConstraint:+ baseTableName: metabase_field+ baseColumnNames: fk_target_field_id+ referencedTableName: metabase_field+ referencedColumnNames: id+ constraintName: fk_field_fk_target_ref_field_id+ onDelete: SET NULL++ ## metabase_field_user_settings.fk_target_field_id mirrors metabase_field.fk_target_field_id+ ## (it stores the user-set override of the same reference) and had the same problem: nothing+ ## enforced it, so it gets the same cleanup + index + FK treatment.+ - changeSet:+ id: v63.2026-07-14T00:00:03+ author: ranquild+ comment: Null out metabase_field_user_settings.fk_target_field_id values that point to deleted fields+ changes:+ - sql:+ dbms: mysql,mariadb+ sql: >-+ UPDATE metabase_field_user_settings AS a+ LEFT JOIN metabase_field AS b ON b.id = a.fk_target_field_id+ SET a.fk_target_field_id = NULL+ WHERE a.fk_target_field_id IS NOT NULL AND b.id IS NULL;+ - sql:+ dbms: postgresql,h2+ sql: >-+ UPDATE metabase_field_user_settings+ SET fk_target_field_id = NULL+ WHERE fk_target_field_id IS NOT NULL+ AND fk_target_field_id NOT IN (SELECT id FROM metabase_field);+ rollback: # no change++ - changeSet:+ id: v63.2026-07-14T00:00:04+ author: ranquild+ comment: Index metabase_field_user_settings.fk_target_field_id+ changes:+ - createIndex:+ tableName: metabase_field_user_settings+ indexName: idx_field_user_settings_fk_target_field_id+ columns:+ - column:+ name: fk_target_field_id++ - changeSet:+ id: v63.2026-07-14T00:00:05+ author: ranquild+ comment: Add FK constraint on metabase_field_user_settings.fk_target_field_id+ changes:+ - addForeignKeyConstraint:+ baseTableName: metabase_field_user_settings+ baseColumnNames: fk_target_field_id+ referencedTableName: metabase_field+ referencedColumnNames: id+ constraintName: fk_field_user_settings_fk_target_ref_field_id+ onDelete: SET NULL++ ## oauth_refresh_token.user_id is documented as an FK to core_user.id and its sibling tables+ ## (oauth_authorization_code, oauth_access_token) got an index + FK when they were created,+ ## but the refresh-token table was left unconstrained. A refresh token whose user is gone is+ ## unusable (and undesirable to keep), so dangling rows are deleted rather than nulled:+ ## user_id NULL is reserved for tokens that were never tied to a user.+ - changeSet:+ id: v63.2026-07-14T00:00:06+ author: ranquild+ comment: Delete oauth_refresh_token rows whose user has been deleted+ changes:+ - sql:+ dbms: mysql,mariadb+ sql: >-+ DELETE a FROM oauth_refresh_token AS a+ LEFT JOIN core_user AS b ON b.id = a.user_id+ WHERE a.user_id IS NOT NULL AND b.id IS NULL;+ - sql:+ dbms: postgresql,h2+ sql: >-+ DELETE FROM oauth_refresh_token+ WHERE user_id IS NOT NULL+ AND user_id NOT IN (SELECT id FROM core_user);+ rollback: # no change++ - changeSet:+ id: v63.2026-07-14T00:00:07+ author: ranquild+ comment: Index oauth_refresh_token.user_id+ changes:+ - createIndex:+ tableName: oauth_refresh_token+ indexName: idx_oauth_refresh_token_user_id+ columns:+ - column:+ name: user_id++ - changeSet:+ id: v63.2026-07-14T00:00:08+ author: ranquild+ comment: Add FK on oauth_refresh_token.user_id+ changes:+ - addForeignKeyConstraint:+ baseTableName: oauth_refresh_token+ baseColumnNames: user_id+ referencedTableName: core_user+ referencedColumnNames: id+ constraintName: fk_oauth_refresh_token_user_id++ ## pulse_channel.channel_id has had an FK since v51 but never an explicit index; it was+ ## grandfathered into the excluded-fks set of fks-are-indexed-test. The FK is dropped and+ ## re-added around the index creation so the explicit index becomes the FK's backing index on+ ## every database: adding the index while the FK exists would make MySQL adopt it in place of+ ## its auto-generated one, and the rollback's DROP INDEX would then fail with errno 1553.+ - changeSet:+ id: v63.2026-07-14T00:00:09+ author: ranquild+ comment: Drop FK on pulse_channel.channel_id ahead of indexing the column+ changes:+ - dropForeignKeyConstraint:+ baseTableName: pulse_channel+ constraintName: fk_pulse_channel_channel_id+ rollback:+ - addForeignKeyConstraint:+ baseTableName: pulse_channel+ baseColumnNames: channel_id+ referencedTableName: channel+ referencedColumnNames: id+ constraintName: fk_pulse_channel_channel_id+ onDelete: CASCADE++ - changeSet:+ id: v63.2026-07-14T00:00:10+ author: ranquild+ comment: Index pulse_channel.channel_id+ changes:+ - createIndex:+ tableName: pulse_channel+ indexName: idx_pulse_channel_channel_id+ columns:+ - column:+ name: channel_id++ - changeSet:+ id: v63.2026-07-14T00:00:11+ author: ranquild+ comment: Re-add FK on pulse_channel.channel_id, backed by the new index+ changes:+ - addForeignKeyConstraint:+ baseTableName: pulse_channel+ baseColumnNames: channel_id+ referencedTableName: channel+ referencedColumnNames: id+ constraintName: fk_pulse_channel_channel_id+ onDelete: CASCADE++ ## Polymorphic references (type + id column pairs) can't have FK constraints, but every other+ ## polymorphic pair in the schema (comment, dependency, cache_config, task_run, ...) carries a+ ## composite (type, id) index. These four were missing theirs.+ - changeSet:+ id: v63.2026-07-14T00:00:12+ author: ranquild+ comment: Index audit_log (model, model_id)+ changes:+ - createIndex:+ tableName: audit_log+ indexName: idx_audit_log_model_model_id+ columns:+ - column:+ name: model+ - column:+ name: model_id++ - changeSet:+ id: v63.2026-07-14T00:00:13+ author: ranquild+ comment: Index recent_views (model, model_id)+ changes:+ - createIndex:+ tableName: recent_views+ indexName: idx_recent_views_model_model_id+ columns:+ - column:+ name: model+ - column:+ name: model_id++ - changeSet:+ id: v63.2026-07-14T00:00:14+ author: ranquild+ comment: Index notification (payload_type, payload_id)+ changes:+ - createIndex:+ tableName: notification+ indexName: idx_notification_payload_type_payload_id+ columns:+ - column:+ name: payload_type+ - column:+ name: payload_id++ - changeSet:+ id: v63.2026-07-14T00:00:15+ author: ranquild+ comment: Index source_replacement_run (source_entity_type, source_entity_id)+ changes:+ - createIndex:+ tableName: source_replacement_run+ indexName: idx_source_replacement_run_source_entity+ columns:+ - column:+ name: source_entity_type+ - column:+ name: source_entity_id++ - changeSet:+ id: v63.2026-07-14T00:00:16+ author: ranquild+ comment: Index source_replacement_run (target_entity_type, target_entity_id)+ changes:+ - createIndex:+ tableName: source_replacement_run+ indexName: idx_source_replacement_run_target_entity+ columns:+ - column:+ name: target_entity_type+ - column:+ name: target_entity_id++ ## workspace.creator_id is in the same boat as pulse_channel.channel_id: an FK since v62 with no+ ## explicit index, grandfathered into the excluded-fks set of fks-are-indexed-test. Same+ ## drop/index/re-add dance for the same MySQL reasons.+ - changeSet:+ id: v63.2026-07-14T00:00:17+ author: ranquild+ comment: Drop FK on workspace.creator_id ahead of indexing the column+ changes:+ - dropForeignKeyConstraint:+ baseTableName: workspace+ constraintName: fk_workspace_creator_id+ rollback:+ - addForeignKeyConstraint:+ baseTableName: workspace+ baseColumnNames: creator_id+ referencedTableName: core_user+ referencedColumnNames: id+ constraintName: fk_workspace_creator_id+ onDelete: SET NULL++ - changeSet:+ id: v63.2026-07-14T00:00:18+ author: ranquild+ comment: Index workspace.creator_id+ changes:+ - createIndex:+ tableName: workspace+ indexName: idx_workspace_creator_id+ columns:+ - column:+ name: creator_id++ - changeSet:+ id: v63.2026-07-14T00:00:19+ author: ranquild+ comment: Re-add FK on workspace.creator_id, backed by the new index+ changes:+ - addForeignKeyConstraint:+ baseTableName: workspace+ baseColumnNames: creator_id+ referencedTableName: core_user+ referencedColumnNames: id+ constraintName: fk_workspace_creator_id+ onDelete: SET NULLdiff --git a/test/metabase/app_db/general_schema_tests.clj b/test/metabase/app_db/general_schema_tests.cljindex daafc8b3d87..d2b7c190fe9 100644--- a/test/metabase/app_db/general_schema_tests.clj+++ b/test/metabase/app_db/general_schema_tests.clj@@ -17,10 +17,7 @@(deftest ^:parallel fks-are-indexed-test(when (= (mdb/db-type) :postgres)- (let [excluded-fks #{{:table_name "pulse_channel"- :column_name "channel_id"}- {:table_name "workspace"- :column_name "creator_id"}}+ (let [excluded-fks #{}indexed-fks (t2/query"SELECTconrelid::regclass::text AS table_name,