Using Django Test to Analyse and Repair Data of a Database
I had an idea and before implementing I would like to throw it out there to get some feedback or even discover that somebody has already done.
Here is the thing: I have a website running with a database that is growing quite rapidly and over the months, and over the开发者_JAVA百科 several issues that took place, got filled with a good amount of garbage. I was thinking in put-up some scripts to run across the database and clean up the mess. So, my idea was to use Django Tests, in a way that one would write a large sum of small simple tests that would do exactly that, except that instead of raising a flag, would actually fix things.
What do you guys think? I can't think in any reason why this wouldn't work. But I'm not that seasoned in Django. Would it be hard? Any foreseeable issue?
Thanks!
No, this is a bad idea for many reasons, starting from the design perspective to implementation issues. Just to mention a few:
- Tests do not run on the actual database. A separate database is created from scratch for it. You'd have to hack this.
- Normally each test case that touches the db, runs in a transaction and then it's rolled back. So in the end the DB has not changed at all. Ofc you can also avoid this, but that's not the point.
- Tests are supposed to be run all the time when you change something. But the kind of issues you are talking about should be one-time fix only (mostly).
But there is a very simple and appropriate solution for what you want to do:
- Install South
- Fix bug that causes incorrect/unwanted data.
- Write data migration that fixes/cleans up the already existing "broken" data.
- Update and migrate.
- Repeat 2-5
Now, this is for one-time fixes only, but in most cases this is the correct way to do it. You fix the bug that causes the data issue, along with fixing the data.
If you really need the same data altering functionality to run more than once (periodically), then for that you can create a custom management command (or just a simple executable python script) and schedule it to run from cron.
精彩评论