How To Be a WhereWolf
- 4 minutes read - 832 wordsBe a what?
So, many a long year ago, when I worked in domestic energy retail, my job was basically coaxing a system along for a business that had long grown out of it. Like, the system was designed for somewhere around a third of the customers that they had when I joined. I coaxed it along to a mere five or six times the design limit.
Unsurprisingly, we had a lot of problems. Weirdly, most of them were actually data problems. That meant that a lot of the time, I was making live data fixes in the live data. Any mistake, at best, could cause someone’s bill to be wrong, and a legal complaint would generally follow. At worst, I could actually trash the data so badly that we’d need to restore from backups, which would cause a vast number of problems with the EDI-style messages the system had to process in and out. So, extreme caution was the order of the day.
Back to the present, and I’m doing some BI reporting for a much more important organisation, Pink Hair. And yes, as I can’t build an automated pipeline from pieces of paper into the database, I’m having to do the data entry and transform by hand. I am, of course, using an Azure SQL database as my reporting source, and I’m planning to use PowerBI to surface the data.
But the problem with manual data herding is the five fat fingers I have on each of my left hands. I am, so it turns out, so terminally clumsy that most of my sports teachers gave up on me as a complete whittle.
So, combining the above two facts, how do I ensure that I am not at home to Mr Cockup? (Sidebar: Irony of ironies, I pasted the wrong link destination in here and had to go back and fix it…)
Well, the colleague who taught me how to do many of the data fixes I was doing, showed me a simple technique to fend off failure, using database transactions. And this I’m going to pass on to you.
Briefly, Microsoft SQL supports implicit transactions. That means, when you
issue an UPDATE command, it happens inside a transaction you don’t see. But if
you explicitly create a transaction, then you can roll it back explicitly if you
want, or you can commit it if you want to. However, while the transaction is
open (neither committed nor rolled back), the affected rows are locked. This can
cause concurrency problems, and almost certainly will if you’re working on the
database used by a system that’s three times over its design limits. So, we need
to make sure that transactions are held open for as little time as possible. So,
here’s the pattern:
1 BEGIN TRANSACTION
2 SELECT ... WHERE ... -- Old Data
3 UPDATE ... SET ... WHERE ...
4 SELECT @@ROWCOUNT
5 SELECT ... WHERE ... -- New Data
6 ROLLBACK TRANSACTION You should run this whole code block in one hit.
The idea first of all is to hold the transaction open for the shortest possible time. A colleague (honestly, not me) once actually wedged the system by forgetting to close a transaction. About half an hour after they left, we worked out what the problem was, and killed their login session, which automatically rolled back the transaction, and brought the system back to life.
So, what you do is you open the transaction, then select the old data. Then
issue the update command to update the data to the new data, and use the
@@ROWCOUNT variable to see how many rows were changed. Then select the new
data, and finally roll back the transaction. This lets you see the old data and
the new data side by side, lets you check you only changed the right number of
rows, and makes sure that no change was written. So you can spend as long as you
like checking the results, so you’re as sure as you can be that the change is
correct.
Once you’re happy to proceed, change ROLLBACK to COMMIT. This arms the code,
and when you run it now, your change will be written to the database. Run it
carefully, then immediately hit Undo. This swaps COMMIT back to ROLLBACK
(or to “COMMI”, whatever), disarming the code again. This way, you know your
changes are done, and are correct.
Getting into the habit of doing this whenever modifying data has saved my bacon
more times than I care to remember. I have made all of the mistakes doing this,
and have saved myself each time. And yes, I did forget the WHERE clause
one day, somehow, and changed something for all of the customers instead of just
the one. I did wonder why my query took so long. I promise you, it’s a habit
worth getting into.
Thoughts? Comments? Got another awesome tip to avoid massive borkage? Leave a comment below. Thanks for reading!