Executing Batch Operations
Use this skill for ANY operation affecting multiple records. Prevents catastrophic data loss.
Before You Start
- The operation affects more than 10 records.
- You have a spec, or a clear understanding of what needs to change.
- A database backup exists or can be created.
The commands below use pnpm tsx to run a TypeScript script. Substitute your project's own script runner (npx tsx, bun, python, a Rails runner, and so on).
The Golden Rule
Dry-run -> Sample Review -> Execute -> Verify
Never skip steps. Every step exists because of past pain.
<critical_sequence enforce_order="strict"><step number="1" required="true" blocking="true"> ## Step 1: Create Dry-Run ScriptAdd a --dry-run flag that:
- Shows what WOULD change without changing it
- Outputs affected record IDs
- Counts total affected records
For a full template with dry-run, rollback, and confirmation, read scripts/batch-template.ts.
Do NOT proceed until dry-run works correctly. </validation_gate> </step>
<step number="2" required="true" depends_on="1" blocking="true"> ## Step 2: Dry-Run on a Samplepnpm tsx scripts/batch-operation.ts --dry-run --limit 10Use 10% of the records or 10, whichever is smaller.
Review output manually:
- Are the right records selected?
- Is the transformation correct?
- Any unexpected cases?
Do NOT proceed to full dry-run until sample looks correct. </validation_gate> </step>
<step number="3" required="true" depends_on="2" blocking="true"> ## Step 3: Full Dry-Runpnpm tsx scripts/batch-operation.ts --dry-run > dry-run-output.txtReview the full output:
- Total count matches expectation?
- Edge cases handled?
- No unexpected records included?
Save this file - you'll need it for audit. </validation_gate> </step>
<step number="4" required="true" depends_on="3" blocking="true"> ## Step 4: Create Rollback FileBefore execution, save current state (see scripts/batch-template.ts for rollback pattern).
You WILL need this rollback file. Create it. </validation_gate> </step>
<step number="5" required="true" depends_on="4" blocking="true"> ## Step 5: Execute with Confirmationpnpm tsx scripts/batch-operation.tsScript must have confirmation prompt (see scripts/batch-template.ts for pattern).
This is the point of no return. Confirm everything is ready. </validation_gate> </step>
<step number="6" required="true" depends_on="5" blocking="true"> ## Step 6: Verify with Audit QueryAfter execution, run verification immediately. Six check classes:
-- 1. Record count (compare before/after against dry-run expectation)
SELECT COUNT(*) FROM table WHERE condition;
-- 2. Orphans (references pointing at nothing)
SELECT * FROM child WHERE parent_id IS NOT NULL
AND parent_id NOT IN (SELECT id FROM parent);
-- 3. Duplicates on unique fields
SELECT field, COUNT(*) FROM table GROUP BY field HAVING COUNT(*) > 1;
-- 4. Timestamp consistency (future dates, inverted pairs)
SELECT * FROM table WHERE created_at > NOW() OR updated_at < created_at;
-- 5. FK integrity: repeat check 2 for EVERY relation the operation touched
-- 6. Business-rule compliance: assert the operation's domain invariants
-- (legal status transitions, required-field combinations) — define per project<validation_gate name="audit-passed" blocking="true">
**BLOCK if audit fails:**
- [ ] Record count matches expected
- [ ] No orphaned records created
- [ ] No duplicate records created
- [ ] Timestamps consistent (no future/inverted)
- [ ] FK integrity holds on every touched relation
- [ ] Business-rule invariants hold
- [ ] Spot-check shows correct valuesIf ANY check fails, STOP and investigate before proceeding. </validation_gate>
Query-correctness validation (when the operation changes search/query behavior)
The audit above proves integrity, not correctness of results. If the operation changes what a search/query/matching path returns:
- Define expected behavior BEFORE running: expected count range, must-include cases, must-exclude cases
- Run the query on real data; save results to a file
- Classify each result correct/incorrect against the expectations
- Compute precision (of returned, % correct) and recall (of expected, % returned)
- Write a short report: query, counts, precision/recall, misclassified examples
Verify with real queries on real data: manual spot-checks check the experience, not correctness. </step>
<step number="7" required="true" depends_on="6"> ## Step 7: Document ResultsUpdate implementation.md or create new documentation:
## Batch Operation: {name}
- Date: {date}
- Records affected: {count}
- Dry-run file: dry-run-output.txt
- Rollback file: rollback-{operation}-{date}.json
- Verification: [PASSED/FAILED]</step></critical_sequence>Safety Checklist
Before executing any batch operation:
- Dry-run flag implemented
- Sample (10%) reviewed manually
- Full dry-run output saved
- Rollback file created
- Confirmation prompt added
- Audit queries written
- Results documented
- Dry-run completed and reviewed
- Rollback file exists
- Execution confirmed by user
- Audit queries all pass
- Results documented
- No orphans or duplicates created</success_criteria>
Affected records: [count] Audit status: [PASSED/FAILED]
What's next?
- Re-run the Step 6 audit taxonomy as a full-table health check (recommended)
- Proceed to next step in implementation
- Rollback changes (if issues found)
- Write down what you learned
- Other
Choose (1-5): _ </decision_gate>
Anti-Patterns
- Running batch operations in production without dry-run
- Skipping sample review "because it's simple"
- No rollback file (you WILL need it)
- Verification after, not during (catch issues early)
- Not saving dry-run output (needed for audit)
Invoked by:
- /creating-specifications skill (when implementation involves bulk changes)
- Manual invocation for data migrations
- Auto-suggested when user mentions bulk operations
Invokes:
- Nothing external — Step 6's audit taxonomy and query-correctness validation are part of this skill
Handoff expectations: Creates rollback file and dry-run output. Step 6 audit uses these to verify integrity. </integration_protocol>