Skip to main content

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/.env must have the correct MONGO_CONNECTION_STRING and DATABASE_NAME for 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

EnvDATABASE_NAMEConnection String Host
Localdtcea-mumbai-demoenergyaudit.0l6duna.mongodb.net
Devdtcea-mumbai-demo-devdev.e45p6au.mongodb.net
Stgdtcea-mumbai-demo-stgstg.ivmjjjb.mongodb.net

Run Order

Scripts have dependencies — run in this exact order.

#Script NameCollectionWhat It DoesIdempotent?Est. Time
1seed_aiAuditTestDatatransformer, installation, userSeeds 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
2transformer_backfillAuditStatustransformerSets auditStatus = AUDITED or AUDIT_FAILED on all TCs based on lossPercentage presenceYes (only touches auditStatus: null)~10s
3transformer_fixInvalidAuditedTCstransformerFixes TCs incorrectly marked AUDITED (have lossPercentage but missing tcConsumption or installationConsumption) → sets to AUDIT_FAILEDYes~10s
4transformer_fixZeroTcConsumptionLosstransformerFixes migrated TCs with tcConsumption: 0 but lossPercentage: 100 → nulls out loss %, sets AUDIT_FAILED, fixes cascading previousMonthLossPercentage / previousToPreviousMonthLossPercentageYes~30s
5installation_backfillBillingFlagsinstallationBackfills 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
6installation_backfillAvgConsumptioninstallationComputes 6-month rolling avg consumption for remarked installations (uses server-side $out + $lookup + $merge)Yes5–15 min
7transformer_backfillHealthScoretransformerComputes healthScore + healthGrade on all TCs using audit fields, billing counts, and loss dataYes (overwrites)3–8 min
8auditSummary_computeAuditSummaryaudit-summariesMUST 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

  • computeAuditSummary DELETES 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 — pass productionConnectionString in config.json input
  • 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 consumption field 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