Scripts Run Order — Post-Migration Reference
Quick reference for which scripts to run (and in what order) after migrating fresh data from production.
Prerequisites
script/.envmust have the correctMONGO_CONNECTION_STRINGandDATABASE_NAMEfor the target environment- Update
script/config.json→"scriptRunArray"with the script name before each run - Run with
yarn script-dev(requires Node >=22)
Environment Config
| Env | DATABASE_NAME | Connection String Host |
|---|---|---|
| Local | dtcea-mumbai-demo | energyaudit.0l6duna.mongodb.net |
| Dev | dtcea-mumbai-demo-dev | dev.e45p6au.mongodb.net |
| Stg | dtcea-mumbai-demo-stg | stg.ivmjjjb.mongodb.net |
Run Order
Scripts have dependencies — run in this exact order.
| # | Script Name | Collection | What It Does | Idempotent? | Est. Time |
|---|---|---|---|---|---|
| 1 | seed_aiAuditTestData | transformer, installation, user | Seeds demo data for section S01 (15 TCs, ~135 installations × 7 months). Run via API: POST /seed/ai-audit-test-data with { "clearExisting": true } | Yes | ~5s |
| 2 | transformer_backfillAuditStatus | transformer | Sets auditStatus = AUDITED or AUDIT_FAILED on all TCs based on lossPercentage presence | Yes (only touches auditStatus: null) | ~10s |
| 3 | transformer_fixInvalidAuditedTCs | transformer | Fixes TCs incorrectly marked AUDITED (have lossPercentage but missing tcConsumption or installationConsumption) → sets to AUDIT_FAILED | Yes | ~10s |
| 4 | transformer_fixZeroTcConsumptionLoss | transformer | Fixes migrated TCs with tcConsumption: 0 but lossPercentage: 100 → nulls out loss %, sets AUDIT_FAILED, fixes cascading previousMonthLossPercentage / previousToPreviousMonthLossPercentage | Yes | ~30s |
| 5 | installation_backfillBillingFlags | installation | Backfills billing boolean flags (unbilled, mnr, vacant, doorLock, zeroConsumption) and numeric billing fields (consumption, demand, collection, emin, emax, billCancellation) by reading production DB's audit[].billing data. Requires productionConnectionString in script input. Excludes section S01 by default. | Yes (overwrites) | 10–30 min |
| 6 | installation_backfillAvgConsumption | installation | Computes 6-month rolling avg consumption for remarked installations (uses server-side $out + $lookup + $merge) | Yes | 5–15 min |
| 7 | transformer_backfillHealthScore | transformer | Computes healthScore + healthGrade on all TCs using audit fields, billing counts, and loss data | Yes (overwrites) | 3–8 min |
| 8 | auditSummary_computeAuditSummary | audit-summaries | MUST BE LAST. Deletes all existing summaries, recomputes from transformer data at all 6 hierarchy levels (Section → Sub-Division → Division → Circle → Zone → MESCOM) | Yes (full rebuild) | 2–5 min |
Dependency Graph
1. seed (API)
│
▼
2. backfillAuditStatus ──► 3. fixInvalidAuditedTCs ──► 4. fixZeroTcConsumptionLoss
│
5. backfillBillingFlags (needs production DB connection) │
│ │
▼ │
6. backfillAvgConsumption (needs consumption from step 5) │
│ │
▼ ▼
7. backfillHealthScore (needs finalized audit fields) ◄─────┘
│
▼
8. computeAuditSummary (ALWAYS LAST — depends on final TC states)
How to Run
For each script, update config.json and run:
# Example: running fixZeroTcConsumptionLoss
# 1. Edit script/config.json:
# { "scriptRunArray": ["transformer_fixZeroTcConsumptionLoss"], ... }
#
# 2. Run:
yarn script-dev
For backfillBillingFlags (step 5), pass the production connection string in config.json input:
# 1. Edit script/config.json:
# {
# "scriptRunArray": ["installation_backfillBillingFlags"],
# "input": {
# "productionConnectionString": "mongodb+srv://user:pass@prod-host.mongodb.net/"
# }
# }
#
# 2. Run:
yarn script-dev
The seed (step 1) is different — it runs via the API, not the script runner:
# Start the API first, then:
curl -X POST http://localhost:3000/dtcea-mumbai-demo/v1/seed/ai-audit-test-data \
-H "Content-Type: application/json" \
-d '{"clearExisting": true}'
Important Notes
computeAuditSummaryDELETES all summaries before recomputing — always run last- Steps 2→3→4 must be sequential — each fixes a subset of transformer audit data that the next depends on
- Step 5 (
backfillBillingFlags) requires a production DB connection string — passproductionConnectionStringinconfig.jsoninput - Step 5 is independent of steps 2–4 (only needs installations to exist) but must run before step 6
- Step 6 must run after step 5 — it uses the
consumptionfield that step 5 backfills - Step 7 must run after audit status is finalized (after steps 2–4) and after step 6
- All scripts are safe to re-run (idempotent)
Scale
- ~150K TCs × 7 months = ~1M transformer docs
- ~3.3M installation docs
- Total time per environment: ~25–45 minutes