# MARS-1409-POSTHOOK: Backfill A_WORKFLOW_HISTORY_KEY for existing records

## Overview

Post-hook for MARS-1409. Backfills `A_WORKFLOW_HISTORY_KEY` in
`CT_MRDS.A_SOURCE_FILE_RECEIVED` for historical records that existed before
MARS-1409 was installed.

Matches records by `SOURCE_FILE_NAME` against `file$name` in the corresponding
ODS table (`ODS.<TABLE_ID>_ODS`) for each `INPUT` source configuration.

## Contents

| File | Description |
|------|-------------|
| `install_mars1409_posthook.sql` | Master installation script (SPOOL, ACCEPT, quit) |
| `rollback_mars1409_posthook.sql` | Master rollback script (SPOOL, ACCEPT, quit) |
| `01_MARS_1409_POSTHOOK_update_existing_workflow_keys.sql` | Backfill UPDATE script |
| `91_MARS_1409_POSTHOOK_rollback_workflow_keys.sql` | Clear backfilled values |
| `track_package_versions.sql` | Universal version tracking (no packages changed) |
| `verify_packages_version.sql` | Universal package verification |
| `README.md` | This file |

## Prerequisites

- MARS-1409 installed (`A_WORKFLOW_HISTORY_KEY` column must exist in `CT_MRDS.A_SOURCE_FILE_RECEIVED`)
- ODS tables populated with ingested data
- ADMIN user with access to CT_MRDS and ODS schemas

## Installation

```powershell
# Execute as ADMIN user
Get-Content "MARS_Packages/REL02_POST/MARS-1409-POSTHOOK/install_mars1409_posthook.sql" | sql "ADMIN/Cloudpass#34@ggmichalski_high"
```

Log file created automatically: `log/INSTALL_MARS_1409_POSTHOOK_<PDB>_<timestamp>.log`

## What it does

- Iterates all `INPUT` source configurations from `CT_MRDS.A_SOURCE_FILE_CONFIG`
- For each config, joins `A_SOURCE_FILE_RECEIVED` with `ODS.<TABLE_ID>_ODS` on `SOURCE_FILE_NAME = file$name`
- Updates `A_WORKFLOW_HISTORY_KEY` for records with statuses:
  `VALIDATED`, `READY_FOR_INGESTION`, `INGESTED`, `ARCHIVED`, `ARCHIVED_AND_TRASHED`, `ARCHIVED_AND_PURGED`
- Skips configs with no NULL records or missing ODS tables
- Prints summary with counts per config and overall totals

## Rollback

```powershell
# Execute as ADMIN user
Get-Content "MARS_Packages/REL02_POST/MARS-1409-POSTHOOK/rollback_mars1409_posthook.sql" | sql "ADMIN/Cloudpass#34@ggmichalski_high"
```

Rollback clears all non-NULL `A_WORKFLOW_HISTORY_KEY` values from `A_SOURCE_FILE_RECEIVED`.
The trigger installed by MARS-1409 will repopulate new records automatically.

## Related

- MARS-1409: Add A_WORKFLOW_HISTORY_KEY column to A_SOURCE_FILE_RECEIVED (main package)
