Make your backfill script unable to write where it only needs to read
In a side project I’m building, the app keeps a copy of what each customer actually pays per month. That stored amount matters, because when it is missing the app falls back to the list price from the catalog. For customers on a discount or an older plan, the fallback is wrong, and the app shows them a number that is not what they are being billed.
New subscriptions now record the real amount as they change. The older ones still needed a backfill: go through every customer with a subscription id, ask the payment provider what the recurring charge really is, and copy that number into the app’s database.
That is a one-off script, the kind that gets written quickly and run once against production. It also holds a secret key for the payment provider, which can do far more than read. So before writing any of the actual logic, I decided what the script should be unable to do.
The script only needs two permissions
Write down what the job needs, and it is usually smaller than what the credentials allow. This one needs to:
- Read subscriptions from the payment provider, with discounts included.
- Write a few amount and interval fields in the app’s own database.
It never needs to create, update, cancel, or delete anything at the provider. A bug that called one of those methods could change what a real person is charged, and that is not a mistake you can quietly undo with another script. So the script makes those calls impossible instead of just avoiding them.
Block the dangerous calls in code
The best fix is a restricted key that can only read. Use one if your provider supports it and you control how the script gets run. I also wanted a guard that lives in the script itself, so it holds no matter which key someone pastes into the environment. Before doing any work, the script replaces every mutating method on the client library with one that raises:
MUTATING = ('create', 'modify', 'update', 'cancel', 'delete', 'del', 'pay', 'void')
class WriteBlocked(RuntimeError):
pass
def block_provider_writes(resources):
for resource in resources:
for name in MUTATING:
if hasattr(resource, name):
def refuse(*args, _name=name, _res=resource.__name__, **kwargs):
raise WriteBlocked(f'{_res}.{_name} is blocked in this script')
setattr(resource, name, refuse)
Now the only path to the provider is a retrieve. If a later edit adds an “update while we are here” line, it fails on the first call instead of reaching a real account. There is a test that calls each blocked method and expects the error, so the guard cannot silently stop working when the library changes.
The script also prints a warning when it sees a live key. It does not refuse to run, because running against live data is the whole point. The warning is there so nobody is surprised which account they are reading.
Dry run by default, writes behind a flag
Writing to my own database is allowed, but it still should not happen by accident. Running the script with no arguments prints a diff and writes nothing:
customer 1042 stored: none provider: 39.20/month (20% coupon)
customer 1077 stored: 119.00 provider: 119.00/month no change
customer 1101 stored: none provider: 0.00/month (100% off)
customer 1130 ERROR discount could not be expanded, skipped
Only --apply writes, and only the fields the diff showed. A --customer-id option limits the
run to one record, so the first real write can be a single customer you can check by hand
before running the whole set.
Running it this way changes the review. Instead of reading the code and imagining what it will do, you read its actual output against real data and decide whether those are the numbers you expected.
Make the second run a no-op
The script compares before it writes. If the stored amount already matches what the provider
reports, it skips that customer. That makes it safe to run again: after a partial run, after a
crash, or a week later when someone is not sure whether it already ran. A second --apply
right after the first should report zero writes, and a test checks exactly that.
Be careful with zero and with errors
Two details took more thought than the main loop.
Zero is a real amount. A customer with a 100% discount pays nothing, and the script stores
0. It has to stay distinct from “no value”, or the app treats zero as missing and goes back to
showing the full list price. In Python that means checking is None rather than truthiness
anywhere the stored amount is read.
Unknown is not a number. If a discount cannot be expanded, or a subscription has no recurring item, the script reports that customer as an error and writes nothing for them. It would be easy to fall back to the undiscounted price and move on, but that writes the exact wrong value the backfill exists to fix, and now it looks like real data. A missing value is honest. A guessed one is not.
It also ignores one-time charges like setup fees. The question is “what is the recurring charge”, and mixing in one-time lines would quietly inflate it.
Tradeoffs
- Patching the client library is a guard, not a permission system. Someone could remove the patch. It protects against mistakes, not against a person who means to do harm. A restricted key is the stronger control, and the two work well together.
- The data is only as fresh as the run. Amounts reflect the provider at the moment the script runs. A coupon that only applied once and is already used up will not show, which is correct for the ongoing charge but can surprise someone looking at last month’s invoice.
- More ceremony for a one-off. Dry run, apply flag, single-customer mode, tests. It is more work than a 30-line loop. For a script that holds a key to real money, I think it is the right amount.
The pattern
For any backfill that reads from one system and writes to another, I now start with the same three questions. What is the smallest set of calls this job needs? Can the rest fail loudly in code instead of relying on care? And what happens if I run it twice? Answering those up front takes about as long as writing the loop, and the script I end up with is one I am comfortable running against production.