Skip to content

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

122 KB
Raw Blame
databaseChangeLog:
  - objectQuotingStrategy: QUOTE_ALL_OBJECTS

  - changeSet:
      id: v59.2025-12-01T18:53:23
      author: nvoxland
      comment: Add source_database_id column to transform table
      changes:
        - addColumn:
            tableName: transform
            columns:
              - column:
                  name: source_database_id
                  type: int
                  remarks: The database containing the source data for this transform

  - changeSet:
      id: v59.2025-12-01T18:53:24
      author: nvoxland
      comment: Add index for transform.source_database_id
      changes:
        - createIndex:
            tableName: transform
            indexName: idx_transform_source_database_id
            columns:
              - column:
                  name: source_database_id

  - changeSet:
      id: v59.2025-12-01T18:53:25
      author: nvoxland
      comment: Add foreign key on transform(source_database_id)
      changes:
        - addForeignKeyConstraint:
            baseTableName: transform
            baseColumnNames: source_database_id
            referencedTableName: metabase_database
            referencedColumnNames: id
            constraintName: fk_tranform_source_database_id

  - changeSet:
      id: v59.2025-12-01T18:53:26
      author: nvoxland
      comment: Transfer data from transform.source to transform.source_database_id
      changes:
        - customChange:
            class: "metabase.app_db.custom_migrations.SetTransformSourceDatabaseId"
      rollback: # No need to roll back

  - changeSet:
      id: v59.2025-12-01T18:53:27
      author: nvoxland
      comment: Make transform.source_database_id not nullable
      changes:
        - addNotNullConstraint:
            tableName: transform
            columnName: source_database_id
            columnDataType: int

  - changeSet:
      id: v59.2025-12-03T17:08:36
      author: nvoxland
      comment: Add collection_id column to transform table
      preConditions:
        - onFail: MARK_RAN
        - not:
            changeSetExecuted:
              id: v58.2025-12-03T17:08:36
              author: nvoxland
              changeLogFile: migrations/058_update_migrations.yaml
        - not:
            columnExists:
              tableName: transform
              columnName: collection_id
      changes:
        - addColumn:
            tableName: transform
            columns:
              - column:
                  name: collection_id
                  type: int
                  remarks: The collection the transform is in

  - changeSet:
      id: v59.2025-12-03T17:08:37
      author: nvoxland
      comment: Add index on transform.collection_id
      preConditions:
        - onFail: MARK_RAN
        - not:
            changeSetExecuted:
              id: v58.2025-12-03T17:08:37
              author: nvoxland
              changeLogFile: migrations/058_update_migrations.yaml
        - not:
            indexExists:
              tableName: transform
              indexName: idx_transform_collection_id
      changes:
        - createIndex:
            tableName: transform
            indexName: idx_transform_collection_id
            columns:
              - column:
                  name: collection_id

  - changeSet:
      id: v59.2025-12-03T17:08:38
      author: nvoxland
      comment: Add fk_transform_collection_id
      preConditions:
        - onFail: MARK_RAN
        - not:
            changeSetExecuted:
              id: v58.2025-12-03T17:08:38
              author: nvoxland
              changeLogFile: migrations/058_update_migrations.yaml
        - not:
            foreignKeyConstraintExists:
              foreignKeyName: fk_transform_collection_id
              foreignKeyTableName: transform
      changes:
        - addForeignKeyConstraint:
            baseTableName: transform
            baseColumnNames: collection_id
            referencedTableName: collection
            referencedColumnNames: id
            constraintName: fk_transform_collection_id

  - changeSet:
      id: v59.2025-12-04T12:00:00
      author: metabase
      comment: Create measure table for saved aggregation expressions
      changes:
        - createTable:
            tableName: measure
            remarks: Stores saved aggregation expressions tied to tables
            columns:
              - column:
                  name: id
                  type: int
                  autoIncrement: true
                  constraints:
                    primaryKey: true
                    nullable: false
              - column:
                  name: table_id
                  type: int
                  remarks: Foreign key to metabase_table
                  constraints:
                    nullable: false
              - column:
                  name: creator_id
                  type: int
                  remarks: Foreign key to core_user who created this measure
                  constraints:
                    nullable: false
              - column:
                  name: name
                  type: varchar(254)
                  remarks: Name of the measure
                  constraints:
                    nullable: false
              - column:
                  name: description
                  type: ${text.type}
                  remarks: Optional description of the measure
                  constraints:
                    nullable: true
              - column:
                  name: archived
                  type: ${boolean.type}
                  defaultValueBoolean: false
                  remarks: Whether the measure has been archived
                  constraints:
                    nullable: false
              - column:
                  name: definition
                  type: ${text.type}
                  remarks: JSON MBQL query containing the aggregation definition
                  constraints:
                    nullable: false
              - column:
                  name: created_at
                  type: ${timestamp_type}
                  remarks: Timestamp when this measure was created
                  constraints:
                    nullable: false
              - column:
                  name: updated_at
                  type: ${timestamp_type}
                  remarks: Timestamp when this measure was last updated
                  constraints:
                    nullable: false
              - column:
                  name: entity_id
                  type: char(21)
                  remarks: Random NanoID tag for unique identity
                  constraints:
                    nullable: true
                    unique: true
              - column:
                  name: dependency_analysis_version
                  type: integer
                  defaultValueNumeric: 0
                  remarks: Version number for dependency analysis to track when dependencies need recalculation
                  constraints:
                    nullable: false

  - changeSet:
      id: v59.2025-12-04T12:00:01
      author: metabase
      comment: Add index on measure.table_id
      changes:
        - createIndex:
            tableName: measure
            indexName: idx_measure_table_id
            columns:
              - column:
                  name: table_id
      rollback:
        - dropIndex:
            tableName: measure
            indexName: idx_measure_table_id

  - changeSet:
      id: v59.2025-12-04T12:00:02
      author: metabase
      comment: Add foreign key from measure to metabase_table
      changes:
        - addForeignKeyConstraint:
            baseTableName: measure
            baseColumnNames: table_id
            referencedTableName: metabase_table
            referencedColumnNames: id
            constraintName: fk_measure_ref_table_id
            onDelete: CASCADE
      rollback:
        - dropForeignKeyConstraint:
            baseTableName: measure
            constraintName: fk_measure_ref_table_id

  - changeSet:
      id: v59.2025-12-04T12:00:03
      author: metabase
      comment: Add index on measure.creator_id
      changes:
        - createIndex:
            tableName: measure
            indexName: idx_measure_creator_id
            columns:
              - column:
                  name: creator_id
      rollback:
        - dropIndex:
            tableName: measure
            indexName: idx_measure_creator_id

  - changeSet:
      id: v59.2025-12-04T12:00:04
      author: metabase
      comment: Add foreign key from measure to core_user
      changes:
        - addForeignKeyConstraint:
            baseTableName: measure
            baseColumnNames: creator_id
            referencedTableName: core_user
            referencedColumnNames: id
            constraintName: fk_measure_ref_creator_id
            onDelete: CASCADE
      rollback:
        - dropForeignKeyConstraint:
            baseTableName: measure
            constraintName: fk_measure_ref_creator_id

  - changeSet:
      id: v59.2025-12-30T22:48:02
      author: edpaget
      comment: Add remote_sync_object.model_table_id for parent table reference
      preConditions:
        - onFail: MARK_RAN
        - not:
            changeSetExecuted:
              id: v58.2025-12-30T22:48:02
              author: edpaget
              changeLogFile: migrations/058_update_migrations.yaml
        - not:
            columnExists:
              tableName: remote_sync_object
              columnName: model_table_id
      changes:
        - addColumn:
            tableName: remote_sync_object
            columns:
              - column:
                  name: model_table_id
                  type: int
                  remarks: Parent table ID for Field/Segment/Table models
                  constraints:
                    nullable: true

  - changeSet:
      id: v59.2025-12-30T22:48:03
      author: edpaget
      comment: Add remote_sync_object.model_table_name for parent table name
      preConditions:
        - onFail: MARK_RAN
        - not:
            changeSetExecuted:
              id: v58.2025-12-30T22:48:03
              author: edpaget
              changeLogFile: migrations/058_update_migrations.yaml
        - not:
            columnExists:
              tableName: remote_sync_object
              columnName: model_table_name
      changes:
        - addColumn:
            tableName: remote_sync_object
            columns:
              - column:
                  name: model_table_name
                  type: varchar(255)
                  remarks: Parent table name for Field/Segment/Table models
                  constraints:
                    nullable: true

  - changeSet:
      id: v59.2026-01-05T12:00:00
      author: metasasha
      comment: Add is_data_analyst column to core_user table for analyst role
      preConditions:
        - onFail: MARK_RAN
        - not:
            changeSetExecuted:
              id: v58.2026-01-05T12:00:00
              author: metasasha
              changeLogFile: migrations/058_update_migrations.yaml
        - not:
            columnExists:
              tableName: core_user
              columnName: is_data_analyst
      changes:
        - addColumn:
            tableName: core_user
            columns:
              - column:
                  name: is_data_analyst
                  type: ${boolean.type}
                  defaultValueBoolean: false
                  remarks: Indicates if the user has data analyst privileges for Data Studio
                  constraints:
                    nullable: false

  - changeSet:
      id: v59.2026-01-06T10:00:00
      author: gantoreno
      comment: Add owner_user_id column to transform table
      changes:
        - addColumn:
            tableName: transform
            columns:
              - column:
                  name: owner_user_id
                  type: int
                  remarks: The user ID of the owner of this transform (can be different from creator)

  - changeSet:
      id: v59.2026-01-06T10:00:01
      author: gantoreno
      comment: Add owner_email column to transform table
      changes:
        - addColumn:
            tableName: transform
            columns:
              - column:
                  name: owner_email
                  type: ${text.type}
                  remarks: An optional email address of the owner of this transform (for external owners), exclusive with owner_user_id

  - changeSet:
      id: v59.2026-01-06T10:00:02
      author: gantoreno
      comment: Add index on transform.owner_user_id
      changes:
        - createIndex:
            tableName: transform
            indexName: idx_transform_owner_user_id
            columns:
              - column:
                  name: owner_user_id

  - changeSet:
      id: v59.2026-01-06T10:00:03
      author: gantoreno
      comment: Add foreign key constraint for transform.owner_user_id
      changes:
        - addForeignKeyConstraint:
            baseTableName: transform
            baseColumnNames: owner_user_id
            referencedTableName: core_user
            referencedColumnNames: id
            constraintName: fk_transform_owner_user_id
            onDelete: SET NULL

  - changeSet:
      id: v59.2026-01-06T10:00:04
      author: gantoreno
      comment: Backfill owner_user_id from creator_id for existing transforms
      changes:
        - sql:
            sql: >-
              UPDATE transform SET owner_user_id = creator_id WHERE owner_user_id IS NULL AND creator_id IS NOT NULL AND creator_id != 13371338
      rollback:
        - sql:
            sql: >-
              UPDATE transform SET owner_user_id = NULL

  - changeSet:
      id: v59.2026-01-06T10:00:05
      author: gantoreno
      comment: Add user_id column to transform_run table for tracking who triggered the run
      changes:
        - addColumn:
            tableName: transform_run
            columns:
              - column:
                  name: user_id
                  type: int
                  remarks: The user ID who triggered this run (owner for scheduled runs, current user for manual runs)

  - changeSet:
      id: v59.2026-01-06T10:00:06
      author: gantoreno
      comment: Add index on transform_run.user_id
      changes:
        - createIndex:
            tableName: transform_run
            indexName: idx_transform_run_user_id
            columns:
              - column:
                  name: user_id

  - changeSet:
      id: v59.2026-01-06T10:00:07
      author: gantoreno
      comment: Add foreign key constraint for transform_run.user_id
      changes:
        - addForeignKeyConstraint:
            baseTableName: transform_run
            baseColumnNames: user_id
            referencedTableName: core_user
            referencedColumnNames: id
            constraintName: fk_transform_run_user_id
            onDelete: SET NULL

  - changeSet:
      id: v59.2026-01-07T00:00:01
      author: bshepherdson
      comment: Create analysis finding table
      changes:
        - createTable:
            tableName: analysis_finding
            remarks: Findings from analysis of cards, transforms, dashboards, etc.
            columns:
              - column:
                  name: id
                  remarks: Unique ID
                  type: int
                  autoIncrement: true
                  constraints:
                    primaryKey: true
                    nullable: false
              - column:
                  name: analyzed_entity_type
                  type: varchar(254)
                  remarks: The type of the analyzed entity
                  constraints:
                    nullable: false
              - column:
                  name: analyzed_entity_id
                  type: int
                  remarks: The ID of the analyzed entity
                  constraints:
                    nullable: false
              - column:
                  name: analysis_version
                  remarks: The version of the analysis that was performed
                  type: int
                  constraints:
                    nullable: false
              - column:
                  name: analyzed_at
                  remarks: The timestamp of when the analysis was performed
                  type: ${timestamp_type}
                  defaultValueComputed: current_timestamp
                  constraints:
                    nullable: false
              - column:
                  name: result
                  type: ${boolean.type}
                  remarks: The result of the analysis - true for passed, false for failed
                  constraints:
                    nullable: false
              - column:
                  name: finding_details
                  type: ${text.type}
                  remarks: Arbitrary EDN text of the findings
                  constraints:
                    nullable: true

  - changeSet:
      id: v59.2026-01-07T00:00:02
      author: bshepherdson
      comment: Unique constraint to prevent duplicate analyses
      changes:
        - addUniqueConstraint:
            tableName: analysis_finding
            columnNames: analyzed_entity_type, analyzed_entity_id
            constraintName: idx_unique_analysis_finding

  - changeSet:
      id: v59.2026-01-07T00:00:03
      author: bshepherdson
      comment: Index for analysis_finding by analyzed entity
      changes:
        - createIndex:
            tableName: analysis_finding
            indexName: idx_analyzed_entity
            columns:
              - column:
                  name: analyzed_entity_type
              - column:
                  name: analyzed_entity_id

  - changeSet:
      id: v59.2026-01-07T00:00:04
      author: bshepherdson
      comment: Index for analysis_finding by analysis result
      changes:
        - createIndex:
            tableName: analysis_finding
            indexName: idx_analysis_result
            columns:
              - column:
                  name: result

  - changeSet:
      id: v59.2026-01-09T11:30:00
      author: johnswanson
      comment: |
        Migration `v58.2026-01-09T12:00:00` had an issue for some customers (where a user-defined Data Analysts group
        already existed). We need to replace this migration with a new one, but don't want the old migration to still
        exist in the `databasechangelog`, to avoid incorrectly reporting that the latest migration is a `v58`
        migration (if someone downgrades to v57).
      preConditions:
        - onFail: MARK_RAN
        - not:
            changeSetExecuted:
              id: v58.2026-01-09T11:30:00
              author: johnswanson
              changeLogFile: migrations/058_update_migrations.yaml
      changes:
        - sql:
            dbms: postgres,h2
            sql: >-
              DELETE FROM databasechangelog WHERE id = 'v58.2026-01-09T12:00:00';
        - sql:
            dbms: mysql,mariadb
            sql: >-
              DELETE FROM DATABASECHANGELOG WHERE id = 'v58.2026-01-09T12:00:00';
      rollback: # not needed

  - changeSet:
      id: v59.2026-01-09T11:30:01
      author: johnswanson
      comment: Create data-analyst magic permissions group, handling name conflicts
      validCheckSum: ANY
      preConditions:
        - onFail: MARK_RAN
        - not:
            changeSetExecuted:
              id: v58.2026-01-09T11:30:01
              author: johnswanson
              changeLogFile: migrations/058_update_migrations.yaml
        - sqlCheck:
            expectedResult: 0
            sql: SELECT COUNT(*) FROM permissions_group WHERE magic_group_type = 'data-analyst'
      changes:
        - sql:
            sql: >-
              INSERT INTO permissions_group (name, magic_group_type)
              SELECT
                CASE WHEN EXISTS (SELECT 1 FROM permissions_group WHERE name = 'Data Analysts')
                THEN 'Metabase Data Analysts'
                ELSE 'Data Analysts'
                END,
                'data-analyst'
      rollback:
        - sql:
            sql: DELETE FROM permissions_group WHERE magic_group_type = 'data-analyst';

  - changeSet:
      id: v59.2026-01-09T12:00:01
      author: johnswanson
      comment: Grant read permissions on existing Library collections to Data Analysts group
      preConditions:
        - onFail: MARK_RAN
        - not:
            changeSetExecuted:
              id: v58.2026-01-09T12:00:01
              author: johnswanson
              changeLogFile: migrations/058_update_migrations.yaml
      changes:
        - sql:
            sql: >-
              INSERT INTO permissions (group_id, object)
              SELECT pg.id, '/collection/' || c.id || '/'
              FROM permissions_group pg, collection c
              WHERE pg.magic_group_type = 'data-analyst'
                AND c.type IN ('library', 'library-data', 'library-metrics')
      rollback:
        - sql:
            # We can safely delete ALL permissions for this group because it will be deleted in the rollback above
            sql: >-
              DELETE FROM permissions
              WHERE group_id IN (SELECT id FROM permissions_group WHERE magic_group_type = 'data-analyst');

  - changeSet:
      id: v59.2026-01-16T00:00:00
      author: rileythomp
      comment: Create analysis_finding_error table for storing individual errors with source info
      changes:
        - createTable:
            tableName: analysis_finding_error
            remarks: Individual validation errors from analysis with source entity information
            columns:
              - column:
                  name: id
                  type: int
                  autoIncrement: true
                  constraints:
                    primaryKey: true
                    nullable: false
              - column:
                  name: analyzed_entity_type
                  type: varchar(254)
                  remarks: The type of the analyzed entity (card, transform, segment, etc.)
                  constraints:
                    nullable: false
              - column:
                  name: analyzed_entity_id
                  type: int
                  remarks: The ID of the analyzed entity
                  constraints:
                    nullable: false
              - column:
                  name: error_type
                  type: varchar(254)
                  remarks: The type of error (missing-column, missing-table-alias, duplicate-column, syntax-error, validation-exception-error)
                  constraints:
                    nullable: false
              - column:
                  name: error_detail
                  type: varchar(254)
                  remarks: Additional error details (e.g., column name, alias name, error message)
                  constraints:
                    nullable: true
              - column:
                  name: source_entity_type
                  type: varchar(254)
                  remarks: The type of the source entity causing the error (table, card, etc.)
                  constraints:
                    nullable: true
              - column:
                  name: source_entity_id
                  type: int
                  remarks: The ID of the source entity causing the error
                  constraints:
                    nullable: true
      rollback:
        - dropTable:
            tableName: analysis_finding_error

  - changeSet:
      id: v59.2026-01-16T00:00:01
      author: rileythomp
      comment: Index for querying errors by source entity
      changes:
        - createIndex:
            tableName: analysis_finding_error
            indexName: idx_analysis_finding_error_source
            columns:
              - column:
                  name: source_entity_type
              - column:
                  name: source_entity_id
      rollback:
        - dropIndex:
            tableName: analysis_finding_error
            indexName: idx_analysis_finding_error_source

  - changeSet:
      id: v59.2026-01-16T00:00:02
      author: rileythomp
      comment: Index for querying/deleting errors by analyzed entity
      changes:
        - createIndex:
            tableName: analysis_finding_error
            indexName: idx_analysis_finding_error_analyzed
            columns:
              - column:
                  name: analyzed_entity_type
              - column:
                  name: analyzed_entity_id
      rollback:
        - dropIndex:
            tableName: analysis_finding_error
            indexName: idx_analysis_finding_error_analyzed

  - changeSet:
      id: v59.2026-01-16T00:00:03
      author: rileythomp
      comment: Index for filtering by error type
      changes:
        - createIndex:
            tableName: analysis_finding_error
            indexName: idx_analysis_finding_error_type
            columns:
              - column:
                  name: error_type
      rollback:
        - dropIndex:
            tableName: analysis_finding_error
            indexName: idx_analysis_finding_error_type

  - changeSet:
      id: v59.2026-01-16T00:00:04
      author: rileythomp
      comment: Drop redundant finding_details column from analysis_finding table
      changes:
        - dropColumn:
            tableName: analysis_finding
            columnName: finding_details
      rollback:
        - addColumn:
            tableName: analysis_finding
            columns:
              - column:
                  name: finding_details
                  type: ${text.type}
                  remarks: Arbitrary EDN text of the findings
                  constraints:
                    nullable: true

  - changeSet:
      id: v59.2026-01-16T09:30:00
      author: bronsa
      comment: Create task_run table for grouping related tasks
      changes:
        - createTable:
            tableName: task_run
            remarks: Groups related tasks from a single operation (subscription, alert, sync, fingerprint)
            columns:
              - column:
                  name: id
                  type: int
                  autoIncrement: true
                  constraints:
                    primaryKey: true
                    nullable: false
              - column:
                  name: run_type
                  type: varchar(32)
                  remarks: "Type of run: subscription, alert, sync, fingerprint"
                  constraints:
                    nullable: false
              - column:
                  name: entity_type
                  type: varchar(32)
                  remarks: "Type of associated entity: database, card, dashboard"
                  constraints:
                    nullable: false
              - column:
                  name: entity_id
                  type: int
                  remarks: ID of associated entity
                  constraints:
                    nullable: false
              - column:
                  name: started_at
                  type: ${timestamp_type}
                  remarks: When the run started
                  defaultValueComputed: current_timestamp
                  constraints:
                    nullable: false
              - column:
                  name: ended_at
                  type: ${timestamp_type}
                  remarks: When the run completed (null if still running)
                  constraints:
                    nullable: true
              - column:
                  name: status
                  type: varchar(21)
                  remarks: "Overall status: started, success, failed"
                  constraints:
                    nullable: false
      rollback:
        - dropTable:
            tableName: task_run

  - changeSet:
      id: v59.2026-01-16T09:30:01
      author: bronsa
      comment: Add composite index on task_run for type/entity queries
      changes:
        - createIndex:
            tableName: task_run
            indexName: idx_task_run_type_entity
            columns:
              - column:
                  name: run_type
              - column:
                  name: entity_type
              - column:
                  name: entity_id
      rollback:
        - dropIndex:
            tableName: task_run
            indexName: idx_task_run_type_entity

  - changeSet:
      id: v59.2026-01-16T09:30:02
      author: bronsa
      comment: Add index on task_run.started_at for ordering
      changes:
        - createIndex:
            tableName: task_run
            indexName: idx_task_run_started_at
            columns:
              - column:
                  name: started_at
                  descending: true
      rollback:
        - dropIndex:
            tableName: task_run
            indexName: idx_task_run_started_at

  - changeSet:
      id: v59.2026-01-16T09:30:03
      author: bronsa
      comment: Add run_id column to task_history
      changes:
        - addColumn:
            tableName: task_history
            columns:
              - column:
                  name: run_id
                  type: int
                  remarks: FK to task_run - groups tasks belonging to the same run
                  constraints:
                    nullable: true
      rollback:
        - dropColumn:
            tableName: task_history
            columnName: run_id

  - changeSet:
      id: v59.2026-01-16T09:30:04
      author: bronsa
      comment: Add index on task_history.run_id
      changes:
        - createIndex:
            tableName: task_history
            indexName: idx_task_history_run_id
            columns:
              - column:
                  name: run_id
      rollback:
        - dropIndex:
            tableName: task_history
            indexName: idx_task_history_run_id

  - changeSet:
      id: v59.2026-01-16T09:30:05
      author: bronsa
      comment: Add foreign key from task_history.run_id to task_run
      changes:
        - addForeignKeyConstraint:
            baseTableName: task_history
            baseColumnNames: run_id
            referencedTableName: task_run
            referencedColumnNames: id
            constraintName: fk_task_history_run_id
            onDelete: SET NULL
      rollback:
        - dropForeignKeyConstraint:
            baseTableName: task_history
            constraintName: fk_task_history_run_id

  - changeSet:
      id: v59.2026-01-16T09:30:06
      author: bronsa
      comment: Add logs column to task_history for captured execution logs
      changes:
        - addColumn:
            tableName: task_history
            columns:
              - column:
                  name: logs
                  type: ${text.type}
                  remarks: JSON array of captured log messages during task execution
                  constraints:
                    nullable: true
      rollback:
        - dropColumn:
            tableName: task_history
            columnName: logs

  - changeSet:
      id: v59.2026-01-16T10:00:00
      author: bronsa
      comment: Add process_uuid to task_run for orphan detection
      changes:
        - addColumn:
            tableName: task_run
            columns:
              - column:
                  name: process_uuid
                  type: varchar(36)
                  remarks: UUID of the Metabase instance that created this run
                  defaultValue: "unknown"
                  constraints:
                    nullable: false
      rollback:
        - dropColumn:
            tableName: task_run
            columnName: process_uuid

  - changeSet:
      id: v59.2026-01-16T10:00:01
      author: bronsa
      comment: Add updated_at to task_run for heartbeat tracking
      changes:
        - addColumn:
            tableName: task_run
            columns:
              - column:
                  name: updated_at
                  type: ${timestamp_type}
                  remarks: Last heartbeat timestamp - updated periodically for running tasks
                  defaultValueComputed: current_timestamp
                  constraints:
                    nullable: false
      rollback:
        - dropColumn:
            tableName: task_run
            columnName: updated_at

  - changeSet:
      id: v59.2026-01-16T11:00:00
      author: bronsa
      comment: Usage analytics; Create v_task_runs view for task run analytics
      changes:
        - sqlFile:
            dbms: postgresql
            path: instance_analytics_views/task_runs/v1/postgres-task_runs.sql
            relativeToChangelogFile: true
        - sqlFile:
            dbms: mysql,mariadb
            path: instance_analytics_views/task_runs/v1/mysql-task_runs.sql
            relativeToChangelogFile: true
        - sqlFile:
            dbms: h2
            path: instance_analytics_views/task_runs/v1/h2-task_runs.sql
            relativeToChangelogFile: true
      rollback:
        - sql:
            dbms: postgresql,mysql,mariadb,h2
            sql: drop view if exists v_task_runs;

  - changeSet:
      id: v59.2026-01-16T11:00:01
      author: bronsa
      comment: Usage analytics; Update v_tasks view to include run_id and logs columns
      changes:
        - sqlFile:
            dbms: postgresql
            path: instance_analytics_views/tasks/v4/postgres-tasks.sql
            relativeToChangelogFile: true
        - sqlFile:
            dbms: mysql,mariadb
            path: instance_analytics_views/tasks/v4/mysql-tasks.sql
            relativeToChangelogFile: true
        - sqlFile:
            dbms: h2
            path: instance_analytics_views/tasks/v4/h2-tasks.sql
            relativeToChangelogFile: true
      rollback:
        - sqlFile:
            dbms: postgresql
            path: instance_analytics_views/tasks/v2/postgres-tasks.sql
            relativeToChangelogFile: true
        - sqlFile:
            dbms: mysql,mariadb
            path: instance_analytics_views/tasks/v3/mysql-tasks.sql
            relativeToChangelogFile: true
        - sqlFile:
            dbms: h2
            path: instance_analytics_views/tasks/v2/h2-tasks.sql
            relativeToChangelogFile: true

  - changeSet:
      id: v59.2026-01-23T05:55:14
      author: johnswanson
      comment: Fix missing perm_value/perm_type/collection_id from v58.2026-01-09T12:00:01
      preConditions:
        - onFail: MARK_RAN
        - not:
            changeSetExecuted:
              id: v58.2026-01-23T05:55:14
              author: johnswanson
              changeLogFile: migrations/058_update_migrations.yaml
      rollback: # not needed
      changes:
        - sql:
            dbms: postgres
            sql: >-
              UPDATE permissions p
              SET perm_value = 'read-and-write',
                  perm_type = 'perms/collection-access',
                  collection_id = c.id
              FROM permissions_group pg, collection c
              WHERE p.group_id = pg.id
                AND pg.magic_group_type = 'data-analyst'
                AND p.object = '/collection/' || c.id || '/'
                AND c.type IN ('library', 'library-data', 'library-metrics')
                AND p.perm_value IS NULL
        - sql:
            dbms: mysql,mariadb
            sql: >-
              UPDATE permissions p
              JOIN permissions_group pg ON p.group_id = pg.id
              JOIN collection c ON p.object = CONCAT('/collection/', c.id, '/')
              SET p.perm_value = 'read-and-write',
                  p.perm_type = 'perms/collection-access',
                  p.collection_id = c.id
              WHERE pg.magic_group_type = 'data-analyst'
                AND c.type IN ('library', 'library-data', 'library-metrics')
                AND p.perm_value IS NULL
        - sql:
            dbms: h2
            sql: >-
              UPDATE permissions p
              SET (perm_value, perm_type, collection_id) = (
                SELECT 'read-and-write', 'perms/collection-access', c.id
                FROM permissions_group pg, collection c
                WHERE p.group_id = pg.id
                  AND pg.magic_group_type = 'data-analyst'
                  AND p.object = '/collection/' || c.id || '/'
                  AND c.type IN ('library', 'library-data', 'library-metrics')
              )
              WHERE p.perm_value IS NULL
                AND EXISTS (
                  SELECT 1
                  FROM permissions_group pg, collection c
                  WHERE p.group_id = pg.id
                    AND pg.magic_group_type = 'data-analyst'
                    AND p.object = '/collection/' || c.id || '/'
                    AND c.type IN ('library', 'library-data', 'library-metrics')
                )

  - changeSet:
      id: v59.2026-01-23T12:00:00
      author: bronsa
      comment: Insert default common.py entry in python_library if not exists
      preConditions:
        - onFail: MARK_RAN
        - sqlCheck:
            expectedResult: 0
            sql: SELECT count(*) FROM python_library WHERE path = 'common.py'
      changes:
        - sql:
            sql: INSERT INTO python_library (path, source) VALUES ('common.py', '')
      rollback:

  - changeSet:
      id: v59.2026-01-23T13:00:00
      author: nvoxland
      comment: Add transform_name for preserving history when transform is deleted
      changes:
        - addColumn:
            tableName: transform_run
            columns:
              - column:
                  name: transform_name
                  type: varchar(254)
                  remarks: Name of transform at time of run, stored for historical purposes

  - changeSet:
      id: v59.2026-01-23T13:00:01
      author: nvoxland
      comment: Add transform_entity_id column for potential reconnection when transform is recreated
      changes:
        - addColumn:
            tableName: transform_run
            columns:
              - column:
                  name: transform_entity_id
                  type: char(21)
                  remarks: Original transform entity_id, stored for historical purposes

  - changeSet:
      id: v59.2026-01-23T13:00:02
      author: nvoxland
      comment: Backfill transform_name and transform_entity_id for existing runs
      changes:
        - sql:
            sql: >-
              UPDATE transform_run
              SET transform_name = (SELECT name FROM transform WHERE transform.id = transform_run.transform_id),
                  transform_entity_id = (SELECT entity_id FROM transform WHERE transform.id = transform_run.transform_id)
      rollback:
        - sql:
            sql: >-
              UPDATE transform_run SET transform_name = NULL, transform_entity_id = NULL

  - changeSet:
      id: v59.2026-01-23T13:00:03
      author: nvoxland
      comment: Drop existing FK constraint on transform_run.transform_id to change to SET NULL behavior
      changes:
        - dropForeignKeyConstraint:
            baseTableName: transform_run
            constraintName: fk_transform_run_transform_id
      rollback:
        - addForeignKeyConstraint:
            baseTableName: transform_run
            baseColumnNames: transform_id
            referencedTableName: transform
            referencedColumnNames: id
            constraintName: fk_transform_run_transform_id
            onDelete: CASCADE

  - changeSet:
      id: v59.2026-01-23T13:00:04
      author: nvoxland
      comment: Make transform_id nullable to support orphaned runs when transform is deleted
      changes:
        - dropNotNullConstraint:
            tableName: transform_run
            columnName: transform_id
            columnDataType: int

  - changeSet:
      id: v59.2026-01-23T13:00:05
      author: nvoxland
      comment: Re-add FK constraint with ON DELETE SET NULL behavior to preserve runs when transform is deleted
      changes:
        - addForeignKeyConstraint:
            baseTableName: transform_run
            baseColumnNames: transform_id
            referencedTableName: transform
            referencedColumnNames: id
            constraintName: fk_transform_run_transform_id
            onDelete: SET NULL

  - changeSet:
      id: v59.2026-01-27T12:00:00
      author: epaget
      comment: Add entity_id column to python_library table
      changes:
        - addColumn:
            tableName: python_library
            columns:
              - column:
                  name: entity_id
                  type: char(21)
                  remarks: Random NanoID tag for unique identity
                  constraints:
                    nullable: true
      rollback:
        - dropColumn:
            tableName: python_library
            columnName: entity_id

  - changeSet:
      id: v59.2026-01-27T12:00:01
      author: epaget
      comment: Backfill entity_id for existing common.py python_library row
      changes:
        - sql:
            sql: UPDATE python_library SET entity_id = 'cWWH9qJPvHNB3rP2vLZrK' WHERE path = 'common.py' AND entity_id IS NULL
      rollback:
        - sql:
            sql: UPDATE python_library SET entity_id = NULL WHERE path = 'common.py'

  - changeSet:
      id: v59.2026-01-27T12:00:02
      author: epaget
      comment: Add index on entity_id column in python_library table
      changes:
        - createIndex:
            tableName: python_library
            indexName: idx_python_library_entity_id
            columns:
              - column:
                  name: entity_id
      rollback:
        - dropIndex:
            tableName: python_library
            indexName: idx_python_library_entity_id

  - changeSet:
      id: v59.2026-01-27T12:00:03
      author: epaget
      comment: Add unique constraint on entity_id column in python_library table
      changes:
        - addUniqueConstraint:
            tableName: python_library
            columnNames: entity_id
            constraintName: unique_python_library_entity_id
      rollback:
        - dropUniqueConstraint:
            tableName: python_library
            constraintName: unique_python_library_entity_id

  - changeSet:
      id: v59.2026-01-28T10:00:00
      author: metamben
      comment: Add stale column to analysis_finding for asynchronous dependent updates
      changes:
        - addColumn:
            tableName: analysis_finding
            columns:
              - column:
                  name: stale
                  type: ${boolean.type}
                  defaultValueBoolean: false
                  remarks: True when the entity needs re-analysis due to upstream changes
                  constraints:
                    nullable: false
      rollback:
        - dropColumn:
            tableName: analysis_finding
            columnName: stale

  - changeSet:
      id: v59.2026-01-28T10:00:01
      author: metamben
      comment: Add partial index on analysis_finding.stale for efficient polling
      changes:
        - sql:
            dbms: postgresql
            sql: CREATE INDEX idx_analysis_finding_stale ON analysis_finding(stale) WHERE stale = true
        - sql:
            dbms: mysql,mariadb,h2
            sql: CREATE INDEX idx_analysis_finding_stale ON analysis_finding(stale)
      rollback:
        - dropIndex:
            tableName: analysis_finding
            indexName: idx_analysis_finding_stale

  - changeSet:
      id: v59.2026-01-28T10:00:02
      author: winlost
      comment: Add disable_links column to notification_card table
      changes:
        - addColumn:
            tableName: notification_card
            columns:
              - column:
                  name: disable_links
                  type: ${boolean.type}
                  remarks: "Whether to disable alert email links"
                  defaultValueBoolean: false
                  constraints:
                    nullable: true
      rollback:
        - dropColumn:
            tableName: notification_card
            columnName: disable_links

  # ============================================================================
  # Workspace feature: clean migration path
  # These changesets create the complete workspace schema in a single pass.
  # On existing databases where workspace tables already exist, every changeset
  # is safely skipped via preConditions: onFail: MARK_RAN.
  # ============================================================================

  # ---------- Group 1: workspace table ----------

  - changeSet:
      id: v59.2026-01-31T12:00:00
      author: christruter
      comment: Create workspace table with all columns
      preConditions:
        - onFail: MARK_RAN
        - not:
            - tableExists:
                tableName: workspace
      changes:
        - createTable:
            tableName: workspace
            remarks: Internal Metabase container
            columns:
              - column:
                  name: id
                  type: int
                  autoIncrement: true
                  constraints:
                    primaryKey: true
                    nullable: false
              - column:
                  name: name
                  type: varchar(254)
                  remarks: Workspace name
              - column:
                  name: collection_id
                  type: int
                  remarks: FK to workspace's collection
                  constraints:
                    nullable: true
              - column:
                  name: creator_id
                  type: int
                  remarks: FK to owner
              - column:
                  name: api_key_id
                  type: int
                  remarks: FK to api key used by agent
              - column:
                  name: execution_user
                  type: int
                  remarks: FK to api key's user
              - column:
                  name: database_id
                  type: int
                  remarks: FK to db used for this workspace
              - column:
                  name: schema
                  type: varchar(254)
                  remarks: Isolated schema where work happens
                  constraints:
                    nullable: true
              - column:
                  name: database_details
                  type: ${text.type}
                  remarks: Database details used for isolation of this workspace
                  constraints:
                    nullable: true
              - column:
                  name: db_status
                  type: varchar(20)
                  remarks: Status of the database isolation resources (uninitialized, pending, ready, broken)
                  defaultValue: uninitialized
                  constraints:
                    nullable: false
              - column:
                  name: base_status
                  type: varchar(20)
                  remarks: Workspace lifecycle status (empty, active, archived)
                  defaultValue: empty
                  constraints:
                    nullable: false
              - column:
                  name: graph_version
                  type: bigint
                  remarks: Incremented when graph structure changes (transform add/remove, global transform changes)
                  defaultValueNumeric: 1
                  constraints:
                    nullable: false
              - column:
                  name: created_at
                  type: ${timestamp_type}
                  remarks: Timestamp when the workspace was created
                  defaultValueComputed: current_timestamp
              - column:
                  name: updated_at
                  type: ${timestamp_type}
                  remarks: Timestamp when the workspace was last updated
                  defaultValueComputed: current_timestamp

  - changeSet:
      id: v59.2026-01-31T12:00:01
      author: christruter
      comment: Add index on workspace.collection_id
      preConditions:
        - onFail: MARK_RAN
        - not:
            - indexExists:
                tableName: workspace
                indexName: idx_workspace_collection_id
      changes:
        - createIndex:
            tableName: workspace
            indexName: idx_workspace_collection_id
            columns:
              - column:
                  name: collection_id
      rollback:

  - changeSet:
      id: v59.2026-01-31T12:00:02
      author: christruter
      comment: Add index on workspace.creator_id
      preConditions:
        - onFail: MARK_RAN
        - not:
            - indexExists:
                tableName: workspace
                indexName: idx_workspace_creator_id
      changes:
        - createIndex:
            tableName: workspace
            indexName: idx_workspace_creator_id
            columns:
              - column:
                  name: creator_id
      rollback:

  - changeSet:
      id: v59.2026-01-31T12:00:03
      author: christruter
      comment: Add index on workspace.api_key_id
      preConditions:
        - onFail: MARK_RAN
        - not:
            - indexExists:
                tableName: workspace
                indexName: idx_workspace_api_key_id
      changes:
        - createIndex:
            tableName: workspace
            indexName: idx_workspace_api_key_id
            columns:
              - column:
                  name: api_key_id
      rollback:

  - changeSet:
      id: v59.2026-01-31T12:00:04
      author: christruter
      comment: Add index on workspace.execution_user
      preConditions:
        - onFail: MARK_RAN
        - not:
            - indexExists:
                tableName: workspace
                indexName: idx_workspace_execution_user_id
      changes:
        - createIndex:
            tableName: workspace
            indexName: idx_workspace_execution_user_id
            columns:
              - column:
                  name: execution_user
      rollback:

  - changeSet:
      id: v59.2026-01-31T12:00:05
      author: christruter
      comment: Add unique constraint on workspace.name
      preConditions:
        - onFail: MARK_RAN
        - not:
            - indexExists:
                tableName: workspace
                indexName: idx_workspace_name_unique
      changes:
        - addUniqueConstraint:
            tableName: workspace
            columnNames: name
            constraintName: idx_workspace_name_unique
      rollback:

  # ---------- Group 2: collection.workspace_id ----------

  - changeSet:
      id: v59.2026-01-31T12:00:06
      author: christruter
      comment: Add workspace_id column to collection table
      preConditions:
        - onFail: MARK_RAN
        - not:
            - columnExists:
                tableName: collection
                columnName: workspace_id
      changes:
        - addColumn:
            tableName: collection
            columns:
              - column:
                  name: workspace_id
                  type: int
                  remarks: Workspace containing this collection
                  constraints:
                    nullable: true
      rollback:

  - changeSet:
      id: v59.2026-01-31T12:00:07
      author: christruter
      comment: Add index on collection.workspace_id
      preConditions:
        - onFail: MARK_RAN
        - not:
            - indexExists:
                tableName: collection
                indexName: idx_collection_workspace_id
      changes:
        - createIndex:
            tableName: collection
            indexName: idx_collection_workspace_id
            columns:
              - column:
                  name: workspace_id
      rollback:

  - changeSet:
      id: v59.2026-01-31T12:00:08
      author: christruter
      comment: Add foreign key constraint from collection.workspace_id to workspace with CASCADE
      preConditions:
        - onFail: MARK_RAN
        - not:
            - foreignKeyConstraintExists:
                foreignKeyName: fk_collection_workspace_id
                foreignKeyTableName: collection
      changes:
        - addForeignKeyConstraint:
            baseTableName: collection
            baseColumnNames: workspace_id
            constraintName: fk_collection_workspace_id
            referencedTableName: workspace
            referencedColumnNames: id
            onDelete: CASCADE

  # ---------- Group 5: workspace_log ----------

  - changeSet:
      id: v59.2026-01-31T12:00:27
      author: christruter
      comment: Create workspace_log table for async workspace setup progress
      preConditions:
        - onFail: MARK_RAN
        - not:
            - tableExists:
                tableName: workspace_log
      changes:
        - createTable:
            tableName: workspace_log
            remarks: Log entries for async workspace setup progress
            columns:
              - column:
                  name: id
                  type: int
                  autoIncrement: true
                  constraints:
                    primaryKey: true
                    nullable: false
              - column:
                  name: workspace_id
                  type: int
                  remarks: FK to workspace being set up
                  constraints:
                    nullable: false
              - column:
                  name: task
                  type: varchar(255)
                  remarks: Name of the setup task
                  constraints:
                    nullable: false
              - column:
                  name: created_at
                  type: ${timestamp_type}
                  remarks: When the task was created
                  constraints:
                    nullable: false
              - column:
                  name: updated_at
                  type: ${timestamp_type}
                  remarks: When the task was updated
                  constraints:
                    nullable: false
              - column:
                  name: started_at
                  type: ${timestamp_type}
                  remarks: When the task started
                  constraints:
                    nullable: false
              - column:
                  name: completed_at
                  type: ${timestamp_type}
                  remarks: When the task completed
              - column:
                  name: status
                  type: varchar(20)
                  remarks: Task status (started, success, failed)
              - column:
                  name: message
                  type: ${text.type}
                  remarks: Details on the task result if any

  - changeSet:
      id: v59.2026-01-31T12:00:28
      author: christruter
      comment: Add index on workspace_log.workspace_id
      preConditions:
        - onFail: MARK_RAN
        - not:
            - indexExists:
                tableName: workspace_log
                indexName: idx_workspace_log_workspace_id
      changes:
        - createIndex:
            tableName: workspace_log
            indexName: idx_workspace_log_workspace_id
            columns:
              - column:
                  name: workspace_id
      rollback:

  - changeSet:
      id: v59.2026-01-31T12:00:29
      author: christruter
      comment: Add FK from workspace_log.workspace_id to workspace with CASCADE
      preConditions:
        - onFail: MARK_RAN
        - not:
            - foreignKeyConstraintExists:
                foreignKeyName: fk_workspace_log_workspace_id
                foreignKeyTableName: workspace_log
      changes:
        - addForeignKeyConstraint:
            baseTableName: workspace_log
            baseColumnNames: workspace_id
            constraintName: fk_workspace_log_workspace_id
            referencedTableName: workspace
            referencedColumnNames: id
            onDelete: CASCADE

  # ---------- Group 6: workspace_transform ----------

  - changeSet:
      id: v59.2026-01-31T12:00:30
      author: christruter
      comment: Create workspace_transform table with all columns
      preConditions:
        - onFail: MARK_RAN
        - not:
            - tableExists:
                tableName: workspace_transform
      changes:
        - createTable:
            tableName: workspace_transform
            remarks: Holds the changeset of transforms being created and edited within a workspace
            columns:
              - column:
                  name: ref_id
                  type: char(36)
                  remarks: Representation id for the workspace transform, not unique across workspaces
                  constraints:
                    nullable: false
              - column:
                  name: workspace_id
                  type: int
                  remarks: FK to workspace containing this transform
                  constraints:
                    nullable: false
              - column:
                  name: global_id
                  type: int
                  remarks: FK to transform table if checked out from global
                  constraints:
                    nullable: true
              - column:
                  name: collection_id
                  type: int
                  remarks: FK to collection containing this transform
                  constraints:
                    nullable: true
              - column:
                  name: creator_id
                  type: int
                  remarks: FK to core_user who created this workspace transform
                  constraints:
                    nullable: true
              - column:
                  name: name
                  type: ${text.type}
                  remarks: Name of the transform
                  constraints:
                    nullable: false
              - column:
                  name: description
                  type: ${text.type}
                  remarks: Description of the transform
                  constraints:
                    nullable: true
              - column:
                  name: source
                  type: ${text.type}
                  remarks: JSON of source configuration
                  constraints:
                    nullable: false
              - column:
                  name: target
                  type: ${text.type}
                  remarks: JSON of target configuration
                  constraints:
                    nullable: false
              - column:
                  name: entity_id
                  type: char(21)
                  remarks: Random NanoID tag for unique identity of this changeset entry only
                  constraints:
                    nullable: true
                    unique: true
              - column:
                  name: checked_out_at
                  type: ${timestamp_type}
                  remarks: Timestamp when the transform was checked out
                  constraints:
                    nullable: true
              - column:
                  name: archived_at
                  type: ${timestamp_type}
                  remarks: If set, marks that the global transform should be archived when merging
                  constraints:
                    nullable: true
              - column:
                  name: created_at
                  type: ${timestamp_type}
                  remarks: Timestamp when the workspace transform was created
                  constraints:
                    nullable: false
              - column:
                  name: updated_at
                  type: ${timestamp_type}
                  remarks: Timestamp when the workspace transform was last updated
                  constraints:
                    nullable: false
              - column:
                  name: last_run_at
                  type: ${timestamp_type}
                  remarks: Timestamp when this transform was last run
                  constraints:
                    nullable: true
              - column:
                  name: last_run_message
                  type: ${text.type}
                  remarks: Message from the last transform run (success or failure message)
                  constraints:
                    nullable: true
              - column:
                  name: last_run_status
                  type: varchar(20)
                  remarks: Status of the last transform run (succeeded or failed)
                  constraints:
                    nullable: true
              - column:
                  name: definition_changed
                  type: ${boolean.type}
                  remarks: Indicates if the transform definition has changed and needs re-execution
                  defaultValueBoolean: true
                  constraints:
                    nullable: false
              - column:
                  name: analysis_version
                  type: bigint
                  remarks: Incremented when transform source/target changes, triggers re-analysis
                  defaultValueNumeric: 1
                  constraints:
                    nullable: false
              - column:
                  name: input_data_changed
                  type: ${boolean.type}
                  remarks: Indicates if upstream data has changed and transform needs re-execution
                  defaultValueBoolean: false
                  constraints:
                    nullable: false

  - changeSet:
      id: v59.2026-01-31T12:00:31
      author: christruter
      comment: Add index on workspace_transform.workspace_id
      preConditions:
        - onFail: MARK_RAN
        - not:
            - indexExists:
                tableName: workspace_transform
                indexName: idx_workspace_transform_workspace_id
      changes:
        - createIndex:
            tableName: workspace_transform
            indexName: idx_workspace_transform_workspace_id
            columns:
              - column:
                  name: workspace_id
      rollback:

  - changeSet:
      id: v59.2026-01-31T12:00:32
      author: christruter
      comment: Add index on workspace_transform.global_id
      preConditions:
        - onFail: MARK_RAN
        - not:
            - indexExists:
                tableName: workspace_transform
                indexName: idx_workspace_transform_global_id
      changes:
        - createIndex:
            tableName: workspace_transform
            indexName: idx_workspace_transform_global_id
            columns:
              - column:
                  name: global_id
      rollback:

  - changeSet:
      id: v59.2026-01-31T12:00:33
      author: christruter
      comment: Add index on workspace_transform.collection_id
      preConditions:
        - onFail: MARK_RAN
        - not:
            - indexExists:
                tableName: workspace_transform
                indexName: idx_workspace_transform_collection_id
      changes:
        - createIndex:
            tableName: workspace_transform
            indexName: idx_workspace_transform_collection_id
            columns:
              - column:
                  name: collection_id
      rollback:

  - changeSet:
      id: v59.2026-01-31T12:00:34
      author: christruter
      comment: Add index on workspace_transform.creator_id
      preConditions:
        - onFail: MARK_RAN
        - not:
            - indexExists:
                tableName: workspace_transform
                indexName: idx_workspace_transform_creator_id
      changes:
        - createIndex:
            tableName: workspace_transform
            indexName: idx_workspace_transform_creator_id
            columns:
              - column:
                  name: creator_id
      rollback:

  - changeSet:
      id: v59.2026-01-31T12:00:35
      author: christruter
      comment: Add FK from workspace_transform.workspace_id to workspace with CASCADE
      preConditions:
        - onFail: MARK_RAN
        - not:
            - foreignKeyConstraintExists:
                foreignKeyName: fk_workspace_transform_workspace_id
                foreignKeyTableName: workspace_transform
      changes:
        - addForeignKeyConstraint:
            baseTableName: workspace_transform
            baseColumnNames: workspace_id
            constraintName: fk_workspace_transform_workspace_id
            referencedTableName: workspace
            referencedColumnNames: id
            onDelete: CASCADE

  - changeSet:
      id: v59.2026-01-31T12:00:36
      author: christruter
      comment: Add FK from workspace_transform.global_id to transform with SET NULL
      preConditions:
        - onFail: MARK_RAN
        - not:
            - foreignKeyConstraintExists:
                foreignKeyName: fk_workspace_transform_global_id
                foreignKeyTableName: workspace_transform
      changes:
        - addForeignKeyConstraint:
            baseTableName: workspace_transform
            baseColumnNames: global_id
            constraintName: fk_workspace_transform_global_id
            referencedTableName: transform
            referencedColumnNames: id
            onDelete: SET NULL

  - changeSet:
      id: v59.2026-01-31T12:00:37
      author: christruter
      comment: Add FK from workspace_transform.collection_id to collection with SET NULL
      preConditions:
        - onFail: MARK_RAN
        - not:
            - foreignKeyConstraintExists:
                foreignKeyName: fk_workspace_transform_collection_id
                foreignKeyTableName: workspace_transform
      changes:
        - addForeignKeyConstraint:
            baseTableName: workspace_transform
            baseColumnNames: collection_id
            constraintName: fk_workspace_transform_collection_id
            referencedTableName: collection
            referencedColumnNames: id
            onDelete: SET NULL

  - changeSet:
      id: v59.2026-01-31T12:00:38
      author: christruter
      comment: Add FK from workspace_transform.creator_id to core_user with SET NULL
      preConditions:
        - onFail: MARK_RAN
        - not:
            - foreignKeyConstraintExists:
                foreignKeyName: fk_workspace_transform_creator_id
                foreignKeyTableName: workspace_transform
      changes:
        - addForeignKeyConstraint:
            baseTableName: workspace_transform
            baseColumnNames: creator_id
            constraintName: fk_workspace_transform_creator_id
            referencedTableName: core_user
            referencedColumnNames: id
            onDelete: SET NULL

  - changeSet:
      id: v59.2026-01-31T12:00:39
      validCheckSum: ANY
      author: christruter
      comment: Add partial unique index on workspace_transform (workspace_id, global_id) where global_id is not null
      preConditions:
        - onFail: MARK_RAN
        - not:
            - indexExists:
                tableName: workspace_transform
                indexName: idx_workspace_transform_workspace_global_unique
      changes:
        # PostgreSQL supports partial indexes for smaller index size
        - sql:
            dbms: postgresql
            sql: CREATE UNIQUE INDEX idx_workspace_transform_workspace_global_unique ON workspace_transform (workspace_id, global_id) WHERE global_id IS NOT NULL
        # H2/MySQL/MariaDB don't support partial indexes, but all databases treat NULLs as distinct
        # in unique indexes, so a regular unique index is functionally equivalent
        - sql:
            dbms: h2,mysql,mariadb
            sql: CREATE UNIQUE INDEX idx_workspace_transform_workspace_global_unique ON workspace_transform (workspace_id, global_id)
      rollback:

  - changeSet:
      id: v59.2026-01-31T12:00:40
      author: christruter
      comment: Add composite primary key on workspace_transform (workspace_id, ref_id)
      preConditions:
        - onFail: MARK_RAN
        - not:
            - primaryKeyExists:
                tableName: workspace_transform
      changes:
        - addPrimaryKey:
            tableName: workspace_transform
            columnNames: workspace_id, ref_id
            constraintName: pk_workspace_transform

  # ---------- Group 7: workspace_input ----------

  - changeSet:
      id: v59.2026-01-31T12:00:41
      author: qnkhuat
      comment: Create workspace_input table for external tables consumed by workspace transforms
      preConditions:
        - onFail: MARK_RAN
        - not:
            - tableExists:
                tableName: workspace_input
      changes:
        - createTable:
            tableName: workspace_input
            remarks: External tables that workspace transforms consume from source databases
            columns:
              - column:
                  name: id
                  type: int
                  autoIncrement: true
                  constraints:
                    primaryKey: true
                    nullable: false
              - column:
                  name: workspace_id
                  type: int
                  remarks: FK to workspace
                  constraints:
                    nullable: false
              - column:
                  name: db_id
                  type: int
                  remarks: Database containing the source table
                  constraints:
                    nullable: false
              - column:
                  name: schema
                  type: varchar(254)
                  remarks: Schema of the source table (nullable for DBs without schemas)
                  constraints:
                    nullable: true
              - column:
                  name: table
                  type: varchar(254)
                  remarks: Name of the source table
                  constraints:
                    nullable: false
              - column:
                  name: table_id
                  type: int
                  remarks: FK to metabase_table if table exists and is synced
                  constraints:
                    nullable: true
              - column:
                  name: ref_id
                  type: char(36)
                  remarks: ref_id of the workspace_transform that depends on this input
                  constraints:
                    nullable: false
              - column:
                  name: access_granted
                  type: ${boolean.type}
                  remarks: Whether read access has been granted to the workspace user
                  defaultValueBoolean: false
                  constraints:
                    nullable: false
              - column:
                  name: transform_version
                  type: bigint
                  remarks: The analysis_version of the transform when this row was created
                  defaultValueNumeric: 0
                  constraints:
                    nullable: false
              - column:
                  name: created_at
                  type: ${timestamp_type}
                  remarks: Timestamp when the input was created
                  constraints:
                    nullable: false
              - column:
                  name: updated_at
                  type: ${timestamp_type}
                  remarks: Timestamp when the input was last updated
                  constraints:
                    nullable: false

  - changeSet:
      id: v59.2026-01-31T12:00:42
      author: qnkhuat
      comment: Add index on workspace_input.workspace_id
      preConditions:
        - onFail: MARK_RAN
        - not:
            - indexExists:
                tableName: workspace_input
                indexName: idx_workspace_input_workspace_id
      changes:
        - createIndex:
            tableName: workspace_input
            indexName: idx_workspace_input_workspace_id
            columns:
              - column:
                  name: workspace_id
      rollback:

  - changeSet:
      id: v59.2026-01-31T12:00:43
      author: qnkhuat
      comment: Add index on workspace_input.table_id
      preConditions:
        - onFail: MARK_RAN
        - not:
            - indexExists:
                tableName: workspace_input
                indexName: idx_workspace_input_table_id
      changes:
        - createIndex:
            tableName: workspace_input
            indexName: idx_workspace_input_table_id
            columns:
              - column:
                  name: table_id
      rollback:

  - changeSet:
      id: v59.2026-01-31T12:00:44
      author: qnkhuat
      comment: Add composite index on workspace_input (workspace_id, ref_id)
      preConditions:
        - onFail: MARK_RAN
        - not:
            - indexExists:
                tableName: workspace_input
                indexName: idx_workspace_input_ref_id
      changes:
        - createIndex:
            tableName: workspace_input
            indexName: idx_workspace_input_ref_id
            columns:
              - column:
                  name: workspace_id
              - column:
                  name: ref_id
      rollback:

  - changeSet:
      id: v59.2026-01-31T12:00:45
      author: qnkhuat
      comment: Add FK from workspace_input.workspace_id to workspace with CASCADE
      preConditions:
        - onFail: MARK_RAN
        - not:
            - foreignKeyConstraintExists:
                foreignKeyName: fk_workspace_input_workspace_id
                foreignKeyTableName: workspace_input
      changes:
        - addForeignKeyConstraint:
            baseTableName: workspace_input
            baseColumnNames: workspace_id
            constraintName: fk_workspace_input_workspace_id
            referencedTableName: workspace
            referencedColumnNames: id
            onDelete: CASCADE

  - changeSet:
      id: v59.2026-01-31T12:00:46
      author: qnkhuat
      comment: Add FK from workspace_input.table_id to metabase_table with SET NULL
      preConditions:
        - onFail: MARK_RAN
        - not:
            - foreignKeyConstraintExists:
                foreignKeyName: fk_workspace_input_table_id
                foreignKeyTableName: workspace_input
      changes:
        - addForeignKeyConstraint:
            baseTableName: workspace_input
            baseColumnNames: table_id
            constraintName: fk_workspace_input_table_id
            referencedTableName: metabase_table
            referencedColumnNames: id
            onDelete: SET NULL

  - changeSet:
      id: v59.2026-01-31T12:00:47
      author: qnkhuat
      comment: Add unique constraint on workspace_input (workspace_id, ref_id, db_id, schema, table, transform_version)
      preConditions:
        - onFail: MARK_RAN
        - not:
            - indexExists:
                tableName: workspace_input
                indexName: uq_workspace_input_versioned
      changes:
        - addUniqueConstraint:
            tableName: workspace_input
            columnNames: workspace_id, ref_id, db_id, schema, table, transform_version
            constraintName: uq_workspace_input_versioned
      rollback:

  # ---------- Group 8: workspace_output ----------

  - changeSet:
      id: v59.2026-01-31T12:00:48
      author: crisptrutski
      comment: Create workspace_output table for tables produced by workspace transforms
      preConditions:
        - onFail: MARK_RAN
        - not:
            - tableExists:
                tableName: workspace_output
      changes:
        - createTable:
            tableName: workspace_output
            remarks: Tables that workspace transforms produce
            columns:
              - column:
                  name: id
                  type: int
                  autoIncrement: true
                  constraints:
                    primaryKey: true
                    nullable: false
              - column:
                  name: workspace_id
                  type: int
                  remarks: FK to workspace
                  constraints:
                    nullable: false
              - column:
                  name: ref_id
                  type: char(36)
                  remarks: FK to workspace_transform.ref_id (producer)
                  constraints:
                    nullable: false
              - column:
                  name: db_id
                  type: int
                  remarks: Database where the output table is created
                  constraints:
                    nullable: false
              - column:
                  name: global_schema
                  type: varchar(254)
                  remarks: Schema of the output table
                  constraints:
                    nullable: true
              - column:
                  name: global_table
                  type: varchar(254)
                  remarks: Name of the output table
                  constraints:
                    nullable: false
              - column:
                  name: global_table_id
                  type: int
                  remarks: FK to metabase table for the global output table
                  constraints:
                    nullable: true
              - column:
                  name: isolated_schema
                  type: varchar(254)
                  remarks: Schema name in the isolated workspace namespace
                  constraints:
                    nullable: true
              - column:
                  name: isolated_table
                  type: varchar(254)
                  remarks: Table name in the isolated workspace namespace
                  constraints:
                    nullable: true
              - column:
                  name: isolated_table_id
                  type: int
                  remarks: FK to metabase table for the isolated output table
                  constraints:
                    nullable: true
              - column:
                  name: transform_version
                  type: bigint
                  remarks: The analysis_version of the transform when this row was created
                  defaultValueNumeric: 0
                  constraints:
                    nullable: false
              - column:
                  name: created_at
                  type: ${timestamp_type}
                  remarks: Timestamp when the output was created
                  constraints:
                    nullable: false
              - column:
                  name: updated_at
                  type: ${timestamp_type}
                  remarks: Timestamp when the output was last updated
                  constraints:
                    nullable: false

  - changeSet:
      id: v59.2026-01-31T12:00:49
      author: crisptrutski
      comment: Add index on workspace_output.workspace_id
      preConditions:
        - onFail: MARK_RAN
        - not:
            - indexExists:
                tableName: workspace_output
                indexName: idx_workspace_output_workspace_id
      changes:
        - createIndex:
            tableName: workspace_output
            indexName: idx_workspace_output_workspace_id
            columns:
              - column:
                  name: workspace_id
      rollback:

  - changeSet:
      id: v59.2026-01-31T12:00:50
      author: crisptrutski
      comment: Add index on workspace_output.ref_id
      preConditions:
        - onFail: MARK_RAN
        - not:
            - indexExists:
                tableName: workspace_output
                indexName: idx_workspace_output_ref_id
      changes:
        - createIndex:
            tableName: workspace_output
            indexName: idx_workspace_output_ref_id
            columns:
              - column:
                  name: ref_id
      rollback:

  - changeSet:
      id: v59.2026-01-31T12:00:51
      author: crisptrutski
      comment: Add FK from workspace_output.workspace_id to workspace with CASCADE
      preConditions:
        - onFail: MARK_RAN
        - not:
            - foreignKeyConstraintExists:
                foreignKeyName: fk_workspace_output_workspace_id
                foreignKeyTableName: workspace_output
      changes:
        - addForeignKeyConstraint:
            baseTableName: workspace_output
            baseColumnNames: workspace_id
            constraintName: fk_workspace_output_workspace_id
            referencedTableName: workspace
            referencedColumnNames: id
            onDelete: CASCADE

  - changeSet:
      id: v59.2026-01-31T12:00:52
      author: crisptrutski
      comment: Add FK from workspace_output.ref_id to workspace_transform with CASCADE
      preConditions:
        - onFail: MARK_RAN
        - not:
            - foreignKeyConstraintExists:
                foreignKeyName: fk_workspace_output_ref_id
                foreignKeyTableName: workspace_output
      changes:
        - addForeignKeyConstraint:
            baseTableName: workspace_output
            baseColumnNames: workspace_id, ref_id
            constraintName: fk_workspace_output_ref_id
            referencedTableName: workspace_transform
            referencedColumnNames: workspace_id, ref_id
            onDelete: CASCADE

  - changeSet:
      id: v59.2026-01-31T12:00:53
      author: crisptrutski
      comment: Add unique constraint on workspace_output (workspace_id, ref_id, transform_version)
      preConditions:
        - onFail: MARK_RAN
        - not:
            - indexExists:
                tableName: workspace_output
                indexName: uq_workspace_output_versioned
      changes:
        - addUniqueConstraint:
            tableName: workspace_output
            columnNames: workspace_id, ref_id, transform_version
            constraintName: uq_workspace_output_versioned
      rollback:

  # ---------- Group 9: workspace_merge ----------

  - changeSet:
      id: v59.2026-01-31T12:00:54
      author: qnkhuat
      comment: Create workspace_merge table to track batch merge events
      preConditions:
        - onFail: MARK_RAN
        - not:
            - tableExists:
                tableName: workspace_merge
      changes:
        - createTable:
            tableName: workspace_merge
            remarks: Tracks batch merge events with commit messages. Survives workspace deletion.
            columns:
              - column:
                  name: id
                  type: int
                  autoIncrement: true
                  constraints:
                    primaryKey: true
                    nullable: false
              - column:
                  name: workspace_id
                  type: int
                  remarks: Foreign key to workspace, SET NULL on delete
                  constraints:
                    nullable: true
              - column:
                  name: workspace_name
                  type: ${text.type}
                  remarks: Inlined copy of workspace name (survives deletion)
                  constraints:
                    nullable: false
              - column:
                  name: commit_message
                  type: ${text.type}
                  remarks: Description of the merge operation
                  constraints:
                    nullable: false
              - column:
                  name: creator_id
                  type: int
                  remarks: User who performed the merge
                  constraints:
                    nullable: false
              - column:
                  name: created_at
                  type: ${timestamp_type}
                  remarks: Timestamp when the merge was performed
                  defaultValueComputed: current_timestamp
                  constraints:
                    nullable: false

  - changeSet:
      id: v59.2026-01-31T12:00:55
      author: qnkhuat
      comment: Add FK from workspace_merge.workspace_id to workspace with SET NULL
      preConditions:
        - onFail: MARK_RAN
        - not:
            - foreignKeyConstraintExists:
                foreignKeyName: fk_workspace_merge_workspace_id
                foreignKeyTableName: workspace_merge
      changes:
        - addForeignKeyConstraint:
            baseTableName: workspace_merge
            baseColumnNames: workspace_id
            constraintName: fk_workspace_merge_workspace_id
            referencedTableName: workspace
            referencedColumnNames: id
            onDelete: SET NULL

  - changeSet:
      id: v59.2026-01-31T12:00:56
      author: qnkhuat
      comment: Add FK from workspace_merge.creator_id to core_user
      preConditions:
        - onFail: MARK_RAN
        - not:
            - foreignKeyConstraintExists:
                foreignKeyName: fk_workspace_merge_creator_id
                foreignKeyTableName: workspace_merge
      changes:
        - addForeignKeyConstraint:
            baseTableName: workspace_merge
            baseColumnNames: creator_id
            constraintName: fk_workspace_merge_creator_id
            referencedTableName: core_user
            referencedColumnNames: id

  - changeSet:
      id: v59.2026-01-31T12:00:57
      author: qnkhuat
      comment: Add index on workspace_merge.workspace_id
      preConditions:
        - onFail: MARK_RAN
        - not:
            - indexExists:
                tableName: workspace_merge
                indexName: idx_workspace_merge_workspace_id
      changes:
        - createIndex:
            tableName: workspace_merge
            indexName: idx_workspace_merge_workspace_id
            columns:
              - column:
                  name: workspace_id
      rollback:

  - changeSet:
      id: v59.2026-01-31T12:00:58
      author: qnkhuat
      comment: Add index on workspace_merge.creator_id
      preConditions:
        - onFail: MARK_RAN
        - not:
            - indexExists:
                tableName: workspace_merge
                indexName: idx_workspace_merge_creator_id
      changes:
        - createIndex:
            tableName: workspace_merge
            indexName: idx_workspace_merge_creator_id
            columns:
              - column:
                  name: creator_id
      rollback:

  # ---------- Group 10: workspace_merge_transform ----------

  - changeSet:
      id: v59.2026-01-31T12:00:59
      author: qnkhuat
      comment: Create workspace_merge_transform table to track individual transform merges
      preConditions:
        - onFail: MARK_RAN
        - not:
            - tableExists:
                tableName: workspace_merge_transform
      changes:
        - createTable:
            tableName: workspace_merge_transform
            remarks: Tracks individual transform merges. Linked to workspace_merge for batch merges, standalone for single-transform merges.
            columns:
              - column:
                  name: id
                  type: int
                  autoIncrement: true
                  constraints:
                    primaryKey: true
                    nullable: false
              - column:
                  name: workspace_merge_id
                  type: int
                  remarks: Foreign key to workspace_merge, CASCADE on delete
                  constraints:
                    nullable: false
              - column:
                  name: workspace_id
                  type: int
                  remarks: Foreign key to workspace, SET NULL on delete
                  constraints:
                    nullable: true
              - column:
                  name: workspace_name
                  type: ${text.type}
                  remarks: Inlined copy of workspace name (survives deletion)
                  constraints:
                    nullable: false
              - column:
                  name: transform_id
                  type: int
                  remarks: Foreign key to transform
                  constraints:
                    nullable: true
              - column:
                  name: commit_message
                  type: ${text.type}
                  remarks: Description of the merge operation
                  constraints:
                    nullable: false
              - column:
                  name: merging_user_id
                  type: int
                  remarks: User who performed the merge
                  constraints:
                    nullable: false
              - column:
                  name: created_at
                  type: ${timestamp_type}
                  remarks: Timestamp when the merge was performed
                  defaultValueComputed: current_timestamp
                  constraints:
                    nullable: false

  - changeSet:
      id: v59.2026-01-31T12:01:00
      author: qnkhuat
      comment: Add FK from workspace_merge_transform.workspace_merge_id to workspace_merge with CASCADE
      preConditions:
        - onFail: MARK_RAN
        - not:
            - foreignKeyConstraintExists:
                foreignKeyName: fk_workspace_merge_transform_workspace_merge_id
                foreignKeyTableName: workspace_merge_transform
      changes:
        - addForeignKeyConstraint:
            baseTableName: workspace_merge_transform
            baseColumnNames: workspace_merge_id
            constraintName: fk_workspace_merge_transform_workspace_merge_id
            referencedTableName: workspace_merge
            referencedColumnNames: id
            onDelete: CASCADE

  - changeSet:
      id: v59.2026-01-31T12:01:01
      author: qnkhuat
      comment: Add FK from workspace_merge_transform.workspace_id to workspace with SET NULL
      preConditions:
        - onFail: MARK_RAN
        - not:
            - foreignKeyConstraintExists:
                foreignKeyName: fk_workspace_merge_transform_workspace_id
                foreignKeyTableName: workspace_merge_transform
      changes:
        - addForeignKeyConstraint:
            baseTableName: workspace_merge_transform
            baseColumnNames: workspace_id
            constraintName: fk_workspace_merge_transform_workspace_id
            referencedTableName: workspace
            referencedColumnNames: id
            onDelete: SET NULL

  - changeSet:
      id: v59.2026-01-31T12:01:02
      author: qnkhuat
      comment: Add FK from workspace_merge_transform.transform_id to transform with CASCADE
      preConditions:
        - onFail: MARK_RAN
        - not:
            - foreignKeyConstraintExists:
                foreignKeyName: fk_workspace_merge_transform_transform_id
                foreignKeyTableName: workspace_merge_transform
      changes:
        - addForeignKeyConstraint:
            baseTableName: workspace_merge_transform
            baseColumnNames: transform_id
            constraintName: fk_workspace_merge_transform_transform_id
            referencedTableName: transform
            referencedColumnNames: id
            onDelete: CASCADE

  - changeSet:
      id: v59.2026-01-31T12:01:03
      author: qnkhuat
      comment: Add FK from workspace_merge_transform.merging_user_id to core_user
      preConditions:
        - onFail: MARK_RAN
        - not:
            - foreignKeyConstraintExists:
                foreignKeyName: fk_workspace_merge_transform_merging_user_id
                foreignKeyTableName: workspace_merge_transform
      changes:
        - addForeignKeyConstraint:
            baseTableName: workspace_merge_transform
            baseColumnNames: merging_user_id
            constraintName: fk_workspace_merge_transform_merging_user_id
            referencedTableName: core_user
            referencedColumnNames: id

  - changeSet:
      id: v59.2026-01-31T12:01:04
      author: qnkhuat
      comment: Add index on workspace_merge_transform.workspace_merge_id
      preConditions:
        - onFail: MARK_RAN
        - not:
            - indexExists:
                tableName: workspace_merge_transform
                indexName: idx_workspace_merge_transform_workspace_merge_id
      changes:
        - createIndex:
            tableName: workspace_merge_transform
            indexName: idx_workspace_merge_transform_workspace_merge_id
            columns:
              - column:
                  name: workspace_merge_id
      rollback:

  - changeSet:
      id: v59.2026-01-31T12:01:05
      author: qnkhuat
      comment: Add index on workspace_merge_transform.workspace_id
      preConditions:
        - onFail: MARK_RAN
        - not:
            - indexExists:
                tableName: workspace_merge_transform
                indexName: idx_workspace_merge_transform_workspace_id
      changes:
        - createIndex:
            tableName: workspace_merge_transform
            indexName: idx_workspace_merge_transform_workspace_id
            columns:
              - column:
                  name: workspace_id
      rollback:

  - changeSet:
      id: v59.2026-01-31T12:01:06
      author: qnkhuat
      comment: Add index on workspace_merge_transform.transform_id
      preConditions:
        - onFail: MARK_RAN
        - not:
            - indexExists:
                tableName: workspace_merge_transform
                indexName: idx_workspace_merge_transform_transform_id
      changes:
        - createIndex:
            tableName: workspace_merge_transform
            indexName: idx_workspace_merge_transform_transform_id
            columns:
              - column:
                  name: transform_id
      rollback:

  - changeSet:
      id: v59.2026-01-31T12:01:07
      author: qnkhuat
      comment: Add index on workspace_merge_transform.merging_user_id
      preConditions:
        - onFail: MARK_RAN
        - not:
            - indexExists:
                tableName: workspace_merge_transform
                indexName: idx_workspace_merge_transform_merging_user_id
      changes:
        - createIndex:
            tableName: workspace_merge_transform
            indexName: idx_workspace_merge_transform_merging_user_id
            columns:
              - column:
                  name: merging_user_id
      rollback:

  # ---------- Group 11: workspace_output_external ----------

  - changeSet:
      id: v59.2026-01-31T12:01:08
      author: christruter
      comment: Create workspace_output_external table for output tables from external transforms
      preConditions:
        - onFail: MARK_RAN
        - not:
            - tableExists:
                tableName: workspace_output_external
      changes:
        - createTable:
            tableName: workspace_output_external
            remarks: Output tables produced by external transforms enclosed in a workspace
            columns:
              - column:
                  name: id
                  type: int
                  autoIncrement: true
                  constraints:
                    primaryKey: true
                    nullable: false
              - column:
                  name: workspace_id
                  type: int
                  remarks: FK to workspace
                  constraints:
                    nullable: false
              - column:
                  name: transform_id
                  type: int
                  remarks: FK to transform (the external transform)
                  constraints:
                    nullable: false
              - column:
                  name: db_id
                  type: int
                  remarks: Database where the output table is created
                  constraints:
                    nullable: false
              - column:
                  name: global_schema
                  type: varchar(254)
                  remarks: Schema of the original output table
                  constraints:
                    nullable: true
              - column:
                  name: global_table
                  type: varchar(254)
                  remarks: Name of the original output table
                  constraints:
                    nullable: false
              - column:
                  name: global_table_id
                  type: int
                  remarks: FK to metabase_table for the global output table
                  constraints:
                    nullable: true
              - column:
                  name: isolated_schema
                  type: varchar(254)
                  remarks: Schema name in the isolated workspace namespace
                  constraints:
                    nullable: true
              - column:
                  name: isolated_table
                  type: varchar(254)
                  remarks: Table name in the isolated workspace namespace
                  constraints:
                    nullable: false
              - column:
                  name: isolated_table_id
                  type: int
                  remarks: FK to metabase_table for the isolated output table
                  constraints:
                    nullable: true
              - column:
                  name: graph_version
                  type: bigint
                  remarks: The graph_version this row was calculated for
                  defaultValueNumeric: 0
                  constraints:
                    nullable: false
              - column:
                  name: created_at
                  type: ${timestamp_type}
                  remarks: Timestamp when the output was created
                  constraints:
                    nullable: false
              - column:
                  name: updated_at
                  type: ${timestamp_type}
                  remarks: Timestamp when the output was last updated
                  constraints:
                    nullable: false

  - changeSet:
      id: v59.2026-01-31T12:01:09
      author: christruter
      comment: Add index on workspace_output_external.workspace_id
      preConditions:
        - onFail: MARK_RAN
        - not:
            - indexExists:
                tableName: workspace_output_external
                indexName: idx_workspace_output_external_workspace_id
      changes:
        - createIndex:
            tableName: workspace_output_external
            indexName: idx_workspace_output_external_workspace_id
            columns:
              - column:
                  name: workspace_id
      rollback:

  - changeSet:
      id: v59.2026-01-31T12:01:10
      author: christruter
      comment: Add index on workspace_output_external.transform_id
      preConditions:
        - onFail: MARK_RAN
        - not:
            - indexExists:
                tableName: workspace_output_external
                indexName: idx_workspace_output_external_transform_id
      changes:
        - createIndex:
            tableName: workspace_output_external
            indexName: idx_workspace_output_external_transform_id
            columns:
              - column:
                  name: transform_id
      rollback:

  - changeSet:
      id: v59.2026-01-31T12:01:11
      author: christruter
      comment: Add FK from workspace_output_external.workspace_id to workspace with CASCADE
      preConditions:
        - onFail: MARK_RAN
        - not:
            - foreignKeyConstraintExists:
                foreignKeyName: fk_workspace_output_external_workspace_id
                foreignKeyTableName: workspace_output_external
      changes:
        - addForeignKeyConstraint:
            baseTableName: workspace_output_external
            baseColumnNames: workspace_id
            constraintName: fk_workspace_output_external_workspace_id
            referencedTableName: workspace
            referencedColumnNames: id
            onDelete: CASCADE

  - changeSet:
      id: v59.2026-01-31T12:01:12
      author: christruter
      comment: Add FK from workspace_output_external.transform_id to transform with CASCADE
      preConditions:
        - onFail: MARK_RAN
        - not:
            - foreignKeyConstraintExists:
                foreignKeyName: fk_workspace_output_external_transform_id
                foreignKeyTableName: workspace_output_external
      changes:
        - addForeignKeyConstraint:
            baseTableName: workspace_output_external
            baseColumnNames: transform_id
            constraintName: fk_workspace_output_external_transform_id
            referencedTableName: transform
            referencedColumnNames: id
            onDelete: CASCADE

  - changeSet:
      id: v59.2026-01-31T12:01:13
      author: christruter
      comment: Add unique constraint on workspace_output_external (workspace_id, transform_id, graph_version)
      preConditions:
        - onFail: MARK_RAN
        - not:
            - indexExists:
                tableName: workspace_output_external
                indexName: uq_workspace_output_external_versioned
      changes:
        - addUniqueConstraint:
            tableName: workspace_output_external
            columnNames: workspace_id, transform_id, graph_version
            constraintName: uq_workspace_output_external_versioned
      rollback:

  # ---------- Group 12: workspace_input_external ----------

  - changeSet:
      id: v59.2026-01-31T12:01:14
      author: christruter
      comment: Create workspace_input_external table for external tables consumed by external transforms
      preConditions:
        - onFail: MARK_RAN
        - not:
            - tableExists:
                tableName: workspace_input_external
      changes:
        - createTable:
            tableName: workspace_input_external
            remarks: External tables consumed by external transforms enclosed in a workspace (deduplicated per workspace)
            columns:
              - column:
                  name: id
                  type: int
                  autoIncrement: true
                  constraints:
                    primaryKey: true
                    nullable: false
              - column:
                  name: workspace_id
                  type: int
                  remarks: FK to workspace
                  constraints:
                    nullable: false
              - column:
                  name: db_id
                  type: int
                  remarks: Database containing the source table
                  constraints:
                    nullable: false
              - column:
                  name: schema
                  type: varchar(254)
                  remarks: Schema of the source table (nullable for DBs without schemas)
                  constraints:
                    nullable: true
              - column:
                  name: table
                  type: varchar(254)
                  remarks: Name of the source table
                  constraints:
                    nullable: false
              - column:
                  name: table_id
                  type: int
                  remarks: FK to metabase_table if table exists and is synced
                  constraints:
                    nullable: true
              - column:
                  name: access_granted
                  type: ${boolean.type}
                  remarks: Whether read access has been granted to the workspace user
                  defaultValueBoolean: false
                  constraints:
                    nullable: false
              - column:
                  name: graph_version
                  type: bigint
                  remarks: The graph_version this row was calculated for
                  defaultValueNumeric: 0
                  constraints:
                    nullable: false
              - column:
                  name: created_at
                  type: ${timestamp_type}
                  remarks: Timestamp when the input was created
                  constraints:
                    nullable: false
              - column:
                  name: updated_at
                  type: ${timestamp_type}
                  remarks: Timestamp when the input was last updated
                  constraints:
                    nullable: false

  - changeSet:
      id: v59.2026-01-31T12:01:15
      author: christruter
      comment: Add index on workspace_input_external.workspace_id
      preConditions:
        - onFail: MARK_RAN
        - not:
            - indexExists:
                tableName: workspace_input_external
                indexName: idx_workspace_input_external_workspace_id
      changes:
        - createIndex:
            tableName: workspace_input_external
            indexName: idx_workspace_input_external_workspace_id
            columns:
              - column:
                  name: workspace_id
      rollback:

  - changeSet:
      id: v59.2026-01-31T12:01:16
      author: christruter
      comment: Add FK from workspace_input_external.workspace_id to workspace with CASCADE
      preConditions:
        - onFail: MARK_RAN
        - not:
            - foreignKeyConstraintExists:
                foreignKeyName: fk_workspace_input_external_workspace_id
                foreignKeyTableName: workspace_input_external
      changes:
        - addForeignKeyConstraint:
            baseTableName: workspace_input_external
            baseColumnNames: workspace_id
            constraintName: fk_workspace_input_external_workspace_id
            referencedTableName: workspace
            referencedColumnNames: id
            onDelete: CASCADE

  - changeSet:
      id: v59.2026-01-31T12:01:17
      author: christruter
      comment: Add unique constraint on workspace_input_external (workspace_id, db_id, schema, table, graph_version)
      preConditions:
        - onFail: MARK_RAN
        - not:
            - indexExists:
                tableName: workspace_input_external
                indexName: uq_workspace_input_external_versioned
      changes:
        - addUniqueConstraint:
            tableName: workspace_input_external
            columnNames: workspace_id, db_id, schema, table, graph_version
            constraintName: uq_workspace_input_external_versioned
      rollback:

  # ---------- Group 13: workspace_graph ----------

  - changeSet:
      id: v59.2026-01-31T12:01:18
      author: christruter
      comment: Create workspace_graph table for cached dependency graphs
      preConditions:
        - onFail: MARK_RAN
        - not:
            - tableExists:
                tableName: workspace_graph
      changes:
        - createTable:
            tableName: workspace_graph
            remarks: Stores cached dependency graph for each workspace
            columns:
              - column:
                  name: id
                  type: int
                  autoIncrement: true
                  constraints:
                    primaryKey: true
                    nullable: false
              - column:
                  name: workspace_id
                  type: int
                  remarks: Reference to the workspace this graph belongs to
                  constraints:
                    nullable: false
              - column:
                  name: graph
                  type: ${text.type}
                  remarks: JSON representation of the dependency graph
                  constraints:
                    nullable: false
              - column:
                  name: graph_version
                  type: bigint
                  remarks: The graph_version this cached graph was calculated for
                  defaultValueNumeric: 0
                  constraints:
                    nullable: false
              - column:
                  name: created_at
                  type: ${timestamp_type}
                  remarks: Timestamp when the cached graph was created
                  defaultValueComputed: current_timestamp
                  constraints:
                    nullable: false
              - column:
                  name: updated_at
                  type: ${timestamp_type}
                  remarks: Timestamp when the cached graph was last updated
                  defaultValueComputed: current_timestamp
                  constraints:
                    nullable: false

  - changeSet:
      id: v59.2026-01-31T12:01:19
      author: christruter
      comment: Add unique index on workspace_graph (workspace_id, graph_version)
      preConditions:
        - onFail: MARK_RAN
        - not:
            - indexExists:
                tableName: workspace_graph
                indexName: idx_workspace_graph_workspace_version
      changes:
        - createIndex:
            tableName: workspace_graph
            indexName: idx_workspace_graph_workspace_version
            unique: true
            columns:
              - column:
                  name: workspace_id
              - column:
                  name: graph_version
      rollback:

  - changeSet:
      id: v59.2026-01-31T12:01:20
      author: christruter
      comment: Add FK from workspace_graph.workspace_id to workspace with CASCADE
      preConditions:
        - onFail: MARK_RAN
        - not:
            - foreignKeyConstraintExists:
                foreignKeyName: fk_workspace_graph_workspace_id
                foreignKeyTableName: workspace_graph
      changes:
        - addForeignKeyConstraint:
            baseTableName: workspace_graph
            baseColumnNames: workspace_id
            constraintName: fk_workspace_graph_workspace_id
            referencedTableName: workspace
            referencedColumnNames: id
            onDelete: CASCADE

  # ---------- Group 14: Existing table changes ----------

  - changeSet:
      id: v59.2026-01-31T12:01:21
      author: piranha
      comment: Add target_db_id column to transform table
      preConditions:
        - onFail: MARK_RAN
        - not:
            - columnExists:
                tableName: transform
                columnName: target_db_id
      changes:
        - addColumn:
            tableName: transform
            columns:
              - column:
                  name: target_db_id
                  type: int
                  remarks: Target database ID for the transform output
                  constraints:
                    nullable: true
      rollback:

  - changeSet:
      id: v59.2026-01-31T12:01:22
      author: qnkhuat
      comment: Add workspace_permissions_status column to metabase_database table
      preConditions:
        - onFail: MARK_RAN
        - not:
            - columnExists:
                tableName: metabase_database
                columnName: workspace_permissions_status
      changes:
        - addColumn:
            tableName: metabase_database
            columns:
              - column:
                  name: workspace_permissions_status
                  type: ${text.type}
                  remarks: Status of workspace isolation permission check (JSON)
                  constraints:
                    nullable: true
      rollback:

  - changeSet:
      id: v59.2026-01-31T12:01:23
      author: christruter
      comment: Backfill target_db_id for existing transforms
      changes:
        - customChange:
            class: "metabase.app_db.custom_migrations.BackfillTransformTargetDbId"

  - changeSet:
      id: v59.2026-01-31T12:01:24
      author: christruter
      comment: Add index on transform.target_db_id
      preConditions:
        - onFail: MARK_RAN
        - not:
            - indexExists:
                tableName: transform
                indexName: idx_transform_target_db_id
      changes:
        - createIndex:
            tableName: transform
            indexName: idx_transform_target_db_id
            columns:
              - column:
                  name: target_db_id

  - changeSet:
      id: v59.2026-01-31T12:01:25
      author: christruter
      comment: Clean up orphaned target_db_id references before adding FK constraint
      changes:
        - sql:
            sql: >-
              UPDATE transform
              SET target_db_id = NULL
              WHERE target_db_id IS NOT NULL
                AND target_db_id NOT IN (SELECT id FROM metabase_database)
      rollback:

  - changeSet:
      id: v59.2026-01-31T12:01:26
      author: christruter
      comment: Add foreign key constraint from transform.target_db_id to metabase_database
      validCheckSum: ANY
      preConditions:
        - onFail: MARK_RAN
        - not:
            - foreignKeyConstraintExists:
                foreignKeyTableName: transform
                foreignKeyName: fk_transform_target_db_id
      changes:
        - addForeignKeyConstraint:
            baseTableName: transform
            baseColumnNames: target_db_id
            referencedTableName: metabase_database
            referencedColumnNames: id
            constraintName: fk_transform_target_db_id
            onDelete: SET NULL

  - changeSet:
      id: v59.2026-02-04T10:45:00
      author: nvoxland
      comment: Add transform_id column to metabase_table
      changes:
        - addColumn:
            tableName: metabase_table
            columns:
              - column:
                  name: transform_id
                  type: int
                  remarks: Foreign key to the transform that created this table

  - changeSet:
      id: v59.2026-02-04T10:45:01
      author: nvoxland
      comment: Add index for metabase_table.transform_id
      changes:
        - createIndex:
            tableName: metabase_table
            indexName: idx_metabase_table_transform_id
            columns:
              - column:
                  name: transform_id

  - changeSet:
      id: v59.2026-02-04T10:45:02
      author: nvoxland
      comment: Add foreign key on metabase_table(transform_id) to transform(id)
      changes:
        - addForeignKeyConstraint:
            baseTableName: metabase_table
            baseColumnNames: transform_id
            referencedTableName: transform
            referencedColumnNames: id
            constraintName: fk_metabase_table_transform_id
            onDelete: SET NULL

  - changeSet:
      id: v59.2026-02-05T12:00:00
      author: edpaget
      comment: Add conflicts column to remote_sync_task table
      changes:
        - addColumn:
            tableName: remote_sync_task
            columns:
              - column:
                  name: conflicts
                  type: ${text.type}
                  remarks: JSON serialized list of conflicts detected during import
                  constraints:
                    nullable: true

  - changeSet:
      id: v59.2026-02-07T17:00:00
      author: johnswanson
      comment: Create premium_features_token_cache table for cross-instance token cache
      changes:
        - createTable:
            tableName: premium_features_token_cache
            remarks: Caches premium features token check results, shared across instances
            columns:
              - column:
                  name: token_hash
                  type: varchar(64)
                  remarks: SHA-256 hex hash of the premium token
                  constraints:
                    primaryKey: true
                    nullable: false
              - column:
                  name: token_status_hash
                  type: varchar(64)
                  remarks: SHA-256 hex hash of the JSON-encoded token status
                  constraints:
                    nullable: false
              - column:
                  name: updated_at
                  type: ${timestamp_type}
                  remarks: When this cache entry was last refreshed
                  constraints:
                    nullable: false

  - changeSet:
      id: v59.2026-02-16T18:00:00
      author: galdre
      comment: Add write_data_details column to metabase_database for PRO-86 separate write connections
      preConditions:
        - onFail: MARK_RAN
        - not:
            - columnExists:
                tableName: metabase_database
                columnName: write_data_details
      changes:
        - addColumn:
            tableName: metabase_database
            columns:
              - column:
                  name: write_data_details
                  type: ${text.type}
                  remarks: Encrypted JSON containing connection details for write operations (transforms, etc.)
                  constraints:
                    nullable: true
      rollback:
        - dropColumn:
            tableName: metabase_database
            columnName: write_data_details

  - changeSet:
      id: v59.2026-03-04T00:00:00
      author: rileythompson
      comment: Fix ClickHouse upload DB schema name and models
      changes:
        - customChange:
            class: "metabase.app_db.custom_migrations.FixClickHouseUploadDBSchemaNames"

  - changeSet:
      id: v59.2026-03-11T00:00:00
      author: christruter
      comment: >-
        Clean up old v58 changeset rows from databasechangelog left behind when
        migrations were moved to v59. These stale rows block downgrade to older versions.
      changes:
        - sql:
            sql: >-
              DELETE FROM ${databasechangelog.name} WHERE id IN (
                'v58.2025-12-03T17:08:36',
                'v58.2025-12-03T17:08:37',
                'v58.2025-12-03T17:08:38',
                'v58.2025-12-30T22:48:02',
                'v58.2025-12-30T22:48:03',
                'v58.2026-01-05T12:00:00',
                'v58.2026-01-09T11:30:00',
                'v58.2026-01-09T11:30:01',
                'v58.2026-01-09T12:00:01',
                'v58.2026-01-23T05:55:14',
                'v58.2026-01-27T13:00:00'
              );
      rollback: # nothing to undo - these rows should not exist in v59

  - changeSet:
      id: v59.2026-03-12T12:00:00
      author: wotbrew
      comment: Add last_checkpoint_value to transform table
      preConditions:
        - onFail: MARK_RAN
        - not:
            - columnExists:
                tableName: transform
                columnName: last_checkpoint_value
      changes:
        - addColumn:
            tableName: transform
            columns:
              - column:
                  name: last_checkpoint_value
                  type: ${text.type}
                  remarks: Serialized checkpoint watermark value for incremental transforms, temporal as ISO8601
                  constraints:
                    nullable: true

  - changeSet:
      id: v59.2026-03-13T00:00:00
      author: bronsa
      comment: Remove legacy incremental transform strategies that use checkpoint-filter instead of checkpoint-filter-field-id
      changes:
        - customChange:
            class: "metabase.app_db.custom_migrations.RemoveLegacyIncrementalStrategies"

  - changeSet:
      id: v59.2026-03-13T00:00:01
      author: bronsa
      comment: Add checkpoint_filter_field_id column to transform_run
      preConditions:
        - onFail: MARK_RAN
        - not:
            - columnExists:
                tableName: transform_run
                columnName: checkpoint_filter_field_id
      changes:
        - addColumn:
            tableName: transform_run
            columns:
              - column:
                  name: checkpoint_filter_field_id
                  type: int
                  remarks: The field ID used for incremental checkpoint filtering
                  constraints:
                    nullable: true

  - changeSet:
      id: v59.2026-03-13T00:00:02
      author: bronsa
      comment: Add checkpoint_lo_value column to transform_run
      preConditions:
        - onFail: MARK_RAN
        - not:
            - columnExists:
                tableName: transform_run
                columnName: checkpoint_lo_value
      changes:
        - addColumn:
            tableName: transform_run
            columns:
              - column:
                  name: checkpoint_lo_value
                  type: ${text.type}
                  remarks: The low checkpoint value for this run (exclusive lower bound)
                  constraints:
                    nullable: true

  - changeSet:
      id: v59.2026-03-13T00:00:03
      author: bronsa
      comment: Add checkpoint_hi_value column to transform_run
      preConditions:
        - onFail: MARK_RAN
        - not:
            - columnExists:
                tableName: transform_run
                columnName: checkpoint_hi_value
      changes:
        - addColumn:
            tableName: transform_run
            columns:
              - column:
                  name: checkpoint_hi_value
                  type: ${text.type}
                  remarks: The high checkpoint value for this run (inclusive upper bound)
                  constraints:
                    nullable: true

  - changeSet:
      id: v59.2026-03-17T10:30:03
      author: nvoxland
      comment: When rolling back, make sure any transform permissions that were added are removed. Because 58 doesn't know about them and errors
      changes:
        - output:
            message: Placeholder for managing transform permissions on rollback
            target: STDOUT
      rollback: delete from data_permissions where perm_type='perms/transforms';



  - changeSet:
      id: v59.2026-03-17T13:00:00
      author: bronsa
      comment: Drop medallion names for data_layer enum use (hidden, internal instead)
      preConditions:
        - onFail: MARK_RAN
        - and:
            - not:
                changeSetExecuted:
                  id: v58.2026-01-27T13:00:00
                  author: wotbrew
                  changeLogFile: migrations/058_update_migrations.yaml
            - not:
                changeSetExecuted:
                  id: v59.2026-01-27T13:00:00
                  author: wotbrew
                  changeLogFile: migrations/059_update_migrations.yaml
      runInTransaction: false
      changes:
        - customChange:
            class: "metabase.app_db.custom_migrations.DropMedallionNamesForDataLayer"

  - changeSet:
      id: v59.2026-07-10T22:29:18
      author: ranquild
      comment: >-
        Retire the changelog rows of v59.2026-07-10T22:29:16 and v59.2026-07-10T22:29:17, which moved to
        migrations/063/20260710_auth_identity_confirmed_at.yaml as v63.2026-07-10T22:29:16 and v63.2026-07-10T22:29:17.
      changes:
        - sql:
            sql: >-
              DELETE FROM DATABASECHANGELOG WHERE id IN (
                'v59.2026-07-10T22:29:16',
                'v59.2026-07-10T22:29:17'
              );
      rollback: # not needed

# >>>>>>>>>> DO NOT ADD NEW MIGRATIONS BELOW THIS LINE! ADD THEM ABOVE <<<<<<<<<<

########################################################################################################################
#
# ADVICE:
#
# 1) Think through some of these points when writing a migration, learn from our past mistakes:
#    - Do you really need a migration? Could the feature work without it? Prefer not to if possible.
#      Data in the wild can be vastly different from what you expect, and it's hard to get right.
#    - Make sure your migration is backward compatible: it might not be possible to add a constraint back
#      if you drop it in a migration.
#    - Postgres, MySQL and H2 have their differences. Make sure your migration works for all.
#    - Database encryption is a major issue:
#      - Fields like `metabase_database.details` or `setting.value` can be encrypted, so you need to decrypt them in
#        your migration. See #42617, #44048.
#      - Database could be partially encrypted, see https://www.notion.so/72575933ef2a446bafd16909e05a7387
#    - Custom migrations:
#      - Prefer SQL migrations when possible.
#      - Never use application code: it can change and *will* break your migration.
#      - Do not use Toucan models - refer table names directly.
#      - If it's a big change, consider using Quartz, see #42279
#      - More in `metabase.app_db.custom_migrations` namespace doc.
#    - Never delete a migration: users won't be able to downgrade. If you really need to, see #44908
#    - Migration id (`vXX.<date>`) must match its earliest released version:
#      - Do not backport `v51....` to version 50, Metabase will try to downgrade it.
#      - Instead, write a migration with an oldest target you plan to backport to in mind.
#
# 2) Migrations should go in the ###_update_migrations.yaml file for the target version.
#
# 3) Run mage lint-migrations to run core.spec checks against any changes you make here. Liquibase is pretty
#    forgiving and won't complain if you accidentally mix up things like deleteCascade and onDelete: CASCADE. CI runs
#    this check but it's nicer to know now instead of waiting for CI.
#
# 3) Migration IDs should follow the format
#
#    vMM.TIMESTAMP
#
#    Where
#
#    M         = major version
#    TIMESTAMP = the current timestamp with format `yyyy-MM-dd'T'HH:mm:ss`
#                To get this timestamp, evaluate this in your REPL: `(dev/migration-timestamp)`
#
#    E.g: You're adding a new migration for version 49, And it's 10:30:00AM on April 1, 2023 (UTC),
#    your migration id should be: `v49.2023-04-01T10:30:00`.
#
# PLEASE KEEP THIS MESSAGE AT THE BOTTOM OF THIS FILE!!!!! Add new migrations above the message.
#
########################################################################################################################