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
Fix task_run.notification_id backfill migration on MySQL (#76509)
Ngoc Khuat7ae5004486ab
diff --git a/resources/migrations/062/20260606_task_run_notification_id.yaml b/resources/migrations/062/20260606_task_run_notification_id.yamlindex 39afc960822..79c9873ad6d 100644--- a/resources/migrations/062/20260606_task_run_notification_id.yaml+++ b/resources/migrations/062/20260606_task_run_notification_id.yaml@@ -64,6 +64,13 @@ databaseChangeLog:id: v62.2026-06-06T00:00:02author: qnkhuatcomment: Backfill task_run.notification_id for the last 90 days (Postgres/MySQL; H2 ages out)+ # Keep this changeset's body unchanged for cross-branch checksum compatibility, but never+ # execute it. v62.2026-06-06T00:00:03 performs the safe replacement backfill.+ preConditions:+ - onFail: MARK_RAN+ - sqlCheck:+ expectedResult: 1+ sql: SELECT 0;changes:- sql:dbms: postgresql@@ -93,3 +100,48 @@ databaseChangeLog:AND tr.notification_id IS NULLAND tr.started_at > now() - interval 90 day;rollback: # nothing to do — column drop in the first changeSet covers it++ # Redo of 00:00:02. Run only if task_run.notification_id has not been populated anywhere;+ # otherwise MARK_RAN so we do not check again on later startups.+ - changeSet:+ id: v62.2026-06-06T00:00:03+ author: qnkhuat+ comment: Redo task_run.notification_id backfill, strict-mode safe (GDGT-2680)+ failOnError: false+ preConditions:+ - onFail: MARK_RAN+ # If any notification_id has already been written, normal app usage may have self-healed the+ # relevant recent runs. Mark this changeset ran so future startups do not keep rechecking.+ - sqlCheck:+ expectedResult: 0+ sql: SELECT COUNT(*) FROM task_run WHERE notification_id IS NOT NULL;+ changes:+ - sql:+ dbms: postgresql+ sql: >-+ UPDATE task_run tr+ SET notification_id = COALESCE(+ (th.task_details::jsonb) ->> 'notification_id',+ (th.task_details::jsonb) #>> '{original-info,notification_id}'+ )::int+ FROM task_history th+ WHERE th.run_id = tr.id+ AND th.task = 'notification-send'+ AND tr.run_type IN ('alert', 'subscription')+ AND tr.notification_id IS NULL+ AND tr.started_at > now() - interval '90 days';+ - sql:+ dbms: mysql,mariadb+ sql: >-+ UPDATE task_run tr+ JOIN task_history th ON th.run_id = tr.id+ SET tr.notification_id = CAST(COALESCE(+ NULLIF(JSON_UNQUOTE(JSON_EXTRACT(th.task_details, '$.notification_id')), 'null'),+ NULLIF(JSON_UNQUOTE(JSON_EXTRACT(th.task_details, '$."original-info".notification_id')), 'null')+ ) AS UNSIGNED)+ WHERE th.task = 'notification-send'+ AND JSON_VALID(th.task_details)+ AND tr.run_type IN ('alert', 'subscription')+ AND tr.notification_id IS NULL+ AND tr.started_at > now() - interval 90 day;+ rollback: # nothing to do — column drop in the first changeSet covers itdiff --git a/test/metabase/app_db/schema_migrations_test.clj b/test/metabase/app_db/schema_migrations_test.cljindex 84665ce6886..4987a64e6f8 100644--- a/test/metabase/app_db/schema_migrations_test.clj+++ b/test/metabase/app_db/schema_migrations_test.clj@@ -3163,14 +3163,29 @@too-old (new-run! old "alert" "success") ; outside the 90-day windowchan-only (new-run! recent "alert" "success") ; only a channel-send child, no notification-sendsubscript (new-run! recent "subscription" "success") ; dashboard subscription, also attributed+ jnull (new-run! recent "alert" "success") ; JSON-null id top-level (GDGT-2680)+ jnull-orig (new-run! recent "alert" "failed") ; JSON-null id under original-info (GDGT-2680)+ no-id (new-run! recent "alert" "success") ; no notification_id key at all+ no-id-orig (new-run! recent "alert" "failed") ; original-info present but no notification_id key+ invalid (new-run! recent "alert" "success") ; non-JSON task_details — MySQL only (GDGT-2680)other (new-run! recent "sync" "success")] ; neither alert nor subscription- (new-th! success "notification-send" "success" "{\"notification_id\":101}")- (new-th! failed "notification-send" "failed" "{\"status\":\"failed\",\"message\":\"boom\",\"original-info\":{\"notification_id\":102}}")+ (new-th! success "notification-send" "success" "{\"notification_id\":101}")+ (new-th! failed "notification-send" "failed" "{\"status\":\"failed\",\"message\":\"boom\",\"original-info\":{\"notification_id\":102}}");; `abandoned` intentionally has NO task_history (the run was killed before writing one)- (new-th! too-old "notification-send" "success" "{\"notification_id\":104}")- (new-th! chan-only "channel-send" "success" "{\"notification_id\":105,\"channel_type\":\"channel/email\"}")- (new-th! subscript "notification-send" "success" "{\"notification_id\":107}")- (new-th! other "notification-send" "success" "{\"notification_id\":106}")+ (new-th! too-old "notification-send" "success" "{\"notification_id\":104}")+ (new-th! chan-only "channel-send" "success" "{\"notification_id\":105,\"channel_type\":\"channel/email\"}")+ (new-th! subscript "notification-send" "success" "{\"notification_id\":107}")+ ;; a JSON-null value (not an absent key) made MySQL strict mode reject CAST('null' AS UNSIGNED)+ (new-th! jnull "notification-send" "success" "{\"notification_id\":null}")+ (new-th! jnull-orig "notification-send" "failed" "{\"status\":\"failed\",\"original-info\":{\"notification_id\":null}}")+ ;; valid JSON, but the notification_id key is simply absent (top-level, and inside original-info)+ (new-th! no-id "notification-send" "success" "{\"status\":\"skipped\"}")+ (new-th! no-id-orig "notification-send" "failed" "{\"status\":\"failed\",\"original-info\":{\"status\":\"started\"}}")+ (new-th! other "notification-send" "success" "{\"notification_id\":106}")+ ;; MySQL-only: invalid JSON is skipped by the JSON_VALID guard. Postgres' ::jsonb cast has no+ ;; such guard and would throw on a non-JSON row, so this case can't run there.+ (when (= :mysql (mdb/db-type))+ (new-th! invalid "notification-send" "success" "not-json"))(migrate!)(let [nid #(t2/select-one-fn :notification_id :task_run :id %)](testing "attributed from a notification-send child in the window"@@ -3188,4 +3203,13 @@(testing "channel-send-only run (no notification-send)"(is (nil? (nid chan-only))))(testing "neither alert nor subscription"- (is (nil? (nid other)))))))))))+ (is (nil? (nid other))))+ (testing "JSON-null notification_id stays null instead of crashing the migration (GDGT-2680)"+ (is (nil? (nid jnull)))+ (is (nil? (nid jnull-orig))))+ (testing "task_details with no notification_id key stays null"+ (is (nil? (nid no-id)))+ (is (nil? (nid no-id-orig))))+ (when (= :mysql (mdb/db-type))+ (testing "invalid JSON task_details is skipped by JSON_VALID, run stays null (GDGT-2680)"+ (is (nil? (nid invalid))))))))))))