Skip to content

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

6.3 KB
Raw Blame
## Add task_run.notification_id so alert/subscription runs can be attributed to the exact
## notification that produced them (instead of only to the shared card/dashboard entity).
## Backfill is Postgres/MySQL only — H2 self-heals as runs age out of the 90-day display window.

databaseChangeLog:

  - changeSet:
      id: v62.2026-06-06T00:00:00
      author: qnkhuat
      comment: Add task_run.notification_id to attribute a run to the notification that produced it
      preConditions:
        - onFail: MARK_RAN
        - not:
            - columnExists:
                tableName: task_run
                columnName: notification_id
      # No FK: entity_id here is already FK-less (it's polymorphic), and task_run is a 90-day
      # operational log, not a relational parent.
      changes:
        - addColumn:
            tableName: task_run
            columns:
              - column:
                  name: notification_id
                  type: int
                  remarks: ID of the notification that produced this run (null for non-notification runs, or runs predating this column)
                  constraints:
                    nullable: true
      rollback:
        - dropColumn:
            tableName: task_run
            columnName: notification_id

  - changeSet:
      id: v62.2026-06-06T00:00:01
      author: qnkhuat
      comment: Index task_run by (notification_id, started_at) for per-notification latest-run lookups
      preConditions:
        - onFail: MARK_RAN
        - not:
            - indexExists:
                indexName: idx_task_run_notification_id_started_at
                tableName: task_run
      changes:
        - createIndex:
            tableName: task_run
            indexName: idx_task_run_notification_id_started_at
            columns:
              - column:
                  name: notification_id
              - column:
                  name: started_at
                  descending: true
      rollback:
        - dropIndex:
            tableName: task_run
            indexName: idx_task_run_notification_id_started_at

  # Backfill the 90-day window (alerts + subscriptions) from each run's notification-send
  # task_history child. notification_id sits in task_details JSON, top-level or under `original-info`
  # on failure. task_details is always JSON-serialized by the model, so the cast is safe; a row
  # missing the key just yields NULL. The two SQL changes are dbms-gated; H2 matches neither and
  # ages out on its own.
  - changeSet:
      id: v62.2026-06-06T00:00:02
      author: qnkhuat
      comment: 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
            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(
                    JSON_UNQUOTE(JSON_EXTRACT(th.task_details, '$.notification_id')),
                    JSON_UNQUOTE(JSON_EXTRACT(th.task_details, '$."original-info".notification_id'))
                  ) AS UNSIGNED)
              WHERE th.task = 'notification-send'
                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 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 it