research/a-dry-run-mode-for-a-data-script
A Dry-Run Mode for a Script That Changes Data: Printing the Plan Before Applying It
Engineering Note · December 1, 2026 · 9 minute read · Node.js · SQLite · Scripts · Reliability
A dry-run mode should be the default, should only read, and should print each change it would make and why. The real run, behind an --apply flag, should make those changes in one transaction and re-check each row’s condition as it changes it, because the data can move between the dry run and the apply. We tested this on a script that deactivates customers with no booking in a year. A “dry run” done by applying the changes and rolling back held SQLite’s write lock, and the live app’s booking failed with database is locked. Applying a saved plan without re-checking deactivated a customer who had booked in the meantime. And testing against a copy only works if the copy is complete: copying the database file while the app had it open missed 50 of 58 bookings.
Everything below was run on Linux 6.18 with Node.js 22.23.5 and its built-in node:sqlite module, which bundles SQLite 3.54.1. The module printed an “experimental” warning on every run, which we removed from the outputs. Otherwise the outputs are real.
What does the script do?
A small business’s customer table collects people who booked once and never came back. Our test script marks a customer inactive when their last booking is more than 365 days old, and deletes their pending reminders so they stop getting emails. It is a typical one-off cleanup: easy to write, run once a quarter, and painful if it touches the wrong rows.
The test database has three tables (customers, bookings, reminders) in WAL mode, which is how most apps open SQLite. Eight customers have last bookings spread over two years, each with one pending reminder. The cutoff is fixed at 2025-12-01 so the results are repeatable.
How is a plan-then-apply script laid out?
The plan is a single read-only query. The script prints it, and only touches data when --apply is present:
const APPLY = args.includes('--apply');
const CUTOFF = '2025-12-01'; // today minus 365 days, fixed for the test
function plan() {
return db.prepare(`
SELECT c.id, c.email, MAX(b.day) AS last_booking,
(SELECT COUNT(*) FROM reminders r WHERE r.customer_id = c.id) AS reminders
FROM customers c JOIN bookings b ON b.customer_id = c.id
WHERE c.status = 'active'
GROUP BY c.id HAVING MAX(b.day) < ? ORDER BY c.id`).all(CUTOFF);
}
const steps = plan();
console.log(`${APPLY ? 'APPLY' : 'DRY RUN'}: ${steps.length} customers to deactivate (last booking before ${CUTOFF})`);
for (const s of steps) console.log(` #${s.id} ${s.email} last booking ${s.last_booking} reminders to delete: ${s.reminders}`);
if (APPLY) apply(steps);
else console.log('Nothing changed. Run again with --apply to make these changes.');
The apply step runs the same plan inside one transaction. Each UPDATE repeats the plan’s condition, so a row that no longer qualifies is left alone and reported:
function apply(steps) {
const deactivate = db.prepare(`UPDATE customers SET status = 'inactive'
WHERE id = ? AND status = 'active'
AND (SELECT MAX(day) FROM bookings WHERE customer_id = ?) < ?`);
const dropReminders = db.prepare('DELETE FROM reminders WHERE customer_id = ?');
const skipped = [];
let changed = 0;
db.exec('BEGIN IMMEDIATE');
try {
for (const s of steps) {
const r = deactivate.run(s.id, s.id, CUTOFF);
if (r.changes === 1) { dropReminders.run(s.id); changed++; } else skipped.push(s.id);
}
db.exec('COMMIT');
} catch (e) { db.exec('ROLLBACK'); throw e; }
console.log(`Done: ${changed} deactivated${skipped.length ? `, skipped (no longer match): ${skipped.map(i => '#' + i).join(', ')}` : ''}`);
}
BEGIN IMMEDIATE takes the write lock at the start, so the transaction cannot fail halfway through for want of it. If anything throws, the whole batch rolls back and the database is as it was.
The dry run:
DRY RUN: 5 customers to deactivate (last booking before 2025-12-01)
#3 customer3@example.com last booking 2025-10-01 reminders to delete: 1
#4 customer4@example.com last booking 2025-08-20 reminders to delete: 1
#6 customer6@example.com last booking 2025-03-11 reminders to delete: 1
#7 customer7@example.com last booking 2025-11-29 reminders to delete: 1
#8 customer8@example.com last booking 2024-12-24 reminders to delete: 1
Nothing changed. Run again with --apply to make these changes.
Each line names the row and the reason, so a person can spot-check it. Customer 7, two days inside the cutoff, is the sort of row worth a second look.
Why make the dry run the default?
Because the failure modes of a flag are mostly typos. We ran the script with --aply:
DRY RUN: 5 customers to deactivate (last booking before 2025-12-01)
Nothing changed. Run again with --apply to make these changes.
With “apply” as the default and a --dry-run flag, the same typo would have changed the data. Strict scripts also reject unknown flags outright, which is better still.
What if the data changes between the dry run and the apply?
A dry run is read on Monday and applied on Tuesday, or the plan is saved to a file and applied later. Meanwhile, customers keep booking. We wrote the dry run’s plan, [3,4,6,7,8], to a file, then had customer 7 book on 2026-11-30. Then we applied the saved plan two ways: by ID alone, and with the condition repeated in the UPDATE:
naive: deactivated 5, skipped []
customer #7 now: status inactive, reminders 0
guarded: deactivated 4, skipped [7]
customer #7 now: status active, reminders 1
Applying by ID deactivated a customer who had booked the day before and deleted their reminder. The guarded version skipped them and said so. The plan tells a person what to expect; the condition in the UPDATE decides what actually changes.
Our script recomputes the plan on --apply, so it would have left customer 7 out entirely:
APPLY: 4 customers to deactivate (last booking before 2025-12-01)
#3 customer3@example.com last booking 2025-10-01 reminders to delete: 1
#4 customer4@example.com last booking 2025-08-20 reminders to delete: 1
#6 customer6@example.com last booking 2025-03-11 reminders to delete: 1
#8 customer8@example.com last booking 2024-12-24 reminders to delete: 1
Done: 4 deactivated
The guard still matters there, for a booking that lands between the plan query and BEGIN IMMEDIATE. Running the dry run again afterwards printed 0 customers to deactivate, which is a cheap check that the apply did what the plan said.
Is “apply and roll back” a good dry run?
It is tempting: run the real changes inside a transaction, print the counts, then ROLLBACK. The counts are exact, because the real code ran. We tested what it does to the live app. The rollback dry run held its transaction for two seconds, standing in for a slower script. Three tenths of a second in, the app tried to save a booking, with a one-second busy timeout:
dry run (rollback): would deactivate 5
app: booking failed after 1004 ms: database is locked
dry run done after 2002 ms
SQLite allows one writer at a time, so a dry run that writes, even temporarily, blocks the app for as long as it runs. The read-only plan query, run alongside the same app write, did not get in the way:
app: booking saved after 1 ms
DRY RUN: 5 customers to deactivate (last booking before 2025-12-01)
A rollback also cannot undo work outside the database. If the script sends an email, calls a payment API or writes a file, the rollback leaves those done. A plan-only dry run never reaches that code.
How do you get a safe copy to test against?
Running the script against a copy first is the best dry run of all, because the real apply code runs and the result can be inspected. The copy has to be complete. In WAL mode, recent writes sit in a separate -wal file until SQLite folds them into the main file, so a plain file copy can miss them. We had the app’s connection insert 50 bookings, kept it open, and copied the database three ways:
fs.copyFileSync('shop.db', 'copy-cp.db'); // like: cp shop.db copy.db
live.exec(`VACUUM INTO 'copy-vacuum.db'`);
await backup(live, 'copy-backup.db'); // from node:sqlite
live database: 58 bookings; files on disk: shop.db, shop.db-shm, shop.db-wal
copied the .db file only: 8 bookings
VACUUM INTO: 58 bookings
backup(): 58 bookings
The file copy had only the 8 bookings from before the app’s writes. A dry run against it would have planned from data that was weeks old in a real system, and it would have looked fine. VACUUM INTO and sqlite.backup() (added in Node.js 22.16) both produced a complete copy while the app kept its connection open.
What should a dry run print?
From these tests, a useful checklist:
- One line per row it would change, with the ID and the value that made it qualify.
- Counts for each kind of change (deactivated, reminders deleted).
- The cutoff or other inputs it used, so a wrong date is visible.
- A last line that says nothing changed and names the flag that applies.
- On apply: the same list, then what changed and what was skipped, and why.
For a script that runs on a schedule, write those lines to a log rather than the terminal. Our note on logging what an automated job did covers one JSON line per item and a run ID.
Limitations
- All tests used SQLite in WAL mode through Node’s
node:sqlitemodule. Other databases lock rows and tables differently, so the lock result may not carry over to them. We did not test them. - The lock test used a one-second busy timeout in the app. With a longer timeout, the booking would have waited instead of failing, which is slower but not lost.
- The file-copy test copied only the
.dbfile while the app held an open connection. Copying the-walfile as well, or copying after every connection closed, may give a complete copy, but we did not test those, and a running app rarely closes its connection. node:sqliteis marked experimental in Node.js 22 and printed a warning on every run. Its API may change.- The guard in the
UPDATEhandles a changed row. It does not handle a new row that would now qualify; that is why the apply step recomputes the plan.
Sources
Related reading
- Logging What an Automated Job Did: One JSON Line per Item and a Run ID, Tested in Node.js
- Expiring Old Rows in SQLite Without Blocking Writers: One DELETE, Batches and Batches With a Pause
- Backups Only Count Once You Have Restored One: A Restore Drill You Can Run in an Afternoon
Frequently asked questions
What is a dry-run mode?
A way to run a script that shows exactly what it would change without changing anything. A good dry run lists each row or record it would touch and why, so a person can check the list before running the script for real. The real run then makes those changes and reports any that no longer apply.
Should a script default to dry run or to applying changes?
Default to the dry run and require a flag such as --apply to change data. Then a mistyped flag, a missing flag or a curious colleague running the script gets a list, not a changed database. In our test, running the script with a misspelled --aply printed the plan and changed nothing.
Can I do a dry run by making the changes in a transaction and rolling back?
It shows accurate counts, but in SQLite it holds the write lock for the whole run. In our test, the live app tried to save a booking during a two-second rollback dry run, waited its one-second timeout and failed with 'database is locked'. It also cannot undo anything outside the database, such as an email the script sent.
How do I make a safe copy of a SQLite database to test against?
Use VACUUM INTO or the SQLite backup API, not a plain file copy. In our test the database was in WAL mode with the app's connection open, and copying the .db file with a normal file copy missed 50 of 58 bookings that were still in the -wal file. VACUUM INTO and Node's sqlite.backup() both copied all 58.
What if the data changes between the dry run and the real run?
Re-check each row's condition at the moment you change it, inside the same transaction. In our test, a customer booked again after the dry run. Applying the saved list of IDs deactivated them anyway; applying it with the condition repeated in the UPDATE skipped them and reported the skip.