{"success":true,"name":"wrap-multi-step-database-writes-in-one-transaction","description":"Backend logic writes to the database in several steps that belong together","category":"ai-brimind","path":"ai-brimind/wrap-multi-step-database-writes-in-one-transaction/SKILL.md","file":"SKILL.md","content":"---\nname: wrap-multi-step-database-writes-in-one-transaction\ndescription: \"Backend logic writes to the database in several steps that belong together\"\n---\n\n# Wrap Multi-Step Database Writes in One Transaction\n\n## When to use\nProject has a backend with a database, and you are about to do two or more writes (insert, update, delete) that only make sense if all of them succeed.\n\n## Example request\ncreating an order then decreasing stock in two separate calls → the second fails and you sold stock that was never reserved; the two writes are one unit, they need a transaction. transferring credit by subtracting from one row then adding to another → a crash in between makes money disappear; the textbook case for all-or-nothing writes. creating a user then their profile row → the profile insert fails and a half-user haunts every later query; either both rows exist or neither. 'fixing' half-written data with cleanup scripts → the cleanup is treating the symptom; the missing transaction is the root cause.\n\n## Instructions\nAct like a database operator who refuses to leave the data half-changed, ever. Before writing backend logic that touches the database more than once, list every write the operation performs and ask: if step 2 fails after step 1 succeeded, is the database now telling a lie (an order without stock, a user without a profile, money missing)? If yes, these writes are one unit and must run inside a transaction: all commit together or all roll back together. Check how the project's database layer already does transactions (the ORM's transaction API, the driver's BEGIN/COMMIT) and use that existing mechanism — do not simulate it with manual undo code, which is exactly the fragile thing transactions exist to replace. Make sure any error inside the unit triggers the rollback and surfaces a clear failure. If you cannot tell whether the writes must succeed together, ask about the business rule instead of guessing. Only proceed once every all-or-nothing group is wrapped, and explain why a crash at the worst possible moment now leaves the data consistent instead of corrupted.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #523\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}